def register_service(su_cmd): if not IS_WINDOWS: logger.info('Register service') if 'ubuntu' in PLATFORM or 'debian' in PLATFORM: format_template(DESCRIPTION['like_debian'], su_cmd) register_status, _ = execute( 'update-rc.d {} defaults'.format(SERVICE_NAME)) if register_status != 0: raise RegisterServiceError( 'Register {} Service Error'.format(SERVICE_NAME)) elif any(_p in PLATFORM for _p in ('centos', 'fedora', 'suse', 'redhat')): format_template(DESCRIPTION['like_redhat'], su_cmd) chk_status, _ = execute('chkconfig {} on'.format(SERVICE_NAME)) service_path = nfs.join(SERVICE_PATH, SERVICE_NAME) echo_status, _ = execute('echo "{service_path} start" ' '| tee --append {path} >/dev/null'.format( service_path=service_path, path=BOOT_SCRIPT)) chmod_status, _ = execute('chmod +x {}'.format(BOOT_SCRIPT)) if chk_status != 0 or echo_status != 0 or chmod_status != 0: raise RegisterServiceError( 'Register {} Service Error'.format(SERVICE_NAME)) else: logger.warn('Unsupported platform')
def _deliver_to_agent(self, compress_name): try: file_url = urlparse.urljoin(self.upstream, UPSTREAM_SUFFIX) file_url = urlparse.urljoin(file_url, 'file/') file_url = urlparse.urljoin(file_url, compress_name) if self.system == 'debian': curl_cmd = \ 'wget "{}" -c -P "{}" --no-check-certificate'\ .format(file_url, self.dst) yield self.do_ssh_cmd(curl_cmd) else: curl_cmd = 'curl -s -w "%{{http_code}}" "{}" -s -m 3600 -o ' \ '"{}" -k --create-dirs'\ .format(file_url, self.dst_name) http_code = yield self.do_ssh_cmd(curl_cmd) logger.info('Download pkg {}, the http_code is {}'.format( compress_name, http_code)) if http_code == '404': raise MessageError( 'The package {} is not exists'.format(compress_name)) if http_code != '200': raise MessageError('Http code is {}!'.format(http_code)) yield self.reporter.log_ok('Download agent pkg successfully') except Exception as e: raise MessageError('Download agent pkg failed. {}'.format(e))
def start_circled(su_cmd): logger.info('Starting Agent ...') if IS_WINDOWS: check_call([BIN_START], shell=True) else: status, _ = execute('{} "{}"'.format(su_cmd, BIN_START)) if status != 0: raise MessageError('Start Agent failed') logger.info('Start Agent done')
def check_win_agent(): if CircleWinService.exists(): answer = raw_input('Ant agent has installed as windows service ' '("Agent") before. \n' 'Do you want to remove it?(yes/no)') if answer == 'yes': if CircleWinService.status() != 'stopped': CircleWinService.stop() CircleWinService.remove() else: logger.info('exit') return False if UpgradeWinService.exists(): if UpgradeWinService.status() != 'stopped': UpgradeWinService.stop() UpgradeWinService.remove() return True
def _get_agent_platform(self): """Detect agent platform (AIX, Linux) and cpu arch (x86, x64)""" yield self.reporter.log_ok('Detecting agent platform') try: system_uname = yield self.do_ssh_cmd('uname') system_uname = system_uname.lower() if 'aix' in system_uname: system = 'aix' output = yield self.do_ssh_cmd( '[[ `getconf HARDWARE_BITMODE | grep 64|wc -l` -gt 0 ]] ' '&& echo 64 || echo 32') output = output.strip() version = yield self.do_ssh_cmd('oslevel') version = '.'.join(version.split('.')[0:2]) else: find_str = 'grep -Eo "{}" '.format(KNOWN_DISTRIBUTION) cmd = 'lsb_release -d 2>/dev/null | {find_str} ' \ '|| cat /etc/*-release 2>/dev/null | {find_str}' \ '|| {find_str} /etc/issue 2>/dev/null' \ '|| uname -s'.format(find_str=find_str) distribution = yield self.do_ssh_cmd(cmd) distribution = de_duplication(distribution) system = distribution.lower() if not system: raise MessageError('Get system distribution error!') output = yield self.do_ssh_cmd( '[[ `uname -m|grep 64|wc -l` -gt 0 ]] ' '&& echo 64 || echo 32') output = output.strip() version = yield self.get_system_version(distribution) logger.info('Get system version: {}'.format(version)) self.system = system agent_platform = self.deal_agent_platform(system, version, output) yield self.reporter.log_ok( 'Agent platform: {}'.format(agent_platform)) raise gen.Return(agent_platform) except SSHRunError: raise NotMatchError('Unsupport system')
def register_upgrade_service(su_cmd): if not IS_WINDOWS: logger.info('Register upgrade service') if 'ubuntu' in PLATFORM or 'debian' in PLATFORM: format_upgrade_template(DESCRIPTION['like_debian'], su_cmd) register_status, _ = execute( 'update-rc.d {} defaults'.format(UPGRADE_SERVICE_NAME)) if register_status != 0: raise RegisterServiceError( 'Register {} Service Error'.format(UPGRADE_SERVICE_NAME)) elif any(_p in PLATFORM for _p in ('centos', 'fedora', 'suse', 'redhat')): format_upgrade_template(DESCRIPTION['like_redhat'], su_cmd) chk_status, _ = execute( 'chkconfig {} on'.format(UPGRADE_SERVICE_NAME)) service_path = nfs.join(SERVICE_PATH, UPGRADE_SERVICE_NAME) echo_status, _ = execute('echo "{service_path} start" ' '| tee --append {path} >/dev/null'.format( service_path=service_path, path=BOOT_SCRIPT)) chmod_status, _ = execute('chmod +x {}'.format(BOOT_SCRIPT)) if chk_status != 0 or echo_status != 0 or chmod_status != 0: raise RegisterServiceError( 'Register {} Service Error'.format(UPGRADE_SERVICE_NAME)) else: logger.warn('Unsupported platform') status, output = execute('{} "{}"'.format(su_cmd, UPGRADE_BIN_START)) if status != 0: raise MessageError( 'Start Upgrade failed: reason: {}'.format(output)) logger.info('Start Upgrade done') else: UpgradeWinService.install() UpgradeWinService.start()
def init_conf(conf_dict): logger.info('Preparing ant agent ...') with open(CONF_PATH, 'a+') as f: yaml.dump(conf_dict, f, default_flow_style=False) logger.info('Prepare done')
def do_ssh_cmd(self, cmd, cmd_prefix=None): cmd_prefix = cmd_prefix if cmd_prefix else self.cmd_prefix logger.info('cmd: {!r}'.format(cmd)) cmd = '{} \'{}\''.format(cmd_prefix, cmd) if cmd_prefix else cmd result = yield self.ssh_client.ssh(cmd) raise gen.Return(result.strip())