Example #1
0
 def sync_dep_vol(dep_vol_info):
     """
     raise RegionApiBaseHttpClient.CallApiError
     """
     dep_vol = volume_repo.get_service_volume_by_name(
         dep_vol_info["service_id"], dep_vol_info["mnt_name"])
     if dep_vol is None:
         logger.warning("dep service id: {}; volume name: {}; fail to \
             sync dep volume: dep volume not found".format(
             dep_vol_info["service_id"], dep_vol_info["mnt_name"]))
         return
     data = {
         "depend_service_id": dep_vol.service_id,
         "volume_name": dep_vol.volume_name,
         "volume_path": dep_vol_info['mnt_dir'].strip(),
         "enterprise_id": self.tenant.enterprise_id,
         "volume_type": dep_vol.volume_type
     }
     if dep_vol.volume_type == "config-file":
         config_file = volume_repo.get_service_config_file(dep_vol.ID)
         data["file_content"] = config_file.file_content
     region_api.add_service_dep_volumes(self.tenant.region,
                                        self.tenant.tenant_name,
                                        self.service.service_alias,
                                        data)
Example #2
0
 def volume_changes(self, new_volumes):
     old_volumes = volume_repo.get_service_volumes(self.service.service_id)
     old_volume_paths = {
         volume.volume_path: 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)
         if not old_volume:
             add.append(new_volume)
             continue
         if not new_volume.get("file_content"):
             continue
         old_file_content = volume_repo.get_service_config_file(
             old_volume.ID)
         if 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,
     }
    def add_service_mnt_relation(self, tenant, service, source_path,
                                 dep_volume):
        if service.create_status == "complete":
            if dep_volume.volume_type != "config-file":
                data = {
                    "depend_service_id": dep_volume.service_id,
                    "volume_name": dep_volume.volume_name,
                    "volume_path": source_path,
                    "enterprise_id": tenant.enterprise_id,
                    "volume_type": dep_volume.volume_type
                }
            else:
                config_file = volume_repo.get_service_config_file(
                    dep_volume.ID)
                data = {
                    "depend_service_id": dep_volume.service_id,
                    "volume_name": dep_volume.volume_name,
                    "volume_path": source_path,
                    "volume_type": dep_volume.volume_type,
                    "file_content": config_file.file_content,
                    "enterprise_id": tenant.enterprise_id
                }
            res, body = region_api.add_service_dep_volumes(
                service.service_region, tenant.tenant_name,
                service.service_alias, data)
            logger.debug("add service mnt info res: {0}, body:{1}".format(
                res, body))

        mnt_relation = mnt_repo.add_service_mnt_relation(
            tenant.tenant_id, service.service_id, dep_volume.service_id,
            dep_volume.volume_name, source_path)
        logger.debug("mnt service {0} to service {1} on dir {2}".format(
            mnt_relation.service_id, mnt_relation.dep_service_id,
            mnt_relation.mnt_dir))
        return 200, "success"
Example #4
0
 def _update_volumes(self, volumes):
     for volume in volumes.get("add"):
         volume["service_id"] = self.service.service_id
         host_path = "/grdata/tenant/{0}/service/{1}{2}".format(
             self.tenant.tenant_id, self.service.service_id,
             volume["volume_path"])
         volume["host_path"] = host_path
         file_content = volume.get("file_content", None)
         if file_content is not None:
             volume.pop("file_content")
         v = volume_repo.add_service_volume(**volume)
         if not file_content and volume["volume_type"] != "config-file":
             continue
         file_data = {
             "service_id": self.service.service_id,
             "volume_id": v.ID,
             "file_content": file_content
         }
         _ = volume_repo.add_service_config_file(**file_data)
     for volume in volumes.get("upd"):
         # only volume of type config-file can be updated,
         # and only the contents of the configuration file can be updated.
         file_content = volume.get("file_content", None)
         if not file_content and volume["volume_type"] != "config-file":
             continue
         v = volume_repo.get_service_volume_by_name(self.service.service_id,
                                                    volume["volume_name"])
         if not v:
             logger.warning(
                 "service id: {}; volume name: {}; failed to update volume: \
                 volume not found.".format(self.service.service_id,
                                           volume["volume_name"]))
         cfg = volume_repo.get_service_config_file(v.ID)
         cfg.file_content = file_content
         cfg.save()
