def get_node_instance(self, node_instance_id, include=None):
     doc = self._get_doc(NODE_INSTANCE_TYPE,
                         node_instance_id,
                         fields=include)
     node = DeploymentNodeInstance(version=doc['_version'],
                                   **doc['_source'])
     return node
Esempio n. 2
0
    def _reduce_node_instances(self, reduced_node_instances,
                               extended_node_instances):
        for reduced_node_instance in reduced_node_instances:
            storage_relationships = self.sm.get_node_instance(
                reduced_node_instance['id']).relationships
            self._clean_relationship_index_field(storage_relationships)
            # Get all the remaining relationships
            remaining_relationships = reduced_node_instance['relationships']
            # Get the extended node instances
            extended_node_instances = \
                [i for i in extended_node_instances
                 if (i['id'] == reduced_node_instance['id'] and
                     'modification' in i)]

            # If this node was indeed extended, append the new relationships
            # to the remaining relationships (from the reduced node instance)
            if extended_node_instances:
                relationships = extended_node_instances[0]['relationships']
                self._clean_relationship_index_field(relationships)
                remaining_relationships.extend(relationships)

            remaining_relationships = \
                filter(lambda r: r in remaining_relationships,
                       storage_relationships)

            self.sm.update_node_instance(
                self._node_instance_template(
                    DeploymentNodeInstance(**reduced_node_instance),
                    relationships=remaining_relationships,
                    version=reduced_node_instance['version']))
Esempio n. 3
0
    def _load_data(self):
        if not os.path.isfile(self._storage_path):
            self._init_file()
        with open(self._storage_path, 'r') as f:
            data = json.load(f)
            deserialized_data = dict()
            deserialized_data[NODES] = \
                {key: DeploymentNode(**val) for key, val in data[NODES]
                    .iteritems()}
            deserialized_data[NODE_INSTANCES] = \
                {key: DeploymentNodeInstance(**val) for key, val in
                 data[NODE_INSTANCES].iteritems()}
            deserialized_data[BLUEPRINTS] = \
                {key: BlueprintState(**val) for key, val in data[BLUEPRINTS]
                    .iteritems()}
            deserialized_data[DEPLOYMENTS] = \
                {key: Deployment(**val) for key, val in data[DEPLOYMENTS]
                    .iteritems()}
            deserialized_data[EXECUTIONS] = \
                {key: Execution(**val) for key, val in data[EXECUTIONS]
                    .iteritems()}
            deserialized_data[PLUGINS] = \
                {key: Plugin(**val) for key, val in data[PLUGINS]
                    .iteritems()}
            deserialized_data[PROVIDER_CONTEXT] = \
                {key: ProviderContext(**val)
                 for key, val in data[PROVIDER_CONTEXT].iteritems()}
            deserialized_data[DEPLOYMENT_MODIFICATIONS] = \
                {key: DeploymentModification(**val) for key, val in
                 data[DEPLOYMENT_MODIFICATIONS].iteritems()}

            return deserialized_data
Esempio n. 4
0
    def _handle_adding_relationship_instance(self, instances, *_):
        """Handles adding a relationship to a node instance

        :param instances:
        :return: the extended and related node instances
        """
        modified_raw_instances = []

        for node_instance in (i for i in instances if 'modification' in i):
            # adding new relationships to the current relationships
            relationships = \
                self.sm.get_node_instance(node_instance['id']).relationships

            node_instance['relationships'] = \
                sorted(node_instance['relationships'],
                       key=lambda r: r.get('rel_index', 0))

            modified_raw_instances.append(node_instance)

            relationships.extend(node_instance['relationships'])
            self.sm.update_node_instance(
                self._node_instance_template(
                    DeploymentNodeInstance(**node_instance),
                    relationships=relationships,
                    version=_handle_version(node_instance['version'])))
        modify_related_raw_instances = \
            [n for n in instances if n not in modified_raw_instances]

        return \
            {
                NODE_MOD_TYPES.AFFECTED: modified_raw_instances,
                NODE_MOD_TYPES.RELATED: modify_related_raw_instances
            }
 def node_instances_list(self, include=None):
     search_result = self._get_es_conn().search(index=STORAGE_INDEX_NAME,
                                                doc_type=NODE_INSTANCE_TYPE,
                                                size=DEFAULT_SEARCH_SIZE,
                                                _source=include or True)
     docs_with_versions = \
         map(lambda hit: (hit['_source'], hit['_version']),
             search_result['hits']['hits'])
     return map(
         lambda doc_with_version: DeploymentNodeInstance(
             version=doc_with_version[1], **doc_with_version[0]),
         docs_with_versions)
Esempio n. 6
0
 def put_node_instance(self, instance_id, deployment_id,
                       runtime_properties):
     from manager_rest.models import DeploymentNodeInstance
     node = DeploymentNodeInstance(id=instance_id,
                                   node_id=None,
                                   deployment_id=deployment_id,
                                   runtime_properties=runtime_properties,
                                   state=None,
                                   version=None,
                                   relationships=None,
                                   host_id=None)
     sm.instance().put_node_instance(node)
Esempio n. 7
0
 def put_node_instance(self,
                       instance_id,
                       deployment_id,
                       runtime_properties=None,
                       node_id=None,
                       version=None):
     runtime_properties = runtime_properties or {}
     from manager_rest.models import DeploymentNodeInstance
     node = DeploymentNodeInstance(id=instance_id,
                                   node_id=node_id,
                                   deployment_id=deployment_id,
                                   runtime_properties=runtime_properties,
                                   state=None,
                                   version=version,
                                   relationships=None,
                                   host_id=None,
                                   scaling_groups=None)
     storage_manager._get_instance().put_node_instance(node)
Esempio n. 8
0
    def _handle_removing_node_instance(instances, *_):
        """Handles removing a node instance

        :param raw_instances:
        :return: the removed and related node instances
        """
        removed_raw_instances = []

        for node_instance in (i for i in instances if 'modification' in i):
            node_instance = DeploymentNodeInstance(**node_instance)
            removed_raw_instances.append(node_instance)

        remove_related_raw_instances = \
            [n for n in instances if n not in removed_raw_instances]

        return {
            NODE_MOD_TYPES.AFFECTED: removed_raw_instances,
            NODE_MOD_TYPES.RELATED: remove_related_raw_instances
        }