예제 #1
0
 def create_or_update(volume: TenantServiceVolume):
     try:
         old_volume = TenantServiceVolume.objects.get(
             service_id=volume.service_id, volume_name=volume.volume_name)
         volume.ID = old_volume.ID
     except TenantServiceVolume.DoesNotExist:
         pass
     volume.save()
 def __save_volume(self, service, tenant_service_volumes):
     volume_list = []
     for volume in tenant_service_volumes:
         volume.pop("ID")
         new_volume = TenantServiceVolume(**volume)
         new_volume.service_id = service.service_id
         volume_list.append(new_volume)
     if volume_list:
         TenantServiceVolume.objects.bulk_create(volume_list)
 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 _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=[],
     )
예제 #5
0
    def _update_volumes(self, volumes):
        old_volumes = {volume.volume_name: volume for volume in self.volumes}
        for volume in volumes.get("add"):
            volume["service_id"] = self.component.service_id
            host_path = "/grdata/tenant/{0}/service/{1}{2}".format(
                self.component.tenant_id, self.component.service_id,
                volume["volume_path"])
            volume["host_path"] = host_path
            file_content = volume.get("file_content")
            if file_content:
                self.config_files.append(
                    TenantServiceConfigurationFile(
                        service_id=self.component.component_id,
                        volume_name=volume["volume_name"],
                        file_content=file_content,
                    ))
            if "file_content" in volume.keys():
                volume.pop("file_content")
            self.volumes.append(TenantServiceVolume(**volume))

        old_config_files = {
            config_file.volume_name: config_file
            for config_file in self.config_files
        }
        for volume in volumes.get("upd"):
            old_volume = old_volumes.get(volume["volume_name"])
            old_volume.mode = volume.get("mode")
            old_config_file = old_config_files.get(volume.get("volume_name"))
            if not old_config_file:
                continue
            old_config_file.file_content = volume.get("file_content")

        self.config_files = old_config_files.values()
        self.update_action_type(ActionType.UPDATE.value)
예제 #6
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
예제 #7
0
    def __save_volume(self, service, tenant_service_volumes,
                      service_config_file):
        if not service_config_file:
            volume_list = []
            for volume in tenant_service_volumes:
                volume.pop("ID")
                new_volume = TenantServiceVolume(**volume)
                new_volume.service_id = service.service_id
                volume_list.append(new_volume)
            if volume_list:
                TenantServiceVolume.objects.bulk_create(volume_list)
        else:
            for volume in tenant_service_volumes:
                volume_list = []
                config_list = []
                for config_file in service_config_file:
                    if config_file["volume_id"] == volume["ID"]:
                        config_file.pop("ID")
                        new_config_file = TenantServiceConfigurationFile(
                            **config_file)
                        new_config_file.service_id = service.service_id
                        config_list.append(new_config_file)
                volume.pop("ID")
                new_volume = TenantServiceVolume(**volume)
                new_volume.service_id = service.service_id
                volume_list.append(new_volume)
                logger.debug('----------------->{0}'.format(
                    new_volume.to_dict()))
                if volume_list:
                    volume_js = TenantServiceVolume.objects.bulk_create(
                        volume_list)
                    volume_j = volume_js[0]
                    logger.debug('--------------555-------------->{0}'.format(
                        volume_j.to_dict()))
                    for config in config_list:
                        volume_obj = volume_repo.get_service_volume_by_name(
                            service.service_id, volume_j.volume_name)
                        logger.debug(
                            '--------------2222121212-------------->{0}'.
                            format(volume_obj.to_dict()))
                        config.volume_id = volume_obj.ID
                        logger.debug(
                            '------------666---------------->{0}'.format(
                                config.volume_id))

                    TenantServiceConfigurationFile.objects.bulk_create(
                        config_list)
예제 #8
0
    def create_service_volume(self,
                              tenant,
                              service,
                              volume_path,
                              volume_type,
                              volume_name,
                              settings=None,
                              mode=None):
        volume_name = volume_name.strip()
        volume_path = volume_path.strip()
        volume_name = self.check_volume_name(service, volume_name)

        self.check_volume_path(service, volume_path)
        host_path = "/grdata/tenant/{0}/service/{1}{2}".format(
            tenant.tenant_id, service.service_id, volume_path)

        volume_data = {
            "service_id": service.service_id,
            "category": service.category,
            "host_path": host_path,
            "volume_type": volume_type,
            "volume_path": volume_path,
            "volume_name": volume_name,
            "mode": mode,
        }

        if settings:
            self.check_volume_options(tenant, service, volume_type, settings)
            settings = self.setting_volume_properties(tenant, service,
                                                      volume_type, settings)

            volume_data['volume_capacity'] = settings['volume_capacity']
            volume_data['volume_provider_name'] = settings[
                'volume_provider_name']
            volume_data['access_mode'] = settings['access_mode']
            volume_data['share_policy'] = settings['share_policy']
            volume_data['backup_policy'] = settings['backup_policy']
            volume_data['reclaim_policy'] = settings['reclaim_policy']
            volume_data['allow_expansion'] = settings['allow_expansion']
        return TenantServiceVolume(**volume_data)
예제 #9
0
 def create_tenant_service_volume(self, **kwargs):
     tenant_service_volume = TenantServiceVolume(**kwargs)
     tenant_service_volume.save()
     return tenant_service_volume
예제 #10
0
 def init(self):
     self.sources = [
         Tenants(),
         TenantRegionInfo(),
         TenantRegionResource(),
         ServiceInfo(),
         TenantServiceInfo(),
         TenantServiceInfoDelete(),
         TenantServiceLog(),
         TenantServiceRelation(),
         TenantServiceEnv(),
         TenantServiceAuth(),
         TenantServiceExtendMethod(),
         ServiceDomain(),
         ServiceDomainCertificate(),
         PermRelService(),
         PermRelTenant(),
         PhoneCode(),
         TenantServiceL7Info(),
         TenantServiceEnvVar(),
         TenantServicesPort(),
         TenantServiceMountRelation(),
         TenantServiceVolume(),
         TenantServiceConfigurationFile(),
         ServiceGroup(),
         ServiceGroupRelation(),
         ImageServiceRelation(),
         ComposeServiceRelation(),
         ServiceRule(),
         ServiceRuleHistory(),
         ServiceCreateStep(),
         ServiceProbe(),
         ConsoleConfig(),
         TenantEnterprise(),
         TenantEnterpriseToken(),
         TenantServiceGroup(),
         ServiceTcpDomain(),
         ThirdPartyServiceEndpoints(),
         ServiceWebhooks(),
         GatewayCustomConfiguration(),
         ConsoleSysConfig(),
         RainbondCenterApp(),
         RainbondCenterAppInherit(),
         RainbondCenterPlugin(),
         ServiceShareRecord(),
         EnterpriseUserPerm(),
         TenantUserRole(),
         TenantUserPermission(),
         TenantUserRolePermission(),
         PermGroup(),
         ServiceRelPerms(),
         AppExportRecord(),
         UserMessage(),
         AppImportRecord(),
         GroupAppBackupRecord(),
         GroupAppMigrateRecord(),
         GroupAppBackupImportRecord(),
         Applicants(),
         DeployRelation(),
         ServiceBuildSource(),
         TenantServiceBackup(),
         AppUpgradeRecord(),
         ServiceUpgradeRecord(),
         RegionConfig(),
         CloundBangImages(),
         Announcement(),
     ]