def generate_root_ca(_dir): """[generate root cert] Arguments: dir {[path]} -- [root cert path] """ try: ca_dir = os.path.abspath(_dir) if utils.Status.gm_option: os.chdir('{}/scripts/gm/'.format(path.get_path())) (status, result) = utils.getstatusoutput('./cts.sh gen_chain_cert {}' .format(ca_dir)) os.chdir('{}'.format(path.get_path())) else: os.chdir('{}/scripts'.format(path.get_path())) (status, result) = utils.getstatusoutput('./cts.sh gen_chain_cert {}' .format(ca_dir)) os.chdir('{}'.format(path.get_path())) if bool(status): LOGGER.error( ' cts.sh failed! status is %d, output is %s, dir is %s.', status, result, ca_dir) raise MCError('cts.sh failed! status is %d, output is %s, dir is %s.' % ( status, result, ca_dir)) LOGGER.info( ' cts.sh success! status is %d, output is %s, dir is %s.', status, result, ca_dir) LOGGER.info(' Generate root cert success, dir is %s', ca_dir) CONSOLER.info(' Generate root cert success, dir is %s', ca_dir) except MCError as cert_exp: console_error(' %s ' % cert_exp) except Exception as gen_cert_exp: console_error( ' Generate root cert failed! excepion is %s.' % gen_cert_exp) LOGGER.error(' Generate root cert failed! Result is %s', result) raise MCError( 'Generate root agency failed! Result is %s' % gen_cert_exp)
def add_group(_group, _node): """ Arguments: _group {[type]} -- [description] _node {[type]} -- [description] """ data_path = _group node_send = [] utils.file_must_exists(data_path) file_name = os.path.basename(data_path) group_id = utils.valid_genesis(file_name) if group_id == 0: raise MCError(' paser %s file failed' % (data_path)) node_name = os.path.basename(os.path.normpath(_node)) if utils.valid_node_dir(node_name): utils.file_must_not_exists('{}/conf/{}'.format(_node, file_name)) shutil.copyfile(data_path, '{}/conf/{}'.format(_node, file_name)) shutil.copyfile('{}/tpl/group.i.ini'.format(path.get_path()), '{}/conf/group.{}.ini'.format(_node, group_id)) else: node_send = utils.get_all_nodes_dir(_node) for node_file in node_send: utils.file_must_not_exists('{}/conf/{}'.format( node_file, file_name)) shutil.copyfile(data_path, '{}/conf/{}'.format(node_file, file_name)) shutil.copyfile('{}/tpl/group.i.ini'.format(path.get_path()), '{}/conf/group.{}.ini'.format(node_file, group_id))
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 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 generator_node_ca(_dir, agent, node): """[generate node cert ] Arguments: agent {[path]} -- [agency cert path] node {[string]} -- [node name] dir {[path]} -- [node cert path] """ node_dir = os.path.abspath(_dir) agent = os.path.abspath(agent) try: if utils.Status.gm_option: os.chdir('{}/scripts/gm/'.format(path.get_path())) (status, result) = utils.getstatusoutput( './cts.sh' ' gen_node_cert {} {}/{}'.format(agent, node_dir, node)) os.chdir('{}'.format(path.get_path())) else: os.chdir('{}/scripts/'.format(path.get_path())) (status, result) = utils.getstatusoutput( './cts.sh' ' gen_node_cert {} {}/{}'.format(agent, node_dir, node)) os.chdir('{}'.format(path.get_path())) if not bool(status): LOGGER.info(' Generate %s cert successful! dir is %s/%s.', node, node_dir, node) os.chdir('{}'.format(path.get_path())) if utils.Status.gm_option: (status, result) = utils.getstatusoutput( 'cat {}/{}/gmagency.crt ' '>> {}/{}/gmnode.crt'.format(_dir, node, _dir, node)) os.remove('{}/{}/gmagency.crt'.format(_dir, node)) os.remove('{}/{}/gmnode.serial'.format(_dir, node)) else: (status, result) = utils.getstatusoutput( 'cat {}/{}/agency.crt ' '>> {}/{}/node.crt'.format(_dir, node, _dir, node)) os.remove('{}/{}/agency.crt'.format(_dir, node)) os.remove('{}/{}/node.ca'.format(_dir, node)) os.remove('{}/{}/node.json'.format(_dir, node)) os.remove('{}/{}/node.private'.format(_dir, node)) os.remove('{}/{}/node.serial'.format(_dir, node)) os.remove('{}/{}/node.param'.format(_dir, node)) os.remove('{}/{}/node.pubkey'.format(_dir, node)) else: # console_error( # ' Generate node cert failed! Please check your network,' # ' and try to check your opennssl version.') 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 cert_exp: console_error(' %s ' % cert_exp) except Exception as gen_cert_exp: console_error(' Generate node cert failed! excepion is %s.' % gen_cert_exp) LOGGER.error(' Generate node cert failed! Result is %s', result) raise MCError('Generate node failed! Result is %s' % gen_cert_exp)
def package(data_path, peer_path): utils.file_must_exists('{}/meta/fisco-bcos'.format(path.get_path())) utils.check_fisco('{}/meta/fisco-bcos'.format(path.get_path())) if (os.path.exists(peer_path) and os.path.isfile(peer_path)): mconf.read_peers(peer_path) else: mconf.default_peers() config.build_package_only(data_path)
def download_tassl(): """[download_tassl] """ os.chdir('{}/scripts/gm/'.format(path.get_path())) (status, result) = utils.getstatusoutput('./cts.sh download_tassl') os.chdir('{}'.format(path.get_path())) if bool(status): raise EOFError(' download tassl failed failed! status is %d, output is %s.' % ( status, result))
def replace(self): (address, publicKey, privateKey) = ('', '', '') if self.fisco.is_gm(): god_file = get_gm_god_path() + '/godInfo.txt' (address, publicKey, privateKey) = self.fromGod(god_file) genesis_path = path.get_path() + '/tpl/GM_temp_node/genesis.json' utils.replace(genesis_path, self.address, address) else: god_file = get_god_path() + '/godInfo.txt' (address, publicKey, privateKey) = self.fromGod(god_file) genesis_path = path.get_path() + '/tpl/temp_node/genesis.json' utils.replace(genesis_path, self.address, address)
def build(peer_path, data_path): """[--build] """ utils.file_must_exists('{}/meta/fisco-bcos'.format(path.get_path())) utils.check_fisco('{}/meta/fisco-bcos'.format(path.get_path())) if utils.Status.gm_option: utils.file_must_exists('{}/meta/gmca.crt'.format(path.get_path())) else: utils.file_must_exists('{}/meta/ca.crt'.format(path.get_path())) utils.file_must_exists(peer_path) mconf.read_peers(peer_path) config.build_config_ini(data_path) opr_cert.deploy_key('{}/meta'.format(path.get_path()), data_path)
def env_check(hosts): """[check and confirm the environment normally] Arguments: hosts {string} -- host list """ if hosts[0] == 'all': ansible.env_check('all', path.get_path()) else: for host in hosts: if utils.valid_ip(host): ansible.env_check(host, path.get_path()) else: consoler.log(' skip, not invalid host, host is %s', host)
def download_console(_dir): """[summary] Arguments: _dir {[type]} -- [description] Raises: MCError -- [description] MCError -- [description] """ dir_must_exists(_dir) bin_path = _dir meta = '{}/meta'.format(path.get_path()) # file_must_exists('{}/ca.crt'.format(meta)) # file_must_exists('{}/agency.crt'.format(meta)) # file_must_exists('{}/agency.key'.format(meta)) download_console_command = "bash {}/tpl/{}".format( path.get_path(), Status.download_console_shell_script) cdn_option = "" if Status.use_cdn: CONSOLER.info("download_console: use cdn") cdn_option = "-n" download_console_command = "{} {}".format(download_console_command, cdn_option) if (Status.download_console_version_specified is True): download_console_command = "{} -c {}".format( download_console_command, Status.download_console_version) if (Status.solidity_version_specified is True): download_console_command = "{} -v {}".format(download_console_command, Status.solidity_version) CONSOLER.info("The download_console_command is %s", download_console_command) # execute the download_console_command (status, result) = getstatusoutput(download_console_command) if bool(status): LOGGER.error(' download console failed, result is %s.', result) raise MCError(' download console failed, result is %s.' % result) chmod_command = 'chmod a+x console/start.sh' if bin_path != "." and bin_path != "./": chmod_command = "{} && mv console {}".format(chmod_command, bin_path) (status, result) = getstatusoutput(chmod_command) if bool(status): LOGGER.error('chmod console failed! status is %d,' ' output is %s.', status, result) raise MCError('chmod console failed!' ' status is %d, output is %s.' % (status, result))
def version(): """load release_node.txt, print version number. """ with open('{}/release_note.txt'.format(path.get_path()), 'r') as file_releas: CONSOLER.info(file_releas.read())
def unregister_module(ip, dest, index): """Using ansible.sh unregister_module, start nodes Arguments: ip {string} -- corresponding server host ip dest {string} -- corresponding server dir path index {int} -- node index to be register Returns: bool -- true or false """ (status, result) = utils.getstatusoutput('bash ' + path.get_path() + '/scripts/ansible.sh unregister ' + ip + ' ' + dest + ' ' + str(index)) logger.debug(' unregister action, status %s, output %s' % (status, result)) if status: consoler.warn(' ansible unregister failed, host is %s, index is %s, dst is %s, status is %s, output is %s.', ip, str(index), dest, status, result) elif result.find('SUCCESS') == -1 and result.find('CHANGED') == -1: consoler.warn(' ansible unregister failed, host is %s, index is %s, dst is %s, status is %s, output is %s.', ip, str(index), dest, status, result) elif not (result.find('success') + 1): consoler.warn(' ansible register failed, host is %s, index is %s, dst is %s, status is %s, output is %s.', ip, str(index), dest, status, result) else: consoler.info(' ansible unregister success, host is %s, index is %s, output is %s.', ip, str(index), result) return True return False
def check_module(ip, dest): """Using ansible.sh check_module, check chain status Arguments: ip {string} -- corresponding server host ip dest {string} -- corresponding server dir path Returns: bool -- true or false """ (status, result) = utils.getstatusoutput('bash ' + path.get_path() + '/scripts/ansible.sh check ' + ip + ' ' + dest) logger.debug('check action , status %s, output %s' % (status, result)) if status: logger.warn('check action failed, status %s' % (status)) consoler.warn(' ansible check failed, host is %s, dst is %s, status is %s, output is %s.', ip, dest, status, result) elif result.find('SUCCESS') == -1 and result.find('CHANGED') == -1: logger.warn('check action failed, output %s' % (result)) consoler.warn(' ansible check failed, host is %s, dst is %s, status is %s, output is %s.', ip, dest, status, result) else: consoler.info(' ansible check success, host is %s, output is %s.', ip, result) return True return False
def cmd_module(ip, cmd): """Using ansible.sh cmd_module, execute commands on the corresponding server. Arguments: ip {string} -- server host ip Keyword Arguments: msg {string} -- execute commands Returns: [bool] -- true or false """ cmd = '"' + cmd + '"' (status, result) = utils.getstatusoutput('bash ' + path.get_path() + '/scripts/ansible.sh cmd ' + ip + ' ' + cmd) logger.debug(' cmd action , status %s, output %s' % (status, result)) if status: consoler.error(' \033[1;31m ansible cmd failed, host is %s, output is %s \033[0m', ip, result) elif result.find('SUCCESS') == -1 and result.find('CHANGED') == -1: consoler.error(' \033[1;31m ansible cmd failed, host is %s, output is %s \033[0m', ip, result) else: consoler.info(' ansible cmd success, host is %s, cmd is %s.', ip, cmd) return True return False
def build_console(_console_dir): """[download console] Arguments: _console_dir {[type]} -- [description] """ data = _console_dir utils.download_console(data) CONSOLER.info( "download console success, obtain the sdk certificates now...") if utils.Status.gm_ssl: opr_cert.get_console_cert_gmssl('{}/console/conf'.format(data)) else: opr_cert.get_console_cert('{}/console/conf'.format(data)) CONSOLER.info( "obtain the sdk certificates success, configure the console now") if utils.console_use_xml_configuration(): CONSOLER.info("configure applicationContext.xml") shutil.copyfile( '{}/tpl/applicationContext.xml'.format(path.get_path()), '{}/console/conf/applicationContext.xml'.format(data)) config.get_console_file( '{}/console/conf/applicationContext.xml'.format(data)) else: CONSOLER.info("configure config-example.toml") # copy the config-example.toml to config.toml shutil.copyfile('{}/console/conf/config-example.toml'.format(data), '{}/console/conf/config.toml'.format(data)) # update the connections config.config_console_toml_file( '{}/console/conf/config.toml'.format(data)) CONSOLER.info("configure the console success")
def copy_module(ip, src, dest): """[Using ansible.sh copy_module, push package to servers] Arguments: ip {string} -- corresponding server host ip src {string} -- files which push dest {string} -- corresponding server dir path Returns: bool -- true or false. """ (status, result) = utils.getstatusoutput('bash ' + path.get_path() + '/scripts/ansible.sh copy ' + ip + ' ' + src + ' ' + dest) logger.debug('copy action , status %s, output %s' % (status, result)) if status: logger.warn('copy action failed, status %s' % (status)) consoler.warn(' ansible copy failed, host is %s, src is %s, dst is %s, status is %s, output is %s.', ip, src, dest, status, result) elif result.find('SUCCESS') == -1 and result.find('CHANGED') == -1: consoler.warn(' ansible copy failed, host is %s, src is %s, dst is %s, status is %s, output is %s.', ip, src, dest, status, result) logger.warn('copy action failed, output %s' % (result)) else: consoler.info(' ansible copy success, host is %s, src is %s, dst is %s.', ip, src, dest) return True return False
def unarchive_module(ip, src, dest): """[Using ansible.sh unarchive_module, compress files to the corresponding server and extract it] Arguments: ip {[string]} -- [corresponding server host ip] src {[string]} -- [files dir path] dest {[string]} -- [corresponding server dir path] Returns: [bool] -- [true or false] """ (status, result) = utils.getstatusoutput('bash ' + path.get_path() + '/scripts/ansible.sh unarchive ' + ip + ' ' + src + ' ' + dest) logger.debug('unarchive action , status %s, output %s' % (status, result)) if status: logger.warn('unarchive action failed, status %s' % (status)) consoler.warn(' ansible unarchive failed, host is %s, src is %s, dst is %s, status is %s, output is %s.', ip, src, dest, status, result) elif result.find('SUCCESS') == -1 and result.find('CHANGED') == -1: logger.warn('unarchive action failed, output %s' % (result)) consoler.warn(' ansible unarchive failed, host is %s, src is %s, dst is %s, status is %s, output is %s.', ip, src, dest, status, result) else: consoler.info(' ansible unarchive success, host is %s, src is %s, dst is %s.', ip, src, dest) return True return False
def init_ansible(hosts_conf, add_opr=False): try: if not os.path.exists(hosts_conf): raise MCError('hosts_conf not exisits! ') if add_opr: src = '/etc/ansible/hosts' dst = '/etc/ansible/hosts.bak' if not os.path.exists(src): raise MCError('/etc/ansible/hosts not exisits! ') os.rename(src, dst) f = open(src, 'w') f.close() for line in open(hosts_conf): line = line.strip() host_value = line.split() if len(host_value) != 4: raise Exception('hosts_conf type error ,host_line -> %s', host_value) user = host_value[0] ip = host_value[1] port = host_value[2] passwd = host_value[3] if not utils.valid_string(user): raise Exception( 'user type error ,user -> %s, host_line -> %s' % (user, host_value)) if not utils.valid_ip(ip): raise Exception('ip type error ,ip -> %s, host_line -> %s' % (ip, host_value)) if not utils.valid_port(int(port)): raise Exception( 'port type error ,port -> %s, host_line -> %s' % (port, host_value)) if not utils.valid_string(passwd): raise Exception( 'passwd type error ,passwd -> %s, host_line -> %s' % (passwd, host_value)) (status, result) = utils.getstatusoutput('bash ' + path.get_path() + '/scripts/ansible_init.sh' + ' ' + user + ' ' + ip + ' ' + port + ' ' + passwd) if status != 0: logger.warn( ' ansible_init failed! status is %d, output is %s.', status, result) raise MCError( 'ansible_init failed! status is %d, output is %s.' % (status, result)) result = [] result.append(ip) opr_tools.telnet_ansible(result) logger.info(' ansible_init success! status is %d, output is %s', status, result) except MCError as me: consoler.error(' \033[1;31m %s \033[0m', me) except Exception as e: consoler.error( ' \033[1;31m ansible_init failed! excepion is %s.\033[0m', e)
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 env_check(ip, src): """Check whether the environment of the corresponding server satisfy the fisco bcos running conditions. Keyword Arguments: ip {string} -- [corresponding server host ip] (default: {'all'}) """ os.system('bash ' + path.get_path() + '/scripts/ansible.sh env_check ' + ip + ' ' + src)
def generator_agent_ca(_dir, _ca, agent): """[generate agency cert] Arguments: dir {[path]} -- [agency cert path] ca {[path]} -- [root cert path] agent {[string]} -- [agency name] """ try: ca_dir = os.path.abspath(_ca) agency_dir = os.path.abspath(_dir) if utils.Status.gm_option: os.chdir('{}/scripts/gm/'.format(path.get_path())) (status, result) = utils.getstatusoutput('./cts.sh' ' gen_agency_cert {} {}/{}' .format(ca_dir, agency_dir, agent)) os.chdir('{}'.format(path.get_path())) else: os.chdir('{}/scripts'.format(path.get_path())) (status, result) = utils.getstatusoutput('./cts.sh' ' gen_agency_cert {} {}/{}' .format(ca_dir, agency_dir, agent)) os.chdir('{}'.format(path.get_path())) if not bool(status): LOGGER.info(' Generate %s cert successful! dir is %s/%s.', agent, agency_dir, agent) else: # console_error( # ' Generate cert failed! Please check your network,' # ' and try to check your opennssl version.') 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 cert_exp: console_error(' %s ' % cert_exp) except Exception as gen_cert_exp: console_error( ' Generate agency cert failed! excepion is %s.' % gen_cert_exp) LOGGER.error(' Generate agency cert failed! Result is %s', result) raise MCError( 'Generate agency agency failed! Result is %s' % gen_cert_exp)
def download_console(_dir): """[summary] Arguments: _dir {[type]} -- [description] Raises: MCError -- [description] MCError -- [description] """ dir_must_exists(_dir) bin_path = _dir meta = '{}/meta'.format(path.get_path()) file_must_exists('{}/ca.crt'.format(meta)) file_must_exists('{}/agency.crt'.format(meta)) file_must_exists('{}/agency.key'.format(meta)) package_name = "console.tar.gz" dir_must_not_exists('{}/console'.format(bin_path)) (status, version) = getstatusoutput( 'curl -s https://api.github.com/repos/FISCO-BCOS/' 'console/releases | grep "tag_name" ' '| sort -u | tail -n 1 | cut -d \\" -f 4 | sed "s/^[vV]//"') if bool(status): LOGGER.error(' get fisco-bcos verion failed, result is %s.', version) raise MCError(' get fisco-bcos verion failed, result is %s.' % version) download_link = 'https://github.com/FISCO-BCOS/console/releases/download/v{}/{}'.format( version.strip('\n'), package_name.strip('\n')) cnd_link = 'https://www.fisco.com.cn/cdn/console/releases/download/v{}/{}'.format( version.strip('\n'), package_name.strip('\n')) if valid_url(cnd_link): LOGGER.info("Downloading console binary from %s", cnd_link) CONSOLER.info("Downloading console binary from %s", cnd_link) download_bin(cnd_link, package_name) elif valid_url(download_link): LOGGER.info("Downloading console binary from %s", download_link) CONSOLER.info("Downloading console binary from %s", download_link) download_bin(download_link, package_name) else: LOGGER.error(' Download console failed, Please check your network!') raise MCError(' Download console failed, Please check your network!') (status, result)\ = getstatusoutput('tar -zxf {} -C {} && ' 'rm {}'.format(package_name, bin_path, package_name)) if bool(status): LOGGER.error(' Decompress console failed, result is %s.', result) raise MCError(' Decompress console failed, result is %s.' % result) (status, result) = getstatusoutput( 'chmod a+x {}/console/start.sh'.format(bin_path)) if bool(status): LOGGER.error('chmod console failed! status is %d,' ' output is %s.', status, result) raise MCError('chmod console failed!' ' status is %d, output is %s.' % (status, result))
def build_console(_console_dir): """[download console] Arguments: _console_dir {[type]} -- [description] """ data = _console_dir utils.download_console(data) opr_cert.get_console_cert('{}/console/conf'.format(data)) shutil.copyfile('{}/tpl/applicationContext.xml'.format(path.get_path()), '{}/console/conf/applicationContext.xml'.format(data)) config.get_console_file( '{}/console/conf/applicationContext.xml'.format(data))
def get_sdk(_dir): """[summary] Arguments: _dir {[type]} -- [description] """ data = _dir utils.dir_must_not_exists(_dir) os.mkdir(_dir) opr_cert.get_console_cert(_dir) shutil.copyfile('{}/tpl/applicationContext.xml'.format(path.get_path()), '{}/applicationContext.xml'.format(data)) config.get_console_file('{}/applicationContext.xml'.format(data))
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 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 get_console_cert(_dir): """get console certs Arguments: _dir {[type]} -- [description] """ LOGGER.info("get console in %s!", _dir) CONSOLER.info("get console in %s!", _dir) meta = '{}/meta'.format(path.get_path()) data = _dir get_sdk_cert() utils.dir_must_exists(data) shutil.copyfile('{}/ca.crt'.format(meta), '{}/ca.crt'.format(data)) shutil.copyfile('{}/sdk/node.key'.format(meta), '{}/node.key'.format(data)) shutil.copyfile('{}/sdk/node.crt'.format(meta), '{}/node.crt'.format(data))
def diagnose_module(ip, dest): """Using ansible.sh diagnose_module, call script -> monotor.sh, Check status of nodes Arguments: ip {string} -- corresponding server host ip dest {string} -- corresponding server dir path """ (status, result) = utils.getstatusoutput('bash ' + path.get_path() + '/scripts/ansible.sh diagnose ' + ip + ' ' + dest) logger.debug('diagnose action , status %s, output %s' % (status, result)) if status: consoler.error(' \033[1;31m ansible diagnose failed, host is %s, output is %s \033[0m', ip, result) elif result.find('SUCCESS') == -1 and result.find('CHANGED') == -1: consoler.error(' \033[1;31m ansible diagnose failed, host is %s, output is %s \033[0m', ip, result) else: consoler.info(' ansible diagnose success, host is %s, result is %s.', ip, result) return True return False
def get_sdk_cert(): """[summary] Arguments: _dir {[type]} -- [description] """ LOGGER.info("get sdk cert in meta!") CONSOLER.info("get sdk cert in meta!") meta = '{}/meta'.format(path.get_path()) utils.file_must_exists('{}/ca.crt'.format(meta)) utils.file_must_exists('{}/agency.crt'.format(meta)) utils.file_must_exists('{}/agency.key'.format(meta)) if os.path.isdir('{}/sdk'.format(meta)): utils.file_must_exists('{}/sdk/ca.crt'.format(meta)) utils.file_must_exists('{}/sdk/node.crt'.format(meta)) utils.file_must_exists('{}/sdk/node.key'.format(meta)) LOGGER.info("sdk cert existed!") CONSOLER.info("sdk cert existed!") else: LOGGER.info("generate console cert!") CONSOLER.info("generate console cert!") ca.generator_node_ca(meta, meta, 'sdk')