From 0e2ad3cc141390e26d7a20bccf5ccc0660f7c172 Mon Sep 17 00:00:00 2001 From: smjiao Date: Thu, 7 Sep 2023 16:24:42 +0800 Subject: [PATCH] add file sync func MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zeus/conf/constant.py | 2 ++ zeus/config_manager/view.py | 45 ++++++++++++++++++++++++++++++++-- zeus/function/verify/config.py | 9 +++++++ zeus/url.py | 6 ++++- 4 files changed, 59 insertions(+), 3 deletions(-) diff --git a/zeus/conf/constant.py b/zeus/conf/constant.py index 2fbf1be..3175c65 100644 --- a/zeus/conf/constant.py +++ b/zeus/conf/constant.py @@ -33,6 +33,7 @@ CERES_CVE_REPO_SET = "aops-ceres apollo --set-repo '%s'" CERES_CVE_SCAN = "aops-ceres apollo --scan '%s'" CERES_CVE_FIX = "aops-ceres apollo --fix '%s'" CERES_CVE_ROLLBACK = "aops-ceres apollo --rollback '%s'" +CERES_SYNC_CONF = "aops-ceres sync --conf '%s'" # zeus route ADD_HOST = "/manage/host/add" @@ -54,6 +55,7 @@ DELETE_GROUP = "/manage/host/group/delete" GET_GROUP = "/manage/host/group/get" COLLECT_CONFIG = '/manage/config/collect' +SYNC_CONFIG = '/manage/config/sync' USER_LOGIN = "/manage/account/login" LOGOUT = "/manage/account/logout" diff --git a/zeus/config_manager/view.py b/zeus/config_manager/view.py index cd83e35..baeef7e 100644 --- a/zeus/config_manager/view.py +++ b/zeus/config_manager/view.py @@ -21,10 +21,11 @@ from typing import List, Dict from vulcanus.multi_thread_handler import MultiThreadHandler from vulcanus.restful.resp import state from vulcanus.restful.response import BaseResponse -from zeus.conf.constant import CERES_COLLECT_FILE +from zeus.conf import configuration +from zeus.conf.constant import CERES_COLLECT_FILE, CERES_SYNC_CONF from zeus.database.proxy.host import HostProxy from zeus.function.model import ClientConnectArgs -from zeus.function.verify.config import CollectConfigSchema +from zeus.function.verify.config import CollectConfigSchema, SyncConfigSchema from zeus.host_manager.ssh import execute_command_and_parse_its_result @@ -218,3 +219,43 @@ class CollectConfig(BaseResponse): return self.response( state.SUCCEED, None, self.generate_target_data_format(multi_thread.get_result(), host_id_with_config_file) ) + + +class SyncConfig(BaseResponse): + + @staticmethod + def sync_config_content(host_info: Dict, sync_config_info: Dict): + command = CERES_SYNC_CONF % json.dumps(sync_config_info) + status, content = execute_command_and_parse_its_result( + ClientConnectArgs(host_info.get("host_ip"), host_info.get("ssh_port"), + host_info.get("ssh_user"), host_info.get("pkey")), command) + return status + + @BaseResponse.handle(schema=SyncConfigSchema, token=False) + def put(self, **params): + + sync_config_info = dict() + sync_config_info['file_path'] = params.get('file_path') + sync_config_info['content'] = params.get('content') + + sync_result = { + "file_path": sync_config_info['file_path'], + "sync_result": False + } + + # Query host address from database + proxy = HostProxy(configuration) + if not proxy.connect(): + return self.response(code=state.DATABASE_CONNECT_ERROR, data={"resp": sync_result}) + + status, host_list = proxy.get_host_info( + {"username": "admin", "host_list": [params.get('host_id')]}, True) + if status != state.SUCCEED or len(host_list) == 1: + return self.response(code=status, data={"resp": sync_result}) + + host_info = host_list[0] + status = self.sync_config_content(host_info, sync_config_info) + if status == state.SUCCEED: + sync_result['sync_result'] = True + return self.response(code=state.SUCCEED, data={"resp": sync_result}) + return self.response(code=state.UNKNOWN_ERROR, data={"resp": sync_result}) diff --git a/zeus/function/verify/config.py b/zeus/function/verify/config.py index 230b65d..6e5bf64 100644 --- a/zeus/function/verify/config.py +++ b/zeus/function/verify/config.py @@ -36,3 +36,12 @@ class CollectConfigSchema(Schema): """ infos = fields.List(fields.Nested(ConfigSchema(), required=True), required=True, validate=lambda s: len(s) > 0) + + +class SyncConfigSchema(Schema): + """ + validators for SyncConfigSchema + """ + host_id = fields.Integer(required=True, validate=lambda s: s > 0) + file_path = fields.String(required=True, validate=lambda s: len(s) > 0) + content = fields.String(required=True, validate=lambda s: len(s) > 0) diff --git a/zeus/url.py b/zeus/url.py index e0cf1de..597dcc7 100644 --- a/zeus/url.py +++ b/zeus/url.py @@ -49,6 +49,7 @@ from zeus.conf.constant import ( REFRESH_TOKEN, UPDATE_HOST, USER_LOGIN, + SYNC_CONFIG, ) from zeus.config_manager import view as config_view from zeus.host_manager import view as host_view @@ -83,7 +84,10 @@ SPECIFIC_URLS = { (host_view.DeleteHostGroup, DELETE_GROUP), (host_view.GetHostGroup, GET_GROUP), ], - "CONFIG_URLS": [(config_view.CollectConfig, COLLECT_CONFIG)], + "CONFIG_URLS": [ + (config_view.CollectConfig, COLLECT_CONFIG), + (config_view.SyncConfig, SYNC_CONFIG) + ], 'AGENT_URLS': [ (agent_view.AgentPluginInfo, AGENT_PLUGIN_INFO), (agent_view.SetAgentPluginStatus, AGENT_PLUGIN_SET), -- Gitee