def check_server(self, info): ssh_client = paramiko.SSHClient() ssh_client.load_system_host_keys() ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: ssh_client.connect(info['host'], port=info['port'], username=info['user'], password=info['passwd'], timeout=1) info.pop('passwd') system = self.do_ssh_cmd(ssh_client, 'uname') system = system.lower() if 'aix' in system: info['node_type'] = "MiniServer" else: result = self.do_ssh_cmd(ssh_client, IS_VIRTUAL_CMD) info['node_type'] = 'VM' if result else 'PCServer' self.result.append({ 'host': info['host'], 'network_domain': info['network_domain'], 'node_type': info['node_type'] }) except socket.error as e: info.pop('passwd') logger.error('The info: {}, connect error: {}'.format( info, str(e))) except Exception as e: info.pop('passwd') logger.error('The info: {}, get system info error: {}'.format( info, str(e)))
def _install(self, agent_platform, compress_name): if not compress_name: raise MessageError("compress_name can't be empty") yield self.reporter.log_ok('Prepare environment for agent') if 'aix' in agent_platform: new_dst_name = re.sub('.gz$', '', compress_name) uncompress_cmd = 'gunzip {compress_name} && ' \ 'tar xf {new_dst_name} && rm -rf {new_dst_name}'\ .format(compress_name=compress_name, new_dst_name=new_dst_name) else: uncompress_cmd = \ 'tar mzxf {compress_name} && rm -f {compress_name}'\ .format(compress_name=compress_name) cmd = 'umask 0027 && cd {dst} && {uncompress_cmd} '\ .format(dst=self.dst, uncompress_cmd=uncompress_cmd) try: yield self.reporter.log_ok('Execute install cmd.') result = yield self.do_ssh_cmd(cmd) if result: yield self.reporter.log_ok(result) except Exception as e: logger.error(traceback.format_exc()) yield self._rollback() raise MessageError('Execute install error on agent. {}'.format(e))
def _get_env(self): try: self.task_id = os.environ.get('ANT_TASK_ID') self.id = os.environ.get('ANT_AGENT_ID') self.reporter = Reporter(self.task_id, logger) except Exception as e: logger.error(e) sys.exit(1)
def execute(self): try: self._prepare() self._validate() running = yield self.do_check() while running: for future in running[:]: if not future or future.done(): running.remove(future) yield self.reporter.log_ok(json.dumps(self.result), done=True) except Exception as e: logger.error(str(e)) yield self.reporter.log_error(str(e), done=True) self.io_loop.stop()
def _prepare(self): self.task_id = os.environ.get('ANT_TASK_ID') if not self.task_id: logger.error('The task_id is None, ' 'please input the right ANT_TASK_ID with env!!!') sys.exit(1) if os.environ.get('ANT_TASK_URL'): tenant = os.environ.get('ANT_TENANT') if not tenant: logger.error('The tenant is None, ' 'please input the right ANT_TASK_ID with env!!!') sys.exit(1) else: tenant = None self.reporter = Reporter(self.task_id, logger, tenant)
def execute(self): runs = [] try: self.validate() yield self.init_openresty() for info in self.task_message: single_install = SingleInstall(info) runs.append(single_install.run()) for i in range(len(runs) / INSTALL_PARALLEL_NUM + 1): try: yield runs[i:i + INSTALL_PARALLEL_NUM] except Exception as e: logger.error(e, exc_info=True) except Exception as e: logger.error(str(e)) self.io_loop.stop()
upgrade_message = self.task_message.get(self.id) if not upgrade_message: yield self.reporter.log_error( 'There is no upgrade information ' 'needed by the agent!', done=True) sys.exit(1) upgrade_message.update({'task_id': self.task_id}) yield self.post_upgrade(json.dumps(upgrade_message)) self.reporter.log_ok('The upgrade information has ' 'been sent to the upgrade process') def run(self): self._get_env() try: self.ioloop.run_sync(self.deal_message) except Exception as e: self.reporter.log_error('Exec upgrade error: {}'.format(e), done=True) sys.exit(1) if __name__ == '__main__': message = sys.stdin.read() if not message: logger.error('The stdin is None, please input the ' 'right args with stdin !!!') sys.exit(1) upgrade = Upgrade(json.loads(message)) upgrade.run()
def handle_cli(): try: cli_args = docopt(__doc__) if IS_WINDOWS and not check_win_agent(): return if not nfs.exists(UPGRADE_PYTHON_DIR): nfs.copy(nfs.join(PYTHON_DIR, '*'), UPGRADE_PYTHON_DIR) # Get upstream message if cli_args['--upstream']: baseurl = cli_args['--upstream'] upstream = urlparse.urljoin(cli_args['--upstream'], UPSTREAM_SUFFIX) upstream_mes = upstream_validate(cli_args['--upstream']) if not upstream_mes: logger.error('The upstream: {} is wrong!' ''.format(cli_args['--upstream'])) return else: upstream_ip = os.environ['SSH_CLIENT'].split()[0] baseurl = 'http://{}:{}/'.format(upstream_ip, NGINX_PORT) upstream = '{}{}'.format(baseurl, UPSTREAM_SUFFIX) upstream_mes = [upstream_ip, NGINX_PORT] if cli_args['--ip']: if not ip_validate(cli_args['--ip']): raise ValueError('Ant agent ip: {} is invalid' ''.format(cli_args['--ip'])) else: agent_ip = cli_args['--ip'] else: agent_ip = get_agent_ip(upstream_mes[0], int(upstream_mes[1])) runner = cli_args['--user'] if cli_args['--user'] else os.environ.get( 'USER') su_cmd = '' if runner == 'root' else 'su - {} -c '.format(runner) conf_dict = { 'tenant': cli_args['--tenant'], 'ip': agent_ip, 'upstream': upstream, 'network_domain': cli_args['--network-domain'] } init_conf(conf_dict) init_bootstrap() init_openresty(baseurl, upstream, runner) register_service(su_cmd) if runner and not IS_WINDOWS and runner != 'root': status, result = execute('chown -R {user}:{user} {path}'.format( user=runner, path=ROOT_DIR)) if status != 0: raise MessageError( 'Change log path owen failed! Error[{}]: {}'.format( status, result)) register_upgrade_service(su_cmd) start_circled(su_cmd) except Exception as e: logger.error(e) sys.exit(1)