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)
def _template_to_volumes(self, component, volumes): if not volumes: return [], [] volumes2 = [] config_files = [] for volume in volumes: try: if volume["volume_type"] == "config-file" and volume["file_content"] != "": settings = None config_file = TenantServiceConfigurationFile( service_id=component.component_id, volume_name=volume["volume_name"], file_content=volume["file_content"]) config_files.append(config_file) else: settings = volume_service.get_best_suitable_volume_settings(self.tenant, component, 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"] if volume["volume_type"] == "share-file": volume["volume_capacity"] = 0 else: settings["volume_capacity"] = volume.get("volume_capacity", 0) volumes2.append( volume_service.create_service_volume(self.tenant, component, volume["volume_path"], volume["volume_type"], volume["volume_name"], settings)) except ErrVolumePath: logger.warning("Volume {0} Path {1} error".format(volume["volume_name"], volume["volume_path"])) return volumes2, config_files
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=[], )
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)
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)
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
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(), ]