示例#1
0
 def volume_changes(self, new_volumes):
     if not new_volumes:
         return
     old_volumes = volume_repo.get_service_volumes_with_config_file(self.service.service_id)
     old_volume_paths = {volume.volume_path: volume for volume in old_volumes}
     old_volume_names = {volume.volume_name: volume for volume in old_volumes}
     add = []
     update = []
     for new_volume in new_volumes:
         old_volume = old_volume_paths.get(new_volume["volume_path"], None)
         old_volume_name = old_volume_names.get(new_volume["volume_name"], None)
         if not old_volume and not old_volume_name:
             add.append(new_volume)
             continue
         if not old_volume and old_volume_name:
             new_volume["volume_name"] = new_volume["volume_name"] + "-" + make_uuid()[:6]
             add.append(new_volume)
             continue
         if not new_volume.get("file_content"):
             continue
         old_file_content = volume_repo.get_service_config_file(old_volume)
         if old_file_content and old_file_content.file_content != new_volume["file_content"]:
             update.append(new_volume)
     if not add and not update:
         return None
     return {
         "add": add,
         "upd": update,
     }
示例#2
0
    def _create_components(self, app_id, upgrade_group_id):
        components = group_service.list_components_by_upgrade_group_id(app_id, upgrade_group_id)
        component_ids = [cpt.component_id for cpt in components]

        http_rules = self._list_http_rules(component_ids)
        tcp_rules = self._list_tcp_rules(component_ids)

        result = []
        # TODO(huangrh): get the attributes at once, don't get it iteratively
        for cpt in components:
            component_source = service_source_repo.get_service_source(cpt.tenant_id, cpt.service_id)
            envs = env_var_repo.get_service_env(cpt.tenant_id, cpt.service_id)
            ports = port_repo.get_service_ports(cpt.tenant_id, cpt.service_id)
            volumes = volume_repo.get_service_volumes_with_config_file(cpt.service_id)
            config_files = volume_repo.get_service_config_files(cpt.service_id)
            probes = probe_repo.list_probes(cpt.service_id)
            monitors = service_monitor_repo.list_by_service_ids(cpt.tenant_id, [cpt.service_id])
            graphs = component_graph_repo.list(cpt.service_id)
            component = Component(
                cpt,
                component_source,
                envs,
                ports,
                volumes,
                config_files,
                probes,
                None,
                monitors,
                graphs, [],
                http_rules=http_rules.get(cpt.component_id),
                tcp_rules=tcp_rules.get(cpt.component_id),
                support_labels=self.support_labels)
            result.append(component)
        return result
 def __save_volume(self, tenant, service, tenant_service_volumes,
                   service_config_file):
     contain_config_file = False if not service_config_file else True
     if not service_config_file:
         contain_config_file = True
     volume_list = []
     config_list = []
     volume_name_id = {}
     for volume in tenant_service_volumes:
         index = volume.pop("ID")
         volume_name_id[volume["volume_name"]] = index
         if volume["volume_type"] == "config-file" and contain_config_file:
             for config_file in service_config_file:
                 if config_file["volume_id"] == index:
                     config_file.pop("ID")
                     new_config_file = TenantServiceConfigurationFile(
                         **config_file)
                     new_config_file.service_id = service.service_id
                     config_list.append(new_config_file)
         settings = volume_service.get_best_suitable_volume_settings(
             tenant, service, volume["volume_type"],
             volume.get("access_mode"), volume.get("share_policy"),
             volume.get("backup_policy"), None,
             volume.get("volume_provider_name"))
         if settings["changed"]:
             logger.debug('volume type changed from {0} to {1}'.format(
                 volume["volume_type"], settings["volume_type"]))
             volume["volume_type"] = settings["volume_type"]
         new_volume = TenantServiceVolume(**volume)
         new_volume.service_id = service.service_id
         volume_list.append(new_volume)
     if volume_list:
         # bulk_create do not return volume's id(django database connection feature can_return_ids_from_bulk_insert)
         TenantServiceVolume.objects.bulk_create(volume_list)
         # query again volume for volume_id
         volumes = volume_repo.get_service_volumes_with_config_file(
             service.service_id)
         # prepare old volume_id and new volume_id relations
         volume_name_ids = [{
             "volume_name": volume.volume_name,
             "volume_id": volume.ID
         } for volume in volumes]
         volume_id_relations = {}
         for vni in volume_name_ids:
             if volume_name_id.get(vni["volume_name"]):
                 old_volume_id = volume_name_id.get(vni["volume_name"])
                 new_volume_id = vni["volume_id"]
                 volume_id_relations[old_volume_id] = new_volume_id
         for config in config_list:
             if volume_id_relations.get(config.volume_id):
                 config.volume_id = volume_id_relations.get(
                     config.volume_id)
         TenantServiceConfigurationFile.objects.bulk_create(config_list)
