def workbook_delete(workbook_name, session=None): workbook = _workbook_get(workbook_name) if not workbook: raise exc.DataAccessException("Workbook not found [workbook_name=%s]" % workbook_name) session.delete(workbook)
def _to_paramiko_private_key(private_key_filename, private_key=None, password=None): if private_key: return paramiko.RSAKey.from_private_key( file_obj=io.StringIO(private_key), password=password) if private_key_filename: if '../' in private_key_filename or '..\\' in private_key_filename: raise exc.DataAccessException( "Private key filename must not contain '..'. " "Actual: %s" % private_key_filename ) if private_key_filename.startswith('/'): private_key_path = private_key_filename else: private_key_path = KEY_PATH + private_key_filename return paramiko.RSAKey( filename=private_key_path, password=password) return None
def save_text_to(text, file_path, overwrite=False): if os.path.exists(file_path) and not overwrite: raise exc.DataAccessException( "Cannot save data to file. File %s already exists.") with open(file_path, 'w') as f: f.write(text)
def delete_workflow_definition(identifier, session=None): wf_def = get_workflow_definition(identifier) if wf_def.project_id != security.get_project_id(): raise exc.NotAllowedException( "Can not delete workflow of other users. [workflow_identifier=%s]" % identifier) if wf_def.is_system: msg = "Attempt to delete a system workflow: %s" % identifier raise exc.DataAccessException(msg) cron_triggers = get_cron_triggers(insecure=True, workflow_id=wf_def.id) if cron_triggers: raise exc.DBError( "Can't delete workflow that has cron triggers associated. " "[workflow_identifier=%s], [cron_trigger_id(s)=%s]" % (identifier, ', '.join([t.id for t in cron_triggers]))) event_triggers = get_event_triggers(insecure=True, workflow_id=wf_def.id) if event_triggers: raise exc.DBError( "Can't delete workflow that has event triggers associated. " "[workflow_identifier=%s], [event_trigger_id(s)=%s]" % (identifier, ', '.join([t.id for t in event_triggers]))) # Delete workflow members first. delete_resource_members(resource_type='workflow', resource_id=wf_def.id) session.delete(wf_def)
def task_delete(workbook_name, execution_id, task_id, session=None): task = _task_get(workbook_name, execution_id, task_id) if not task: raise exc.DataAccessException( "Task not found [workbook_name=%s, execution_id=%s, task_id=%s]" % (workbook_name, execution_id, task_id)) session.delete(task)
def rollback_tx(): """Rolls back previously started database transaction.""" ses = _get_thread_local_session() if not ses: raise exc.DataAccessException("Nothing to roll back. Database" " transaction has not been started.") ses.rollback()
def commit_tx(): """Commits previously started database transaction.""" ses = _get_thread_local_session() if not ses: raise exc.DataAccessException("Nothing to commit. Database transaction" " has not been previously started.") ses.commit()
def execution_delete(workbook_name, execution_id, session=None): execution = _execution_get(workbook_name, execution_id) if not execution: raise exc.DataAccessException( "Execution not found [workbook_name=%s, execution_id=%s]" % (workbook_name, execution_id)) session.delete(execution)
def execution_update(workbook_name, execution_id, values, session=None): execution = _execution_get(workbook_name, execution_id) if not execution: raise exc.DataAccessException( "Execution not found [workbook_name=%s, execution_id=%s]" % (workbook_name, execution_id)) execution.update(values.copy()) return execution
def workbook_update(workbook_name, values, session=None): workbook = _workbook_get(workbook_name) if not workbook: raise exc.DataAccessException("Workbook not found [workbook_name=%s]" % workbook_name) workbook.update(values.copy()) return workbook
def trigger_update(trigger_id, values, session=None): trigger = _trigger_get(trigger_id) if trigger is None: raise exc.DataAccessException("Trigger not found [trigger_id=%s]" % trigger_id) trigger.update(values.copy()) return trigger
def start_tx(): """Opens new database session and starts new transaction assuming there wasn't any opened sessions within the same thread. """ if _get_thread_local_session(): raise exc.DataAccessException( "Database transaction has already been started.") _set_thread_local_session(_get_session())
def _delete_action_definition(): with db_api.transaction(): db_model = db_api.get_action_definition(identifier) if db_model.is_system: msg = "Attempt to delete a system action: %s" % identifier raise exc.DataAccessException(msg) db_api.delete_action_definition(identifier)
def _to_paramiko_private_key(private_key_filename, password=None): if '../' in private_key_filename or '..\\' in private_key_filename: raise exc.DataAccessException( "Private key filename must not contain '..'. " "Actual: %s" % private_key_filename) private_key_path = KEY_PATH + private_key_filename return paramiko.RSAKey(filename=private_key_path, password=password)
def task_update(workbook_name, execution_id, task_id, values, session=None): task = _task_get(workbook_name, execution_id, task_id) if not task: raise exc.DataAccessException( "Task not found [workbook_name=%s, execution_id=%s, task_id=%s]" % (workbook_name, execution_id, task_id)) task.update(values.copy()) return task
def start_tx(): """Opens new database session and starts new transaction assuming there wasn't any opened sessions within the same thread. """ ses = _get_thread_local_session() if ses: raise exc.DataAccessException("Database transaction has already been" " started.") _set_thread_local_session(get_session(autocommit=False))
def _delete_action_definition(): with db_api.transaction(): db_model = db_api.get_action_definition(identifier, namespace=namespace) if db_model.is_system: raise exc.DataAccessException( "Attempt to delete a system action: %s" % identifier) db_api.delete_action_definition(identifier, namespace=namespace)
def delete(self, name): """Delete the named action.""" LOG.info("Delete action [name=%s]" % name) with db_api.transaction(): db_model = db_api.get_action_definition(name) if db_model.is_system: msg = "Attempt to delete a system action: %s" % name raise exc.DataAccessException(msg) db_api.delete_action_definition(name)
def put(self, id, execution): """Update the specified Execution. :param id: execution ID. :param execution: Execution objects """ LOG.info("Update execution [id=%s, execution=%s]" % (id, execution)) db_api.ensure_workflow_execution_exists(id) new_state = execution.state new_description = execution.description msg = execution.state_info # Currently we can change only state or description. if (not (new_state or new_description) or (new_state and new_description)): raise exc.DataAccessException( "Only state or description of execution can be changed. " "But they can not be changed at the same time.") if new_description: wf_ex = db_api.update_workflow_execution( id, {"description": new_description}) elif new_state == states.PAUSED: wf_ex = rpc.get_engine_client().pause_workflow(id) elif new_state == states.RUNNING: wf_ex = rpc.get_engine_client().resume_workflow(id) elif new_state in [states.SUCCESS, states.ERROR]: wf_ex = rpc.get_engine_client().stop_workflow(id, new_state, msg) else: # To prevent changing state in other cases throw a message. raise exc.DataAccessException( "Can not change state to %s. Allowed states are: '%s" % (new_state, ", ".join([ states.RUNNING, states.PAUSED, states.SUCCESS, states.ERROR ]))) return Execution.from_dict( wf_ex if isinstance(wf_ex, dict) else wf_ex.to_dict())
def delete(self, identifier): """Delete the named action.""" acl.enforce('actions:delete', context.ctx()) LOG.info("Delete action [identifier=%s]", identifier) with db_api.transaction(): db_model = db_api.get_action_definition(identifier) if db_model.is_system: msg = "Attempt to delete a system action: %s" % identifier raise exc.DataAccessException(msg) db_api.delete_action_definition(identifier)
def generate_key_pair(key_length=2048): """Create RSA key pair with specified number of bits in key. Returns tuple of private and public keys. """ with tempdir() as tmpdir: keyfile = os.path.join(tmpdir, 'tempkey') args = [ 'ssh-keygen', '-q', # quiet '-N', '', # w/o passphrase '-t', 'rsa', # create key of rsa type '-f', keyfile, # filename of the key file '-C', 'Generated-by-Mistral' # key comment ] if key_length is not None: args.extend(['-b', key_length]) processutils.execute(*args) if not os.path.exists(keyfile): raise exc.DataAccessException( "Private key file hasn't been created") private_key = open(keyfile).read() public_key_path = keyfile + '.pub' if not os.path.exists(public_key_path): raise exc.DataAccessException( "Public key file hasn't been created") public_key = open(public_key_path).read() return private_key, public_key
def end_tx(): """Ends current database transaction. It rolls back all uncommitted changes and closes database session. """ ses = _get_thread_local_session() if not ses: raise exc.DataAccessException("Database transaction has not been" " started.") if ses.dirty: ses.rollback() ses.close() _set_thread_local_session(None)
def tempdir(**kwargs): argdict = kwargs.copy() if 'dir' not in argdict: argdict['dir'] = '/tmp/' tmpdir = tempfile.mkdtemp(**argdict) try: yield tmpdir finally: try: shutil.rmtree(tmpdir) except OSError as e: raise exc.DataAccessException( "Failed to delete temp dir %(dir)s (reason: %(reason)s)" % {'dir': tmpdir, 'reason': e} )
def delete_workflow_definition(identifier, session=None): wf_def = get_workflow_definition(identifier) if wf_def.project_id != security.get_project_id(): raise exc.NotAllowedException( "Can not delete workflow of other users. [workflow_identifier=%s]" % identifier) if wf_def.is_system: msg = "Attempt to delete a system workflow: %s" % identifier raise exc.DataAccessException(msg) cron_triggers = _get_associated_cron_triggers(identifier) if cron_triggers: raise exc.DBException( "Can't delete workflow that has triggers associated. " "[workflow_identifier=%s], [cron_trigger_name(s)=%s]" % (identifier, ', '.join(cron_triggers))) session.delete(wf_def)