cic-cli/cic/actions/deploy.py

97 lines
3.4 KiB
Python
Raw Permalink Normal View History

2022-02-21 06:34:25 +01:00
from __future__ import annotations
2022-02-10 09:22:21 +01:00
# standard imports
import importlib
2022-02-21 06:34:25 +01:00
import logging
2022-02-10 09:22:21 +01:00
import os
2022-02-21 06:34:25 +01:00
from typing import TYPE_CHECKING
2022-02-10 09:22:21 +01:00
# local imports
2022-02-21 06:34:25 +01:00
from cic import Processor, Proof
2022-02-10 09:22:21 +01:00
from cic.attachment import Attachment
2022-02-21 06:34:25 +01:00
from cic.meta import Meta, MetadataWriter
2022-02-10 09:22:21 +01:00
from cic.network import Network
2022-02-21 06:34:25 +01:00
from cic.output import HTTPWriter, KeyedWriterFactory
2022-02-10 09:22:21 +01:00
from cic.token import Token
2022-02-21 06:34:25 +01:00
# external imports
from cic.MetaRequestHandler import MetadataRequestsHandler
from cic_types.ext.metadata.signer import Signer as MetadataSigner
2022-02-10 09:22:21 +01:00
2022-02-21 06:34:25 +01:00
logg = logging.getLogger(__name__)
2022-02-10 09:22:21 +01:00
2022-02-21 06:34:25 +01:00
if TYPE_CHECKING:
from cic.cmd.arg import CmdCtrl
from cic.actions.types import Options
2022-02-10 09:22:21 +01:00
def init_writers_from_config(config):
w = {
'meta': None,
'attachment': None,
'proof': None,
'ext': None,
}
for v in w.keys():
k = 'CIC_CORE_{}_WRITER'.format(v.upper())
(d, c) = config.get(k).rsplit('.', maxsplit=1)
m = importlib.import_module(d)
o = getattr(m, c)
w[v] = o
return w
2022-02-21 06:34:25 +01:00
def deploy(ctrl: CmdCtrl, target: str, contract_directory: str, keystore_directory: str, options: Options):
auth_passphrase=options.auth_passphrase,
auth_key_file_path=options.auth_keyfile_path,
metadata_endpoint=options.metadata_endpoint,
modname = f'cic.ext.{target}'
2022-02-10 09:22:21 +01:00
cmd_mod = importlib.import_module(modname)
2022-02-21 06:34:25 +01:00
writers = init_writers_from_config(ctrl.config)
2022-02-10 09:22:21 +01:00
output_directory = os.path.join(contract_directory, 'out')
output_writer_path_meta = output_directory
if metadata_endpoint != None:
MetadataRequestsHandler.base_url = metadata_endpoint
2022-02-21 06:34:25 +01:00
MetadataRequestsHandler.ctrl = ctrl
MetadataSigner.gpg_path = '/tmp'
MetadataSigner.key_file_path = auth_key_file_path # This is a p2p key for add data to meta
MetadataSigner.gpg_passphrase = auth_passphrase
writers['proof'] = KeyedWriterFactory(MetadataWriter, None).new
2022-02-10 09:22:21 +01:00
writers['attachment'] = KeyedWriterFactory(None, HTTPWriter).new
writers['meta'] = MetadataWriter
output_writer_path_meta = metadata_endpoint
ct = Token(path=contract_directory)
cm = Meta(path=contract_directory, writer=writers['meta'](path=output_writer_path_meta))
ca = Attachment(path=contract_directory, writer=writers['attachment'](path=output_writer_path_meta))
cp = Proof(path=contract_directory, attachments=ca, writer=writers['proof'](path=output_writer_path_meta))
cn = Network(path=contract_directory)
ca.load()
ct.load()
cp.load()
cm.load()
cn.load()
chain_spec = None
try:
2022-02-21 06:34:25 +01:00
chain_spec = ctrl.config.get('CHAIN_SPEC')
2022-02-10 09:22:21 +01:00
except KeyError:
chain_spec = cn.chain_spec
2022-02-21 06:34:25 +01:00
ctrl.config.add(chain_spec, 'CHAIN_SPEC', exists_ok=True)
2022-02-10 09:22:21 +01:00
logg.debug(f'CHAIN_SPEC config set to {str(chain_spec)}')
2022-02-21 06:34:25 +01:00
(rpc, signer) = cmd_mod.parse_adapter(ctrl.config, keystore_directory)
2022-02-10 09:22:21 +01:00
2022-02-21 06:34:25 +01:00
target_network_reference = cn.resource(target)
2022-02-10 09:22:21 +01:00
chain_spec = cn.chain_spec(target)
2022-02-21 06:34:25 +01:00
logg.debug(f'found reference {target_network_reference["contents"]} chain spec {chain_spec} for target {target}')
c = getattr(cmd_mod, 'new')(chain_spec, target_network_reference['contents'], cp, signer_hint=signer, rpc=rpc, outputs_writer=writers['ext'](path=output_directory))
2022-02-10 09:22:21 +01:00
c.apply_token(ct)
p = Processor(proof=cp, attachment=ca, metadata=cm, extensions=[c])
p.process()