示例#4
0
    def get_service_details(self, tenant, service):
        service_base = service.to_dict()
        service_labels = service_label_repo.get_service_labels(service.service_id)
        service_domains = domain_repo.get_service_domains(service.service_id)
        service_tcpdomains = tcp_domain.get_service_tcpdomains(service.service_id)
        service_perms = service_perm_repo.get_service_perms_by_service_pk(service.ID)
        service_probes = probe_repo.get_service_probe(service.service_id)
        service_source = service_source_repo.get_service_source(tenant.tenant_id, service.service_id)
        service_auths = auth_repo.get_service_auth(service.service_id)
        service_env_vars = env_var_repo.get_service_env(tenant.tenant_id, service.service_id)
        service_compile_env = compile_env_repo.get_service_compile_env(service.service_id)
        service_extend_method = extend_repo.get_extend_method_by_service(service)
        service_mnts = mnt_repo.get_service_mnts(tenant.tenant_id, service.service_id)
        service_volumes = volume_repo.get_service_volumes_with_config_file(service.service_id)
        service_config_file = volume_repo.get_service_config_files(service.service_id)
        service_ports = port_repo.get_service_ports(tenant.tenant_id, service.service_id)
        service_relation = dep_relation_repo.get_service_dependencies(tenant.tenant_id, service.service_id)
        # plugin
        service_plugin_relation = app_plugin_relation_repo.get_service_plugin_relation_by_service_id(service.service_id)
        service_plugin_config = service_plugin_config_repo.get_service_plugin_all_config(service.service_id)

        app_info = {
            "service_base": service_base,
            "service_labels": [label.to_dict() for label in service_labels],
            "service_domains": [domain.to_dict() for domain in service_domains],
            "service_tcpdomains": [tcpdomain.to_dict() for tcpdomain in service_tcpdomains],
            "service_perms": [perm.to_dict() for perm in service_perms],
            "service_probes": [probe.to_dict() for probe in service_probes],
            "service_source": service_source.to_dict() if service_source else None,
            "service_auths": [auth.to_dict() for auth in service_auths],
            "service_env_vars": [env_var.to_dict() for env_var in service_env_vars],
            "service_compile_env": service_compile_env.to_dict() if service_compile_env else None,
            "service_extend_method": service_extend_method.to_dict() if service_extend_method else None,
            "service_mnts": [mnt.to_dict() for mnt in service_mnts],
            "service_plugin_relation": [plugin_relation.to_dict() for plugin_relation in service_plugin_relation],
            "service_plugin_config": [config.to_dict() for config in service_plugin_config],
            "service_relation": [relation.to_dict() for relation in service_relation],
            "service_volumes": [volume.to_dict() for volume in service_volumes],
            "service_config_file": [config_file.to_dict() for config_file in service_config_file],
            "service_ports": [port.to_dict() for port in service_ports]
        }

        plugin_ids = [pr.plugin_id for pr in service_plugin_relation]

        return app_info, plugin_ids
    def get_service_details(self, tenant, service):
        service_base = service.to_dict()
        service_labels = service_label_repo.get_service_labels(
            service.service_id)
        service_domains = domain_repo.get_service_domains(service.service_id)
        http_rule_configs = configuration_repo.list_by_rule_ids(
            [sd.http_rule_id for sd in service_domains])
        service_tcpdomains = tcp_domain.get_service_tcpdomains(
            service.service_id)
        service_probes = probe_repo.get_service_probe(service.service_id)
        service_source = service_source_repo.get_service_source(
            tenant.tenant_id, service.service_id)
        service_auths = auth_repo.get_service_auth(service.service_id)
        service_env_vars = env_var_repo.get_service_env(
            tenant.tenant_id, service.service_id)
        service_compile_env = compile_env_repo.get_service_compile_env(
            service.service_id)
        service_extend_method = extend_repo.get_extend_method_by_service(
            service)
        service_mnts = mnt_repo.get_service_mnts(tenant.tenant_id,
                                                 service.service_id)
        service_volumes = volume_repo.get_service_volumes_with_config_file(
            service.service_id)
        service_config_file = volume_repo.get_service_config_files(
            service.service_id)
        service_ports = port_repo.get_service_ports(tenant.tenant_id,
                                                    service.service_id)
        service_relation = dep_relation_repo.get_service_dependencies(
            tenant.tenant_id, service.service_id)
        service_monitors = service_monitor_repo.get_component_service_monitors(
            tenant.tenant_id, service.service_id)
        component_graphs = component_graph_repo.list(service.service_id)
        # plugin
        service_plugin_relation = app_plugin_relation_repo.get_service_plugin_relation_by_service_id(
            service.service_id)
        service_plugin_config = service_plugin_config_repo.get_service_plugin_all_config(
            service.service_id)
        # third_party_service
        third_party_service_endpoints = service_endpoints_repo.get_service_endpoints_by_service_id(
            service.service_id)
        if service.service_source == "third_party":
            if not third_party_service_endpoints:
                raise ServiceHandleException(
                    msg="third party service endpoints can't be null",
                    msg_show="第三方组件实例不可为空")
        app_info = {
            "component_id":
            service.component_id,
            "service_base":
            service_base,
            "service_labels": [label.to_dict() for label in service_labels],
            "service_domains":
            [domain.to_dict() for domain in service_domains],
            "http_rule_configs":
            [config.to_dict() for config in http_rule_configs],
            "service_tcpdomains":
            [tcpdomain.to_dict() for tcpdomain in service_tcpdomains],
            "service_probes": [probe.to_dict() for probe in service_probes],
            "service_source":
            service_source.to_dict() if service_source else None,
            "service_auths": [auth.to_dict() for auth in service_auths],
            "service_env_vars":
            [env_var.to_dict() for env_var in service_env_vars],
            "service_compile_env":
            service_compile_env.to_dict() if service_compile_env else None,
            "service_extend_method":
            service_extend_method.to_dict() if service_extend_method else None,
            "service_mnts": [mnt.to_dict() for mnt in service_mnts],
            "service_plugin_relation": [
                plugin_relation.to_dict()
                for plugin_relation in service_plugin_relation
            ],
            "service_plugin_config":
            [config.to_dict() for config in service_plugin_config],
            "service_relation":
            [relation.to_dict() for relation in service_relation],
            "service_volumes":
            [volume.to_dict() for volume in service_volumes],
            "service_config_file":
            [config_file.to_dict() for config_file in service_config_file],
            "service_ports": [port.to_dict() for port in service_ports],
            "third_party_service_endpoints":
            [endpoint.to_dict() for endpoint in third_party_service_endpoints],
            "service_monitors":
            [monitor.to_dict() for monitor in service_monitors],
            "component_graphs":
            [graph.to_dict() for graph in component_graphs]
        }
        plugin_ids = [pr.plugin_id for pr in service_plugin_relation]

        return app_info, plugin_ids
