def _exec_script_single_host(self, script, module_name, single_host_ip): context_path, sudoer_uid, module = self._get_module_path_and_uid( module_name) ghost_env_vars = get_ghost_env_variables(self._app, module, self._job['user']) ec2_obj = get_ec2_instance(self._cloud_connection, self._app['region'], { 'private-ip-address': single_host_ip, 'vpc-id': self._app['vpc_id'], }) if not ec2_obj or ec2_obj.vpc_id != self._app[ 'vpc_id'] or ec2_obj.private_ip_address != single_host_ip: raise GCallException( "Cannot found the single instance with private IP '{ip}' in VPC '{vpc}'" .format(ip=single_host_ip, vpc=self._app['vpc_id'])) if ec2_obj.tags['app'] != self._app['name'] or ec2_obj.tags[ 'env'] != self._app['env'] or ec2_obj.tags[ 'role'] != self._app['role']: raise GCallException( "Cannot execute script on this instance ({ip} - {id}), invalid Ghost tags" .format(ip=single_host_ip, id=ec2_obj.id)) log( "EC2 instance found, ready to execute script ({ip} - {id} - {name})" .format(ip=single_host_ip, id=ec2_obj.id, name=ec2_obj.tags.get('Name', '')), self._log_file) launch_executescript(self._app, script, context_path, sudoer_uid, self._job['_id'], [single_host_ip], 'serial', self._log_file, ghost_env_vars)
def execute_module_script_on_ghost(app, module, script_name, script_friendly_name, clone_path, log_file, job, config): """ Executes the given script on the Ghost instance :param app: Ghost application :param module: Ghost module to extract script from :param script_name: string: the name of the script to find in module :param script_friendly_name: string: the friendly name of the script for logs :param clone_path: string: working directory of the current module :param log_file: string: Log file path :param job: Ghost job :param config: Ghost config """ # Execute script if available if script_name in module: theorical_script_path = "{0}/{1}".format(clone_path, script_name) if os.path.isfile(theorical_script_path): script_path = theorical_script_path else: script_source = b64decode_utf8(module[script_name]) script, script_path = tempfile.mkstemp(dir=clone_path) os.close(script) with io.open(script_path, mode='w', encoding='utf-8') as f: f.write(script_source) script_env = os.environ.copy() script_env.update(get_ghost_env_variables(app, module)) if app['build_infos'].get('container_image') and lxd_is_available( config): source_module = get_buildpack_clone_path_from_module(app, module) container = LXDImageBuilder(app, job, None, log_file, config) if not container.deploy(script_path, module, source_module): raise GCallException( "ERROR: %s execution on container failed" % script_name) else: log("Change directory to working dir ({w})".format(w=clone_path), log_file) os.chdir(clone_path) gcall('bash %s' % script_path, '%s: Execute' % script_friendly_name, log_file, env=script_env) gcall('du -hs .', 'Display current build directory disk usage', log_file) gcall('rm -vf %s' % script_path, '%s: Done, cleaning temporary file' % script_friendly_name, log_file)
def _exec_script(self, script, module_name, fabric_execution_strategy, safe_deployment_strategy): context_path, sudoer_uid, module = self._get_module_path_and_uid( module_name) ghost_env_vars = get_ghost_env_variables(self._app, module, self._job['user']) deploy_manager = HostDeploymentManager( self._cloud_connection, self._app, module, self._log_file, self._app['safe-deployment'], fabric_execution_strategy, 'executescript', { 'script': script, 'context_path': context_path, 'sudoer_uid': sudoer_uid, 'jobid': self._job['_id'], 'env_vars': ghost_env_vars, }) deploy_manager.deployment(safe_deployment_strategy)
def _execute_swap_hook(online_app, to_deploy_app, script_name, script_message, log_file): for status, app in (('active', online_app), ('inactive', to_deploy_app)): script = app.get('blue_green', {}).get('hooks', {}).get(script_name, None) if script: script_path = os.path.join(get_path_from_app_with_color(app), script_name) with open(script_path, 'w') as f: f.write(b64decode_utf8(script)) script_env = os.environ.copy() script_env.update(get_ghost_env_variables(app)) gcall('bash {}'.format(script_path), '{}: Execute'.format( script_message.format(status=status)), log_file, env=script_env)