Example #5
0
 def put(self, request, *args, **kwargs):
     """
     修改存储设置
     :param request:
     :param args:
     :param kwargs:
     :return:
     """
     # try:
     volume_id = kwargs.get("volume_id", None)
     new_volume_path = request.data.get("new_volume_path", None)
     new_file_content = request.data.get("new_file_content", None)
     if not volume_id:
         return Response(general_message(400, "volume_id is null",
                                         "未指定需要编辑的配置文件存储"),
                         status=400)
     volume = volume_repo.get_service_volume_by_pk(volume_id)
     if not volume:
         return Response(general_message(400, "volume is null", "存储不存在"),
                         status=400)
     mode = request.data.get("mode")
     if mode is not None:
         mode = ensure_volume_mode(mode)
     service_config = volume_repo.get_service_config_file(volume)
     if volume.volume_type == 'config-file':
         if not service_config:
             return Response(general_message(400, "file_content is null",
                                             "配置文件内容不存在"),
                             status=400)
         if new_volume_path == volume.volume_path and new_file_content == service_config.file_content and volume.mode == mode:
             return Response(general_message(400, "no change",
                                             "没有变化,不需要修改"),
                             status=400)
     else:
         if new_volume_path == volume.volume_path:
             return Response(general_message(400, "no change",
                                             "没有变化,不需要修改"),
                             status=400)
     data = {
         "volume_name": volume.volume_name,
         "volume_path": new_volume_path,
         "volume_type": volume.volume_type,
         "file_content": new_file_content,
         "operator": self.user.nick_name,
         "mode": mode,
     }
     res, body = region_api.upgrade_service_volumes(
         self.service.service_region, self.tenant.tenant_name,
         self.service.service_alias, data)
     if res.status == 200:
         volume.volume_path = new_volume_path
         if mode is not None:
             volume.mode = mode
         volume.save()
         if volume.volume_type == 'config-file':
             service_config.file_content = new_file_content
             service_config.save()
         result = general_message(200, "success", "修改成功")
         return Response(result, status=result["code"])
     return Response(general_message(405, "success", "修改失败"), status=405)
Example #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(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]

        # 数据中心创建
        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