示例#6
0
    def create_region_service(self, tenant, service, user_name, do_deploy=True, dep_sids=None):
        data = self.__init_create_data(tenant, service, user_name, do_deploy, dep_sids)
        service_dep_relations = dep_relation_repo.get_service_dependencies(tenant.tenant_id, service.service_id)
        # 依赖
        depend_ids = [{
            "dep_order": dep.dep_order,
            "dep_service_type": dep.dep_service_type,
            "depend_service_id": dep.dep_service_id,
            "service_id": dep.service_id,
            "tenant_id": dep.tenant_id
        } for dep in service_dep_relations]
        data["depend_ids"] = depend_ids
        # 端口
        ports = port_repo.get_service_ports(tenant.tenant_id, service.service_id)
        ports_info = ports.values('container_port', 'mapping_port', 'protocol', 'port_alias', 'is_inner_service',
                                  'is_outer_service')

        for port_info in ports_info:
            port_info["is_inner_service"] = False
            port_info["is_outer_service"] = False

        if ports_info:
            data["ports_info"] = list(ports_info)
        # 环境变量
        envs_info = env_var_repo.get_service_env(tenant.tenant_id, service.service_id).values(
            'container_port', 'name', 'attr_name', 'attr_value', 'is_change', 'scope')
        if envs_info:
            data["envs_info"] = list(envs_info)
        # 持久化目录
        volume_info = volume_repo.get_service_volumes_with_config_file(service.service_id).values(
            'ID', 'service_id', 'category', 'volume_name', 'volume_path', 'volume_type')
        if volume_info:
            logger.debug('--------volume_info----->{0}'.format(volume_info))
            for volume in volume_info:
                volume_id = volume['ID']
                config_file = volume_repo.get_service_config_file(volume_id)
                if config_file:
                    volume.update({"file_content": config_file.file_content})
            logger.debug('--------volume_info22222----->{0}'.format(volume_info))
            data["volumes_info"] = list(volume_info)

        logger.debug(tenant.tenant_name + " start create_service:" + datetime.datetime.now().strftime('%Y%m%d%H%M%S'))
        # 挂载信息
        mnt_info = mnt_repo.get_service_mnts(service.tenant_id, service.service_id)
        if mnt_info:
            data["dep_volumes_info"] = [{
                "dep_service_id": mnt.dep_service_id,
                "volume_path": mnt.mnt_dir,
                "volume_name": mnt.mnt_name
            } for mnt in mnt_info]

        # etcd keys
        data["etcd_key"] = service.check_uuid

        # runtime os name
        data["os_type"] = label_service.get_service_os_name(service)
        # 数据中心创建
        region_api.create_service(service.service_region, tenant.tenant_name, data)
        # 将组件创建状态变更为创建完成
        service.create_status = "complete"
        self.__handle_service_ports(tenant, service, ports)
        service.save()
        return service