Пример #1
0
 def _create_component(snap):
     # component
     component = TenantServiceInfo(**snap["service_base"])
     # component source
     component_source = ServiceSourceInfo(**snap["service_source"])
     # environment
     envs = [TenantServiceEnvVar(**env) for env in snap["service_env_vars"]]
     # ports
     ports = [TenantServicesPort(**port) for port in snap["service_ports"]]
     # service_extend_method
     extend_info = ServiceExtendMethod(**snap["service_extend_method"])
     # volumes
     volumes = [TenantServiceVolume(**volume) for volume in snap["service_volumes"]]
     # configuration files
     config_files = [TenantServiceConfigurationFile(**config_file) for config_file in snap["service_config_file"]]
     # probe
     probes = [ServiceProbe(**probe) for probe in snap["service_probes"]]
     # monitors
     monitors = [ServiceMonitor(**monitor) for monitor in snap["service_monitors"]]
     # graphs
     graphs = [ComponentGraph(**graph) for graph in snap["component_graphs"]]
     return Component(
         component=component,
         component_source=component_source,
         envs=envs,
         ports=ports,
         volumes=volumes,
         config_files=config_files,
         probe=probes[0] if probes else None,
         extend_info=extend_info,
         monitors=monitors,
         graphs=graphs,
         plugin_deps=[],
     )
Пример #2
0
    def create_internal_graphs(self, component_id, graph_name):
        _, internal_graphs = self._load_internal_graphs()
        if not internal_graphs or not internal_graphs.get(graph_name):
            raise ErrInternalGraphsNotFound

        graphs = []
        seq = self._next_sequence(component_id)
        for graph in internal_graphs.get(graph_name):
            try:
                _ = component_graph_repo.get_by_title(component_id, graph.get("title"))
                continue
            except ErrComponentGraphNotFound:
                pass

            try:
                promql = promql_service.add_or_update_label(component_id, graph["promql"])
            except AbortRequest as e:
                logger.warning("promql {}: {}".format(graph["promql"], e))
                continue
            # make sure there are no duplicate graph
            graphs.append(
                ComponentGraph(
                    component_id=component_id,
                    graph_id=make_uuid(),
                    title=graph["title"],
                    promql=promql,
                    sequence=seq,
                ))
            seq += 1
        ComponentGraph.objects.bulk_create(graphs)
Пример #3
0
    def bulk_create(self, component_id, graphs):
        if not graphs:
            return
        cgs = []
        for graph in graphs:
            try:
                _ = component_graph_repo.get_by_title(component_id, graph.get("title"))
                continue
            except ErrComponentGraphNotFound:
                pass

            try:
                promql = promql_service.add_or_update_label(component_id, graph.get("promql"))
            except AbortRequest as e:
                logger.warning("promql: {}, {}".format(graph.get("promql"), e))
                continue

            cgs.append(
                ComponentGraph(
                    component_id=component_id,
                    graph_id=make_uuid(),
                    title=graph.get("title"),
                    promql=promql,
                    sequence=graph.get("sequence"),
                ))
        ComponentGraph.objects.bulk_create(cgs)
Пример #4
0
    def _update_component_graphs(self, component_graphs):
        if not component_graphs:
            return
        graphs = component_graphs.get("add", [])
        for graph in graphs:
            new_graph = ComponentGraph(**graph)
            new_graph.graph_id = make_uuid()
            new_graph.component_id = self.component.component_id
            self.graphs.append(new_graph)

        graphs = component_graphs.get("upd", [])
        old_graphs = {graph.title: graph for graph in self.graphs}
        for graph in graphs:
            old_graph = old_graphs.get(graph.get("title"))
            if not old_graph:
                continue
            old_graph.promql = graph.get("promql", "")
            old_graph.sequence = graph.get("sequence", 99)
Пример #5
0
 def _create_component(self, snap):
     # component
     component = TenantServiceInfo(**snap["service_base"])
     # component source
     component_source = ServiceSourceInfo(**snap["service_source"])
     # environment
     envs = [TenantServiceEnvVar(**env) for env in snap["service_env_vars"]]
     # ports
     ports = [TenantServicesPort(**port) for port in snap["service_ports"]]
     # service_extend_method
     extend_info = None
     if snap.get("service_extend_method"):
         extend_info = ServiceExtendMethod(
             **snap.get("service_extend_method"))
     # volumes
     volumes = [
         TenantServiceVolume(**volume) for volume in snap["service_volumes"]
     ]
     # configuration files
     config_files = [
         TenantServiceConfigurationFile(**config_file)
         for config_file in snap["service_config_file"]
     ]
     # probe
     probes = [ServiceProbe(**probe) for probe in snap["service_probes"]]
     # monitors
     monitors = [
         ServiceMonitor(**monitor) for monitor in snap["service_monitors"]
     ]
     # graphs
     graphs = [
         ComponentGraph(**graph) for graph in snap["component_graphs"]
     ]
     service_labels = [
         ServiceLabels(**label) for label in snap["service_labels"]
     ]
     cpt = Component(
         component=component,
         component_source=component_source,
         envs=envs,
         ports=ports,
         volumes=volumes,
         config_files=config_files,
         probes=probes,
         extend_info=extend_info,
         monitors=monitors,
         graphs=graphs,
         plugin_deps=[],
         labels=service_labels,
         support_labels=self.support_labels,
     )
     cpt.action_type = snap.get("action_type", ActionType.BUILD.value)
     return cpt
Пример #6
0
 def _template_to_component_graphs(self, component, graphs):
     if not graphs:
         return []
     new_graphs = {}
     for graph in graphs:
         try:
             promql = promql_service.add_or_update_label(component.service_id, graph.get("promql"))
         except AbortRequest as e:
             logger.warning("promql: {}, {}".format(graph.get("promql"), e))
             continue
         new_graph = ComponentGraph(
             component_id=component.service_id,
             graph_id=make_uuid(),
             title=graph.get("title"),
             promql=promql,
             sequence=graph.get("sequence"),
         )
         new_graphs[new_graph.title] = new_graph
     return new_graphs.values()