Jelajahi Sumber

0503 update

rita 2 tahun lalu
induk
melakukan
1e28453380
1 mengubah file dengan 118 tambahan dan 83 penghapusan
  1. 118 83
      manage.py

+ 118 - 83
manage.py

@@ -351,6 +351,10 @@ Automatic_Build_System = {
     'python3-tk': '0',
     'wget': '0',
     'python3-dev': '0',
+    'default-libmysqlclient-dev': '0',
+    'build-essential': '0',
+    'tasksel': '0',
+    'lamp-server': '0',
     'tasksel': '0',
     'lamp-server': '0',
     'phpMyAdmin-5.1.0-all-languages.tar.gz': '0',
@@ -370,7 +374,8 @@ Automatic_Build_System = {
     'php7.2-json': '0',
     'php7.2-zip': '0',
     'apache2': '0',
-    'mysql -u root -e': '0',
+    'paho-mqtt': '0',
+    'mosquitto': '0',
     'paho-mqtt': '0',
     'mosquitto': '0',
     'mqtt_server_setting': '0',
@@ -378,9 +383,14 @@ Automatic_Build_System = {
     'golang': '0',
     'gogsfile.git': '0',
     'requirements': '0',
+    'CoffeeProject.git': '0',
+    'BBend-Test-JWT.git': '0',
+    'Coffee_MySQL.sql': '0',
     'chrony': '0',
+    'ntpdate': '0',
     '--keyserver': '0',
     'update': '0',
+    'upgrade': '0',
     'ros-melodic-desktop-full': '0',
     'ros-melodic-rqt*': '0',
     'python-rosdep': '0',
@@ -400,15 +410,31 @@ def add_server_views():
     username = session['uname']
     L = []
     if request.method == 'GET':
+        # # # 232 路徑
         os.system('git clone -b sh --single-branch http://60.250.156.230:3000/Automatic_build_system/Automatically_install_files.git /home/gs1802/BBend-Test-JWT/newSH')
-        # os.system('git clone -b sh --single-branch http://60.250.156.230:3000/Automatic_build_system/Automatically_install_files.git C:\\Users\\USER\\Rita\\operating\\sixthproject\\new')
         with open('/home/gs1802/BBend-Test-JWT/newSH/coffeesystem.sh', encoding='utf-8') as f:
             D['command'] = f.read().split('\n')
-        print("D['command']: ", D['command'])
-        
-        shutil.rmtree('/home/gs1802/BBend-Test-JWT/newSH', onerror=remove_readonly)
         sl(3)
+        shutil.rmtree('/home/gs1802/BBend-Test-JWT/newSH', onerror=remove_readonly)
+        with open('/home/gs1802/BBend-Test-JWT/Item.txt') as f:
+            D['item_name'] = f.read().split('\n')
+            for item in D['item_name']:
+                L.append('.'.join(item.split('.')[1:]))         # 拿掉 '.'.join 的話會變成 [['python3', '8'], ['python3-pip'],
+        # ============================================================================================
+        # # # 本機 路徑
+        # os.system('git clone -b sh --single-branch http://60.250.156.230:3000/Automatic_build_system/Automatically_install_files.git C:/Users/USER/Rita/operating/sixthproject/BBend-Test-JWT/newSH')
+        # with open('C:/Users/USER/Rita/operating/sixthproject/BBend-Test-JWT/newSH/coffeesystem.sh', encoding='utf-8') as f:
+        #     D['command'] = f.read().split('\n')
+        # # print("D['command']: ", D['command'])
+        # sl(3)
+        # shutil.rmtree('newSH', onerror=remove_readonly)
+        # with open('Item.txt') as f:
+        #     D['item_name'] = f.read().split('\n')
+        #     for item in D['item_name']:
+        #         L.append('.'.join(item.split('.')[1:]))         # 拿掉 '.'.join 的話會變成 [['python3', '8'], ['python3-pip'],
+        # # ============================================================================================
 
+        sl(3)
         L2= []
         for i in range(0, len(D['command'])):
             if D['command'][i] == "'" or D['command'][i] == '' or "#" in D['command'][i] or D['command'][i] == ' ':
@@ -416,21 +442,9 @@ def add_server_views():
             L2.append(D['command'][i])
         D['command'] = L2
 
-        # ===========================================================================================================
-
-        # 0407 測試中暫時註解
-        # with open('/home/benson/Project/new/Item.txt') as f:
-        with open('Item.txt') as f:
-            D['item_name'] = f.read().split('\n')
-            for item in D['item_name']:
-                L.append('.'.join(item.split('.')[1:]))         # 拿掉 '.'.join 的話會變成 [['python3', '8'], ['python3-pip'],
-
-        # # 0407 測試註解一下
-        # # for i in range(0, len(D['item_name'])):
-        # #     item = D['item_name'][i].split('=')[-1].replace('"','')
-        # #     L.append(item)
-        # # L = L[:-1]
+        # # ===========================================================================================================
 
+        
         return render_template('add_server.html', params=locals())
     else:
         item_install_fail = 0
@@ -444,6 +458,7 @@ def add_server_views():
         print(username)
         print(password)
         try:
+            # Rita 0428 測試
             # 建立一個sshclient物件
             ssh = paramiko.SSHClient()
             # 允許將信任的主機自動加入到host_allow 列表,此方法必須放在connect方法的前面
@@ -466,16 +481,10 @@ def add_server_views():
             
             # # # # Rita TEST
             # D['command'] = [
-            #     # 'cd /home/使用者;pwd;sudo git clone -b git_repo --single-branch http://60.250.156.230:3000/Automatic_build_system/git_mysql.git', 
-            #     # 'cd /home/使用者/git_mysql/;pwd;sudo mv gogs.tar.gz /home/使用者/;sudo mv repo.tar.gz /home/使用者/',
-            #     # 'cd /home/使用者;pwd;sudo tar -zxvf /home/使用者/gogs.tar.gz;sudo tar -zxvf /home/使用者/repo.tar.gz ;sudo mv /home/使用者/git_mysql/app.ini /home/使用者/gogs/custom/conf/',
-            #     # 'sudo git config --global --add safe.directory /home/使用者/gogs-repositories/admin1/sh.git'
-
-            #     # 'cd /home/' + username + '/gogs/custom/conf/;' + \
-            #     # 'pwd;' + \
-            #     # 'sed -i "s/使_用_者/使用者/g" app.ini'
-
-            #     'cd /home/使用者;pwd;sudo tar -zxvf /home/使用者/gogs.tar.gz;sudo tar -zxvf /home/使用者/repo.tar.gz ;sudo mv /home/使用者/git_mysql/app.ini /home/使用者/gogs/custom/conf/'
+            #     # 'sudo pip3 install cryptography==3.3.2',
+            #     'sudo apt-key adv --keyserver "hkp://keyserver.ubuntu.com:80" --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654',
+            #     # 'cd /home/gs1802;pwd;sudo git clone http://60.250.156.230:3000/Automatic_build_system/Coffee_MySQL.git /home/gs1802/Coffee_MySQL/',
+            #     # 'rosdep update'
             # ]
 
             for command in D['command']:
@@ -487,12 +496,21 @@ def add_server_views():
                 # 判斷若此 command 符合 Automatic_Build_System 的 Key, 則改變狀態
                 for key, value in Automatic_Build_System.items():
                     if key in command and value == '0':
+                        print('key: ', key, ' / command: ', command)
                         Automatic_Build_System[key] = "Install"
-                sl(6)      # Rita 原本為 sl(6) 增加後觀察是否 clone 輸出會改變
+                        # sl(1)      # Rita 原本為 sl(6) 增加後觀察是否 clone 輸出會改變
+
+                        # # 0429 測試用
+                        # import random
+                        # tt = round(random.uniform(10.0,20.0), 3)
+                        # sl(tt)
+                        # Automatic_Build_System[key] = tt
+
+                # Rita 0428 測試
                 command_start_time = time.time()
                 # command = "bash -lc '{}'".format(command)
                 # print(command)
-                stdin, stdout, stderr = ssh.exec_command(command)
+                stdin, stdout, stderr = ssh.exec_command(command)   # # Rita 0428 測試
                 
                 error = stderr.read().decode()
                 success = stdout.read().decode()
@@ -505,55 +523,72 @@ def add_server_views():
 
                 # if error and 'mysql: [Warning] Using a password on the command line interface can be insecure.' not in error:
                 if error:
-                    print('=== error ===')
-
-                    # # 排除 錯誤訊息中的 警告訊息
-                    # Warning_list = [
-                    #     "WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager.", 
-                    #     "Cloning into", 
-                    #     "Warning: apt-key output should not be parsed (stdout is not a terminal)", 
-                    #     "gpg", 
-                    #     "Warning: running 'rosdep update' as root is not recommended.", 
-                    #     "You should run"
-                    # ]
-                    # error_command_list = error.split('\n')
-                    # for_error_command_list = error_command_list.copy()
-                    # # print("a: ", a)
-                    # # print("a[0]: ", a[0])
-                    # for cl in range(0, len(error_command_list)):
-                    #     print("=================================")
-                    #     print("error_command_list[cl]: ", error_command_list[cl])
-                    #     for W in range(len(error_command_list)):
-                    #         print("W: ", W)
-                    #         try:
-                    #             if Warning_list[W] in error_command_list[cl]:
-                    #                 print(Warning_list[W])
-                    #                 print(error_command_list[cl])
-                    #                 #  and error_command_list.index(Warning_list[W]) != -1
-                    #                 # command_index = error_command_list[0].index(Warning_list[W])
-                    #                 # print(command_index)
-                    #                 for_error_command_list.remove(error_command_list[cl])
-                    #                 print(error_command_list)
-                    #                 print(for_error_command_list)
-                    #         except IndexError:
-                    #             pass
-
-
-                    if item_install_fail == 0:
-                        item_install_fail = 1
-                    with open('../ItemFile.csv', 'a', newline='') as csvFile:
-                        csvWriter = csv.writer(csvFile)
-                        csvWriter.writerow([command.replace(';', ';\n'), 
-                                            'success: \n' + success + '\n' + 'error: \n' + error, 
-                                            # 'success: \n' + success, 
-                                            # 'error: \n' + error, 
-                                            execution_time, 
-                                            'error'])
-                    # socketio.emit('stderr', error, namespace='/test_conn')
-                    # 判斷若此 command 符合 Automatic_Build_System 的 Key, 則改變狀態
-                    for key, value in Automatic_Build_System.items():
-                        if key in command:
-                            Automatic_Build_System[key] = "Error"
+
+                    # 排除 錯誤訊息中的 警告訊息
+                    Warning_list = [
+                        "WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager.", 
+                        "Cloning into", 
+                        "Warning: apt-key output should not be parsed (stdout is not a terminal)", 
+                        "gpg", 
+                        "Warning: running 'rosdep update' as root is not recommended.", 
+                        "You should run",
+                        "[Warning] Using a password on the command line interface can be insecure."
+                    ]
+                    error_command_list = error.split('\n')
+                    for_error_command_list = error_command_list.copy()
+                    # print("a: ", a)
+                    # print("a[0]: ", a[0])
+                    for cl in range(0, len(error_command_list)):
+                        # print("=================================")
+                        # print("error_command_list[cl]: ", error_command_list[cl])
+                        for W in range(len(Warning_list)):
+                        # for W in range(len(error_command_list)):
+                            try:
+                                if Warning_list[W] in error_command_list[cl]:
+                                    # print("AA: ", Warning_list[W])
+                                    # print("BB: ", error_command_list[cl])
+                                    # #  and error_command_list.index(Warning_list[W]) != -1
+                                    # # command_index = error_command_list[0].index(Warning_list[W])
+                                    # # print(command_index)
+                                    for_error_command_list.remove(error_command_list[cl])
+                                    # print(error_command_list)
+                                    # print(for_error_command_list)
+                            except IndexError:
+                                pass
+                    if for_error_command_list == [''] or for_error_command_list == []:
+                        print('=== Warning ===')
+                        with open('../ItemFile.csv', 'a', newline='') as csvFile:
+                            csvWriter = csv.writer(csvFile)
+                            csvWriter.writerow([command.replace(';', ';\n'), 
+                                                'success: \n' + success + '\n' + 'error: \n' + error, 
+                                                # 'success: \n' + success, 
+                                                # 'error: \n' + error, 
+                                                execution_time, 
+                                                'warning'])
+                        # socketio.emit('stderr', error, namespace='/test_conn')
+                        # 判斷若此 command 符合 Automatic_Build_System 的 Key, 則改變狀態
+                        for key, value in Automatic_Build_System.items():
+                            if key in command:
+                                Automatic_Build_System[key] = execution_time
+                    else:
+                        print('for_error_command_list: ', for_error_command_list)
+                        print('=== error ===')
+
+                        if item_install_fail == 0:
+                            item_install_fail = 1
+                        with open('../ItemFile.csv', 'a', newline='') as csvFile:
+                            csvWriter = csv.writer(csvFile)
+                            csvWriter.writerow([command.replace(';', ';\n'), 
+                                                'success: \n' + success + '\n' + 'error: \n' + error, 
+                                                # 'success: \n' + success, 
+                                                # 'error: \n' + error, 
+                                                execution_time, 
+                                                'error'])
+                        # socketio.emit('stderr', error, namespace='/test_conn')
+                        # 判斷若此 command 符合 Automatic_Build_System 的 Key, 則改變狀態
+                        for key, value in Automatic_Build_System.items():
+                            if key in command:
+                                Automatic_Build_System[key] = "Error"
                 else:
                     print('=== success ===')
 
@@ -598,7 +633,7 @@ def add_server_views():
                 #                                 'success'])
 
 
-                sl(10)
+                sl(3)
                 print('-----------------------------------------------------------------------------')
                 
 
@@ -615,8 +650,8 @@ def add_server_views():
             # # 關閉寫入的 CSV 檔案
             # csvFile.close()
 
-            # 關閉連線
-            ssh.close()
+            # # 關閉連線
+            # ssh.close()
             if item_install_fail:
                 res = '有套件未安裝完成!'
             else: