def setUp(self): super(ExPlanBerkWrongVersion, self).setUp() self.execution_plan = bunch.Bunch( Action='Execute', Body='return deploy(args.appName).stdout\n', Files={ 'ID1': { 'Name': 'tomcat.git', 'Type': 'Downloadable', 'URL': 'https://github.com/tomcat.git' } }, FormatVersion='2.1.0', ID='ID', Name='Deploy Chef', Parameters={}, Scripts={ 'deploy': { 'EntryPoint': 'cookbook::recipe', 'Files': [ 'ID1' ], 'Options': { 'captureStderr': True, 'captureStdout': True, 'useBerkshelf': True }, 'Type': 'Chef', 'Version': '1.0.0' } }, Version='1.0.0' ) self.addCleanup(delattr, self, 'execution_plan')
def run(self, function, recipe_attributes=None, input=None): """It runs the chef executor. :param function: The function :param recipe_attributes: recipe attributes :param input: """ self._valid_module_name() cookbook_path = self._create_cookbook_path(self.module_name) try: self._configure_chef(cookbook_path) self._generate_manifest(self.module_name, self.module_recipe, recipe_attributes) except Exception as e: result = { 'exitCode': 2, 'stdout': None, 'stderr': e.strerror } raise muranoagent.exceptions.CustomException( 0, message='Cookbook {0} returned error code {1}: {2}'.format( self.module_name, self.module_recipe, e.strerror, ), additional_data=result) solo_file = os.path.join(self._path, "solo.rb") command = 'chef-solo -j node.json -c {0}'.format(solo_file) result = self._execute_command(command) return bunch.Bunch(result)
def run(self, function, recipe_attributes=None, input=None): """It runs the puppet executor. :param function: The function :param recipe_attributes: recipe attributes :param input: """ self._valid_module_name() try: self._configure_puppet() self._generate_files(self.module_name, self.module_recipe, recipe_attributes) except Exception as e: result = {'exitCode': 2, 'stdout': None, 'stderr': e.strerror} raise muranoagent.exceptions.CustomException( 0, message='Module %s returned error code %s: %s' % (self.module_name, self.module_recipe, e.strerror), additional_data=result) command = 'puppet apply --hiera_config=hiera.yaml --modulepath ' \ '{0} manifest.pp'.format(self._path) result = self._execute_command(command) return bunch.Bunch(result)
def get_template_downloable_git(self): return bunch.Bunch(ID='ID', Files={ 'mycoockbook': { 'Name': 'mycoockbook.txt', 'URL': 'https://github.com/tomcat.git', 'Type': 'Downloadable' } }, Scripts={ 'deploy': { 'EntryPoint': 'cookbook/recipe', 'Files': [ 'https://github.com/tomcat.git', { 'java': 'https://github.com/java.git' } ], 'Options': { 'captureStderr': True, 'captureStdout': True }, 'Type': 'Chef', 'Version': '1.0.0' } })
def get_execution_plan(self): while True: ep_info, timestamp = self._get_first_file( ExecutionPlanQueue.plan_filename) if ep_info is None: return None try: data = base64.b64decode(ep_info['Data']) if self._key: signature = base64.b64decode(ep_info['Signature']) self._verify_signature(data, signature) ep = json.loads(data) if not isinstance(ep, dict): raise ValueError('Message is not a document') stamp = ep.get('Stamp', -1) if stamp >= 0: if stamp <= self._last_stamp: raise ValueError('Dropping old/duplicate message') self._save_stamp(stamp) if 'ID' not in ep: ep['ID'] = ep_info['ID'] if 'ReplyTo' not in ep: ep['ReplyTo'] = ep_info['ReplyTo'] ep['_timestamp'] = timestamp return bunch.Bunch(ep) except Exception as ex: LOG.exception(ex) self.remove(timestamp)
def test_verify_execution_plan_wrong_format(self): template = bunch.Bunch( ID='ID', FormatVersion='0.0.0', ) self.assertRaises(exc.IncorrectFormat, self.agent._verify_plan, template)
def setUp(self): super(ExPlanDownloableWrongFormat, self).setUp() self.execution_plan = bunch.Bunch( ID='ID', FormatVersion='0.0.0' ) self.addCleanup(delattr, self, 'execution_plan')
def get_template_file(self): return bunch.Bunch(ID='ID', Files={ 'test': { 'Body': 'dGV4dA==\n', 'BodyType': 'Base64', 'Name': 'installer' } })
def get_template_svn(self): return bunch.Bunch(ID='ID', Files={ 'file': { 'Name': 'svn', 'URL': 'https://mysvn/svn/repo', 'Type': 'Downloadable' } })
def get_template_downloable(self): return bunch.Bunch(ID='ID', Files={ 'file': { 'Name': 'myfile', 'URL': 'https://www.apache.org/licenses', 'Type': 'Downloadable' } })
def get_template_downloable_git(self): return bunch.Bunch(ID='ID', Files={ 'mycoockbook': { 'Name': 'mycoockbook.txt', 'URL': 'git://github.com/tomcat.git', 'Type': 'Downloadable' } })
def run(self): script_globals = { "args": bunch.Bunch(self._execution_plan.get('Parameters') or {}) } script_globals.update(self._script_funcs) exec(self._main_script, script_globals) if '__execution_plan_exception' in script_globals: raise script_globals['__execution_plan_exception'] return script_globals['__execution_plan_result']
def get_template_downloable(self): return bunch.Bunch(ID='ID', Files={ 'file': { 'Name': 'myfile', 'URL': 'https://github.com' '/apache/tomcat/blob/trunk/LICENSE', 'Type': 'Downloadable' } })
def run(self, function, commandline='', input=None): dir_name = os.path.dirname(self._path) os.chdir(dir_name) app = '"{0}" {1}'.format(os.path.basename(self._path), commandline) if not sys.platform == 'win32': os.chmod(self._path, stat.S_IEXEC | stat.S_IREAD) app = './' + app stdout = subprocess.PIPE if self._capture_stdout else None stderr = subprocess.PIPE if self._capture_stderr else None script_name = os.path.relpath(self._path) LOG.debug("Starting '{0}' script execution".format(script_name)) popen_kwargs = { 'stdout': stdout, 'stderr': stderr, 'universal_newlines': True, 'cwd': dir_name, 'shell': True } if os.name != 'nt': popen_kwargs['preexec_fn'] = lambda: signal.signal( signal.SIGPIPE, signal.SIG_DFL) process = subprocess.Popen(app, **popen_kwargs) stdout, stderr = process.communicate(input) retcode = process.poll() LOG.debug("Script {0} execution finished " "with retcode: {1}".format(script_name, retcode)) if stdout is not None: if hasattr(stdout, 'decode'): stdout = stdout.decode('utf-8') LOG.debug(u"'{0}' execution stdout: " u"'{1}'".format(script_name, stdout)) if stderr is not None: if hasattr(stderr, 'decode'): stderr = stderr.decode('utf-8') LOG.debug(u"'{0}' execution stderr: " u"'{1}'".format(script_name, stderr)) result = { 'exitCode': retcode, 'stdout': stdout.strip() if stdout else None, 'stderr': stderr.strip() if stderr else None } if self._verify_exitcode and retcode != 0: raise muranoagent.exceptions.CustomException( 0, message='Script {0} returned error code'.format(self._name), additional_data=result) return bunch.Bunch(result)
def test_execution_plan_type_downloable_no_Url(self, mock_makedir): """It validates the URL.""" mock_makedir.return_value = None template = bunch.Bunch(ID='ID', Files={ 'mycoockbook': { 'Name': 'mycoockbook.txt', 'Type': 'Downloadable' } }) files = files_manager.FilesManager(template) self.assertRaises(ValueError, files._download_url_file, template.Files['mycoockbook'], "script")
def setUp(self): super(ExPlanApplication, self).setUp() self.execution_plan = bunch.Bunch( Action='Execute', Body='return deploy(args.appName).stdout', Files={ 'ID1': { 'Body': 'text', 'BodyType': 'Text', 'Name': 'deployTomcat.sh' }, 'ID2': { 'Body': 'dGV4dA==\n', 'BodyType': 'Base64', 'Name': 'installer' }, 'ID3': { 'Body': 'dGV4dA==\n', 'BodyType': 'Base64', 'Name': 'common.sh' } }, FormatVersion='2.1.0', ID='ID', Name='Deploy Tomcat', Parameters={ 'appName': '$appName' }, Scripts={ 'deploy': { 'EntryPoint': 'ID1', 'Files': [ 'ID2', 'ID3' ], 'Options': { 'captureStderr': True, 'captureStdout': True }, 'Type': 'Application', 'Version': '1.0.0' } }, Version='1.0.0' ) self.addCleanup(delattr, self, 'execution_plan')
def _handle_message(self, msg): if 'ID' not in msg.body and msg.id: msg.body['ID'] = msg.id if 'ReplyTo' not in msg.body and msg.reply_to: msg.body['ReplyTo'] = msg.reply_to try: self._verify_plan(msg.body) self._queue.put_execution_plan(msg.body) except Exception as err: try: execution_result = ex_result.ExecutionResult.from_error( err, bunch.Bunch(msg.body)) if ('ReplyTo' in msg.body) and \ CONF.enable_dynamic_result_queue: execution_result['ReplyTo'] = msg.body.get('ReplyTo') self._send_result(execution_result) except ValueError: LOG.warn('Execution result is not produced')
def setUp(self): super(ExPlanDownloableNoFiles, self).setUp() self.execution_plan = bunch.Bunch( ID='ID', FormatVersion='2.1.0', Scripts={ 'deploy': { 'EntryPoint': 'cookbook::recipe', 'Files': [ 'https://github.com/tomcat.git', {'java': 'https://github.com/java.git'} ], 'Options': { 'captureStderr': True, 'captureStdout': True }, 'Type': 'Chef', 'Version': '1.0.0' } } ) self.addCleanup(delattr, self, 'execution_plan')
def _prepare_executors(self, execution_plan): for key, value in execution_plan.Scripts.items(): self._script_funcs[key] = script_runner.ScriptRunner( key, bunch.Bunch(value), self._files_manager)
def get_execution_plan(self): ep, timestamp = self._get_first_file(ExecutionPlanQueue.plan_filename) if ep is None: return None ep['_timestamp'] = timestamp return bunch.Bunch(ep)