Example #7
0
 def put(self, request, *args, **kwargs):
     """
     修改存储设置
     :param request:
     :param args:
     :param kwargs:
     :return:
     """
     try:
         volume_id = kwargs.get("volume_id", None)
         new_volume_path = request.data.get("new_volume_path", None)
         new_file_content = request.data.get("new_file_content", None)
         if not volume_id:
             return Response(general_message(400, "volume_id is null",
                                             u"未指定需要编辑的配置文件存储"),
                             status=400)
         volume = volume_repo.get_service_volume_by_pk(volume_id)
         if not volume:
             return Response(general_message(400, "volume is null",
                                             u"存储不存在"),
                             status=400)
         service_config = volume_repo.get_service_config_file(volume_id)
         if not service_config:
             return Response(general_message(400, "file_content is null",
                                             u"配置文件内容不存在"),
                             status=400)
         if new_volume_path == volume.volume_path and new_file_content == service_config.file_content:
             return Response(general_message(400, "no change",
                                             u"没有变化,不需要修改"),
                             status=400)
         try:
             data = {
                 "volume_name": volume.volume_name,
                 "volume_path": new_volume_path,
                 "volume_type": volume.volume_type,
                 "file_content": new_file_content
             }
             res, body = region_api.upgrade_service_volumes(
                 self.service.service_region, self.tenant.tenant_name,
                 self.service.service_alias, data)
             if res.status == 200:
                 volume.volume_path = new_volume_path
                 volume.save()
                 service_config.file_content = new_file_content
                 service_config.save()
                 result = general_message(200, "success", u"修改成功")
                 return Response(result, status=result["code"])
             return Response(general_message(405, "success", u"修改失败"),
                             status=405)
         except Exception as e:
             logger.exception(e)
             result = error_message(e.message)
             return Response(result, status=500)
     except Exception as e:
         logger.exception(e)
         result = error_message(e.message)
         return Response(result, status=500)
    def get(self, request, *args, **kwargs):
        """
        获取服务的持久化路径
        ---
        parameters:
            - name: tenantName
              description: 租户名
              required: true
              type: string
              paramType: path
            - name: serviceAlias
              description: 服务别名
              required: true
              type: string
              paramType: path
        """
        volume_types = parse_argument(request, 'volume_types', value_type=list)
        try:
            q = Q(volume_type__in=volume_types) if volume_types else Q()
            tenant_service_volumes = volume_service.get_service_volumes(
                self.tenant, self.service).filter(q)

            volumes_list = []
            if tenant_service_volumes:
                for tenant_service_volume in tenant_service_volumes:
                    volume_dict = dict()
                    volume_dict[
                        "service_id"] = tenant_service_volume.service_id
                    volume_dict["category"] = tenant_service_volume.category
                    volume_dict["host_path"] = tenant_service_volume.host_path
                    volume_dict[
                        "volume_type"] = tenant_service_volume.volume_type
                    volume_dict[
                        "volume_path"] = tenant_service_volume.volume_path
                    volume_dict[
                        "volume_name"] = tenant_service_volume.volume_name
                    volume_dict["ID"] = tenant_service_volume.ID
                    if tenant_service_volume.volume_type == "config-file":
                        cf_file = volume_repo.get_service_config_file(
                            tenant_service_volume.ID)
                        if cf_file:
                            volume_dict["file_content"] = cf_file.file_content
                    volumes_list.append(volume_dict)
            result = general_message(200, "success", "查询成功", list=volumes_list)
        except Exception as e:
            logger.exception(e)
            result = error_message(e.message)
        return Response(result, status=result["code"])
