def execute(self, args_dict): if not self.slack_webhook: raise PluginError('SlackExecutor is missing slack_webhook parameter. Can not execute') if 'message' not in args_dict: raise PluginError('SlackExecutor got incorrect arguments, got: {}'.format( args_dict.keys() )) message = args_dict['message'] if self.dry_run: return TaskResult(EXECUTION_SUCCESSFUL) payload = { 'username': '******', 'text': message } for i in range(RETRY_COUNT + 1): resp = requests.post(self.slack_webhook, json=payload) if resp.ok: log.debug('Successfully sent slack message') break log.debug('Problem sending slack message. Status {}'.format(resp.status_code)) else: log.warning('Could not successfully send slack message. Status: {}'.format(resp.status_code)) return TaskResult(EXECUTION_FAILED, 'Sending slack notification failed') return TaskResult(EXECUTION_SUCCESSFUL, 'Sending slack notification successful')
def from_dict(cls, conf_dict, event_mgr=None): if 'status_file' not in conf_dict: raise PluginError('File logger is missing status_file' 'configuration parameter') if not isinstance(conf_dict['status_file'], basestring): raise PluginError('File logger has invalid status_file parameter') return StatusLogger(conf_dict['status_file'])
def _validate_args_dict(self, args_dict): if 'file' not in args_dict and 'script' not in args_dict: raise PluginError('PythonExecutor requires either "file" or "script" arguments. Given: {}'.format(args_dict)) if args_dict.get('file') and args_dict.get('script'): raise PluginError( 'PythonExecutor got both "file" and "script" aguments. Only one is supported.') if args_dict.get('workspace') and args_dict.get('script'): raise PluginError('PythonExecutor requires either "file" or "script" arguments. Given: {}'.format(args_dict))
def from_dict(cls, conf_dict): if 'webhook_url' not in conf_dict: raise PluginError('WebhookLogger is missing webhook_url' 'configuration parameter') if not isinstance(conf_dict['webhook_url'], basestring): raise PluginError( 'WebhookLogger has invalid webhook_url parameter') return WebhookLogger(conf_dict['webhook_url'])
def _parse_args_dict(self, args_dict): if 'cmd' not in args_dict: raise PluginError( 'BashExecutor got incorrect arguments, got: {}'.format( args_dict.keys())) timeout = args_dict.get('timeout') or DEFAULT_TIMEOUT if not isinstance(timeout, int): raise PluginError( 'BashExecutor got incorrect timeout argument type, got: {} expecting int' .format(type(timeout))) return args_dict['cmd'], timeout
def _parse_class(plugin_path): try: module, class_name = plugin_path.rsplit('.', 1) m = importlib.import_module(module) except ImportError: raise PluginError('Could not import plugin {}'.format(plugin_path)) if not hasattr(m, class_name): raise PluginError('Could not find class for plugin {}'.format(plugin_path)) return getattr(m, plugin_path)
def _load_plugin(self, plugin_str): if not isinstance(plugin_str, basestring): raise PluginError( 'Plugins must be a string, got {}'.format(plugin_str)) plugin_class = self._resolve_plugin_class(plugin_str) self.plugin_mgr.register_plugin(plugin_class, self.context.get('vars'))
def execute(self, args_dict): workdir, virtualenv, script = self._parse_args_dict(args_dict) activate_path = path.join(virtualenv, 'bin', 'activate') if not path.exists(activate_path) or not path.isfile(activate_path): raise PluginError( 'Python virtualenv doesn\'t exist: {}'.format(activate_path)) bash_input = ('cd {}\n' 'source {}\n' 'python {}').format(workdir, activate_path, script) log.debug('Running python script: {}'.format(bash_input)) if self.dry_run: return TaskResult(EXECUTION_SUCCESSFUL) try: stdout = self._run_bash(bash_input) status = EXECUTION_SUCCESSFUL except BashExecuteError as e: status = EXECUTION_FAILED stdout = e.stderr return TaskResult(status, stdout)
def _parse_args_dict(self, args_dict): if 'cmd' not in args_dict: raise PluginError( 'DummyExecutor got incorrect arguments, got: {}'.format( args_dict.keys())) return args_dict['cmd']
def _parse_args_dict(self, args_dict): for k in ['workdir', 'virtualenv', 'script']: if k not in args_dict: raise PluginError( 'PythonExecutor is missing argument: {}'.format(k)) return args_dict['workdir'], args_dict['virtualenv'], args_dict[ 'script']
def from_dict(cls, conf_dict, event_mgr=None): if 'slack_webhook' not in conf_dict: log.debug('SlackExecutor is missing slack_webhook parameter') else: if not isinstance(conf_dict['slack_webhook'], basestring): raise PluginError('WebhookLogger has invalid slack_webhook parameter') log.debug('SlackExecutor got slack_webhook parameter: %s' % conf_dict['slack_webhook']) return cls(slack_webhook=conf_dict.get('slack_webhook'))
def from_dict(cls, conf_dict, event_mgr=None): if 'webhook_url' not in conf_dict: # raise PluginError('WebhookLogger is missing webhook_url' # 'configuration parameter') log.debug( 'WebhookLogger missing webhook_url parameter. Disabling.') else: if not isinstance(conf_dict['webhook_url'], str): raise PluginError( 'WebhookLogger has invalid webhook_url parameter') return WebhookLogger(conf_dict.get('webhook_url'))
def execute(self, args_dict): self._validate_args_dict(args_dict) workdir = args_dict.get('workdir') script = args_dict.get('script') file = args_dict.get('file') virtualenv = args_dict.get('virtualenv') bash_input = '' if workdir: bash_input += 'cd {}\n'.format(workdir) if virtualenv: activate_path = path.join(virtualenv, 'bin', 'activate') if not path.exists(activate_path) or not path.isfile( activate_path): raise PluginError( 'Python virtualenv doesn\'t exist: {}'.format( activate_path)) bash_input += 'source {}\n'.format(activate_path) filename = file if script: f = NamedTemporaryFile(delete=False) f.write(script) log.debug('Wrote script to file {}, {}'.format(f.name, script)) filename = f.name f.close() bash_input += 'python {}'.format(filename) log.debug('Running python script: {}'.format(bash_input)) if self.dry_run: return TaskResult(EXECUTION_SUCCESSFUL) return_obj = None try: stdout, return_obj = self._run_bash(bash_input) status = EXECUTION_SUCCESSFUL except BashExecuteError as e: status = EXECUTION_FAILED stdout = e.data.get('stdout') print('Finished, stdout: %s' % (stdout)) return TaskResult(status, 'Execution finished', data={'output': stdout}, return_obj=return_obj)
def register_plugin(self, plugin_class, conf_dict): if not issubclass(plugin_class, BasePlugin): raise PluginError( 'Trying to register plugin that is not extending BasePlugin: {}' .format(class_name(plugin_class))) plugin = plugin_class.from_dict(conf_dict, self) for k in ['hook_prefix', 'hooks']: if not hasattr(plugin, k): raise PluginError( 'Plugin is missing "{}" attribute.'.format(k)) prefix = '{}.'.format(plugin.hook_prefix) if plugin.hook_prefix else '' for hook in plugin.hooks: if not hasattr(plugin, hook): raise PluginError('Plugin {} is missing {}-function'.format( class_name(plugin), hook)) hook_key = '{}{}'.format(prefix, hook) if hook_key not in self.plugins: self.plugins[hook_key] = [] self.plugins[hook_key].append(getattr(plugin, hook))