def check_chain(chain): """[Check if the nodes is running normally.] Arguments: chain {[list]} -- [get chain_id:host_ip from command Line] """ if chain[0] == 'all': dir = data.meta_dir_base() if os.path.exists(dir): for chain_id in os.listdir(dir): check_server(chain_id) else: consoler.info(' No published chain exist, do nothing.') else: for i in range(len(chain)): chain_get = chain[i].split(':') if len(chain_get) == 1: if utils.valid_chain_id(chain_get[0]): check_server(chain_get[0]) else: consoler.info(' skip, invalid chain_id, chain_id is %s', chain_get[0]) elif len(chain_get) == 2: if utils.valid_chain_id(chain_get[0]): if utils.valid_ip(chain_get[1]): ansible.check_module(chain_get[1], ansible.get_dir() + '/' + chain_get[0]) else: consoler.info(' skip, invalid host, chain_id is %s, host is %s', chain_get[0], chain_get[1]) else: consoler.info(' skip, invalid chain_id, chain_id is %s, host is %s', chain_get[0], chain_get[1]) else: consoler.info(' skip, invalid format, not chain_id:host, input %s', chain_get)
def stop_chain(chain): """[stop nodes] Arguments: chain {[list]} -- [get chain_id:host_ip from command Line] """ if chain[0] == 'all': consoler.info('You want to stop all node,are you sure? yes or no? y/n') consoler.info('Your choice is : ') choice = sys.stdin.readline().strip('\n') if ((choice == 'yes') | (choice == 'Yes') | (choice == 'Y') | (choice == 'y')): dir = data.meta_dir_base() if os.path.exists(dir): for chain_id in os.listdir(dir): stop_server(chain_id) else: consoler.info(' No published chain exist, do nothing.') else: consoler.info(' input No, and will do nothing.') logger.info('refuse stop all node') else: for i in range(len(chain)): chain_get = chain[i].split(':') if len(chain_get) == 1: if utils.valid_chain_id(chain_get[0]): stop_server(chain_get[0]) else: consoler.info(' skip, invalid chain_id, chain_id is %s', chain_get[0]) elif len(chain_get) == 2: if utils.valid_chain_id(chain_get[0]): if utils.valid_ip(chain_get[1]): ansible.stop_module( chain_get[1], ansible.get_dir() + '/' + chain_get[0]) else: consoler.info( ' skip, invalid host, chain_id is %s, host is %s', chain_get[0], chain_get[1]) else: consoler.info( ' skip, invalid chain_id, chain_id is %s, host is %s', chain_get[0], chain_get[1]) else: consoler.info( ' skip, invalid format, not chain_id:host, input %s', chain_get)
def do_cmd(dst, cmd): """do cmd on remote server, dst can be one server, one chain or all server Arguments: dst {string} -- host ip or chain id or 'all' cmd {string} -- shell cmd or shell file """ if dst == 'all': ansible.cmd_module('all', cmd) elif utils.valid_chain_id(dst): mm = Meta(dst) if not mm.exist(): consoler.error( ' \033[1;31m chain is not published, can not cmd action, chain_id is %s \033[0m', dst) else: consoler.info(' => do cmd, chain id is %s', dst) for k in mm.get_nodes().keys(): logger.debug('host ip is ' + k) ansible.cmd_module(k, cmd) elif utils.valid_ip(dst): ansible.cmd_module(dst, cmd) else: consoler.error( ' \033[1;31m invalid docmd dst, dst is %s, dst should be invalid chain_id or invali host ip or \'all\'. \033[0m', dst)
def load(self): dir = data.package_dir_base() if os.path.exists(dir): for list_dir in os.listdir(dir): if utils.valid_chain_id(list_dir): self.chains.append(list_dir) logger.info(' append chain , chain id is %s', list_dir) logger.info(' chains is %s', self.chains)
def push_file(host, src, dst): """push file to remote server Arguments: host {string} -- host ip or chain id or 'all' src {string} -- file or dir dst {string} -- dst dir """ if not os.path.exists(src): consoler.error(' \033[1;31m src is not exist, src is %s. \033[0m', src) return logger.info(' host is %s, src is %s, dst is %s', host, src, dst) if host == 'all': if mkdir_and_push(host, src, dst): consoler.info(' push %s to %s of all server success.', src, dst) elif utils.valid_chain_id(host): consoler.info(' => push %s to %s of chain %s.', src, dst, host) mm = Meta(host) if not mm.exist(): consoler.error( ' \033[1;31m chain is not published, can not push file action, chain_id is %s \033[0m', host) else: consoler.info(' => do cmd, chain id is %s', host) for k in mm.get_nodes().keys(): logger.debug(' host is %s', k) if mkdir_and_push(k, src, dst): consoler.info(' \t\t push %s to %s of %s server success.', src, dst, k) consoler.info(' => push %s to %s of chain %s end.', src, dst, host) elif utils.valid_ip(host): if mkdir_and_push(host, src, dst): consoler.info(' push %s to %s of %s server success.', src, dst, host) else: consoler.error( ' \033[1;31m invalid push file host, host is %s, dst should be invalid chain_id or invali host ip or \'all\'. \033[0m', host)
def parser(self): ''' resolve config.conf, return object[Config.conf] ''' cfg = self.cfg logger.info('cfg parser %s', cfg) # read and parser config file cf = configparser.ConfigParser() with codecs.open(cfg, 'r', encoding='utf-8') as f: cf.readfp(f) chain_id = cf.get('chain', 'chainid') if not utils.valid_string(chain_id): raise Exception('chain_id empty.') if not utils.valid_chain_id(chain_id): raise Exception('invalid chain_id, ', chain_id) chain_version = cf.get('chain', 'version') if not utils.valid_string(chain_id): raise Exception('chain_version empty.') chain_name = str(chain_id) try: chain_name = cf.get('chain', 'chainname') if not utils.valid_string(chain_name): chain_name = str(id) except Exception as e: pass self.set_chain(Chain(chain_id, chain_version, chain_name)) rpc_port = cf.getint('ports', 'rpc_port') if not utils.valid_port(rpc_port): raise Exception('invalid rpc_port, ', rpc_port) p2p_port = cf.getint('ports', 'p2p_port') if not utils.valid_port(p2p_port): raise Exception('invalid p2p_port, ', p2p_port) channel_port = cf.getint('ports', 'channel_port') if not utils.valid_port(channel_port): raise Exception('invalid channel_port, ', channel_port) port = Port(rpc_port, p2p_port, channel_port) if port.check_port(): raise Exception('port config dup, ', port) self.set_port(port) index = 0 while True: try: n = NodeEle(cf.get('pkgs', 'pkg%u' % index)) index += 1 n.do_parser() except Exception as e: logger.info('cfg parser end, e is %s, result is %s', e, self) break else: if not self.add_node(n): raise Exception(' duplicate host ip, host is ', n.get_host_ip()) if len(self.get_nodes()) == 0: raise Exception('invalid cfg format, nodes empty') logger.info('cfg parser end, result is %s', self)