def load_playbook(resource): """Loads a playbook from a file. Args: resource (str): Path to the workflow. """ try: playbook_file = open(resource, 'r') except (IOError, OSError) as e: logger.error('Could not load workflow from {0}. Reason: {1}'.format(resource, format_exception_message(e))) return None else: with playbook_file: workflow_loaded = playbook_file.read() try: playbook_json = json.loads(workflow_loaded) playbook = PlaybookSchema().load(playbook_json) return playbook.data except ValueError as e: logger.exception('Cannot parse {0}. Reason: {1}'.format(resource, format_exception_message(e))) except (InvalidArgument, UnknownApp, UnknownAppAction, UnknownTransform, UnknownCondition) as e: logger.error( 'Error constructing playbook from {0}. ' 'Reason: {1}'.format(resource, format_exception_message(e))) return None
def test_export_workflow(self): playbook = execution_db_help.standard_load() response = self.get_with_status_check( '/api/playbooks/{}?mode=export'.format(playbook.id), headers=self.headers) self.assertDictEqual(PlaybookSchema().dump(playbook).data, response)
def convert_playbook(path, current_version): print('Converting {}'.format(path)) with open(path, 'r+') as f: playbook = json.load(f) if validate_path(mode, cur_version, tgt_version): next_version = "" while continue_condition(mode, cur_version, tgt_version) and next_version is not None: next_version = get_next_version(mode, cur_version) print("{}ing playbook from {} to {}".format( mode[:-1], cur_version, next_version)) path = "scripts.migrations.workflows." rev = importlib.import_module(path + next_version.replace(".", "_")) if rev.upgrade_supported: rev.upgrade_playbook(playbook) cur_version = next_version playbook_obj = executiondb.execution_db.session.query( Playbook).filter_by(name=playbook['name']).first() f.seek(0) json.dump(PlaybookSchema().dump(playbook_obj).data, f, sort_keys=True, indent=4, separators=(',', ': ')) f.truncate()
def convert_playbook(path, mode, tgt_version): print('Converting {}'.format(path)) with open(path, 'r+') as f: playbook = json.load(f) if 'walkoff_version' not in playbook: if mode == DOWNGRADE: print("Cannot downgrade, no version specified in playbook.") return else: # upgrade print("No version specified in playbook, assuming " + PREV_VERSION) cur_version = PREV_VERSION else: cur_version = playbook['walkoff_version'] if validate_path(mode, cur_version, tgt_version): next_version = "" while continue_condition(mode, cur_version, tgt_version) and next_version is not None: next_version = get_next_version(mode, cur_version) print("{}ing playbook from {} to {}".format( mode[:-1], cur_version, next_version)) path = "scripts.migrations.workflows." if mode == DOWNGRADE: rev = importlib.import_module( path + cur_version.replace(".", "_")) if rev.downgrade_supported: rev.downgrade_playbook(playbook) else: print("Downgrade not supported.") elif mode == UPGRADE: rev = importlib.import_module( path + next_version.replace(".", "_")) if rev.upgrade_supported: rev.upgrade_playbook(playbook) else: print("Upgrade not supported.") cur_version = next_version playbook_obj = executiondb.execution_db.session.query( Playbook).filter_by(name=playbook['name']).first() f.seek(0) json.dump(PlaybookSchema().dump(playbook_obj).data, f, sort_keys=True, indent=4, separators=(',', ': ')) f.truncate()
def test_export_workflow(self): from walkoff.helpers import strip_device_ids, strip_argument_ids playbook = execution_db_help.standard_load() response = self.get_with_status_check( '/api/playbooks/{}?mode=export'.format(playbook.id), headers=self.headers) expected = PlaybookSchema().dump(playbook).data strip_device_ids(expected) strip_argument_ids(expected) self.assertDictEqual(response, expected)
def test_import_workflow(self): path = os.path.join(test_workflows_path, 'basicWorkflowTest.playbook') files = {'file': (path, open(path, 'r'), 'application/json')} response = self.post_with_status_check( '/api/playbooks', headers=self.headers, status_code=OBJECT_CREATED, data=files, content_type='multipart/form-data') playbook = executiondb.execution_db.session.query(Playbook).filter_by( id=response['id']).first() self.assertIsNotNone(playbook) self.assertDictEqual(PlaybookSchema().dump(playbook).data, response)
def load_playbooks(playbooks): paths = [] paths.extend([ os.path.join(test_workflows_path, filename) for filename in os.listdir(test_workflows_path) if filename.endswith('.playbook') and filename.split('.')[0] in playbooks ]) for path in paths: with open(path, 'r') as playbook_file: playbook = PlaybookSchema().load(json.load(playbook_file)) if playbook.errors: print(playbook.errors) raise Exception('There be errors in yer playbooks') executiondb.execution_db.session.add(playbook.data) executiondb.execution_db.session.commit()
def load_playbooks(playbooks): execution_db = ExecutionDatabase.instance paths = [] paths.extend([os.path.join(walkoff.config.Config.WORKFLOWS_PATH, filename) for filename in os.listdir(walkoff.config.Config.WORKFLOWS_PATH) if filename.endswith('.playbook') and filename.split('.')[0] in playbooks]) for path in paths: with open(path, 'r') as playbook_file: playbook = PlaybookSchema().load(json.load(playbook_file)) if playbook.errors: print(playbook.errors) raise Exception('There be errors in yer playbooks') execution_db.session.add(playbook.data) execution_db.session.commit()
from flask_jwt_extended import jwt_required from sqlalchemy import exists, and_ from sqlalchemy.exc import IntegrityError, StatementError from walkoff.appgateway.apiutil import UnknownApp, UnknownFunction, InvalidArgument from walkoff.executiondb.playbook import Playbook from walkoff.executiondb.schemas import PlaybookSchema, WorkflowSchema from walkoff.executiondb.workflow import Workflow from walkoff.helpers import regenerate_workflow_ids from walkoff.helpers import strip_device_ids, strip_argument_ids from walkoff.security import permissions_accepted_for_resources, ResourcePermissions from walkoff.server.decorators import with_resource_factory, validate_resource_exists_factory, is_valid_uid from walkoff.server.problem import Problem from walkoff.server.returncodes import * playbook_schema = PlaybookSchema() workflow_schema = WorkflowSchema() invalid_execution_element_exceptions = (InvalidArgument, UnknownApp, UnknownFunction) def does_workflow_exist(playbook_id, workflow_id): return current_app.running_context.execution_db.session.query( exists().where( and_(Workflow.id == workflow_id, Workflow.playbook_id == playbook_id))).scalar() def playbook_getter(playbook_id): playbook = current_app.running_context.execution_db.session.query(