From 23bc224b09377214cba030ed090cb7db3f65b5c9 Mon Sep 17 00:00:00 2001 From: rabbitali Date: Wed, 13 Sep 2023 14:45:56 +0800 Subject: [PATCH] add file sync func (cherry picked from commit c252a1e2f959a5bf9e8404664fc969ea804b00f3) --- 0002-add-file-sync-func.patch | 155 ++++++++++++++++++++++++++++++++++ aops-ceres.spec | 6 +- 2 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 0002-add-file-sync-func.patch diff --git a/0002-add-file-sync-func.patch b/0002-add-file-sync-func.patch new file mode 100644 index 0000000..f667757 --- /dev/null +++ b/0002-add-file-sync-func.patch @@ -0,0 +1,155 @@ +From b0f71927a3bdb3096757ca8cdedb233d2b886a4d Mon Sep 17 00:00:00 2001 +From: smjiao +Date: Thu, 7 Sep 2023 16:28:49 +0800 +Subject: [PATCH] add file sync func +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +--- + ceres/__main__.py | 6 ++++ + ceres/function/command.py | 11 ++++++++ + ceres/function/schema.py | 12 ++++++++ + ceres/manages/sync_manage.py | 55 ++++++++++++++++++++++++++++++++++++ + 4 files changed, 84 insertions(+) + create mode 100644 ceres/manages/sync_manage.py + +diff --git a/ceres/__main__.py b/ceres/__main__.py +index d1bbee3..a93ec49 100644 +--- a/ceres/__main__.py ++++ b/ceres/__main__.py +@@ -17,6 +17,7 @@ from ceres.function.command import ( + cve_command_manage, + plugin_command_manage, + register_on_manager, ++ sync_conf_manage, + ) + from ceres.function.log import LOGGER + +@@ -55,6 +56,11 @@ def main(): + cve_group.add_argument("--rollback", type=str) + subparsers_cve.set_defaults(function=cve_command_manage) + ++ subparsers_sync = subparsers.add_parser("sync", help='sync conf file') ++ sync_group = subparsers_sync.add_mutually_exclusive_group(required=True) ++ sync_group.add_argument("--conf", type=str) ++ subparsers_sync.set_defaults(function=sync_conf_manage) ++ + args = parser.parse_args() + try: + args.function(args) +diff --git a/ceres/function/command.py b/ceres/function/command.py +index e4d367a..7324f23 100644 +--- a/ceres/function/command.py ++++ b/ceres/function/command.py +@@ -25,11 +25,13 @@ from ceres.function.schema import ( + HOST_INFO_SCHEMA, + REPO_SET_SCHEMA, + STRING_ARRAY, ++ CONF_SYNC_SCHEMA, + ) + from ceres.function.status import SUCCESS, StatusCode + from ceres.function.util import convert_string_to_json, get_dict_from_file, plugin_status_judge, validate_data + from ceres.manages import plugin_manage + from ceres.manages.collect_manage import Collect ++from ceres.manages.sync_manage import SyncManage + from ceres.manages.vulnerability_manage import VulnerabilityManage + + +@@ -191,3 +193,12 @@ def cve_command_manage(args): + else: + print("Please check the input parameters!") + exit(1) ++ ++ ++def sync_conf_manage(args): ++ if args.conf: ++ config = convert_string_to_json(args.conf) ++ if not validate_data(config, CONF_SYNC_SCHEMA): ++ exit(1) ++ res = StatusCode.make_response_body(SyncManage.sync_contents_to_conf(config)) ++ print(json.dumps(res)) +diff --git a/ceres/function/schema.py b/ceres/function/schema.py +index ada35c3..794152d 100644 +--- a/ceres/function/schema.py ++++ b/ceres/function/schema.py +@@ -113,3 +113,15 @@ CVE_ROLLBACK_SCHEMA = { + } + }, + } ++ ++CONF_SYNC_SCHEMA = { ++ "type": "object", ++ "required": [ ++ "file_path", ++ "content" ++ ], ++ "properties": { ++ "file_path": {"type": "string", "minLength": 1}, ++ "content": {"type": "string", "minLength": 1} ++ } ++} +diff --git a/ceres/manages/sync_manage.py b/ceres/manages/sync_manage.py +new file mode 100644 +index 0000000..9be2a47 +--- /dev/null ++++ b/ceres/manages/sync_manage.py +@@ -0,0 +1,55 @@ ++#!/usr/bin/python3 ++# ****************************************************************************** ++# Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved. ++# licensed under the Mulan PSL v2. ++# You can use this software according to the terms and conditions of the Mulan PSL v2. ++# You may obtain a copy of Mulan PSL v2 at: ++# http://license.coscl.org.cn/MulanPSL2 ++# THIS SOFTWARE IS PROVIDED ON AN 'AS IS' BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR ++# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR ++# PURPOSE. ++# See the Mulan PSL v2 for more details. ++# ******************************************************************************/ ++# Author: Lay ++# Description: default ++# Date: 2023/6/14 16:31 ++import os ++from ceres.function.log import LOGGER ++from ceres.function.status import ( ++ FILE_NOT_FOUND, ++ UNKNOWN_ERROR, ++ SUCCESS ++) ++ ++ ++class SyncManage: ++ """ ++ Sync managed conf to the host ++ """ ++ ++ @staticmethod ++ def sync_contents_to_conf(config: dict) -> str: ++ """ ++ Write conf into file ++ Args: ++ config(dict): filepath and content for file sync, only. eg: ++ { ++ "file_path" = "/tmp/test" ++ "content" = "contents for this file" ++ } ++ Returns: ++ str: status code ++ """ ++ file_path = config.get('file_path') ++ ++ contents = config.get('content') ++ lines = contents.split('\n') ++ try: ++ with open(file_path, "w", encoding="utf-8") as file: ++ for line in lines: ++ file.write(line + "\n") ++ except Exception as e: ++ LOGGER.error("write sync content to conf failed, with msg{}".format(e)) ++ return UNKNOWN_ERROR ++ ++ return SUCCESS +-- +Gitee + diff --git a/aops-ceres.spec b/aops-ceres.spec index 21f131c..9a5e344 100644 --- a/aops-ceres.spec +++ b/aops-ceres.spec @@ -1,11 +1,12 @@ Name: aops-ceres Version: v1.3.1 -Release: 2 +Release: 3 Summary: An agent which needs to be adopted in client, it managers some plugins, such as gala-gopher(kpi collection), fluentd(log collection) and so on. License: MulanPSL2 URL: https://gitee.com/openeuler/%{name} Source0: %{name}-%{version}.tar.gz Patch0001: 0001-update-func-named-set-hotpatch-status-by-dnf-plugin.patch +Patch0002: 0002-add-file-sync-func.patch BuildRequires: python3-setuptools Requires: python3-requests python3-jsonschema python3-libconf @@ -40,6 +41,9 @@ An agent which needs to be adopted in client, it managers some plugins, such as %changelog +* Wed Sep 13 2023 wenxin - v1.3.1-3 +- add file sync func + * Wed Sep 13 2023 wenxin - v1.3.1-2 - update func named set_hotpatch_status_by_dnf_plugin