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