def copy_to(self, dst, plugin_parent='plugins'): if self.is_plugin: plugin_yml_path = fs.join(self.path, 'module.yml') if not fs.exists(plugin_yml_path): plugin_yml_path = fs.join(self.path, 'plugin.yml') if fs.exists(plugin_yml_path): import yaml info = yaml.load(open(plugin_yml_path)) fullname = '{}@{}'.format(info['name'], info['version']) dst = fs.join(dst, plugin_parent, fullname) fs.makedirs(dst) else: logger.error('module.yml or plugin.yml not exists') sys.exit(1) logger.info('Copy project: {!r} from {!r} to {!r}'.format( self.name, self.path, dst)) for dirname in fs.listdir(self.path): dirpath = fs.join(self.path, dirname) if dirname in (EXCLUDE_DIRS + EXCLUDE_FILES) \ or dirname.startswith('.'): continue fs.copy(dirpath, dst, exclude_dirs=EXCLUDE_DIRS, exclude_files=['*.exe', '*.bat'] if not IS_WINDOWS else ['*.sh']) return dst
def download_pkg(self, filename): yield self.reporter.log_ok('Begin to download package ' '{} ...'.format(filename)) down_url = self.download_url + '?filename=' + filename try: response = yield self.client.fetch( down_url, connect_timeout=config.get('file_service_connect_timeout', 3600.0), request_timeout=config.get('file_service_request_timeout', 3600.0), validate_cert=False) if response.code == 200: if not nfs.exists(PKG_CACHE_DIR): os.makedirs(PKG_CACHE_DIR) nfs.copy(response.body, nfs.join(PKG_CACHE_DIR, filename)) yield self.reporter.log_ok( 'Download package {} success'.format(filename)) else: raise MessageError( 'Download package {} failed, reason: {}!'.format( filename, response.body)) except HTTPError as e: raise MessageError( 'Download package {} failed, reason: {}, {}!'.format( filename, e, e.message))
def copy(source, target, exclude_files, exclude_dirs): fs.copy( source, target, exclude_files=exclude_files, exclude_dirs=exclude_dirs, symlinks=True )
def backup_files(self): if nfs.exists(AGENT_BACK_DIR): nfs.remove(nfs.join(AGENT_BACK_DIR, '*')) else: nfs.makedirs(AGENT_BACK_DIR) # Copy self.http_handler.log_ok('Backup files') for dir_name in nfs.listdir(ROOT_DIR): if dir_name in EXCLUDE_BACK_DIRS: continue nfs.copy(nfs.join(ROOT_DIR, dir_name), AGENT_BACK_DIR) self.http_handler.log_ok('Backup done')
def with_module(module_names): if module_names == '': logger.info('Need not modules') else: for module_name in module_names.split(','): module_string = '' module_version = [] module_url = REPO_URL[module_name] module_names = [ f for f in os.listdir(DIST_ROOT) if os.path.isfile(os.path.join(DIST_ROOT, f)) ] module_names = [f for f in module_names if module_name in f] if len(module_names) == 0: content = requests.get(module_url).text module_names = re.findall( r'>({}.*)</a>'.format(module_name), content) if len(module_names) == 0: logger.info( 'Can not find the {} package ,dispatcher build failed'. format(module_name)) sys.exit() for name in module_names: module_string += name module_version = re.findall('[-@](\d+\.\d+\.\d+)', module_string) module_version = max(module_version) for diff_version_module in REPO_NAME[module_name]: source_name = diff_version_module.format(module_version) link_name = diff_version_module.format('latest') source = join(module_url, source_name) target = join(DIST_ROOT, 'platform-ant-dispatcher/repo/', source_name) if os.path.exists(join(DIST_ROOT, source_name)): fs.copy(join(DIST_ROOT, source_name), target) else: Step.download(source, target) fs.chdir(join(DIST_ROOT, 'platform-ant-dispatcher/repo/')) check_call('ln -s {} {}'.format(source_name, link_name), shell=True) fs.chdir('../../../')
def init_bootstrap(): bootstrap_file = nfs.join(ROOT_DIR, 'templates', 'bootstrap.py') nfs.copy(bootstrap_file, ROOT_DIR)
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)