def generator_sdk_ca(agency_dir, sdk_dir, gm=False): """[generate sdkcert] Arguments: dir {[path]} -- [agency cert path] If operation success, dir will generate sdk dir under the target path, the content is sdk_cert. """ dir = os.path.abspath(agency_dir) sdk_dir = os.path.abspath(sdk_dir) try: if gm: (status, result ) = utils.getstatusoutput('bash ' + path.get_path() + '/scripts/ca/gm/cts.sh gen_sdk_cert ' + dir + ' ' + sdk_dir) else: (status, result ) = utils.getstatusoutput('bash ' + path.get_path() + '/scripts/ca/cts.sh gen_sdk_cert ' + dir + ' ' + sdk_dir) if not status: consoler.info(' Generate sdk cert successful! dir is %s.', sdk_dir + '/sdk') logger.info(' Generate sdk cert successful! dir is %s.', sdk_dir + '/sdk') else: logger.error(' Generate sdk cert failed! Result is %s' % result) raise MCError(' Generate sdk cert failed! Result is %s' % result) except MCError as me: consoler.error(' \033[1;31m %s \033[0m', me) except Exception as e: consoler.error( ' \033[1;31m Generate root cert failed! excepion is %s.\033[0m', e) logger.error(' Generate root cert failed! Result is %s' % result)
def parser(self): self.clear() if os.path.exists(self.cfg) and os.path.isfile(self.cfg): logger.info(' single config is %s', self.cfg) # resolve one config.json try: self.append(ConfigConf(self.cfg)) except Exception as e: logger.warn('parser cfg %s end exception, e is %s ', self.cfg, e) raise MCError(' parser config failed, invalid format, config is %s, exception is %s' % (self.cfg, e)) elif os.path.isdir(self.cfg): logger.info(' config dir is %s', self.cfg) # resolve dir, if not config.json goto next for c in os.listdir(self.cfg): try: logger.debug(' config dir is %s, config file is %s', self.cfg, c) cc = ConfigConf(self.cfg + '/' + c) chain = cc.get_chain() if not self.append(cc): cc = self.get_cc(chain) logger.error(' chain_id: %s and chain_version: %s duplicate, config is %s:%s', chain.get_id(), chain.get_version(), cc.get_cfg(), c) raise MCError(' chain_id: %s and chain_version: %s duplicate, config is %s:%s' % (chain.get_id(), chain.get_version(), cc.get_cfg(), c)) logger.debug(' append cc, cc is %s', cc) consoler.info(' parser config %s success, chain_id is %s, chain_version is %s', c, chain.get_id(), chain.get_version()) except Exception as e: consoler.error( ' \033[1;31m skip config %s, invalid config format parser failed, exception is %s \033[0m', c, e) logger.warn(' parser cfg %s end exception, e %s ', c, e) else: raise MCError(' invalid config, %s not exist' % self.cfg)
def generate_root_ca(dir, gm=False): """[generate root cert] Arguments: dir {[path]} -- [root cert path] """ try: dir = os.path.abspath(dir) if gm: (status, result) = utils.getstatusoutput( 'bash ' + path.get_path() + '/scripts/ca/gm/cts.sh gen_chain_cert ' + dir) else: (status, result ) = utils.getstatusoutput('bash ' + path.get_path() + '/scripts/ca/cts.sh gen_chain_cert ' + dir) if status != 0: logger.warn( ' cts.sh failed! status is %d, output is %s, dir is %s.', status, result, dir) raise MCError( 'cts.sh failed! status is %d, output is %s, dir is %s.' % (status, result, dir)) logger.info(' cts.sh success! status is %d, output is %s, dir is %s.', status, result, dir) logger.info(' Generate root cert success, dir is %s', dir) consoler.info(' Generate root cert success, dir is %s' % dir) except MCError as me: consoler.error(' \033[1;31m %s \033[0m', me) except Exception as e: consoler.error( ' \033[1;31m Generate root cert failed! excepion is %s.\033[0m', e) logger.error(' Generate root cert failed! Result is %s' % result)
def load(self): self.clear() namesjson = data.package_names_dir() + '/names.json' if not os.path.exists(namesjson): logger.info(' names.json is not exist, path is %s', namesjson) return logger.debug(' load begin, path is %s', namesjson) try: with open(namesjson) as f: jsondata = json.load(f) if 'names' in jsondata: for k in jsondata['names'].keys(): chain_id = k chain_name = jsondata['names'][chain_id] self.append(chain_id, chain_name) logger.debug( ' load one name, chain id is %s, chain name is %s', chain_id, chain_name) return True except Exception as e: logger.error( ' parser namesjson failed, namesjson is %s, exception is %s', namesjson, e) return False logger.info(' load names end, info %s', self.names)
def publish_chain(chains, force = False): """Publish a chain to designated servers using publish_server Arguments: chains {list} -- chain which you wanto published, type chain_id:chain_version. Returns: null """ pchains = [] for i in range(len(chains)): chain = chains[i].split(':') if len(chain) != 2: logger.error('not chain_id:chain_version format, str is %s', chains[i]) consoler.error(' \033[1;31m skip, invalid publish format, chain_id:chain_version should require, chain is %s \033[0m', chain) continue chain_id = chain[0] chain_version = chain[1] pchains.append(Chain(chain_id, chain_version)) # consoler.info('\t append publish chain, chain_id %s:chain_version %s', chain_id, chain_version) logger.debug('chain_id is %s, chain_version is %s', chain_id, chain_version) if len(pchains) != 0: for chain in pchains: logger.info('publish, chain_id is %s, chain_version is %s', chain_id, chain_version) publish_server(chain.get_id(), chain.get_version(), force)
def load(self): self.clear() if not self.exist(): logger.info(' meta.json not exist, chain_id is ' + self.chain_id) return try: with open(data.meta_dir(self.chain_id) + '/meta.json', 'r') as f: jsondata = json.load(f) if 'chain_version' in jsondata: self.chain_version = str(jsondata['chain_version']) if 'nodes' in jsondata: for hm in jsondata['nodes'].values(): for v in hm: mn = MetaNode(v['host_ip'], v['rpc_port'], v['p2p_port'], v['channel_port'], v['node']) logger.info( 'load from meta.json, meta node is %s', mn) self.append(mn) logger.info(' Meta is %s', self.to_json()) except Exception as e: logger.error( ' load meta failed, chaind id is %s, exception is %s', self.chain_id, e) # raise or not ??? raise MCError(' load meta.json data failed, chain id is %s, exception is %s' % (self.chain_id, e))
def check_cert_sdk(path): """[check sdk cert exist] Arguments: path {[dir -> sdk cert]} -- [where sdk cert put] Raises: MCError -- [catch sdk Init status] Returns: [bool] -- [true or false] """ result = os.path.exists(path + '/sdk/sdk.crt') and os.path.exists(path + '/sdk/ca.crt') and os.path.exists(path + '/sdk/client.keystore')\ and os.path.exists(path + '/sdk/keystore.p12') and os.path.exists(path + '/sdk/sdk.csr') and os.path.exists(path + '/sdk/sdk.key') and os.path.exists(path + '/sdk/sdk.param')\ and os.path.exists(path + '/sdk/sdk.private') and os.path.exists(path + '/sdk/sdk.pubkey') try: if result: consoler.info(' Init sdk cert success!') else: logger.error(' Init sdk cert failed!') raise MCError(' Init sdk cert failed!') except MCError as me: logger.error(me) consoler.error(me) return result
def check_fisco_version(self): # check if fisco-bcos exists if not (os.path.exists(self.fisco_path) and os.path.isfile(self.fisco_path)): logger.error(' fisco-bcos not exist, fisco-bcos is %s', self.fisco_path) raise MCError(' fisco-bcos not exist, fisco-bcos is %s' % self.fisco_path) cmd = self.fisco_path + ' --version' status, output = utils.getstatusoutput(cmd) if status != 0: logger.error( ' fisco-bcos --version failed, fisco-bcos is %s, status is %d, output is %s', self.fisco_path, status, output) raise MCError('fisco-bcos --version failed , fisco-bcos is %s.' % self.fisco_path) logger.debug(' fisco-bcos --version, status is %d, output is %s', status, output) version_info = output.split() if version_info[0] == 'FISCO-BCOS' and len(version_info) > 2: version = version_info[2].split('.') if not len(version) == 3: logger.error( ' fisco-bcos --version invalid format, 00 status is %d, output is %s', status, output) raise MCError( ' fisco-bcos --version invalid format , fisco-bcos is %s, version is %s.' % (self.fisco_path, version_info[2])) if version[2].endswith('-gm'): self.gm = True self.major = str(int(version[0])) self.minor = str(int(version[1])) if self.gm: self.revision = str(int(version[2][:-3])) else: self.revision = str(int(version[2])) logger.info(' fisco-bcos is %s', self) # parser fisco-bcos version and check it. if not self.is_13_version(): logger.error( ' fisco-bcos is not 1.3.x version, not support now, %s', fisco) raise MCError( ' fisco-bcos is not 1.3.x version, not support now, %s' % fisco) else: logger.error( ' fisco-bcos --version invalid format, fisco-bcos is %s, status is %d, output is %s', self.fisco_path, status, output) raise MCError( ' fisco-bcos --version invalid format , fisco-bcos is %s, status is %d, output is %s.' % (self.fisco_path, status, output))
def register(self, node_json): register_command = 'bash %s NodeAction registerNode file:%s' % ( self.register_shell_file(), node_json) # node_all_command = 'bash %s NodeAction all' status, output = utils.getstatusoutput(register_command) if status != 0: logger.error( ' register node failed, node_json is %s,status is %d, output is %s', node_json, status, output) raise MCError(' register opr failed, status is %d ' % status)
def writeFile(self, file): try: with open(file, "w+") as f: f.write(self.toJson()) logger.debug(' write file success, file is %s', file) except Exception as e: logger.error(' write file failed, file is %s, exception is %s.', file, e) raise e
def expand_on_nonexist_chain(cc, dir): fisco_path = dir + '/' + 'fisco-bcos' genesisjson = dir + '/' + 'genesis.json' bootstrapnodesjson = dir + '/' + 'bootstrapnodes.json' # check if fisco-bcos、genesis.json、bootstrapsnode.json exist. if not os.path.exists(fisco_path): raise MCError( ' fisco bcos not exist, dir path is %s' % dir) if not os.path.exists(genesisjson): raise MCError( ' genesis.json not exist, dir path is %s' % dir) if not os.path.exists(bootstrapnodesjson): raise MCError( ' bootstrapnodes.json not exist, dir path is %s' % dir) chain = cc.get_chain() port = cc.get_port() chain_id = chain.get_id() chain_version = chain.get_version() # parser fisco-bcos version and check it. fisco = Fisco(fisco_path) logger.debug(' fisco is %s', fisco) acp = AllChainPort() # port check for node in cc.get_nodes(): for index in range(node.get_node_num()): # create dir for every node on the server acp.port_conflicts_outside_chain(chain.get_id(), node.get_host_ip(), port.to_port(index)) acp.port_conflicts_inside_chain(node.get_host_ip(), port.to_port(index) ,chain.get_id(), chain.get_version()) try: # create host dir os.makedirs(chain.data_dir()) # copy genesis.json bootstrapnodes.json to chain dir. shutil.copy(genesisjson, chain.data_dir() + '/') shutil.copy(bootstrapnodesjson, chain.data_dir() + '/') # create common dir build_pkg.build_common_dir(chain, fisco) # build install dir for every server for node in cc.get_nodes(): build_pkg.expand_host_dir(chain, node, port, fisco) # update web3sdk config web3_conf_by_chain(chain, fisco.is_gm()) except Exception as e: if os.path.exists(chain.data_dir()): shutil.rmtree(chain.data_dir()) logger.error(' expand failed, chain id is %s, chain version is %s, exception is %s.', chain_id, chain_version, e) raise MCError(' expand failed, chain id is %s, chain version is %s, exception is %s' % ( chain_id, chain_version, e))
def export(self): self.stop() export_command = 'bash ' + self.export_shell_file() + ' ' + \ self.chain.data_dir() + '/genesis.json' status, output = utils.getstatusoutput(export_command) if not os.path.exists(self.dir() + '/../genesis.json'): logger.error('export genesis.json failed, output is %s', output) raise MCError( ' export genesis.json failed, output is %s.' % output) else: logger.debug( 'export status, status is %d, output is %s', status, output)
def expand_host_dir(chain, node, port, fisco): """expand install pacakge of the server. Arguments: chain {Chain} -- chain info, chain id and chain version. node {Node} -- node info. port {Port} -- port info. Raises: Exception -- exception description """ logger.info(' chain is %s, node is %s, port is %s', chain, node, port) h = HostNodeDirs(chain.get_id(), chain.get_version(), node.get_host_ip()) index = h.get_max_index() append_host_dir = False if not h.exist(): h.create() append_host_dir = True logger.info(' append host dir, chain is %s, node is %s, index is %s.', chain, node, index) else: logger.info(' append node dir, chain is %s, node is %s, index is %s.', chain, node, index) try: # create node dir for i in range(node.get_node_num()): build_node_dir(chain, node, fisco, port.to_port(i), index + i + 1) except Exception as e: logger.error( ' expand operation failed, chain is %s, node is %s, append_host is %s, e is %s ', chain, node, append_host_dir, e) # Delete the created folder if append_host_dir: h.remove() else: for i in range(node.get_node_num()): # remove node dir node_dir = chain.data_dir() + '/' + node.get_host_ip( ) + '/' + str(index + i) if os.path.exists(node_dir): shutil.rmtree(node_dir) # raise exception again raise e logger.info(' expand_host_dir end.')
def mkdir_and_push(host, src, dst): # create dir on the target server ret = ansible.mkdir_module(host, dst) if not ret: logger.error(' mkdir dir on server %s failed, dir is %s.', host, dst) return ret # push file ret = ansible.copy_module(host, src, dst + '/') if not ret: logger.error(' push file to %s failed, src is %s, dst is %s.', host, src, dst) return ret return True
def valid_chain_id(chain_id): """[Determine if the chain id is valid] Arguments: ip {[string]} -- [chain id] Returns: [bool] -- [true or false] """ try: int(chain_id) return True except Exception as e: logger.error('%s is not a valid chain_id', e) return False
def build(cfg, fisco_path): """parser input config file, build install pacakge by Arguments: cfg {string} --config, maybe a config.json or a dictionary of conf,eg: ./conf/config.conf or ./conf fisco_path {string} -- path of fisco-bcos, eg: /usr/local/bin/fisco-bcos """ logger.info('build cfg is %s, fisco is %s ', cfg, fisco_path) try: # check java env java = JAVA() fisco = Fisco(fisco_path) # parser and check config if valid ccs = ConfigConfs(cfg).get_ccs() ns = Names() # build all chain if len(ccs) != 0: for cc in ccs.values(): try: chain_id = cc.get_chain().get_id() chain_version = cc.get_chain().get_version() chain_name = cc.get_chain().get_name() consoler.info( ' build install package for chain %s version %s.', chain_id, chain_version) build_chain.build(cc, fisco) ns.append(chain_id, chain_name) consoler.info( ' \t build install package for chain %s version %s success.', chain_id, chain_version) except MCError as me: logger.error(me) consoler.error(' \033[1;31m \t %s \033[0m', me) ns.write() else: consoler.info(' build operation will do nothing.') except MCError as me: logger.error(me) consoler.error(' \033[1;31m \t %s \033[0m', me) logger.info(' chain build end.')
def build(self): logger.info(' build temp dir, dir is %s', self.dir()) try: os.makedirs(self.dir()) shutil.copytree(path.get_path() + '/tpl/web3sdk', self.dir() + '/web3sdk') if self.is_gm(): shutil.move(self.dir() + '/web3sdk/conf/applicationContext_GM.xml', self.dir() + '/web3sdk/conf/applicationContext.xml') shutil.copytree(path.get_path() + '/tpl/GM_temp_node', self.dir() + '/node') shutil.copy(self.dir() + '/node/data/sdk/ca.crt', self.dir() + '/web3sdk/conf') shutil.copy(self.dir() + '/node/data/sdk/client.keystore', self.dir() + '/web3sdk/conf') else: shutil.move(self.dir() + '/web3sdk/conf/applicationContext_NB.xml', self.dir() + '/web3sdk/conf/applicationContext.xml') shutil.copytree(path.get_path() + '/tpl/temp_node', self.dir() + '/node') shutil.copy(self.dir() + '/node/sdk/ca.crt', self.dir() + '/web3sdk/conf') shutil.copy(self.dir() + '/node/sdk/client.keystore', self.dir() + '/web3sdk/conf') # copy fisco-bcos shutil.copy(self.fisco.get_fisco_path(), self.dir() + '/node/') # config.json for temp node Config('12345', self.port.get_rpc_port(), self.port.get_p2p_port(), self.port.get_channel_port(), self.is_gm()).writeFile(self.dir() + '/node/config.json') # web3sdk config for temp node utils.replace(self.dir() + '/web3sdk/conf/applicationContext.xml', 'WEB3SDK_NODES_LIST', '<value>[email protected]:%s</value>' % str(self.port.get_channel_port())) except Exception as e: logger.error( ' temp node build opr failed , chain is %s, exception is %s .', self.chain, e) raise MCError( ' build temp node failed, chain is %s, exception is %s ' % (self.chain, e))
def load(self): self.clear() hn = HostNodeDirs(self.chain_id, self.chain_version, self.host) host_dir = Chain(self.chain_id, self.chain_version).data_dir() + '/' + self.host + '/' for node in hn.get_node_dirs(): cfg_json = host_dir + '/' + node + '/config.json' if not os.path.exists(cfg_json): logger.error(' config not exist, config is %s .', cfg_json) continue cf = Config(self.chain_id) if cf.fromJson(cfg_json): p = Port(int(cf.get_rpc_port()), int(cf.get_p2p_port()), int(cf.get_channel_port())) self.ports[node] = p logger.debug(' append node, node is %s, port is %s', node, p) logger.info(' load end, hp ports is %s', self)
def fromJson(self, sjson): ''' resolve .json, convert to config ''' try: with open(sjson) as f: js = json.load(f) self.systemproxyaddress = js['systemproxyaddress'] self.rpcport = js['rpcport'] self.p2pport = js['p2pport'] self.channelPort = js['channelPort'] logger.debug( ' parser config success, cfg is %s, rpc : %s, p2p : %s, channel : %s', sjson, str(self.rpcport), str(self.p2pport), str(self.channelPort)) return True except Exception as e: logger.error(' parser config failed, cfg is %s, exception is %s', sjson, e) return False
def replace(filepath, old, new): """[replace old string to new from filepath] Arguments: filepath {[path]} -- [file path that needs to be replaced] old {[string]} -- [old string] new {[string]} -- [new string] """ if not os.path.exists(filepath): return False cmd = "sed -i 's|%s|%s|g' %s " % (old, new, filepath) status, output = getstatusoutput(cmd) if status != 0: logger.error( ' replace failed, new is %s, old is %s, file is %s, status is %s, output is %s ', new, old, filepath, str(status), output) return False return True
def write(self): if not self.changed: logger.debug(' do nothing. ') return if not os.path.exists(data.package_names_dir()): data.create_names_dir() names_file = data.package_names_dir() + '/names.json' names_bak_file = names_file + '_bak_' + \ time.strftime("%Y-%m-%d_%H-%M%S", time.localtime()) if os.path.exists(names_file): shutil.copy(names_file, names_bak_file) logger.info('names.json is exist, backup it, name is ' + names_bak_file) try: with open(names_file, "w+") as f: f.write(self.to_json()) logger.info(' write info names.json, content is ' + self.to_json()) except Exception as e: logger.error(' write names.json failed, exception is %s', e)
def fromGod(self, god_file): ''' resolve .json, convert to config ''' (address, publicKey, privateKey) = ('', '', '') try : with open(god_file) as f: for line in f.readlines(): line = line.strip() if line.startswith('address'): address = line.split(':')[1].strip() elif line.startswith('publicKey'): publicKey = line.split(':')[1].strip() elif line.startswith('privateKey'): privateKey = line.split(':')[1].strip() logger.info(' god config success, file is %s, address is %s, publicKey : %s, privateKey : %s', god_file, address, publicKey, privateKey) return (address, publicKey, privateKey) except Exception as e: logger.error(' god config failed, cfg is %s, exception is %s', god_file, e) raise e
def expand_on_exist_chain(cc): chain = cc.get_chain() port = cc.get_port() chain_id = chain.get_id() chain_version = chain.get_version() # expand on exist chain, check common、 genesis.json、 bootstrapnodes.json file exist. if not os.path.exists(chain.data_dir() + '/common'): raise MCError(' chain dir exist ,but common dir not exist, chain_id %s、chain_version %s' % ( chain_id, chain_version)) if not os.path.exists(chain.data_dir() + '/genesis.json'): raise MCError(' chain dir exist ,but genesis.json not exist, chain_id %s、chain_version %s' % ( chain_id, chain_version)) if not os.path.exists(chain.data_dir() + '/bootstrapnodes.json'): raise MCError(' chain dir exist ,but bootstrapnodes.json not exist, chain_id %s、and chain_version %s' % ( chain_id, chain_version)) acp = AllChainPort() # port check for node in cc.get_nodes(): for index in range(node.get_node_num()): # create dir for every node on the server acp.port_conflicts_outside_chain(chain.get_id(), node.get_host_ip(), port.to_port(index)) acp.port_conflicts_inside_chain(node.get_host_ip(), port.to_port(index) ,chain.get_id(), chain.get_version()) fisco = Fisco(chain.data_dir() + '/' + 'common' + '/' + 'fisco-bcos') # expand install dir for every server for node in cc.get_nodes(): try: build_pkg.expand_host_dir(chain, node, port, fisco) except Exception as e: logger.error(' expand failed, chain id is %s, chain version is %s, exception is %s.', chain_id, chain_version, e) raise MCError(' expand failed, chain id is %s, chain version is %s, exception is %s' % ( chain_id, chain_version, e)) web3_conf_by_chain(chain, fisco.is_gm())
def parser(mchain): """resolve mchain.conf Arguments: mchain {string} -- path of mchain.conf Raises: MCError -- exception description """ logger.info('mchain.conf is %s', mchain) # resolve configuration if not utils.valid_string(mchain): logger.error(' mchain.conf not invalid path, mchain.conf is %s', mchain) raise MCError(' mchain.conf not invalid path, mchain.conf is %s' % mchain) # read and parser config file cf = configparser.ConfigParser() try: with codecs.open(mchain, 'r', encoding='utf-8') as f: cf.readfp(f) except Exception as e: logger.error(' open mchain.conf file failed, exception is %s', e) raise MCError(' open mchain.conf file failed, exception is %s' % e) agent_name = cf.get('agent', 'agent_name') if not utils.valid_string(agent_name): logger.error( ' invalid mchain.conf format, agent_name empty, agent_name is %s', agent_name) raise MCError( ' invalid mchain.conf format, agent_name empty, agent_name is %s' % agent_name) MchainConf.agent_name = agent_name ansible_dir = cf.get('ansible', 'dir') if not utils.valid_string(ansible_dir): logger.error( ' invalid mchain.conf format, ansible_dir empty, ansible_dir is %s', ansible_dir) raise MCError( ' invalid mchain.conf format, ansible_dir empty, ansible_dir is %s' % ansible_dir) MchainConf.ansible_dir = ansible_dir logger.info('mchain.conf end, result is %s', MchainConf())
def generator_node_ca(agent, dir, node, gm=False): """[generate node cert ] Arguments: agent {[path]} -- [agency cert path] node {[string]} -- [node name] dir {[path]} -- [node cert path] """ _dir = os.path.abspath(dir) agent = os.path.abspath(agent) try: if gm: (status, result ) = utils.getstatusoutput('bash ' + path.get_path() + '/scripts/ca/gm/cts.sh gen_node_cert ' + agent + ' ' + _dir + '/ ' + node) else: (status, result ) = utils.getstatusoutput('bash ' + path.get_path() + '/scripts/ca/cts.sh gen_node_cert ' + agent + ' ' + _dir + '/ ' + node) if not status: logger.info(' Generate %s cert successful! dir is %s.', node, _dir + '/' + node) else: consoler.error( ' \033[1;31m Generate node cert failed! Please check your network, and try to check your opennssl version.\033[0m' ) logger.error(' Generate %s cert failed! Result is %s' % (node, result)) raise MCError(' Generate %s cert failed! Result is %s' % (node, result)) except MCError as me: consoler.error(' \033[1;31m %s \033[0m', me) except Exception as e: consoler.error( ' \033[1;31m Generate root cert failed! excepion is %s.\033[0m', e) logger.error(' Generate root cert failed! Result is %s' % result)
def generator_agent_ca(dir, ca, agent, gm=False): """[generate agency cert] Arguments: dir {[path]} -- [agency cert path] ca {[path]} -- [root cert path] agent {[string]} -- [agency name] """ try: ca = os.path.abspath(ca) dir = os.path.abspath(dir) if gm: (status, result) = utils.getstatusoutput( 'bash ' + path.get_path() + '/scripts/ca/gm/cts.sh gen_agency_cert ' + ca + ' ' + dir + ' ' + agent) else: (status, result ) = utils.getstatusoutput('bash ' + path.get_path() + '/scripts/ca/cts.sh gen_agency_cert ' + ca + ' ' + dir + ' ' + agent) if not status: logger.info(' Generate %s cert successful! dir is %s.' % (agent, dir + '/' + agent)) else: consoler.error( ' \033[1;31m Generate %s cert failed! Please check your network, and try to check your opennssl version.\033[0m' ) logger.error(' Generate %s cert failed! Result is %s' % (agent, result)) raise MCError(' Generate %s cert failed! Result is %s' % (agent, result)) except MCError as me: consoler.error(' \033[1;31m %s \033[0m', me) except Exception as e: consoler.error( ' \033[1;31m Generate root cert failed! excepion is %s.\033[0m', e) logger.error(' Generate root cert failed! Result is %s' % result)
def init_ca(cert_path): """[init users' agenct ca] Arguments: cert_path {[dir]} -- [usrs' cert path] Raises: Exception -- [normal error] Returns: [bool] -- [true or flase] """ if check_ca_exist(cert_path) and check_agent_ca_exist(cert_path): shutil.copytree(cert_path, get_ca_path()) logger.info("Init cert, copy cert to cert_path") elif check_gmca_exist(cert_path) and check_agent_gmca_exist(cert_path): shutil.copytree(cert_path, get_GM_ca_path()) logger.info("Init gm cert, copy gm cert to cert_path") else: logger.error("Init cert failed! files not completed") raise Exception("Init cert failed! files not completed") return 0
def export(self): try: if self.fisco.is_gm(): shutil.move(get_gm_god_path() + '/godInfo.txt', get_gm_god_path() + '/godInfo.txt.bak') cmd = self.fisco.get_fisco_path() + ' --newaccount ' + get_gm_god_path() + '/godInfo.txt' status, result = utils.getstatusoutput(cmd) logger.debug(' start status, status is %d, output is %s', status, result) else: shutil.move(get_god_path() + '/godInfo.txt', get_god_path() + '/godInfo.txt.bak') cmd = self.fisco.get_fisco_path() + ' --newaccount ' + get_god_path() + '/godInfo.txt' status, result = utils.getstatusoutput(cmd) logger.debug(' start status, status is %d, output is %s', status, result) if status != 0: logger.warn(' export godInfo.txt failed! status is %d, output is %s.', status, result) raise MCError('godInfo.txt failed! status is %d, output is %s.' % (status, result)) logger.info(' export godInfo.txt status is %d, output is %s.', status, result) consoler.info(' export godInfo.txt success') except MCError as me: consoler.error(' \033[1;31m %s \033[0m', me) except Exception as e: consoler.error(' \033[1;31m export godInfo.txt failed! excepion is %s.\033[0m', e) logger.error(' export godInfo.txt failed!')
def write_to_file(self): #if len(self.nodes) == 0: # logger.debug('nodes empty, write return') # return if not data.meta_dir_exist(self.chain_id): data.create_meta_dir(self.chain_id) meta_file = data.meta_dir(self.chain_id) + '/meta.json' meta_bak_file = meta_file + '_bak_' + \ time.strftime("%Y-%m-%d_%H-%M%S", time.localtime()) if os.path.exists(meta_file): shutil.copy(meta_file, meta_bak_file) logger.info( 'meta.json is exist, backup it, name is ' + meta_bak_file) try: with open(data.meta_dir(self.chain_id) + '/meta.json', "w+") as f: f.write(self.to_json()) logger.info( 'write info meta.json, content is ' + self.to_json()) except Exception as e: logger.error( ' write meta failed, chaind id is %s, exception is %s', self.chain_id, e) # raise or not ??? raise MCError(' write meta.json failed, chain id is %s, exception is %s' % (self.chain_id, e))
def check_java(self): cmd = 'java -version' status, output = utils.getstatusoutput(cmd) if status != 0: logger.error(' java -version failed , status is %d, output is %s', status, output) raise MCError(' java -version failed , java not installed.') version_str = output.split("\"") if not len(version_str) > 1: logger.error( ' cannot get java version, status is %d, output is %s', status, output) raise MCError( ' cannot get java version, oracle jdk need >=1.8 or openjdk need >= 1.9, please try \'java -version\'. ' ) version_arr = version_str[1].split('.') if not len(version_arr) > 2: logger.error( ' cannot get java version, status is %d, output is %s', status, output) raise MCError( ' cannot get java version, oracle jdk need >=1.8 or openjdk need >= 1.9, please try \'java -version\' ' ) self.major = version_arr[0] self.minor = version_arr[1] if output.lower().find('openjdk') != -1: self.openjdk = True else: self.openjdk = False if not self.is_suitable(): raise MCError( ' invalid java version, oracle jdk need >=1.8 or openjdk need >= 1.9, now %s ' % self) logger.info(' java version is %s ', self)