Example #9
0
    def get(self, request, *args, **kwargs):
        """
        获取组件的持久化路径
        ---
        parameters:
            - name: tenantName
              description: 租户名
              required: true
              type: string
              paramType: path
            - name: serviceAlias
              description: 组件别名
              required: true
              type: string
              paramType: path
        """
        is_config = parse_argument(request,
                                   'is_config',
                                   value_type=bool,
                                   default=False)

        volumes = volume_service.get_service_volumes(self.tenant, self.service,
                                                     is_config)
        volumes_list = []
        if is_config:
            for tenant_service_volume in volumes:
                volume = volume_repo.get_service_volume_by_pk(
                    tenant_service_volume["ID"])
                cf_file = volume_repo.get_service_config_file(volume)
                if cf_file:
                    tenant_service_volume[
                        "file_content"] = cf_file.file_content
                volumes_list.append(tenant_service_volume)
        else:
            dependents = mnt_service.get_volume_dependent(
                self.tenant, self.service)
            name2deps = {}
            if dependents:
                for dep in dependents:
                    if name2deps.get(dep["volume_name"], None) is None:
                        name2deps[dep["volume_name"]] = []
                    name2deps[dep["volume_name"]].append(dep)
            for vo in volumes:
                vo["dep_services"] = name2deps.get(vo["volume_name"], None)
                volumes_list.append(vo)
        result = general_message(200, "success", "查询成功", list=volumes_list)
        return Response(result, status=result["code"])
    def query_share_service_info(self, team, group_id):
        service_list = share_repo.get_service_list_by_group_id(
            team=team, group_id=group_id)
        if service_list:
            array_ids = [x.service_id for x in service_list]
            deploy_versions = self.get_team_service_deploy_version(
                service_list[0].service_region, team, array_ids)
            array_keys = []
            for x in service_list:
                if x.service_key == "application" or x.service_key == "0000" or x.service_key == "":
                    array_keys.append(x.service_key)
            # 查询组件端口信息
            service_port_map = self.get_service_ports_by_ids(array_ids)
            # 查询组件依赖
            dep_service_map = self.get_service_dependencys_by_ids(array_ids)
            # 查询组件可变参数和不可变参数
            # service_env_change_map, service_env_nochange_map = self.get_service_env_by_ids(array_ids)
            service_env_map = self.get_service_env_by_ids(array_ids)
            # 查询组件持久化信息
            service_volume_map = self.get_service_volume_by_ids(array_ids)
            # dependent volume
            dep_mnt_map = self.get_dep_mnts_by_ids(team.tenant_id, array_ids)
            # 查询组件伸缩方式信息
            extend_method_map = self.get_service_extend_method_by_keys(
                array_keys)
            # 获取组件的健康检测设置
            probe_map = self.get_service_probes(array_ids)

            all_data_map = dict()

            for service in service_list:
                data = dict()
                data['service_id'] = service.service_id
                data['tenant_id'] = service.tenant_id
                data['service_cname'] = service.service_cname
                data['service_key'] = service.service_key
                if (service.service_key == 'application'
                        or service.service_key == '0000'
                        or service.service_key == 'mysql'):
                    data['service_key'] = make_uuid()
                    service.service_key = data['service_key']
                    service.save()
                #     data['need_share'] = True
                # else:
                #     data['need_share'] = False
                data["service_share_uuid"] = "{0}+{1}".format(
                    data['service_key'], data['service_id'])
                data['need_share'] = True
                data['category'] = service.category
                data['language'] = service.language
                data['extend_method'] = service.extend_method
                data['version'] = service.version
                data['memory'] = service.min_memory - service.min_memory % 32
                data['service_type'] = service.service_type
                data['service_source'] = service.service_source
                data['deploy_version'] = deploy_versions[data[
                    'service_id']] if deploy_versions else service.deploy_version
                data['image'] = service.image
                data['service_alias'] = service.service_alias
                data['service_name'] = service.service_name
                data['service_region'] = service.service_region
                data['creater'] = service.creater
                data["cmd"] = service.cmd
                data['probes'] = [
                    probe.to_dict()
                    for probe in probe_map.get(service.service_id, [])
                ]
                extend_method = extend_method_map.get(service.service_key)
                if extend_method:
                    e_m = dict()
                    e_m['min_node'] = service.min_node
                    e_m['max_node'] = extend_method.max_node
                    e_m['step_node'] = extend_method.step_node
                    e_m['min_memory'] = service.min_memory - service.min_memory % 32
                    e_m['max_memory'] = extend_method.max_memory
                    e_m['step_memory'] = extend_method.step_memory
                    e_m['is_restart'] = extend_method.is_restart
                    data['extend_method_map'] = e_m
                else:
                    data['extend_method_map'] = {
                        "min_node": service.min_node,
                        "max_node": 20,
                        "step_node": 1,
                        "min_memory": service.min_memory,
                        "max_memory": 65536,
                        "step_memory": 128,
                        "is_restart": 0
                    }
                data['port_map_list'] = list()
                if service_port_map.get(service.service_id):
                    for port in service_port_map.get(service.service_id):
                        p = dict()
                        # 写需要返回的port数据
                        p['protocol'] = port.protocol
                        p['tenant_id'] = port.tenant_id
                        p['port_alias'] = port.port_alias
                        p['container_port'] = port.container_port
                        p['is_inner_service'] = port.is_inner_service
                        p['is_outer_service'] = port.is_outer_service
                        data['port_map_list'].append(p)

                data['service_volume_map_list'] = list()
                if service_volume_map.get(service.service_id):
                    for volume in service_volume_map.get(service.service_id):
                        s_v = dict()
                        s_v['file_content'] = ''
                        if volume.volume_type == "config-file":
                            config_file = volume_repo.get_service_config_file(
                                volume.ID)
                            if config_file:
                                s_v['file_content'] = config_file.file_content
                        s_v['category'] = volume.category
                        s_v['volume_type'] = volume.volume_type
                        s_v['volume_path'] = volume.volume_path
                        s_v['volume_name'] = volume.volume_name
                        data['service_volume_map_list'].append(s_v)

                data['service_env_map_list'] = list()
                data['service_connect_info_map_list'] = list()
                if service_env_map.get(service.service_id):
                    for env_change in service_env_map.get(service.service_id):
                        if env_change.container_port == 0:
                            e_c = dict()
                            e_c['name'] = env_change.name
                            e_c['attr_name'] = env_change.attr_name
                            e_c['attr_value'] = env_change.attr_value
                            e_c['is_change'] = env_change.is_change
                            if env_change.scope == "outer":
                                e_c['container_port'] = env_change.container_port
                                data['service_connect_info_map_list'].append(
                                    e_c)
                            else:
                                data['service_env_map_list'].append(e_c)

                data['service_related_plugin_config'] = list()
                # plugins_attr_list = share_repo.get_plugin_config_var_by_service_ids(service_ids=service_ids)
                plugins_relation_list = share_repo.get_plugins_relation_by_service_ids(
                    service_ids=[service.service_id])
                for spr in plugins_relation_list:
                    service_plugin_config_var = service_plugin_config_repo.get_service_plugin_config_var(
                        spr.service_id, spr.plugin_id, spr.build_version)
                    plugin_data = spr.to_dict()
                    plugin_data["attr"] = [
                        var.to_dict() for var in service_plugin_config_var
                    ]
                    data['service_related_plugin_config'].append(plugin_data)

                all_data_map[service.service_id] = data

            all_data = list()
            for service_id in all_data_map:
                service = all_data_map[service_id]
                service['dep_service_map_list'] = list()
                if dep_service_map.get(service['service_id']):
                    for dep in dep_service_map.get(service['service_id']):
                        d = dict()
                        if all_data_map.get(dep.service_id):
                            # 通过service_key和service_id来判断依赖关系
                            d['dep_service_key'] = all_data_map[
                                dep.service_id]["service_share_uuid"]
                            service['dep_service_map_list'].append(d)

                service["mnt_relation_list"] = list()

                if dep_mnt_map.get(service_id):
                    for dep_mnt in dep_mnt_map.get(service_id):
                        if not all_data_map.get(dep_mnt.dep_service_id):
                            continue
                        service["mnt_relation_list"].append({
                            "service_share_uuid":
                            all_data_map[dep_mnt.dep_service_id]
                            ["service_share_uuid"],
                            "mnt_name":
                            dep_mnt.mnt_name,
                            "mnt_dir":
                            dep_mnt.mnt_dir
                        })
                all_data.append(service)
            return all_data
        else:
            return []
Example #11
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', 'k8s_service_name')

        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)
        if volume_info:
            volume_list = []
            for volume in volume_info:
                volume_info = model_to_dict(volume)
                if volume.volume_type == "config-file":
                    config_file = volume_repo.get_service_config_file(
                        volume.ID)
                    if config_file:
                        volume_info.update(
                            {"file_content": config_file.file_content})
                volume_list.append(volume_info)
            data["volumes_info"] = volume_list

        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)
        # 数据中心创建
        app_id = service_group_relation_repo.get_group_id_by_service(service)
        region_app_id = region_app_repo.get_region_app_id(
            service.service_region, app_id)
        data["app_id"] = region_app_id
        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