def _template_to_envs(self, component, inner_envs, outer_envs): if not inner_envs and not outer_envs: return [] envs = [] for env in inner_envs: if not env.get("attr_name"): continue envs.append( TenantServiceEnvVar( tenant_id=component.tenant_id, service_id=component.service_id, container_port=0, name=env.get("name"), attr_name=env.get("attr_name"), attr_value=env.get("attr_value"), is_change=env.get("is_change", True), scope="inner")) for env in outer_envs: if not env.get("attr_name"): continue container_port = env.get("container_port", 0) if env.get("attr_value") == "**None**": env["attr_value"] = make_uuid()[:8] envs.append( TenantServiceEnvVar( tenant_id=component.tenant_id, service_id=component.service_id, container_port=container_port, name=env.get("name"), attr_name=env.get("attr_name"), attr_value=env.get("attr_value"), is_change=env.get("is_change", True), scope="outer")) # port envs return envs
def __save_env(self, tenant, service, tenant_service_env_vars): env_list = [] for env in tenant_service_env_vars: env.pop("ID") new_env = TenantServiceEnvVar(**env) new_env.tenant_id = tenant.tenant_id new_env.service_id = service.service_id env_list.append(new_env) if env_list: TenantServiceEnvVar.objects.bulk_create(env_list)
def create_or_update(env: TenantServiceEnvVar): try: old_env = TenantServiceEnvVar.objects.get( tenant_id=env.tenant_id, service_id=env.service_id, attr_name=env.attr_name) env.ID = old_env.ID env.save() except TenantServiceEnvVar.DoesNotExist: env.save()
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_envs(self, envs, scope): if envs is None: return # create envs add = envs.get("add", []) for env in add: container_port = env.get("container_port", 0) value = env.get("attr_value", "") name = env.get("name", "") attr_name = env.get("attr_name", "") if not attr_name: continue if container_port == 0 and value == "**None**": value = self.component.service_id[:8] try: env_var_service.check_env(self.component, attr_name, value) except (EnvAlreadyExist, InvalidEnvName) as e: logger.warning( "failed to create env: {}; will ignore this env".format(e)) continue self.envs.append( TenantServiceEnvVar( tenant_id=self.component.tenant_id, service_id=self.component.service_id, container_port=container_port, name=name, attr_name=attr_name, attr_value=value, scope=scope, ))
def envs(self, service_env_vars): env_var_repo.delete_service_env(self.tenant.tenant_id, self.service.service_id) if service_env_vars: envs = [] for item in service_env_vars: item.pop("ID") envs.append(TenantServiceEnvVar(**item)) env_var_repo.bulk_create(envs)
def _create_port_env(component: TenantServiceInfo, port: TenantServicesPort, name, attr_name, attr_value): return TenantServiceEnvVar( tenant_id=component.tenant_id, service_id=component.component_id, container_port=port.container_port, name=name, attr_name=attr_name, attr_value=attr_value, is_change=False, scope="outer", create_time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'), )
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 create_port_env(port: TenantServicesPort, name, attr_name_suffix, attr_value): return TenantServiceEnvVar( tenant_id=port.tenant_id, service_id=port.service_id, container_port=port.container_port, name=name, attr_name=port.port_alias + "_" + attr_name_suffix, attr_value=attr_value, is_change=False, scope="outer", create_time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'), )
def _check_service_env_scope(scope): try: return TenantServiceEnvVar.ScopeType(scope).value except ValueError: raise AbortRequest(msg="the value of scope is outer or inner")
def create_tenant_service_env_var(self, **kwargs): tenant_service_env_var = TenantServiceEnvVar(**kwargs) tenant_service_env_var.save() return tenant_service_env_var
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(), ]