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()
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)
def create_service_volume(self, tenant, service, dep_vol): """ raise ErrInvalidVolume raise ErrDepVolumeNotFound """ tenant_service_volumes = volume_service.get_service_volumes( tenant, service) local_path = [l_path.volume_path for l_path in tenant_service_volumes] code, msg = volume_service.check_volume_path(service, dep_vol["path"], local_path=local_path) if code != 200: logger.debug("Service id: {0}; ingore mnt; msg: {1}".format( service.service_id, msg)) raise ErrInvalidVolume(msg) dep_volume = volume_repo.get_service_volume_by_name( dep_vol["service_id"], dep_vol["volume_name"]) if not dep_volume: raise ErrDepVolumeNotFound(dep_vol["service_id"], dep_vol["volume_name"]) source_path = dep_vol['path'].strip() return mnt_repo.add_service_mnt_relation(tenant.tenant_id, service.service_id, dep_volume.service_id, dep_volume.volume_name, source_path)
def get_service_mnt_details(self, tenant, service, volume_types, page=1, page_size=20): all_mnt_relations = mnt_repo.get_service_mnts_filter_volume_type(tenant.tenant_id, service.service_id, volume_types) total = len(all_mnt_relations) mnt_paginator = JuncheePaginator(all_mnt_relations, int(page_size)) mnt_relations = mnt_paginator.page(page) mounted_dependencies = [] if mnt_relations: for mount in mnt_relations: dep_service = service_repo.get_service_by_service_id(mount.dep_service_id) if dep_service: gs_rel = group_service_relation_repo.get_group_by_service_id(dep_service.service_id) group = None if gs_rel: group = group_repo.get_group_by_pk(tenant.tenant_id, service.service_region, gs_rel.group_id) dep_volume = volume_repo.get_service_volume_by_name(dep_service.service_id, mount.mnt_name) if dep_volume: mounted_dependencies.append({ "local_vol_path": mount.mnt_dir, "dep_vol_name": dep_volume.volume_name, "dep_vol_path": dep_volume.volume_path, "dep_vol_type": dep_volume.volume_type, "dep_app_name": dep_service.service_cname, "dep_app_group": group.group_name if group else '未分组', "dep_vol_id": dep_volume.ID, "dep_group_id": group.ID if group else -1, "dep_app_alias": dep_service.service_alias }) return mounted_dependencies, total
def get_volume_dependent(self, tenant, service): mnts = mnt_repo.get_by_dep_service_id(tenant.tenant_id, service.service_id) if not mnts: return None service_ids = [mnt.service_id for mnt in mnts] services = service_repo.get_services_by_service_ids(service_ids) # to dict id_to_services = {} for svc in services: if not id_to_services.get(svc.service_id, None): id_to_services[svc.service_id] = [svc] continue id_to_services[svc.service_id].append(svc) result = [] for mnt in mnts: # get volume vol = volume_repo.get_service_volume_by_name(service.service_id, mnt.mnt_name) # services that depend on this volume services_dep_vol = id_to_services[mnt.service_id] for svc in services_dep_vol: result.append({ "volume_name": vol.volume_name, "service_name": svc.service_cname, "service_alias": svc.service_alias, }) return result
def create_service_volume(self, tenant, service, dep_vol): """ raise ErrInvalidVolume raise ErrDepVolumeNotFound """ tenant_service_volumes = volume_service.get_service_volumes( tenant, service) local_path = [ l_path["volume_path"] for l_path in tenant_service_volumes ] volume_service.check_volume_path(service, dep_vol["path"], local_path=local_path) dep_volume = volume_repo.get_service_volume_by_name( dep_vol["service_id"], dep_vol["volume_name"]) if not dep_volume: raise ErrDepVolumeNotFound(dep_vol["service_id"], dep_vol["volume_name"]) source_path = dep_vol['path'].strip() return mnt_repo.add_service_mnt_relation(tenant.tenant_id, service.service_id, dep_volume.service_id, dep_volume.volume_name, source_path)
def check_volume_name(self, service, volume_name): r = re.compile(u'^[a-zA-Z0-9_]+$') if not r.match(volume_name): if service.service_source != AppConstants.MARKET: raise ServiceHandleException(msg="volume name illegal", msg_show="持久化名称只支持数字字母下划线") volume_name = service.service_cname + make_uuid()[-3:] volume = volume_repo.get_service_volume_by_name(service.service_id, volume_name) if volume: raise ServiceHandleException(msg="volume name already exists", msg_show="持久化名称[{0}]已存在".format(volume_name)) return volume_name
def check_volume_name(self, service, volume_name): r = re.compile(u'^[a-zA-Z0-9_]+$') if not r.match(volume_name): if service.service_source != AppConstants.MARKET: return 400, u"持久化名称只支持数字字母下划线", volume_name else: volume_name = service.service_cname + make_uuid()[-3:] volume = volume_repo.get_service_volume_by_name(service.service_id, volume_name) if volume: return 412, u"持久化名称{0}已存在".format(volume_name), volume_name else: return 200, u"success", volume_name
def check_volume_name(self, service_id, volume_name): zhPattern = re.compile(u'[\u4e00-\u9fa5]+') match = zhPattern.search(volume_name.decode('utf-8')) if match: return 400, u"持久化名称不能包含中文" volume = volume_repo.get_service_volume_by_name( service_id, volume_name) if volume: return 412, u"持久化名称{0}已存在".format(volume_name) else: return 200, u"success"
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)