def create_config_group(self, plugin_id, build_version, config_group): """创建配置组信息""" pbvs = PluginBuildVersion.objects.filter(plugin_id=plugin_id, build_version=build_version) if not pbvs: logger.error("plugin id {0} and build version {1} is not found !".format(plugin_id, build_version)) raise Exception("version not found ! ") plugin_config_meta_list = [] config_items_list = [] if config_group: for config in config_group: options = config["options"] plugin_config_meta = PluginConfigGroup( plugin_id=plugin_id, build_version=build_version, config_name=config["config_name"], service_meta_type=config["service_meta_type"], injection=config["injection"]) plugin_config_meta_list.append(plugin_config_meta) for option in options: config_item = PluginConfigItems( plugin_id=plugin_id, build_version=build_version, service_meta_type=config["service_meta_type"], attr_name=option["attr_name"], attr_alt_value=option["attr_alt_value"], attr_type=option.get("attr_type", "string"), attr_default_value=option.get("attr_default_value", None), is_change=option.get("is_change", False), attr_info=option.get("attr_info", "")) config_items_list.append(config_item) self.bulk_create_plugin_config_group(plugin_config_meta_list) self.bulk_create_plugin_config_items(config_items_list)
def create_config_groups(self, plugin_id, build_version, config_group): plugin_config_meta_list = [] config_items_list = [] if config_group: for config in config_group: options = config["options"] plugin_config_meta = PluginConfigGroup( plugin_id=plugin_id, build_version=build_version, config_name=config["config_name"], service_meta_type=config["service_meta_type"], injection=config["injection"]) plugin_config_meta_list.append(plugin_config_meta) for option in options: config_item = PluginConfigItems( plugin_id=plugin_id, build_version=build_version, service_meta_type=config["service_meta_type"], attr_name=option.get("attr_name", ""), attr_alt_value=option.get("attr_alt_value", ""), attr_type=option.get("attr_type", "string"), attr_default_value=option.get("attr_default_value", None), is_change=option.get("is_change", False), attr_info=option.get("attr_info", ""), protocol=option.get("protocol", "")) config_items_list.append(config_item) config_group_repo.bulk_create_plugin_config_group( plugin_config_meta_list) config_item_repo.bulk_create_items(config_items_list)
def _create_config_groups(plugin_id, build_version, config_groups_tmpl): config_groups = [] config_items = [] for config in config_groups_tmpl: options = config["options"] plugin_config_meta = PluginConfigGroup( plugin_id=plugin_id, build_version=build_version.build_version, config_name=config["config_name"], service_meta_type=config["service_meta_type"], injection=config["injection"]) config_groups.append(plugin_config_meta) for option in options: config_item = PluginConfigItems( plugin_id=plugin_id, build_version=build_version.build_version, service_meta_type=config["service_meta_type"], attr_name=option.get("attr_name", ""), attr_alt_value=option.get("attr_alt_value", ""), attr_type=option.get("attr_type", "string"), attr_default_value=option.get("attr_default_value", None), is_change=option.get("is_change", False), attr_info=option.get("attr_info", ""), protocol=option.get("protocol", "")) config_items.append(config_item) return config_groups, config_items
def __copy_config_items(self, plugin_id, old_version, new_version): config_items = self.get_config_items_by_id_and_version(plugin_id, old_version) config_items_copy = [] for item in config_items: item_dict = model_to_dict(item) # 剔除主键 item_dict.pop("ID") item_dict["build_version"] = new_version config_items_copy.append(PluginConfigItems(**item_dict)) self.bulk_create_plugin_config_items(config_items_copy)
def create_config_items(self, plugin_id, build_version, service_meta_type, *options): config_items_list = [] for option in options: config_item = PluginConfigItems( plugin_id=plugin_id, build_version=build_version, service_meta_type=service_meta_type, attr_name=option["attr_name"], attr_alt_value=option["attr_alt_value"], attr_type=option.get("attr_type", "string"), attr_default_value=option.get("attr_default_value", None), is_change=option.get("is_change", False), attr_info=option.get("attr_info", "")) config_items_list.append(config_item) self.bulk_create_plugin_config_items(config_items_list)
def install_plugin(self, user, tenant, region_name, market_plugin): plugin_template = json.loads(market_plugin.plugin_template) share_plugin_info = plugin_template.get("share_plugin_info") sid = transaction.savepoint() try: image = None image_tag = None if plugin_template["share_image"]: image_and_tag = plugin_template["share_image"].rsplit(":", 1) if len(image_and_tag) > 1: image = image_and_tag[0] image_tag = image_and_tag[1] else: image = image_and_tag[0] image_tag = "latest" status, msg, plugin_base_info = plugin_service.create_tenant_plugin( tenant, user.user_id, region_name, share_plugin_info.get("desc"), plugin_template["plugin_name"], plugin_template["category"], "image", image, plugin_template["code_repo"]) if status != 200: return status, msg plugin_base_info.origin = 'local_market' if market_plugin.source == 'local' else market_plugin.source plugin_base_info.origin_share_id = share_plugin_info.get( "plugin_key") plugin_base_info.save() build_version = plugin_template.get('build_version') min_memory = build_version.get('min_memory') plugin_build_version = plugin_version_service.create_build_version( region_name, plugin_base_info.plugin_id, tenant.tenant_id, user.user_id, "", "unbuild", min_memory, image_tag=image_tag, code_version="") config_groups, config_items = [], [] share_config_groups = share_plugin_info.get('config_groups', []) for group in share_config_groups: plugin_config_group = PluginConfigGroup( plugin_id=plugin_base_info.plugin_id, build_version=plugin_build_version.build_version, config_name=group.get("config_name"), service_meta_type=group.get("service_meta_type"), injection=group.get("injection")) config_groups.append(plugin_config_group) share_config_items = group.get('config_items', []) for item in share_config_items: plugin_config_item = PluginConfigItems( plugin_id=plugin_base_info.plugin_id, build_version=plugin_build_version.build_version, service_meta_type=item.get("service_meta_type"), attr_name=item.get("attr_name"), attr_alt_value=item.get("attr_alt_value"), attr_type=item.get("attr_type", "string"), attr_default_value=item.get("attr_default_value", None), is_change=item.get("is_change", False), attr_info=item.get("attr_info", ""), protocol=item.get("protocol", "")) config_items.append(plugin_config_item) PluginConfigGroup.objects.bulk_create(config_groups) PluginConfigItems.objects.bulk_create(config_items) event_id = make_uuid() plugin_build_version.event_id = event_id plugin_build_version.plugin_version_status = "fixed" plugin_service.create_region_plugin(region_name, tenant, plugin_base_info, image_tag=image_tag) ret = plugin_service.build_plugin( region_name, plugin_base_info, plugin_build_version, user, tenant, event_id, share_plugin_info.get("plugin_image", None)) plugin_build_version.build_status = ret.get('bean').get('status') plugin_build_version.save() return 200, '安装成功' except Exception as e: logger.exception(e) if sid: transaction.savepoint_rollback(sid) return 500, '插件安装失败'
def add_default_plugin(self, user, tenant, region, plugin_type="perf_analyze_plugin"): plugin_base_info = None try: all_default_config = None module_dir = os.path.dirname(__file__) file_path = os.path.join(module_dir, 'default_config.json') with open(file_path) as f: all_default_config = json.load(f) if not all_default_config: raise Exception("no config was found") needed_plugin_config = all_default_config[plugin_type] code, msg, plugin_base_info = self.create_tenant_plugin( tenant, user.user_id, region, needed_plugin_config["desc"], needed_plugin_config["plugin_alias"], needed_plugin_config["category"], needed_plugin_config["build_source"], needed_plugin_config["image"], needed_plugin_config["code_repo"]) plugin_base_info.origin = "local_market" plugin_base_info.origin_share_id = plugin_type plugin_base_info.save() plugin_build_version = plugin_version_service.create_build_version( region, plugin_base_info.plugin_id, tenant.tenant_id, user.user_id, "", "unbuild", 64) plugin_config_meta_list = [] config_items_list = [] config_group = needed_plugin_config["config_group"] if config_group: for config in config_group: options = config["options"] plugin_config_meta = PluginConfigGroup( plugin_id=plugin_build_version.plugin_id, build_version=plugin_build_version.build_version, config_name=config["config_name"], service_meta_type=config["service_meta_type"], injection=config["injection"]) plugin_config_meta_list.append(plugin_config_meta) for option in options: config_item = PluginConfigItems( plugin_id=plugin_build_version.plugin_id, build_version=plugin_build_version.build_version, service_meta_type=config["service_meta_type"], attr_name=option["attr_name"], attr_alt_value=option["attr_alt_value"], attr_type=option.get("attr_type", "string"), attr_default_value=option.get( "attr_default_value", None), is_change=option.get("is_change", False), attr_info=option.get("attr_info", ""), protocol=option.get("protocol", "")) config_items_list.append(config_item) config_group_repo.bulk_create_plugin_config_group( plugin_config_meta_list) config_item_repo.bulk_create_items(config_items_list) event_id = make_uuid() plugin_build_version.event_id = event_id plugin_build_version.plugin_version_status = "fixed" self.create_region_plugin(region, tenant, plugin_base_info) self.build_plugin(region, plugin_base_info, plugin_build_version, user, tenant, event_id) plugin_build_version.build_status = "build_success" plugin_build_version.save() except Exception as e: logger.exception(e) if plugin_base_info: self.delete_plugin(region, tenant, plugin_base_info.plugin_id) raise e
def add_default_plugin(self, user, tenant, region, plugin_type="perf_analyze_plugin"): plugin_base_info = None try: if not self.all_default_config: raise Exception("no config was found") needed_plugin_config = self.all_default_config[plugin_type] image = needed_plugin_config.get("image", "") build_source = needed_plugin_config.get("build_source", "") image_tag = "latest" if image and build_source and build_source == "image": ref = reference.Reference.parse(image) if ref["tag"]: image_tag = ref["tag"] if "goodrain.me" in image: _, name = ref.split_hostname() image = settings.IMAGE_REPO + "/" + name else: image = image.split(":")[0] plugin_params = { "tenant_id": tenant.tenant_id, "region": region, "create_user": user.user_id, "desc": needed_plugin_config["desc"], "plugin_alias": needed_plugin_config["plugin_alias"], "category": needed_plugin_config["category"], "build_source": build_source, "image": image, "code_repo": needed_plugin_config["code_repo"], "username": "", "password": "" } code, msg, plugin_base_info = self.create_tenant_plugin( plugin_params) plugin_base_info.origin = "local_market" plugin_base_info.origin_share_id = plugin_type plugin_base_info.save() plugin_build_version = plugin_version_service.create_build_version( region, plugin_base_info.plugin_id, tenant.tenant_id, user.user_id, "", "unbuild", 64, image_tag=image_tag) plugin_config_meta_list = [] config_items_list = [] config_group = needed_plugin_config["config_group"] if config_group: for config in config_group: options = config["options"] plugin_config_meta = PluginConfigGroup( plugin_id=plugin_build_version.plugin_id, build_version=plugin_build_version.build_version, config_name=config["config_name"], service_meta_type=config["service_meta_type"], injection=config["injection"]) plugin_config_meta_list.append(plugin_config_meta) for option in options: config_item = PluginConfigItems( plugin_id=plugin_build_version.plugin_id, build_version=plugin_build_version.build_version, service_meta_type=config["service_meta_type"], attr_name=option["attr_name"], attr_alt_value=option["attr_alt_value"], attr_type=option.get("attr_type", "string"), attr_default_value=option.get( "attr_default_value", None), is_change=option.get("is_change", False), attr_info=option.get("attr_info", ""), protocol=option.get("protocol", "")) config_items_list.append(config_item) config_group_repo.bulk_create_plugin_config_group( plugin_config_meta_list) config_item_repo.bulk_create_items(config_items_list) event_id = make_uuid() plugin_build_version.event_id = event_id plugin_build_version.plugin_version_status = "fixed" self.create_region_plugin(region, tenant, plugin_base_info) self.build_plugin(region, plugin_base_info, plugin_build_version, user, tenant, event_id) plugin_build_version.build_status = "build_success" plugin_build_version.save() except Exception as e: logger.exception(e) if plugin_base_info: self.delete_plugin(region, tenant, plugin_base_info.plugin_id) raise e