From 75714106374a4e62630c0e6b131f74a0717518bc Mon Sep 17 00:00:00 2001
From: lixin <lixinb@uniontech.com>
Date: Mon, 13 Nov 2023 15:17:47 +0800
Subject: [PATCH] =?UTF-8?q?=E8=BF=81=E7=A7=BB=E6=A3=80=E6=B5=8B=E6=8A=A5?=
 =?UTF-8?q?=E5=91=8A=E5=AF=BC=E5=87=BA?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 views/reports.py | 25 +++++++++++++++++++++++++
 views/server.py  | 33 ++++++++++++++++++++++++++++++++-
 2 files changed, 57 insertions(+), 1 deletion(-)
 create mode 100644 views/reports.py

diff --git a/views/reports.py b/views/reports.py
new file mode 100644
index 0000000..6295283
--- /dev/null
+++ b/views/reports.py
@@ -0,0 +1,25 @@
+import os
+from logger import *
+from connect_sql import DBHelper
+
+
+os.chdir('/usr/lib/uos-sysmig-server')
+migration_log = Logger('/var/tmp/uos-migration/migration.log', logging.DEBUG, logging.DEBUG)
+
+
+def migration_detection(data):
+    """
+    迁移检测报告
+    :return:
+    """
+    agent_ip = data.get('agent_ip')
+    info_sql = "select AES_DECRYPT(agent_passwd, 'coco'),agent_username from agent_info where agent_ip='%s'" % agent_ip
+    info = DBHelper().execute(info_sql).fetchall()
+    scp_log = "sshpass -p %s scp -r %s@%s:/var/tmp/uos-migration/UOS_analysis_report*.tar.gz /var/uos-migration/" % \
+              (str(info[0][0], encoding="utf-8"), info[0][1], agent_ip)
+    try:
+        os.system(scp_log)
+        migration_log.info(scp_log)
+    except:
+        migration_log.error('export report scp error:%s' % scp_log)
+    return 'success'
\ No newline at end of file
diff --git a/views/server.py b/views/server.py
index 369a750..4cfab8f 100644
--- a/views/server.py
+++ b/views/server.py
@@ -1,8 +1,13 @@
 import json
+import os
 from datetime import datetime
 
 from connect_sql import DBHelper
 from sysmig_agent.share import getSysMigConf
+from views import reports
+from logger import *
+
+migration_log = Logger('/var/tmp/uos-migration/migration.log', logging.DEBUG, logging.DEBUG)
 
 def import_host_info(data):
     """
@@ -343,4 +348,30 @@ def get_storage_num(data):
     faild = len(get_faild_num)
     data = {'success': success, 'faild': faild}
     json_data = json.dumps(data)
-    return json_data
\ No newline at end of file
+    return json_data
+
+
+reports_type = {
+    "migration_detection": reports.migration_detection,
+}
+
+def export_reports(data):
+    """
+    导出各种报告
+    :param data:
+    :return:
+    """
+    data = json.loads(data)
+    report_type = reports_type.get(data.get('reports_type'))
+    if report_type:
+        mkdir_log_pwd = "/var/uos-migration/"
+        isExists = os.path.exists(mkdir_log_pwd)
+        if not isExists:
+            try:
+                os.makedirs(mkdir_log_pwd)
+                migration_log.info(mkdir_log_pwd)
+            except:
+                migration_log.war("export report mkdir war:%s" % mkdir_log_pwd)
+
+        report_type(data)
+    return 'success'
\ No newline at end of file
-- 
2.20.1

