def _verify_and_get_source_and_target_deployments( sm, source_deployment_id, target_deployment_id, is_component_deletion=False, external_source=None, external_target=None): if external_source: source_deployment = None else: source_deployment = sm.get(models.Deployment, source_deployment_id, fail_silently=True) if not source_deployment: raise manager_exceptions.NotFoundError( 'Given source deployment with ID `{0}` does not ' 'exist.'.format(source_deployment_id)) target_deployment = sm.get(models.Deployment, target_deployment_id, fail_silently=True) if not (is_component_deletion or external_source or external_target or target_deployment): raise manager_exceptions.NotFoundError( 'Given target deployment with ID `{0}` does not ' 'exist.'.format(target_deployment_id)) return source_deployment, target_deployment
def get(self, model_class, element_id, include=None, filters=None, locking=False, all_tenants=None): """Return a single result based on the model class and element ID """ current_app.logger.debug('Get `{0}` with ID `{1}`'.format( model_class.__name__, element_id)) filters = filters or {'id': element_id} query = self._get_query(model_class, include, filters, all_tenants=all_tenants) if locking: query = query.with_for_update() result = query.first() if not result: raise manager_exceptions.NotFoundError( 'Requested `{0}` with ID `{1}` was not found'.format( model_class.__name__, element_id)) current_app.logger.debug('Returning {0}'.format(result)) return result
def get(self, blueprint_id, **kwargs): """ Download blueprint's archive """ blueprint = get_storage_manager().get(models.Blueprint, blueprint_id) for arc_type in SUPPORTED_ARCHIVE_TYPES: # attempting to find the archive file on the file system local_path = os.path.join(config.instance.file_server_root, FILE_SERVER_UPLOADED_BLUEPRINTS_FOLDER, blueprint.tenant.name, blueprint.id, '{0}.{1}'.format(blueprint.id, arc_type)) if os.path.isfile(local_path): archive_type = arc_type break else: raise manager_exceptions.NotFoundError( "Could not find blueprint's archive; Blueprint ID: {0}".format( blueprint.id)) blueprint_path = '{0}/{1}/{2}/{3}/{3}.{4}'.format( FILE_SERVER_RESOURCES_FOLDER, FILE_SERVER_UPLOADED_BLUEPRINTS_FOLDER, blueprint.tenant.name, blueprint.id, archive_type) return make_streaming_response(blueprint.id, blueprint_path, archive_type)
def get_node(self, deployment_id, node_id, **_): data = self._load_data() node_id = '{}_{}'.format(deployment_id, node_id) if node_id in data[NODES]: return data[NODES][node_id] raise manager_exceptions.NotFoundError( "Deployment {0} not found".format(deployment_id))
def update_node(self, deployment_id, node_id, number_of_instances=None, planned_number_of_instances=None, relationships=None, operations=None, plugins=None, properties=None): storage_node_id = self._storage_node_id(deployment_id, node_id) update_doc_data = {} if relationships: update_doc_data['relationships'] = relationships if operations: update_doc_data['operations'] = operations if plugins: update_doc_data['plugins'] = plugins if properties: update_doc_data['properties'] = properties if number_of_instances is not None: update_doc_data['number_of_instances'] = number_of_instances if planned_number_of_instances is not None: update_doc_data[ 'planned_number_of_instances'] = planned_number_of_instances update_doc = {'doc': update_doc_data} try: self._connection.update(index=STORAGE_INDEX_NAME, doc_type=NODE_TYPE, id=storage_node_id, body=update_doc, **MUTATE_PARAMS) return update_doc_data except elasticsearch.exceptions.NotFoundError: raise manager_exceptions.NotFoundError( "Node {0} not found".format(node_id))
def update_provider_context(self, provider_context): data = self._load_data() if PROVIDER_CONTEXT_ID not in data[PROVIDER_CONTEXT]: raise manager_exceptions.NotFoundError('Provider Context not ' 'found') data[PROVIDER_CONTEXT][PROVIDER_CONTEXT_ID] = provider_context self._dump_data(data)
def _delete_object(self, object_id, object_type, object_type_name): data = self._load_data() if object_id in data[object_type]: obj = data[object_type][object_id] del(data[object_type][object_id]) self._dump_data(data) return obj raise manager_exceptions.NotFoundError( "{0} {1} not found".format(object_type_name, object_id))
def put_deployment_update_step(self, deployment_update_id, step): data = self._load_data() if str(deployment_update_id) not in data[DEPLOYMENT_UPDATES]: raise manager_exceptions.NotFoundError( "Deployment Update {0} doesn't exist".format( deployment_update_id)) data[DEPLOYMENT_UPDATES][str(deployment_update_id)].steps.append(step) self._dump_data(data)
def update_deployment(self, deployment): deployment_id = deployment.id data = self._load_data() if deployment_id not in data[DEPLOYMENTS]: raise manager_exceptions.NotFoundError( 'Deployment {0} not found'.format(deployment_id)) updated_deployment = data[DEPLOYMENTS][deployment_id] if deployment.scaling_groups is not None: updated_deployment.scaling_groups = deployment.scaling_groups self._dump_data(data)
def _check_if_deployment_exists(self, deployment_id, all_tenants): deployments_list = get_storage_manager().list( models.Deployment, include=['id'], filters={'id': deployment_id}, all_tenants=all_tenants) if not deployments_list: raise manager_exceptions.NotFoundError( 'Requested `Deployment` with ID `{0}` was not found'.format( deployment_id))
def update_provider_context(self, provider_context): doc_data = {'doc': provider_context.to_dict()} try: self._connection.update(index=STORAGE_INDEX_NAME, doc_type=PROVIDER_CONTEXT_TYPE, id=PROVIDER_CONTEXT_ID, body=doc_data, **MUTATE_PARAMS) except elasticsearch.exceptions.NotFoundError: raise manager_exceptions.NotFoundError( 'Provider Context not found')
def _verify_and_get_source_and_target_deployments(sm, source_deployment_id, target_deployment_id): source_deployment = sm.get(models.Deployment, source_deployment_id, fail_silently=True) if not source_deployment: raise manager_exceptions.NotFoundError( 'Given source deployment with ID `{0}` does not exist.'.format( source_deployment_id) ) target_deployment = sm.get(models.Deployment, target_deployment_id, fail_silently=True) if not target_deployment: raise manager_exceptions.NotFoundError( 'Given target deployment with ID `{0}` does not exist.'.format( target_deployment_id) ) return source_deployment, target_deployment
def get_deployment(self, deployment_id, include=None): data = self._load_data() if deployment_id in data[DEPLOYMENTS]: dep = data[DEPLOYMENTS][deployment_id] if include: for field in Deployment.fields: if field not in include: setattr(dep, field, None) return dep raise manager_exceptions.NotFoundError( "Deployment {0} not found".format(deployment_id))
def get_plugin(self, plugin_id, include=None): data = self._load_data() if plugin_id in data[PLUGINS]: plugin = data[PLUGINS][plugin_id] if include: for field in Plugin.fields: if field not in include: setattr(plugin, field, None) return plugin raise manager_exceptions.NotFoundError( "Plugin {0} not found".format(plugin_id))
def update_execution_status(self, execution_id, status, error): data = self._load_data() if execution_id not in data[EXECUTIONS]: raise manager_exceptions.NotFoundError( "Execution {0} not found".format(execution_id)) execution = data[EXECUTIONS][execution_id] execution.status = status execution.error = error data[EXECUTIONS][execution_id] = execution self._dump_data(data)
def get_blueprint(self, blueprint_id, include=None): data = self._load_data() if blueprint_id in data[BLUEPRINTS]: bp = data[BLUEPRINTS][blueprint_id] if include: for field in BlueprintState.fields: if field not in include: setattr(bp, field, None) return bp raise manager_exceptions.NotFoundError( "Blueprint {0} not found".format(blueprint_id))
def _delete_doc(self, doc_type, doc_id, model_class, id_field='id'): try: res = self._connection.delete(STORAGE_INDEX_NAME, doc_type, doc_id, **MUTATE_PARAMS) except elasticsearch.exceptions.NotFoundError: raise manager_exceptions.NotFoundError("{0} {1} not found".format( doc_type, doc_id)) fields_data = {id_field: res['_id']} return self._fill_missing_fields_and_deserialize( fields_data, model_class)
def update_snapshot_status(self, snapshot_id, status, error): update_doc_data = {'status': status, 'error': error} update_doc = {'doc': update_doc_data} try: self._connection.update(index=STORAGE_INDEX_NAME, doc_type=SNAPSHOT_TYPE, id=str(snapshot_id), body=update_doc, **MUTATE_PARAMS) except elasticsearch.exceptions.NotFoundError: raise manager_exceptions.NotFoundError( "Snapshot {0} not found".format(snapshot_id))
def update_execution_status(self, execution_id, status, error): update_doc_data = {'status': status, 'error': error} update_doc = {'doc': update_doc_data} try: self._get_es_conn().update(index=STORAGE_INDEX_NAME, doc_type=EXECUTION_TYPE, id=str(execution_id), body=update_doc) except elasticsearch.exceptions.NotFoundError: raise manager_exceptions.NotFoundError( "Execution {0} not found".format(execution_id))
def _get_config(self, name): sm = get_storage_manager() scope, _, name = name.rpartition('.') filters = {'name': name} if scope: filters['scope'] = scope results = sm.list(models.Config, None, filters=filters) if not results: raise manager_exceptions.NotFoundError(name) elif len(results) > 1: raise manager_exceptions.AmbiguousName( 'Expected 1 value, but found {0}'.format(len(results))) return results[0]
def update_node(self, deployment_id, node_id, number_of_instances=None, planned_number_of_instances=None): data = self._load_data() storage_node_id = '{0}_{1}'.format(deployment_id, node_id) if storage_node_id not in data[NODES]: raise manager_exceptions.NotFoundError( 'Node {0} not found'.format(node_id)) node = data[NODES][storage_node_id] if number_of_instances is not None: node.number_of_instances = number_of_instances if planned_number_of_instances is not None: node.planned_number_of_instances = planned_number_of_instances self._dump_data(data)
def _delete_instance_by_id(self, model_class, element_id, filters=None): """Delete a single result based on the model class and element ID """ try: instance = self._get_by_id(model_class, element_id, filters=filters) except manager_exceptions.NotFoundError: raise manager_exceptions.NotFoundError( 'Could not delete {0} with ID `{1}` - element not found'. format(model_class.__name__, element_id)) db.session.delete(instance) self._safe_commit() return instance
def _get_doc(self, doc_type, doc_id, fields=None): try: if fields: return self._connection.get(index=STORAGE_INDEX_NAME, doc_type=doc_type, id=doc_id, _source=[f for f in fields]) else: return self._connection.get(index=STORAGE_INDEX_NAME, doc_type=doc_type, id=doc_id) except elasticsearch.exceptions.NotFoundError: raise manager_exceptions.NotFoundError( '{0} {1} not found'.format(doc_type, doc_id))
def update_deployment(self, deployment): deployment_id = deployment.id update_doc = \ {'doc': {k: v for k, v in deployment.to_dict().iteritems() if v is not None}} try: self._connection.update(index=STORAGE_INDEX_NAME, doc_type=DEPLOYMENT_TYPE, id=deployment_id, body=update_doc, **MUTATE_PARAMS) except elasticsearch.exceptions.NotFoundError: raise manager_exceptions.NotFoundError( "Deployment {0} not found".format(deployment_id))
def update_node_instance(self, node_update): data = self._load_data() if node_update.id not in data[NODE_INSTANCES]: raise manager_exceptions.NotFoundError( "Node {0} not found".format(node_update.id)) node = data[NODE_INSTANCES][node_update.id] if node_update.state is not None: node.state = node_update.state if node_update.runtime_properties is not None: node.runtime_properties = node_update.runtime_properties data[NODE_INSTANCES][node.id] = node self._dump_data(data)
def _get_by_id(self, model_class, element_id, include=None, filters=None): """Return a single result based on the model class and element ID """ filters = filters or {'id': element_id} result = self._get_query(model_class, include, filters).first() if not result: raise manager_exceptions.NotFoundError( 'Requested {0} with ID `{1}` was not found' .format(model_class.__name__, element_id) ) return result
def update_deployment_modification(self, modification): modification_id = modification.id data = self._load_data() if modification_id not in data[DEPLOYMENT_MODIFICATIONS]: raise manager_exceptions.NotFoundError( 'Deployment modification {0} not found' .format(modification_id)) updated_modification = \ data[DEPLOYMENT_MODIFICATIONS][modification_id] if modification.status is not None: updated_modification.status = modification.status if modification.ended_at is not None: updated_modification.ended_at = modification.ended_at if modification.node_instances is not None: updated_modification.node_instances = \ modification.node_instances self._dump_data(data)
def update_deployment_modification(self, modification): modification_id = modification.id update_doc_data = {} if modification.status is not None: update_doc_data['status'] = modification.status if modification.ended_at is not None: update_doc_data['ended_at'] = modification.ended_at if modification.node_instances is not None: update_doc_data['node_instances'] = modification.node_instances update_doc = {'doc': update_doc_data} try: self._connection.update(index=STORAGE_INDEX_NAME, doc_type=DEPLOYMENT_MODIFICATION_TYPE, id=modification_id, body=update_doc, **MUTATE_PARAMS) except elasticsearch.exceptions.NotFoundError: raise manager_exceptions.NotFoundError( "Modification {0} not found".format(modification_id))
def extract_blueprint_archive_to_file_server(self, blueprint_id, tenant): sm = get_resource_manager().sm file_server_root = config.instance.file_server_root local_path = os.path.join(config.instance.file_server_root, FILE_SERVER_UPLOADED_BLUEPRINTS_FOLDER, tenant, blueprint_id) for arc_type in SUPPORTED_ARCHIVE_TYPES: # attempting to find the archive file on the file system local_file_path = os.path.join( local_path, '{0}.{1}'.format(blueprint_id, arc_type)) if os.path.isfile(local_file_path): break else: error_msg = "Could not find blueprint's archive; " \ "Blueprint ID: {0}".format(blueprint_id) blueprint = sm.get(Blueprint, blueprint_id) blueprint.state = \ BlueprintUploadState.FAILED_EXTRACTING_TO_FILE_SERVER blueprint.error = error_msg sm.update(blueprint) raise manager_exceptions.NotFoundError(error_msg) try: app_dir = self._extract_file_to_file_server( local_file_path, file_server_root) except Exception as e: blueprint = sm.get(Blueprint, blueprint_id) blueprint.state = \ BlueprintUploadState.FAILED_EXTRACTING_TO_FILE_SERVER blueprint.error = str(e) sm.update(blueprint) remove(local_path) raise e tenant_dir = os.path.join(file_server_root, FILE_SERVER_BLUEPRINTS_FOLDER, tenant) mkdirs(tenant_dir) shutil.move(os.path.join(file_server_root, app_dir), os.path.join(tenant_dir, blueprint_id)) self._process_plugins(file_server_root, blueprint_id)
def update_node_instance(self, node_update): data = self._load_data() if node_update.id not in data[NODE_INSTANCES]: raise manager_exceptions.NotFoundError("Node {0} not found".format( node_update.id)) node = data[NODE_INSTANCES][node_update.id] if node.version is not None and node_update.version is not None: if node_update.version <= node.version: raise manager_exceptions.ConflictError( 'Node instance update conflict [current_version={0}, ' 'updated_version={1}]'.format(node.version, node_update.version)) if node_update.state is not None: node.state = node_update.state if node_update.runtime_properties is not None: node.runtime_properties = node_update.runtime_properties if node_update.relationships is not None: node.relationships = node_update.relationships data[NODE_INSTANCES][node.id] = node self._dump_data(data)