Index: /branches/amp_3_7_2/src/webui/webui/htdocs/new/src/cm/lib/task_scheduler.py
===================================================================
--- /branches/amp_3_7_2/src/webui/webui/htdocs/new/src/cm/lib/task_scheduler.py	(revision 2946)
+++ /branches/amp_3_7_2/src/webui/webui/htdocs/new/src/cm/lib/task_scheduler.py	(working copy)
@@ -1,7 +1,7 @@
 from cm.lib.libmonitor import Decision, SSLCertDecision
 from cm.lib.libconfig import Backup_AG, Backup_APV, Recover_AG, Recover_APV, Backup_Custom, Apply_AG
-#from cm.models.device_mgmt.device import get_rest_info_from_device
-#from cm.models.configuration.update import update_updatelist
+# from cm.models.device_mgmt.device import get_rest_info_from_device
+# from cm.models.configuration.update import update_updatelist
 from djproject.an_settings import *
 from djproject.settings import *
 import socket
\ No newline at end of file
@@ -16,22 +16,25 @@
 import re
 import uuid
 import difflib
-from cm.lib.parse_configfile import TarFiles, parse_vsite_config_from_configfile, parse_vsite_from_configfile, parse_diff_config
+from cm.lib.parse_configfile import TarFiles, parse_vsite_config_from_configfile, parse_vsite_from_configfile, \
+    parse_diff_config
 from apscheduler.schedulers.background import BackgroundScheduler
 from apscheduler.events import EVENT_JOB_ERROR
 from multiprocessing.pool import ThreadPool
-from cm.lib.libbasic_operation import oper_log, send_cli_to_device, send_command_to_device, send_xmlrpc_to_device, get_rest_info_from_device, get_device_type, get_ip_address
+from cm.lib.libbasic_operation import oper_log, send_cli_to_device, send_command_to_device, send_xmlrpc_to_device, \
+    get_rest_info_from_device, get_device_type, get_ip_address
 from cm.lib.postgres_db import DB
 from hive.model.loading import get_model
 from hive.model.query import mark_expire_all
 from hive.notification import send_notification
 from cm.lib.communication import send_https_rest_request, modify_url, call_restapi
 from hive.model.legacycli import cli_parse, RegexParser, MATCHONE
-from hive.utils import andebug
+from hive.utils import andebug, get_current_session
 
 logger = logging.getLogger('hive.debug')
 device_check_pool = ThreadPool(processes=20)
 
+
 def append_job_description(name, description):
     db = DB.get_connected_db()
     select_sql = "SELECT description FROM task WHERE name='%s'" % name
\ No newline at end of file
@@ -43,19 +46,26 @@
 
     db.close()
 
+
 def remove_job_from_database(name):
     db = DB.get_connected_db()
     del_sql = "DELETE FROM task WHERE name='%s' " % name
     db.execute_sql(del_sql)
     db.close()
 
-def add_job_into_database(name, type, description, trigger, state, next_run_time, scheduler, failed_times=3, device_list=''):
+
+def add_job_into_database(name, type, description, trigger, state, next_run_time, scheduler, failed_times=3,
+                          device_list=''):
     db = DB.get_connected_db()
-    save_sql = "INSERT INTO task(name, type, description, trigger, state, next_run_time, scheduler, failed_times, device_list) values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s') " % (name, type, description, trigger, state, next_run_time, scheduler, failed_times, device_list)
+    save_sql = "INSERT INTO task(name, type, description, trigger, state, next_run_time, scheduler, failed_times, device_list) values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s') " % (
+    name, type, description, trigger, state, next_run_time, scheduler, failed_times, device_list)
     db.execute_sql(save_sql)
     db.close()
-    oper_log('info', 'system', 'Add a new task. Task name: %s, task type: %s, task description: %s, task trigger: %s.' % (name, type, description, trigger))
+    oper_log('info', 'system',
+             'Add a new task. Task name: %s, task type: %s, task description: %s, task trigger: %s.' % (
+             name, type, description, trigger))
 
+
 def update_job_state(name, state):
     db = DB.get_connected_db()
     update_sql = "UPDATE task SET state='%s' WHERE name='%s'" % (state, name)
\ No newline at end of file
@@ -63,12 +73,14 @@
     db.close()
     oper_log('info', 'system', 'Update task state. Task name: %s, task result: %s.' % (name, state))
 
+
 def update_job_runtime(name, time):
     db = DB.get_connected_db()
     update_sql = "UPDATE task SET next_run_time='%s' WHERE name='%s'" % (time, name)
     db.execute_sql(update_sql)
     db.close()
 
+
 def update_job_result_field(name, value, username):
     db = DB.get_connected_db()
     update_sql = "UPDATE task SET result_field='%s' WHERE name='%s'" % (value, name)
\ No newline at end of file
@@ -76,24 +88,28 @@
     db.close()
     oper_log('info', 'system', 'Update task state. Task name: %s, task result: %s.' % (name, value))
 
+
 def update_job_failed(name, failed_times):
     db = DB.get_connected_db()
     update_sql = "UPDATE task SET failed_times='%d' WHERE name='%s'" % (failed_times, name)
     db.execute_sql(update_sql)
-    db.close()   
+    db.close()
 
+
 def get_job_failed(name):
     db = DB.get_connected_db()
     select_sql = "SELECT failed_times FROM task WHERE name='%s'" % name
     data = db.fetchall(select_sql)
     return data[0][0]
 
+
 def update_job_custom_field(name, values):
     db = DB.get_connected_db()
     update_sql = "UPDATE task SET custom_fields='%s' WHERE name='%s'" % (values, name)
     db.execute_sql(update_sql)
-    db.close()       
+    db.close()
 
+
 def add_description(task_name, values):
     description = values + ' ' + datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + '\n'
     append_job_description(task_name, description)
\ No newline at end of file
@@ -102,6 +118,8 @@
 '''
 start upload job
 '''
+
+
 def update_updatelist(file_size, id, path):
     file_path = CONFIG_UPLOAD_STORE_DIR + os.path.basename(path)
     update_sql = "UPDATE update_list SET file_size='%d', location='%s' WHERE id='%d'" % (file_size, file_path, id)
\ No newline at end of file
@@ -109,6 +127,8 @@
     db.execute_sql(update_sql)
     db.close()
     mark_expire_all(get_model('cm', ['configuration', 'update', 'Update']))
+
+
 def get_file_md5(filename):
     myhash = hashlib.md5()
     f = file(filename, 'rb')
\ No newline at end of file
@@ -118,8 +138,10 @@
             break
         myhash.update(b)
     f.close()
-    file_md5 = myhash.hexdigest()  
-    return file_md5              
+    file_md5 = myhash.hexdigest()
+    return file_md5
+
+
 def compare_md5(file_md5, id):
     select_sql = "SELECT md5_value FROM update_list WHERE id='%d'" % id
     db = DB.get_connected_db()
\ No newline at end of file
@@ -131,6 +153,8 @@
         return True
     else:
         return False
+
+
 def mv_file(path):
     file_name = os.path.basename(path)
     file_path = CONFIG_UPLOAD_STORE_DIR + file_name
\ No newline at end of file
@@ -138,12 +162,13 @@
         os.remove(file_path)
     shutil.move(path, file_path)
 
+
 def get_file_from_url(get_url):
     file_name = get_url.split('/')[-1]
-    get_params = {'filename':file_name}
+    get_params = {'filename': file_name}
     full_path = FILE_UPLOAD_DIR + file_name
     try:
-        rt = requests.get(url = get_url, stream = True, params = get_params)
+        rt = requests.get(url=get_url, stream=True, params=get_params)
     except:
         return None
     if not os.path.exists(FILE_UPLOAD_DIR):
\ No newline at end of file
@@ -153,6 +178,8 @@
             f.write(chunk)
             f.flush()
     return full_path
+
+
 def upload_job(update_list_id, name, tmp_url):
     description = "The execution time: " + datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + '\n'
     append_job_description(name, description)
\ No newline at end of file
@@ -160,7 +187,7 @@
     mark_expire_all(get_model('cm', ['tasking', 'Tasks']))
     path = get_file_from_url(tmp_url)
     if not path:
-        #raise ModelQueryException(CLICmdError(__('Getting file failed, please check your url!')))
+        # raise ModelQueryException(CLICmdError(__('Getting file failed, please check your url!')))
         append_job_description(name, "Getting file failed, please check your url!\n")
         update_job_state(name, 'failed')
         mark_expire_all(get_model('cm', ['tasking', 'Tasks']))
\ No newline at end of file
@@ -171,7 +198,7 @@
         mv_file(path)
         update_updatelist(file_size, update_list_id, path)
     else:
-        #raise ModelQueryException(CLICmdError(__('Md5 error, please check the file!')))
+        # raise ModelQueryException(CLICmdError(__('Md5 error, please check the file!')))
         append_job_description(name, "Md5 error, please check your url!\n")
         update_job_state(name, 'failed')
         mark_expire_all(get_model('cm', ['tasking', 'Tasks']))
\ No newline at end of file
@@ -183,6 +210,8 @@
     mark_expire_all(get_model('cm', ['tasking', 'Tasks']))
     send_notification('success', 'Task <%s> has done.' % name, model='image')
     return
+
+
 '''
 end upload job
 '''
\ No newline at end of file
@@ -190,6 +219,8 @@
 '''
 start update job
 '''
+
+
 def update_failed(description, task_name):
     add_description(task_name, description)
     update_job_state(task_name, 'failed')
\ No newline at end of file
@@ -203,12 +234,14 @@
     db.close()
     return data.split('/')[-1] if data else None
 
-def check_package_from_device(update_list_id, filename, task_name, ip_address, restapi_port, restapi_username, restapi_password, device_type, device_info):
+
+def check_package_from_device(update_list_id, filename, task_name, ip_address, restapi_port, restapi_username,
+                              restapi_password, device_type, device_info):
     db = DB.get_connected_db()
     fetchall_sql = '''SELECT md5_value, file_size FROM update_list where id='%d' ''' % update_list_id
     data = db.fetchall(fetchall_sql)
     db.close()
-    #4 | image3   | Rel_APV_10_3_1_9 | 515895165 | fdce12389937be070d2da9d058423a34 | http://10.23.0.4/10.3.1/ArrayOS-Rel_APV_10_3_1_9.array | /ca/package/ArrayOS-Rel_APV_10_3_1_9.array |
+    # 4 | image3   | Rel_APV_10_3_1_9 | 515895165 | fdce12389937be070d2da9d058423a34 | http://10.23.0.4/10.3.1/ArrayOS-Rel_APV_10_3_1_9.array | /ca/package/ArrayOS-Rel_APV_10_3_1_9.array |
     md5_value = data[0][0]
     file_size = data[0][1]
 
\ No newline at end of file
@@ -223,7 +256,8 @@
             time.sleep(3)
             if device_info['protocol'] == "restapi":
                 new_url = modify_url('/rest/device_type/cli_extend', device_type)
-                rest_response_data = send_https_rest_request('POST', new_url, running_params, ip_address, restapi_port, restapi_username, restapi_password)
+                rest_response_data = send_https_rest_request('POST', new_url, running_params, ip_address, restapi_port,
+                                                             restapi_username, restapi_password)
             else:
                 rest_response_data = send_xmlrpc_to_device(device_info, '', cli_cmd)
         except Exception, e:
\ No newline at end of file
@@ -247,10 +281,9 @@
                     add_description(task_name, description)
                     return False
 
-
             if contents.split('\n')[1]:
                 file_info = contents.strip().split('\n')[1].split(' ')
-                #apv: ['515895165', '', '', '', 'Dec', '28', '2019', '05:49:18', 'ArrayOS-Rel_APV_10_3_1_9.array', '', 'fdce12389937be070d2da9d058423a34', '', '/var/crash/package/ArrayOS-Rel_APV_10_3_1_9.array']
+                # apv: ['515895165', '', '', '', 'Dec', '28', '2019', '05:49:18', 'ArrayOS-Rel_APV_10_3_1_9.array', '', 'fdce12389937be070d2da9d058423a34', '', '/var/crash/package/ArrayOS-Rel_APV_10_3_1_9.array']
             else:
                 description = 'Error, device download the build failed.'
                 add_description(task_name, description)
\ No newline at end of file
@@ -282,26 +315,31 @@
 
     return True
 
+
 def update_custom_field(task_name, step, args):
     custom_field = {}
-    custom_field['update'] = {'step':step, 'args':args}
+    custom_field['update'] = {'step': step, 'args': args}
     custom_str = json.dumps(custom_field)
     update_job_custom_field(task_name, custom_str)
 
-def waiting_for_device(version, task_name, ip_address, restapi_port, restapi_username, restapi_password, device_type, device_info):
+
+def waiting_for_device(version, task_name, ip_address, restapi_port, restapi_username, restapi_password, device_type,
+                       device_info):
     # description = 'Waiting for AG response: ' + datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + '\n'
     # append_job_description(task_name, description)
     add_description(task_name, 'Waiting for device response: ')
     mark_expire_all(get_model('cm', ['tasking', 'Tasks']))
 
-    update_custom_field(task_name, 'waiting_for_device', [version, task_name, ip_address, restapi_port, restapi_username, restapi_password, device_type, device_info])
+    update_custom_field(task_name, 'waiting_for_device',
+                        [version, task_name, ip_address, restapi_port, restapi_username, restapi_password, device_type,
+                         device_info])
 
-    time.sleep(60*2)
+    time.sleep(60 * 2)
 
     if isinstance(device_type, list):
         deviceType = device_type[0]
         if device_type[0].lower() in WAF_TYPE_LIST and device_type[1]:
-            time.sleep(60*6)
+            time.sleep(60 * 6)
     else:
         deviceType = device_type
     description = ''
\ No newline at end of file
@@ -312,7 +350,8 @@
         try:
             if device_info['protocol'] == 'restapi':
                 new_url = modify_url('/rest/device_type/system/SystemInfo/version', deviceType)
-                rest_response_data = send_https_rest_request('GET', new_url, '', ip_address, restapi_port, restapi_username, restapi_password)
+                rest_response_data = send_https_rest_request('GET', new_url, '', ip_address, restapi_port,
+                                                             restapi_username, restapi_password)
             else:
                 rest_response_data = send_xmlrpc_to_device(device_info, '', 'show version')
         except Exception, e:
\ No newline at end of file
@@ -335,8 +374,8 @@
                         continue
                     version_ag = rest_body['SystemInfo']['version']
                     rtn = cli_parse(version_ag, [
-                                 RegexParser('([ArrayOS|InfosecOS].+?)\n', MATCHONE),
-                             ])
+                        RegexParser('([ArrayOS|InfosecOS].+?)\n', MATCHONE),
+                    ])
                     new_version = rtn[0][0]
                 else:
                     time.sleep(10)
\ No newline at end of file
@@ -345,7 +384,7 @@
             else:
                 try:
                     version_ag = json.loads(rest_response_data)[0]["output"]
-                    rtn = cli_parse(version_ag, [ RegexParser('([ArrayOS|InfosecOS].+?)\n', MATCHONE)])
+                    rtn = cli_parse(version_ag, [RegexParser('([ArrayOS|InfosecOS].+?)\n', MATCHONE)])
                     new_version = rtn[0][0]
                 except Exception, e:
                     time.sleep(10)
\ No newline at end of file
@@ -356,7 +395,7 @@
             db.execute_sql(update_sql)
             db.close()
             break
-            #hide following code because the case "upgrade with build of current version will fail"
+            # hide following code because the case "upgrade with build of current version will fail"
             # if new_version != version:
             #     db = DB.get_connected_db()
             #     update_sql = "UPDATE device SET version='%s' WHERE ip_address='%s'" % (version_ag, ip_address)
\ No newline at end of file
@@ -369,7 +408,7 @@
             #     break
 
     if not failed_time:
-        #add_description(task_name, description)
+        # add_description(task_name, description)
         update_failed(description, task_name)
         return False
     add_description(task_name, 'System update successful! ')
\ No newline at end of file
@@ -379,17 +418,21 @@
 
     return True
 
-def system_update_device(file_name, task_name, ip_address, restapi_port, restapi_username, restapi_password, device_type, device_info):
+
+def system_update_device(file_name, task_name, ip_address, restapi_port, restapi_username, restapi_password,
+                         device_type, device_info):
     # description = 'Send command <system update> to AG: ' + datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + '\n'
     # append_job_description(task_name, description)
     add_description(task_name, 'Send command <system update> to Device: ')
     mark_expire_all(get_model('cm', ['tasking', 'Tasks']))
-    update_custom_field(task_name, 'system_update_device', [file_name, task_name, ip_address, restapi_port, restapi_username, restapi_password, device_type, device_info])
+    update_custom_field(task_name, 'system_update_device',
+                        [file_name, task_name, ip_address, restapi_port, restapi_username, restapi_password,
+                         device_type, device_info])
     if isinstance(device_type, list):
         deviceType = device_type[0]
     else:
         deviceType = device_type
-    
+
     failed_time = get_job_failed(task_name)
     description = ''
     version = ''
\ No newline at end of file
@@ -398,7 +441,8 @@
             time.sleep(1)
             if device_info['protocol'] == 'restapi':
                 new_url = modify_url('/rest/device_type/system/SystemInfo/version', deviceType)
-                rest_response_data = send_https_rest_request('GET', new_url, '', ip_address, restapi_port, restapi_username, restapi_password)
+                rest_response_data = send_https_rest_request('GET', new_url, '', ip_address, restapi_port,
+                                                             restapi_username, restapi_password)
             else:
                 rest_response_data = send_xmlrpc_to_device(device_info, '', 'show version')
         except Exception, e:
\ No newline at end of file
@@ -413,8 +457,8 @@
                     rest_body = json.loads(rest_response_data['body'])
                     version = rest_body['SystemInfo']['version']
                     rtn = cli_parse(version, [
-                                 RegexParser('([ArrayOS|InfosecOS].+?)\n', MATCHONE),
-                             ])
+                        RegexParser('([ArrayOS|InfosecOS].+?)\n', MATCHONE),
+                    ])
                     version = rtn[0][0]
                     break
                 else:
\ No newline at end of file
@@ -430,8 +474,8 @@
     if not failed_time:
         update_failed(description, task_name)
         return False
-        #add_description(task_name, description)
-        #return False
+        # add_description(task_name, description)
+        # return False
     description = ''
     update_cmd_str = 'system update "/var/crash/package/%s" \nYES\n' % file_name
     if isinstance(device_type, list):
\ No newline at end of file
@@ -447,13 +491,16 @@
             time.sleep(1)
             if device_info['protocol'] == 'restapi':
                 new_url = modify_url('/rest/device_type/cli_extend', deviceType)
-                rest_response_data = send_https_rest_request('POST', new_url, running_params, ip_address, restapi_port, restapi_username, restapi_password)
+                rest_response_data = send_https_rest_request('POST', new_url, running_params, ip_address, restapi_port,
+                                                             restapi_username, restapi_password)
             else:
-                rest_response_data = send_xmlrpc_to_device(device_info, '', 'system update "/var/crash/package/%s"\nYES' % file_name, mode="config_with_input")
+                rest_response_data = send_xmlrpc_to_device(device_info, '',
+                                                           'system update "/var/crash/package/%s"\nYES' % file_name,
+                                                           mode="config_with_input")
         except Exception, e:
             failed_time -= 1
             if 'timed out' in str(e) or 'Empty reply from server' in str(e):
-                #description = 'Error, please check the ip address and make sure the device have already turned on the RESTful API service! '
+                # description = 'Error, please check the ip address and make sure the device have already turned on the RESTful API service! '
                 break
             description = 'Error, can not connect the device! '
             continue
\ No newline at end of file
@@ -475,26 +522,31 @@
                     # print 'contents', contents
                     break
                 else:
-                    #APV timeout
+                    # APV timeout
                     break
-                    #failed_time -= 1
+                    # failed_time -= 1
             else:
                 if rest_response_data:
                     break
     if not failed_time:
         update_failed(description, task_name)
-        #add_description(task_name, description)
+        # add_description(task_name, description)
         return False
-    return waiting_for_device(version, task_name, ip_address, restapi_port, restapi_username, restapi_password, device_type, device_info)
+    return waiting_for_device(version, task_name, ip_address, restapi_port, restapi_username, restapi_password,
+                              device_type, device_info)
 
-def upload_to_device(update_list_id, task_name, ip_address, restapi_port, restapi_username, restapi_password, device_type, device_name):
+
+def upload_to_device(update_list_id, task_name, ip_address, restapi_port, restapi_username, restapi_password,
+                     device_type, device_name, mgmt_ip_addr):
     add_description(task_name, "The execution time: ")
     update_job_state(task_name, 'ongoing')
 
     add_description(task_name, 'Upload build to Device: ')
     mark_expire_all(get_model('cm', ['tasking', 'Tasks']))
 
-    update_custom_field(task_name, 'upload_to_device', [update_list_id, task_name, ip_address, restapi_port, restapi_username, restapi_password, device_type, device_name])
+    update_custom_field(task_name, 'upload_to_device',
+                        [update_list_id, task_name, ip_address, restapi_port, restapi_username, restapi_password,
+                         device_type, device_name])
 
     if isinstance(device_type, list):
         deviceType = device_type[0]
\ No newline at end of file
@@ -509,30 +561,20 @@
         update_job_state(task_name, 'failed')
         mark_expire_all(get_model('cm', ['tasking', 'Tasks']))
         return False
-    cm = __import__("cm")
-    reload(cm.conf)
-    network_card = cm.conf.NETWORK_CARD
-    try:
-        local_ip = get_ip_address(network_card) 
-    except Exception, e:
-        description = 'Local network settings are abnormal. Please configuare management interface by CLI "system management intertface <port_name>".'
-        add_description(task_name, description)
-        update_job_state(task_name, 'failed')
-        mark_expire_all(get_model('cm', ['tasking', 'Tasks']))
-        return False
     device_info = get_rest_info_from_device(device_name)[0]
-    file_path = 'ftp://array:admin@%s:9993/%s' % (local_ip, filename)  #maybe you have a better idea
+    file_path = 'ftp://array:admin@%s:9993/%s' % (mgmt_ip_addr, filename)  # maybe you have a better idea
     running_params = 'system package "%s"' % file_path
     if deviceType.lower() in VPN_TYPE_LIST:
         running_params = json.dumps({"config": running_params})
     new_url = modify_url('/rest/device_type/batch_cli', deviceType)
     try:
         if device_info['protocol'] == 'restapi':
-            rest_response_data = send_https_rest_request('POST', new_url, running_params, ip_address, restapi_port, restapi_username, restapi_password)
+            rest_response_data = send_https_rest_request('POST', new_url, running_params, ip_address, restapi_port,
+                                                         restapi_username, restapi_password)
         else:
             rest_response_data = send_xmlrpc_to_device(device_info, '', 'system package "%s"' % file_path)
     except Exception, e:
-        #mybe timeout, do not return fialed here, continue do check_package_from_device
+        # mybe timeout, do not return fialed here, continue do check_package_from_device
         pass
     else:
         if device_info['protocol'] == 'restapi':
\ No newline at end of file
@@ -553,9 +595,11 @@
                             fetchall_sql = '''SELECT location FROM update_list where id='%d' ''' % update_list_id
                             location = db.fetchall(fetchall_sql)[0][0]
 
-                            new_location = location[:-6] + ".array" if location.endswith(".click") else location[:-6] + ".click"
+                            new_location = location[:-6] + ".array" if location.endswith(".click") else location[
+                                                                                                        :-6] + ".click"
                             # update this new location for `update` db and move this upgrade file with new suffix.
-                            update_location_sql = "UPDATE update_list SET location = '%s' where id = '%d'" % (new_location, update_list_id)
+                            update_location_sql = "UPDATE update_list SET location = '%s' where id = '%d'" % (
+                            new_location, update_list_id)
                             db.execute_sql(update_location_sql)
                             db.close()
 
\ No newline at end of file
@@ -571,12 +615,14 @@
 
                         filename = new_location.split('/')[-1]
 
-                        file_path = 'ftp://array:admin@%s:9993/%s' % (local_ip, filename)
+                        file_path = 'ftp://array:admin@%s:9993/%s' % (mgmt_ip_addr, filename)
                         running_params = 'system package "%s"' % file_path
                         try:
-                            rest_response_data = send_https_rest_request('POST', new_url, running_params, ip_address, restapi_port, restapi_username, restapi_password)
+                            rest_response_data = send_https_rest_request('POST', new_url, running_params, ip_address,
+                                                                         restapi_port, restapi_username,
+                                                                         restapi_password)
                         except Exception, e:
-                            #maybe timeout, do not return fialed here, continue do check_package_from_device
+                            # maybe timeout, do not return fialed here, continue do check_package_from_device
                             pass
                         else:
                             if rest_response_data['status'] == 200:
\ No newline at end of file
@@ -593,7 +639,7 @@
                 elif not check_upload_response(task_name, contents):
                     return False
         else:
-            if rest_response_data != "failed" and  rest_response_data != "time out":
+            if rest_response_data != "failed" and rest_response_data != "time out":
                 contents = None
                 try:
                     contents = json.loads(rest_response_data)[0]["output"]
\ No newline at end of file
@@ -608,12 +654,15 @@
             else:
                 config_failed('Failed to upload build to device.', task_name)
                 return False
-    if not check_package_from_device(update_list_id, filename, task_name, ip_address, restapi_port, restapi_username, restapi_password, deviceType, device_info):
+    if not check_package_from_device(update_list_id, filename, task_name, ip_address, restapi_port, restapi_username,
+                                     restapi_password, deviceType, device_info):
         update_job_state(task_name, 'failed')
         mark_expire_all(get_model('cm', ['tasking', 'Tasks']))
-        return        
-    return system_update_device(filename, task_name, ip_address, restapi_port, restapi_username, restapi_password, device_type, device_info)
-     
+        return
+    return system_update_device(filename, task_name, ip_address, restapi_port, restapi_username, restapi_password,
+                                device_type, device_info)
+
+
 def check_upload_response(task_name, contents):
     if contents.find('Invalid package name') != -1:
         description = 'Error, invalid package name;'
\ No newline at end of file
@@ -640,7 +689,8 @@
         mark_expire_all(get_model('cm', ['tasking', 'Tasks']))
         return False
     return True
- 
+
+
 '''
 end update job
 '''
\ No newline at end of file
@@ -648,11 +698,14 @@
 '''
 start config jobs
 '''
+
+
 def config_failed(description, task_name):
     add_description(task_name, description)
     update_job_state(task_name, 'failed')
     mark_expire_all(get_model('cm', ['tasking', 'Tasks']))
 
+
 def config_batch_cli(task_name, device_info, file_path):
     add_description(task_name, "The execution time: ")
     update_job_state(task_name, 'ongoing')
\ No newline at end of file
@@ -665,8 +718,8 @@
         return
     try:
         if device_info['type'].lower() in VPN_TYPE_LIST:
-            sync_config = json.dumps({'config':file_command.read()})
-        elif device_info['type'].lower() in ADC_TYPE_LIST: 
+            sync_config = json.dumps({'config': file_command.read()})
+        elif device_info['type'].lower() in ADC_TYPE_LIST:
             sync_config = file_command.read()
     except:
         config_failed("The file(%s) has special character." % os.path.basename(file_path), task_name)
\ No newline at end of file
@@ -676,7 +729,9 @@
 
     try:
         new_url = modify_url('/rest/device_type/batch_cli', device_info['type'])
-        rest_response_data = send_https_rest_request('POST', new_url, sync_config, device_info['ip_address'], device_info['restapi_port'], device_info['restapi_username'], device_info['restapi_password'])
+        rest_response_data = send_https_rest_request('POST', new_url, sync_config, device_info['ip_address'],
+                                                     device_info['restapi_port'], device_info['restapi_username'],
+                                                     device_info['restapi_password'])
     except Exception as e:
         config_failed("Can not connect device!", task_name)
         return
\ No newline at end of file
@@ -701,7 +756,7 @@
         else:
             config_failed(config_result, task_name)
             return
-    elif device_info['type'].lower() in ADC_TYPE_LIST: 
+    elif device_info['type'].lower() in ADC_TYPE_LIST:
         if not config_result:
             add_description(task_name, config_result.replace('\\', ''))
             update_job_state(task_name, 'done')
\ No newline at end of file
@@ -711,35 +766,39 @@
             config_failed(config_result, task_name)
             return
 
+
 def refresh_device(device_list):
-    #refresh device config
+    # refresh device config
     for device in device_list:
         device_info = get_rest_info_from_device(device)
-        #device_info [{'restapi_port': 9997, 'restapi_password': u'admin', 'ip_address': u'10.8.2.54', 'restapi_username': u'array', 'type': u'AG'}]
+        # device_info [{'restapi_port': 9997, 'restapi_password': u'admin', 'ip_address': u'10.8.2.54', 'restapi_username': u'array', 'type': u'AG'}]
         _type = get_device_type(device_info[0]['type'])
         if device_info[0]['type'].lower() in VPN_TYPE_LIST:
-            data = {'cmd' : 'show running all'}
+            data = {'cmd': 'show running all'}
             new_url = '/rest/%s/global/cli_extend' % _type
         elif device_info[0]['type'].lower() in ADC_TYPE_LIST:
-            data = {'cmd' : 'show running'}
+            data = {'cmd': 'show running'}
             new_url = '/rest/%s/cli_extend' % _type
         running_params = json.dumps(data)
-        
+
         try:
-            rest_response_data = send_https_rest_request('POST', new_url, running_params, device_info[0]['ip_address'], device_info[0]['restapi_port'], device_info[0]['restapi_username'], device_info[0]['restapi_password'])
+            rest_response_data = send_https_rest_request('POST', new_url, running_params, device_info[0]['ip_address'],
+                                                         device_info[0]['restapi_port'],
+                                                         device_info[0]['restapi_username'],
+                                                         device_info[0]['restapi_password'])
         except:
             continue
         if rest_response_data['status'] == 200:
             config_result = json.loads(rest_response_data['body'])['contents']
         else:
             continue
-        
+
         device_path = os.path.join(DEFAULT_CONFIG_FILE_PATH, 'device')
         if not os.path.isdir(device_path):
             os.makedirs(device_path)
 
-        #create_time = datetime.datetime.now().strftime('%Y-%m-%d')
-        #file_name = device_info[0]['name'] + '_' + datetime.datetime.now().strftime('%Y-%m-%d')
+        # create_time = datetime.datetime.now().strftime('%Y-%m-%d')
+        # file_name = device_info[0]['name'] + '_' + datetime.datetime.now().strftime('%Y-%m-%d')
         file_name = device_info[0]['name']
         try:
             config_file = open(device_path + '/' + file_name, 'w')
\ No newline at end of file
@@ -752,29 +811,31 @@
         db = None
         try:
             db = DB.get_connected_db()
-            insert_sql = "INSERT INTO file_list(name, create_time, modify_time, type, device_type, device_id) values('%s', '%s', '%s', '%s', '%s', '%s') on conflict (name) do update set modify_time=excluded.modify_time" % (file_name, create_time, create_time, 'device', device_info[0]['type'], device_info[0]['id'])
+            insert_sql = "INSERT INTO file_list(name, create_time, modify_time, type, device_type, device_id) values('%s', '%s', '%s', '%s', '%s', '%s') on conflict (name) do update set modify_time=excluded.modify_time" % (
+            file_name, create_time, create_time, 'device', device_info[0]['type'], device_info[0]['id'])
             db.execute_sql(insert_sql)
         except Exception:
             pass
         finally:
             if db:
                 db.close()
-  
+
     if device_info[0]['type'].lower() in ADC_TYPE_LIST:
         return
 
-    #refresh vsite file
+    # refresh vsite file
     device_path = os.path.join(DEFAULT_CONFIG_FILE_PATH, 'device')
     if not os.path.isdir(device_path):
         return
     for device in device_list:
         device_file_path = device_path + '/' + device
-        #parse vsite name, ip... from global config
+        # parse vsite name, ip... from global config
         parse_vsite_config_from_configfile(device_file_path, device)
-        #parse all vsite config from 'show running all', and write to vsite file
+        # parse all vsite config from 'show running all', and write to vsite file
         parse_vsite_from_configfile(device_file_path, device)
     return
 
+
 def write_diff_file(task_name, check_result, result_file_path, task_type, username):
     if task_type == 'interval' and os.path.exists(result_file_path):
         try:
\ No newline at end of file
@@ -797,24 +858,27 @@
 
     create_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
     db = DB.get_connected_db()
-    insert_sql = "INSERT INTO file_list(name, create_time, modify_time, type) values('%s', '%s', '%s', '%s') " % (os.path.basename(result_file_path), create_time, create_time, 'diff')
+    insert_sql = "INSERT INTO file_list(name, create_time, modify_time, type) values('%s', '%s', '%s', '%s') " % (
+    os.path.basename(result_file_path), create_time, create_time, 'diff')
     delete_sql = "DELETE FROM file_list where name='%s' " % os.path.basename(result_file_path)
     db.execute_sql(delete_sql)
     db.execute_sql(insert_sql)
     db.close()
 
-    update_job_result_field(task_name, json.dumps({"config_check":{os.path.basename(result_file_path):"diff"}}), username)
+    update_job_result_field(task_name, json.dumps({"config_check": {os.path.basename(result_file_path): "diff"}}),
+                            username)
 
     mark_expire_all(get_model('cm', ['configuration', 'config_file', 'ConfigFile']))
     return True
 
+
 def config_compliance_check_all(task_name, check_info, file_name, file_type, result_file_path, task_type, username):
     add_description(task_name, "The execution time: ")
     update_job_state(task_name, 'ongoing')
     mark_expire_all(get_model('cm', ['tasking', 'Tasks']))
 
     create_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
-    diff_result = {create_time:{}}
+    diff_result = {create_time: {}}
     check_result = diff_result[create_time]
 
     device_name = check_info['device']
\ No newline at end of file
@@ -829,9 +893,9 @@
         except IOError:
             config_failed("Get global config from AMP falied.", task_name)
             return
-        #For vpn customize config, just diff with show runnning all
-        running_params = json.dumps({'cmd':'show running all'})
-        new_url = modify_url('/rest/device_type/cli_extend', device_info['type'])    
+        # For vpn customize config, just diff with show runnning all
+        running_params = json.dumps({'cmd': 'show running all'})
+        new_url = modify_url('/rest/device_type/cli_extend', device_info['type'])
 
         check_result[device_name]["device_ip"] = device_info["ip_address"]
         content = send_command_to_device(device_info, new_url, running_params)
\ No newline at end of file
@@ -842,16 +906,16 @@
         d = difflib.unified_diff(sorted(file_content), sorted(content.splitlines()))
         d_list = '\n'.join(list(d))
 
-        check_result[device_name]['global'] = {"add":[], "delete":[]}
+        check_result[device_name]['global'] = {"add": [], "delete": []}
         parse_diff_config(d_list, check_result[device_name]['global'])
     else:
-        running_params = json.dumps({'cmd':'show running'})
+        running_params = json.dumps({'cmd': 'show running'})
         new_url = modify_url('/rest/device_type/cli_extend', device_info['type'])
 
         if device_info['type'].lower() in VPN_TYPE_LIST:
-            running_params = json.dumps({'config':'show running'})
+            running_params = json.dumps({'config': 'show running'})
             new_url = modify_url('/rest/device_type/batch_cli', device_info['type'])
-            #get global config from AMP
+            # get global config from AMP
             file_path = DEFAULT_CONFIG_FILE_PATH + file_type + '/' + file_name
             try:
                 file_global = open(file_path + '/ca.conf', 'r')
\ No newline at end of file
@@ -860,7 +924,7 @@
                 return
             file_content = file_global.read().splitlines()
             file_global.close()
-        elif device_info['type'].lower() in ADC_TYPE_LIST + WAF_TYPE_LIST: 
+        elif device_info['type'].lower() in ADC_TYPE_LIST + WAF_TYPE_LIST:
             file_path = DEFAULT_CONFIG_FILE_PATH + file_type + '/' + file_name
             try:
                 with open(file_path, 'r') as f:
\ No newline at end of file
@@ -889,11 +953,11 @@
         d = difflib.unified_diff(sorted(file_content), sorted(content.splitlines()))
         d_list = '\n'.join(list(d))
 
-        check_result[device_name]['global'] = {"add":[], "delete":[]}
+        check_result[device_name]['global'] = {"add": [], "delete": []}
         parse_diff_config(d_list, check_result[device_name]['global'])
 
         if device_info['type'].lower() in VPN_TYPE_LIST:
-            #get vsite config from AMP and device
+            # get vsite config from AMP and device
             db = DB.get_connected_db()
             fetchall_sql = "SELECT * FROM tar_file where name='%s'" % file_name
             data = db.fetchall(fetchall_sql)
\ No newline at end of file
@@ -912,7 +976,7 @@
                 file_vsite.close()
 
                 if device_info['protocol'] == 'restapi':
-                    running_params = json.dumps({'config':'show running', 'vsite_name':item['vsite_name']})
+                    running_params = json.dumps({'config': 'show running', 'vsite_name': item['vsite_name']})
                     new_url = '/rest/%s/global/batch_cli' % get_device_type(device_info['type'])
                     content = send_command_to_device(device_info, new_url, running_params)
                 else:
\ No newline at end of file
@@ -927,9 +991,9 @@
                 d = difflib.unified_diff(sorted(file_content), sorted(content.splitlines()))
                 d_list = '\n'.join(list(d))
 
-                check_result[device_name][item['vsite_name']] = {"add":[], "delete":[]}
+                check_result[device_name][item['vsite_name']] = {"add": [], "delete": []}
                 parse_diff_config(d_list, check_result[device_name][item['vsite_name']])
-        
+
     if task_type == 'interval':
         return write_diff_file(task_name, diff_result, result_file_path, task_type, username)
     else:
\ No newline at end of file
@@ -944,6 +1008,7 @@
     mark_expire_all(get_model('cm', ['configuration', 'config_file', 'ConfigFile']))
     send_notification('success', 'Task <%s> has done.' % task_name, model='config')
 
+
 def config_compliance_check_custom(task_name, check_info, options, file_type, diff_file_path, task_type, username):
     add_description(task_name, "The execution time: ")
     update_job_state(task_name, 'ongoing')
\ No newline at end of file
@@ -951,7 +1016,7 @@
 
     check_name = options["source_device"] + "-" + options["target_device"]
     create_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
-    diff_result = {create_time:{}}
+    diff_result = {create_time: {}}
     check_result = diff_result[create_time]
     check_result[check_name] = {}
 
\ No newline at end of file
@@ -964,7 +1029,8 @@
         with open(file_path, 'r') as f:
             source_config_content = f.read().splitlines()
     except IOError:
-        check_result[check_name]["error_msg"] = "Failed to read the source device <%s> config." % options["source_device"]
+        check_result[check_name]["error_msg"] = "Failed to read the source device <%s> config." % options[
+            "source_device"]
     # query the target device config.
     try:
         if options["target_config"].endswith(".all_cfg_tar"):
\ No newline at end of file
@@ -974,7 +1040,8 @@
         with open(file_path, 'r') as f:
             target_config_content = f.read().splitlines()
     except IOError:
-        check_result[check_name]["error_msg"] = "Failed to read the target device <%s> config." % options["target_device"]
+        check_result[check_name]["error_msg"] = "Failed to read the target device <%s> config." % options[
+            "target_device"]
 
     # diff viste if have same name viste.
     if options["source_config"].endswith(".all_cfg_tar") and options["target_config"].endswith(".all_cfg_tar"):
\ No newline at end of file
@@ -1004,7 +1071,6 @@
     check_result[check_name]['global'] = {"add": [], "delete": [], "original": None}
     parse_diff_config(d_list, check_result[check_name]['global'])
 
-
     res = write_diff_file(task_name, diff_result, diff_file_path, task_type, username)
     if not res:
         return
\ No newline at end of file
@@ -1014,6 +1080,7 @@
     mark_expire_all(get_model('cm', ['tasking', 'Tasks']))
     mark_expire_all(get_model('cm', ['configuration', 'config_file', 'ConfigFile']))
     send_notification('success', 'Task <%s> has done.' % task_name, model='config')
+
 
 def config_summary_diff(task_name, target_list, task_name_list, diff_file_path, task_file_list, source, source_file):
     add_description(task_name, "The execution time: ")
\ No newline at end of file
@@ -1069,10 +1136,11 @@
         result_file.write(json.dumps(result))
 
     db = DB.get_connected_db()
-    insert_sql = "INSERT INTO file_list(name, create_time, modify_time, type) values('%s', '%s', '%s', '%s') " % (os.path.basename(diff_file_path), create_time, create_time, 'diff')
+    insert_sql = "INSERT INTO file_list(name, create_time, modify_time, type) values('%s', '%s', '%s', '%s') " % (
+    os.path.basename(diff_file_path), create_time, create_time, 'diff')
     db.execute_sql(insert_sql)
     db.close()
-    
+
     add_description(task_name, "Successful!")
     update_job_state(task_name, 'done')
     mark_expire_all(get_model('cm', ['tasking', 'Tasks']))
\ No newline at end of file
@@ -1080,21 +1148,19 @@
     send_notification('success', 'Task <%s> has done.' % task_name, model='config')
 
 
-
-    
-
 # module_splite_line = {
 #     "vpn" : {"start_line":"#VPN configuration\n", "end_line":"#aaa configuration\n", "cmd":"show vpn config"},
 #     "acl" : {"start_line":"#acl configuration\n", "end_line":"#url policy configuration\n", "cmd":"show acl config"},
 #     "role" : {"start_line":"#role configuration\n", "end_line":"#acl configuration\n", "cmd":"show role config"}
 # }
-def config_compliance_check(task_name, check_info, file_name, file_type, result_file_path, module_name, task_type, username):
+def config_compliance_check(task_name, check_info, file_name, file_type, result_file_path, module_name, task_type,
+                            username):
     add_description(task_name, "The execution time: ")
     update_job_state(task_name, 'ongoing')
     mark_expire_all(get_model('cm', ['tasking', 'Tasks']))
-  
+
     create_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
-    diff_result = {create_time:{}}
+    diff_result = {create_time: {}}
     check_result = diff_result[create_time]
 
     device = check_info['device']
\ No newline at end of file
@@ -1107,16 +1173,16 @@
         if check_info["is_global"]:
             vsite_name = 'global'
             config_path = "%s/ca.conf" % file_path
-            running_params = json.dumps({"config":"show running %s" % module_name})
+            running_params = json.dumps({"config": "show running %s" % module_name})
         else:
             vsite_name = check_info['vsite_name']
             config_path = '%s/vsites/%s/ca.conf.%s' % (file_path, vsite_name, vsite_name)
-            running_params = json.dumps({"config":"show running %s" % module_name, "vsite_name":vsite_name})
+            running_params = json.dumps({"config": "show running %s" % module_name, "vsite_name": vsite_name})
 
         if file_type == 'customize':
             config_path = file_path
-        #awk_config = "%s/%s" % (os.getcwd(), "cm/lib/get_conf_by_key.awk")
-        #cmd_value = "cat %s | awk -v key='%s' -f %s" % (config_path, module_name, awk_config)
+        # awk_config = "%s/%s" % (os.getcwd(), "cm/lib/get_conf_by_key.awk")
+        # cmd_value = "cat %s | awk -v key='%s' -f %s" % (config_path, module_name, awk_config)
         cmd_value = "cat %s | awk -v key='%s' -f %s" % (config_path, module_name, CM_AWK_CONF)
 
         try:
\ No newline at end of file
@@ -1126,7 +1192,7 @@
             return
         module_config = cm_vsite_file.read().splitlines()
         cm_vsite_file.close()
-    elif device_info['type'].lower() in ADC_TYPE_LIST: 
+    elif device_info['type'].lower() in ADC_TYPE_LIST:
         file_path = DEFAULT_CONFIG_FILE_PATH + file_type + '/' + file_name
         cmd_value = "cat %s | awk -v key='%s' -f %s" % (file_path, module_name, CM_AWK_CONF)
 
\ No newline at end of file
@@ -1137,10 +1203,10 @@
             return
         module_config = cm_vsite_file.read().splitlines()
         cm_vsite_file.close()
-        running_params = json.dumps({"cmd":"show running %s" % module_name})
+        running_params = json.dumps({"cmd": "show running %s" % module_name})
         vsite_name = 'global'
 
-    #get vsite config from device
+    # get vsite config from device
     check_result[device]["device_ip"] = device_info["ip_address"]
 
     if device_info['protocol'] == 'restapi':
\ No newline at end of file
@@ -1150,7 +1216,8 @@
         if check_info["is_global"]:
             content = send_xmlrpc_to_device(device_info, new_url, "show running %s" % module_name)
         else:
-            content = send_xmlrpc_to_device(device_info, new_url, "sw %s\nshow running %s" % (check_info['vsite_name'], module_name))
+            content = send_xmlrpc_to_device(device_info, new_url,
+                                            "sw %s\nshow running %s" % (check_info['vsite_name'], module_name))
 
     if content == "failed":
         config_failed("Can not connect the device.", task_name)
\ No newline at end of file
@@ -1164,7 +1231,7 @@
     d = difflib.unified_diff(sorted(module_config), sorted(content.splitlines()))
     d_list = '\n'.join(list(d))
 
-    check_result[device][vsite_name] = {"add":[], "delete":[]}
+    check_result[device][vsite_name] = {"add": [], "delete": []}
     parse_diff_config(d_list, check_result[device][vsite_name])
     if task_type == 'interval':
         return write_diff_file(task_name, diff_result, result_file_path, task_type, username)
\ No newline at end of file
@@ -1172,42 +1239,44 @@
         res = write_diff_file(task_name, diff_result, result_file_path, task_type, username)
         if not res:
             config_failed(res, task_name)
-            return 
+            return
     add_description(task_name, "Successful!")
     update_job_state(task_name, 'done')
     mark_expire_all(get_model('cm', ['tasking', 'Tasks']))
-    #mark_expire_all(get_model('cm', ['configuration', 'config_file', 'ConfigFile']))
+    # mark_expire_all(get_model('cm', ['configuration', 'config_file', 'ConfigFile']))
     send_notification('success', 'Task <%s> has done.' % task_name, model='config')
 
+
 def config_backup(task_name, device_info, comment, file_num=0):
-    #device_info, virtual_file_path, cli_url, check_config_url, real_file_path, cli_cmd
+    # device_info, virtual_file_path, cli_url, check_config_url, real_file_path, cli_cmd
     add_description(task_name, "The execution time: ")
     update_job_state(task_name, 'ongoing')
     mark_expire_all(get_model('cm', ['tasking', 'Tasks']))
-     
+
     try:
-        #refresh_device([device_info['name'],])
-        #device_path = os.path.join(DEFAULT_CONFIG_FILE_PATH, 'device')
-        #if not os.path.isdir(device_path):
+        # refresh_device([device_info['name'],])
+        # device_path = os.path.join(DEFAULT_CONFIG_FILE_PATH, 'device')
+        # if not os.path.isdir(device_path):
         #    os.makedirs(device_path)
-        
-        cli_url = modify_url('/rest/device_type/cli_extend', device_info['type'])    
+
+        cli_url = modify_url('/rest/device_type/cli_extend', device_info['type'])
         if device_info['type'].lower() in VPN_TYPE_LIST:
             uid = uuid.uuid1()
             cm = __import__("cm")
             reload(cm.conf)
             network_card = cm.conf.NETWORK_CARD
-            #get config file from device
+            # get config file from device
             try:
                 local_ip = get_ip_address(network_card)
             except Exception, e:
                 description = 'Local network settings are abnormal. Please configuare management interface by CLI "system management intertface <port_name>".'
                 config_failed(description, task_name)
                 return -1
-            
-            cli_cmd = {}    
+
+            cli_cmd = {}
             virtual_file_path = DEFAULT_CONFIG_FILE_PATH + 'backup/' + '%s.all_cfg_tar' % uid
-            real_file_name = device_info['name'] + '-' + datetime.datetime.now().strftime('%Y_%m_%d_%H:%M:%S') + '.all_cfg_tar'
+            real_file_name = device_info['name'] + '-' + datetime.datetime.now().strftime(
+                '%Y_%m_%d_%H:%M:%S') + '.all_cfg_tar'
             real_file_path = DEFAULT_CONFIG_FILE_PATH + 'backup/' + real_file_name
             cli_cmd['export_config'] = 'config export all tftp %s %s' % (local_ip, uid)
             cli_cmd['write_config'] = 'write file all %s' % uid
\ No newline at end of file
@@ -1220,29 +1289,30 @@
                 config_failed(ret, task_name)
                 return -1
             # copy config to device path
-            #sys_cmd = "rm -rf %s/%s && cp -r %s %s/%s" %(device_path, device_info['name'], real_file_path[0:-12], device_path, device_info['name'])
-            #os.popen(sys_cmd).read()
-            #parse_vsite_config_from_configfile('%s/%s/ca.conf' %(device_path, device_info['name']), device_info['name'])
-        elif device_info['type'].lower() in ADC_TYPE_LIST + WAF_TYPE_LIST:  
+            # sys_cmd = "rm -rf %s/%s && cp -r %s %s/%s" %(device_path, device_info['name'], real_file_path[0:-12], device_path, device_info['name'])
+            # os.popen(sys_cmd).read()
+            # parse_vsite_config_from_configfile('%s/%s/ca.conf' %(device_path, device_info['name']), device_info['name'])
+        elif device_info['type'].lower() in ADC_TYPE_LIST + WAF_TYPE_LIST:
             real_file_name = device_info['name'] + '-' + datetime.datetime.now().strftime('%Y_%m_%d_%H:%M:%S') + '.cfg'
             back_file_dir = DEFAULT_CONFIG_FILE_PATH + 'backup/'
             if not os.path.exists(back_file_dir):
                 os.makedirs(back_file_dir)
-            real_file_path = back_file_dir + real_file_name   
+            real_file_path = back_file_dir + real_file_name
             bu = Backup_APV(device_info, real_file_path, cli_url, 'show running')
-        
+
             ret = bu.run()
             if ret:
                 config_failed(ret, task_name)
                 return -1
             # copy config to device path
-            #sys_cmd = "rm -rf %s/%s && cp %s %s/%s" %(device_path, device_info['name'], real_file_path[0:-4], device_path, device_info['name'])
-            #os.popen(sys_cmd).read()
+            # sys_cmd = "rm -rf %s/%s && cp %s %s/%s" %(device_path, device_info['name'], real_file_path[0:-4], device_path, device_info['name'])
+            # os.popen(sys_cmd).read()
 
         db = DB.get_connected_db()
         create_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
         # for backup config file, use 'modify_time' to save device name
-        insert_sql = "INSERT INTO file_list(name, create_time, modify_time, type, comment, device_type) values('%s', '%s', '%s', '%s', '%s', '%s')" % (real_file_name, create_time, device_info['name'], 'backup', comment, device_info['type'])
+        insert_sql = "INSERT INTO file_list(name, create_time, modify_time, type, comment, device_type) values('%s', '%s', '%s', '%s', '%s', '%s')" % (
+        real_file_name, create_time, device_info['name'], 'backup', comment, device_info['type'])
         db.execute_sql(insert_sql)
         db.close()
 
\ No newline at end of file
@@ -1252,16 +1322,17 @@
         mark_expire_all(get_model('cm', ['configuration', 'config_file', 'ConfigFile']))
         send_notification('success', 'Task <%s> has done.' % task_name, model='config')
     except Exception, e:
-        config_failed("Internal Error: %s" %e, task_name)
+        config_failed("Internal Error: %s" % e, task_name)
         return -1
     return 0
 
+
 def get_recent_file(device_name):
     db = DB.get_connected_db()
     fetchall_sql = "SELECT name FROM file_list where type='backup' and name like '%" + device_name + "%'"
     data = db.fetchall(fetchall_sql)
-    key = ['name',]
-    backup_list = [dict(zip(key, each)) for each in data]   
+    key = ['name', ]
+    backup_list = [dict(zip(key, each)) for each in data]
     db.close()
 
     device_backup_list = []
\ No newline at end of file
@@ -1275,6 +1346,7 @@
 
     return sorted(device_backup_list)[-1]
 
+
 def _reorder_show_slb_summary(msg):
     config_list = msg.split('\n')
     vs_list = []
\ No newline at end of file
@@ -1309,8 +1381,9 @@
             if cf not in policy_list:
                 policy_list.append(cf)
             continue
-    return "\n".join(vs_list+vlink_list+rs_list+group_list+member_list+policy_list)
+    return "\n".join(vs_list + vlink_list + rs_list + group_list + member_list + policy_list)
 
+
 def clone_apv_vs(pk_list, file_name, comment, options):
     device_info = get_rest_info_from_device(pk_list['name'])[0]
     vs_name = options["vs"]
\ No newline at end of file
@@ -1319,12 +1392,14 @@
     if not os.path.isdir(file_path):
         os.makedirs(file_path)
 
-    data = {"cmd" : 'show slb summary "%s"' % vs_name}
+    data = {"cmd": 'show slb summary "%s"' % vs_name}
     new_url = '/rest/%s/cli_extend' % get_device_type(device_info['type'].lower())
     running_params = json.dumps(data)
 
     try:
-        rest_response_data = send_https_rest_request('POST', new_url, running_params, device_info['ip_address'], device_info['restapi_port'], device_info['restapi_username'], device_info['restapi_password'])
+        rest_response_data = send_https_rest_request('POST', new_url, running_params, device_info['ip_address'],
+                                                     device_info['restapi_port'], device_info['restapi_username'],
+                                                     device_info['restapi_password'])
     except:
         return "Can not connect device!"
 
\ No newline at end of file
@@ -1339,7 +1414,8 @@
 
     with open(dest_file_path, 'w') as f:
         f.write(config_result)
-    insert_sql = "INSERT INTO file_list(name, create_time, modify_time, type, comment, device_type) values('%s', '%s', '%s', '%s', '%s', '%s')" % (dest_file_name, create_time, pk_list['name'], 'vs', comment, device_info['type'])
+    insert_sql = "INSERT INTO file_list(name, create_time, modify_time, type, comment, device_type) values('%s', '%s', '%s', '%s', '%s', '%s')" % (
+    dest_file_name, create_time, pk_list['name'], 'vs', comment, device_info['type'])
     delete_sql = "DELETE FROM file_list where name='%s' " % file_name
     db = DB.get_connected_db()
     db.execute_sql(delete_sql)
\ No newline at end of file
@@ -1347,6 +1423,7 @@
     db.close()
     return ""
 
+
 def clone_apv(pk_list, file_name, comment, device_type=None):
     file_path = os.path.join(DEFAULT_CONFIG_FILE_PATH, 'system')
     dest_file_name = file_name + '.cfg'
\ No newline at end of file
@@ -1356,17 +1433,20 @@
     if pk_list['file_type'] == 'device':
         # in amp 2.1, will clone the last backup config file.
         # in amp 3.0, change to clone the device running config.
-        #src_file_name = get_recent_file(pk_list['name'])
-        #src_file_path = DEFAULT_CONFIG_FILE_PATH + 'backup/' + src_file_name
+        # src_file_name = get_recent_file(pk_list['name'])
+        # src_file_path = DEFAULT_CONFIG_FILE_PATH + 'backup/' + src_file_name
         device_info = get_rest_info_from_device(pk_list['name'])
-        #device_info [{'restapi_port': 9997, 'restapi_password': u'admin', 'ip_address': u'10.8.2.54', 'restapi_username': u'array', 'type': u'AG'}]
+        # device_info [{'restapi_port': 9997, 'restapi_password': u'admin', 'ip_address': u'10.8.2.54', 'restapi_username': u'array', 'type': u'AG'}]
         _type = get_device_type(device_info[0]['type'])
-        data = {'cmd' : 'show running'}
+        data = {'cmd': 'show running'}
         new_url = '/rest/%s/cli_extend' % _type
         running_params = json.dumps(data)
-        
+
         try:
-            rest_response_data = send_https_rest_request('POST', new_url, running_params, device_info[0]['ip_address'], device_info[0]['restapi_port'], device_info[0]['restapi_username'], device_info[0]['restapi_password'])
+            rest_response_data = send_https_rest_request('POST', new_url, running_params, device_info[0]['ip_address'],
+                                                         device_info[0]['restapi_port'],
+                                                         device_info[0]['restapi_username'],
+                                                         device_info[0]['restapi_password'])
         except:
             return "Send request to device failed."
         if rest_response_data['status'] == 200:
\ No newline at end of file
@@ -1384,16 +1464,18 @@
         src_file_path = DEFAULT_CONFIG_FILE_PATH + pk_list['file_type'] + '/' + src_file_name
         if not os.path.exists(src_file_path):
             return "The file[%s] is not found!" % src_file_path
-        
+
         shutil.copyfile(src_file_path, dest_file_path)
 
     create_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
-    insert_sql = "INSERT INTO file_list(name, create_time, modify_time, type, comment, device_type) values('%s', '%s', '%s', '%s', '%s', '%s')" % (dest_file_name, create_time, pk_list['name'], 'system', comment, device_type)
+    insert_sql = "INSERT INTO file_list(name, create_time, modify_time, type, comment, device_type) values('%s', '%s', '%s', '%s', '%s', '%s')" % (
+    dest_file_name, create_time, pk_list['name'], 'system', comment, device_type)
     db = DB.get_connected_db()
     db.execute_sql(insert_sql)
     db.close()
     return ""
 
+
 def local_clone_vs(pk_list, file_name, comment, device_type=""):
     file_path = os.path.join(DEFAULT_CONFIG_FILE_PATH, 'vs')
     if not os.path.isdir(file_path):
\ No newline at end of file
@@ -1418,11 +1500,13 @@
     dest_file_name = file_name + '.cfg'
     dest_file_path = DEFAULT_CONFIG_FILE_PATH + 'vs/' + dest_file_name
     shutil.copyfile(src_file_path, dest_file_path)
-    insert_sql = "INSERT INTO file_list(name, create_time, modify_time, type, comment, device_type) values('%s', '%s', '%s', '%s', '%s', '%s')" % (dest_file_name, create_time, src_file_name, 'vs', comment, device_type)
+    insert_sql = "INSERT INTO file_list(name, create_time, modify_time, type, comment, device_type) values('%s', '%s', '%s', '%s', '%s', '%s')" % (
+    dest_file_name, create_time, src_file_name, 'vs', comment, device_type)
     db.execute_sql(insert_sql)
     db.close()
     return ""
 
+
 def clone_ag(pk_list, file_name, comment, options):
     file_type = pk_list["file_type"]
     db = DB.get_connected_db()
\ No newline at end of file
@@ -1434,14 +1518,14 @@
         cm = __import__("cm")
         reload(cm.conf)
         network_card = cm.conf.NETWORK_CARD
-        #get config file from device
+        # get config file from device
         try:
             local_ip = get_ip_address(network_card)
         except Exception, e:
             return 'Local network settings are abnormal. Please configuare management interface by CLI "system management intertface <port_name>".'
         device_info = get_rest_info_from_device(pk_list['name'])[0]
         cli_url = modify_url('/rest/device_type/cli_extend', device_info['type'])
-        cli_cmd = {}    
+        cli_cmd = {}
         virtual_file_path = DEFAULT_CONFIG_FILE_PATH + 'backup/' + '%s.all_cfg_tar' % uid
         cli_cmd['export_config'] = 'config export all tftp %s %s' % (local_ip, uid)
         cli_cmd['write_config'] = 'write file all %s' % uid
\ No newline at end of file
@@ -1451,7 +1535,7 @@
         bu = Backup_AG(device_info, virtual_file_path, cli_url, check_config_url, target_all_path, cli_cmd)
         ret = bu.run()
         if ret:
-            return ret 
+            return ret
     else:
         clone_file_path = os.path.join(DEFAULT_CONFIG_FILE_PATH, pk_list['file_type'])
         clone_file_name = pk_list['name']
\ No newline at end of file
@@ -1461,7 +1545,7 @@
             return "The file[%s] is not found!" % clone_file_name
 
         create_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
-        
+
         target_all_path = target_path + '.all_cfg_tar'
         file_path = os.path.join(DEFAULT_CONFIG_FILE_PATH, 'system')
         if not os.path.isdir(file_path):
\ No newline at end of file
@@ -1469,14 +1553,14 @@
         sys_cmd = "cp -r %s/%s %s" % (clone_file_path, clone_file_name, target_path)
         os.popen(sys_cmd).read()
         tar = TarFiles()
-        ret = tar.create_tar(target_path, file_name+".all_cfg_tar")
+        ret = tar.create_tar(target_path, file_name + ".all_cfg_tar")
         if ret:
-            return "create tar failed, errno %d" % ret 
+            return "create tar failed, errno %d" % ret
 
     vsite_list = []
     for root, dir, files in os.walk(target_path):
         for file in files:
-            fullpath=os.path.join(root, file)
+            fullpath = os.path.join(root, file)
             path_list = fullpath.split('/')
             if 'vsites' in path_list and path_list[path_list.index('vsites') + 1] not in vsite_list:
                 vsite_list.append(path_list[path_list.index('vsites') + 1])
\ No newline at end of file
@@ -1486,12 +1570,14 @@
         insert_sql = "INSERT INTO tar_file(name, vsite_name) values('%s', '%s')" % (file_name, vsite)
         db.execute_sql(insert_sql)
     create_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
-    insert_sql = "INSERT INTO file_list(name, create_time, modify_time, type, comment, device_type) values('%s', '%s', '%s', '%s', '%s', '%s')" % (file_name + '.all_cfg_tar', create_time, pk_list['name'], 'system', comment, pk_list["device_type"])
+    insert_sql = "INSERT INTO file_list(name, create_time, modify_time, type, comment, device_type) values('%s', '%s', '%s', '%s', '%s', '%s')" % (
+    file_name + '.all_cfg_tar', create_time, pk_list['name'], 'system', comment, pk_list["device_type"])
     db.execute_sql(insert_sql)
     db.close()
     mark_expire_all(get_model('cm', ['configuration', 'config_file', 'TarFile']))
     return ""
 
+
 def config_clone(task_name, options):
     add_description(task_name, "The execution time: ")
     update_job_state(task_name, 'ongoing')
\ No newline at end of file
@@ -1503,9 +1589,9 @@
     device_type = pk_list["device_type"]
 
     if device_type.lower() in ADC_TYPE_LIST + WAF_TYPE_LIST:
-        new_file_name = file_name+'.cfg'
+        new_file_name = file_name + '.cfg'
     elif device_type.lower() in VPN_TYPE_LIST:
-        new_file_name = file_name+'.all_cfg_tar'
+        new_file_name = file_name + '.all_cfg_tar'
     else:
         new_file_name = file_name
     select_sql = "SELECT type from file_list where name='%s' " % new_file_name
\ No newline at end of file
@@ -1552,18 +1638,19 @@
     send_notification('success', 'Task <%s> has done.' % task_name, model='config')
     return
 
+
 def config_custom(task_name, config_content, device_type, comment):
     add_description(task_name, "The execution time: ")
     update_job_state(task_name, 'ongoing')
     mark_expire_all(get_model('cm', ['tasking', 'Tasks']))
-    
+
     real_file_name = task_name
     back_file_dir = DEFAULT_CONFIG_FILE_PATH + 'customize/'
     if not os.path.exists(back_file_dir):
         os.makedirs(back_file_dir)
     real_file_path = back_file_dir + real_file_name
     bu = Backup_Custom(config_content, task_name, real_file_path)
-    
+
     ret = bu.run()
     if ret:
         config_failed(ret, task_name)
\ No newline at end of file
@@ -1574,6 +1661,7 @@
     mark_expire_all(get_model('cm', ['configuration', 'config_file', 'ConfigFile']))
     send_notification('success', 'Task <%s> has done.' % task_name, model='config')
 
+
 def _check_xmlrpc_status(device_info):
     if device_info["protocol"] == "xmlrpc":
         return {"status": True, "protocol": "https", "port": device_info["restapi_port"]}
\ No newline at end of file
@@ -1581,7 +1669,7 @@
     _type = get_device_type(device_info['type'])
     cli_url = modify_url('/rest/device_type/cli_extend', _type)
 
-    data = {'cmd' : 'show xmlrpc'}
+    data = {'cmd': 'show xmlrpc'}
     xmlrpc_params = json.dumps(data)
 
     content = send_command_to_device(device_info, cli_url, xmlrpc_params)
\ No newline at end of file
@@ -1598,12 +1686,14 @@
         return {"status": True, "protocol": res[0]["protocol"], "port": res[1]["port"]}
     else:
         # The output of APV version before 10.4 is like 'XMLRPC is ON using port 9999'.
-        res = cli_parse(content, RegexParser('XMLRPC is (?P<status>ON|OFF) using (?P<protocol>http|https), port (?P<port>[0-9]+)', MATCHONE))
+        res = cli_parse(content, RegexParser(
+            'XMLRPC is (?P<status>ON|OFF) using (?P<protocol>http|https), port (?P<port>[0-9]+)', MATCHONE))
         if res and res["status"] == "ON":
             return {"status": True, "protocol": res["protocol"], "port": res["port"]}
         else:
             return {"status": False}
 
+
 def apply_config_backup(device_name, device_info, description):
     date = datetime.datetime.now() + datetime.timedelta(seconds=1)
 
\ No newline at end of file
@@ -1617,7 +1707,8 @@
     new_schedule['args'] = [backup_task_name, device_info, description]
     new_schedule_str = json.dumps(new_schedule)
 
-    add_job_into_database(backup_task_name, 'config_backup', description, 'date', 'waiting', date.strftime('%Y-%m-%d %H:%M:%S'), new_schedule_str, device_list=device_name)
+    add_job_into_database(backup_task_name, 'config_backup', description, 'date', 'waiting',
+                          date.strftime('%Y-%m-%d %H:%M:%S'), new_schedule_str, device_list=device_name)
 
     res = config_backup(backup_task_name, device_info, 'Backup configuration before applying.')
 
\ No newline at end of file
@@ -1625,6 +1716,7 @@
 
     return res
 
+
 def config_recover(task_name, device_info, file_type, file_name, backup=False, device_name=None):
     add_description(task_name, "The execution time: ")
     update_job_state(task_name, 'ongoing')
\ No newline at end of file
@@ -1668,8 +1760,9 @@
             cli_cmd['remove_config'] = 'no config %s \nYES' % (uid)
             check_config_url = '/rest/%s/global/admintools/config_mgmt/SavedAllFile' % _type
             batch_cli_url = '/rest/%s/global/batch_cli' % _type
-            rcv = Recover_AG(real_file_path, virtual_file_path, device_info, cli_cmd, cli_url, check_config_url, batch_cli_url, xmlprc_status["status"])
-    elif device_info['type'].lower() in ADC_TYPE_LIST + WAF_TYPE_LIST: 
+            rcv = Recover_AG(real_file_path, virtual_file_path, device_info, cli_cmd, cli_url, check_config_url,
+                             batch_cli_url, xmlprc_status["status"])
+    elif device_info['type'].lower() in ADC_TYPE_LIST + WAF_TYPE_LIST:
         if xmlprc_status["status"]:
             batch_cli_url = '/cgi-bin/xmlrpc_server'
             device_info["xmlrpc_port"] = xmlprc_status["port"]
\ No newline at end of file
@@ -1702,9 +1795,12 @@
         apply_config_backup(device_name, device_info, 'Backup configuration after applying.')
     return
 
+
 def config_init_device(task_name, device_info, file_type, file_name, backup=False, device_name=None):
     config_recover(task_name, device_info, file_type, file_name, backup, device_name)
     return
+
+
 '''
 end config jobs
 '''
\ No newline at end of file
@@ -1713,6 +1809,7 @@
 start sync
 '''
 
+
 def update_cm_role_status(status, pk_dict):
     ''' status: 0-new 1-success 2-failed
         pk_dict: {'table':<value>, 'pk_field':<value>}
\ No newline at end of file
@@ -1731,6 +1828,8 @@
     db.close()
     mark_expire_all(get_model('cm', ['configuration', 'vsite_config', 'role', 'CMRoleGroup']))
     return
+
+
 def update_cm_role_devicelist(device_list, pk_dict, action, failed_device=None):
     table = pk_dict.pop('table')
     flag = 0
\ No newline at end of file
@@ -1745,7 +1844,7 @@
     db = DB.get_connected_db()
     select_sql = "SELECT device_list from %s where %s" % (table, condition)
     data = db.fetchall(select_sql)
-    result=[each[0] for each in data]
+    result = [each[0] for each in data]
     if result[0]:
         device = unicode(result[0], 'utf-8').split(',')
     else:
\ No newline at end of file
@@ -1765,25 +1864,29 @@
     mark_expire_all(get_model('cm', ['configuration', 'vsite_config', 'acl', 'CMACLResource']))
     mark_expire_all(get_model('cm', ['configuration', 'vsite_config', 'vpn', 'CMVPNResource']))
 
-    return    
+    return
+
+
 def remove_resource_device_list(device_list, role_name, database):
     db = DB.get_connected_db()
-    #select id, device_list from cm_vpn_resource where group_name=role_name
+    # select id, device_list from cm_vpn_resource where group_name=role_name
     select_sql = "SELECT id, device_list from %s where group_name='%s'" % (database, role_name)
     data = db.fetchall(select_sql)
     key = ['id', 'device_list']
     result = [dict(zip(key, each)) for each in data]
 
-    #get difference between device_list and device_list of database
+    # get difference between device_list and device_list of database
     for item in result:
         result_list = list(set(unicode(item['device_list'], 'utf-8').split(',')).difference(set(device_list)))
 
-        #update cm_vpn_resource set device_list where id=<>
+        # update cm_vpn_resource set device_list where id=<>
         update_sql = "UPDATE %s SET device_list='%s' where id=%d" % (database, ','.join(result_list), item['id'])
         db.execute_sql(update_sql)
     db.close()
 
     return
+
+
 # def sync_to_device(device_list, url, running_params, pk_dict, action, task_name=None):
 #     if task_name:
 #         add_description(task_name, "The execution time: ")
\ No newline at end of file
@@ -1854,9 +1957,12 @@
             if not device_info:
                 continue
             try:
-                rest_response_data = send_https_rest_request('POST', url, params['params'], device_info['ip_address'], device_info['restapi_port'], device_info['restapi_username'], device_info['restapi_password'])
+                rest_response_data = send_https_rest_request('POST', url, params['params'], device_info['ip_address'],
+                                                             device_info['restapi_port'],
+                                                             device_info['restapi_username'],
+                                                             device_info['restapi_password'])
             except Exception, e:
-                #description = 'Error, please check the ip address and make sure the device have already turned on the RESTful API service!'
+                # description = 'Error, please check the ip address and make sure the device have already turned on the RESTful API service!'
                 description += 'Can not connect the device %s[%s].\n' % (device, device_info['ip_address'])
             else:
                 if rest_response_data['status'] == 200:
\ No newline at end of file
@@ -1882,6 +1988,8 @@
 
     mark_expire_all(get_model('cm', ['tasking', 'Tasks']))
     return ''
+
+
 '''
 end sync
 '''
\ No newline at end of file
@@ -1889,29 +1997,32 @@
 '''
 start monitor_alert
 '''
+
+
 def get_monitor_info(monitor_id):
     db = DB.get_connected_db()
     select_sql = "select level, extend, interval from monitor where id=%d" % monitor_id
     data = db.fetchone(select_sql)
     db.close()
     result = {
-        "id" : monitor_id,
-        "level" : data[0],
-        "condition" : data[1]["condition"],
-        "message" : data[1]["message"],
-        "assign" : data[1]["assign"],
-        "type_id" : data[1]["type_id"],
-        "target_status": data[1]["target_status"] if "target_status" in data[1] else None,
-        "continued" : data[2]
-        }
+        "id": monitor_id,
+        "level": data[0],
+        "condition": data[1]["condition"],
+        "message": data[1]["message"],
+        "assign": data[1]["assign"],
+        "type_id": data[1]["type_id"],
+        "target_status": data[1]["target_status"] if "target_status" in data[1] else None,
+        "continued": data[2]
+    }
     return result
 
+
 def get_global_settings():
     global_settings = {}
     select_interval_sql = "select name, settings from global_settings where name='alert_interval';"
     select_inhibit_sql = "select name, settings from global_settings where name='alert_inhibit';"
     db = DB.get_connected_db()
-    data = db.fetchone(select_interval_sql) 
+    data = db.fetchone(select_interval_sql)
     if not data:
         global_settings["interval"] = 5
     else:
\ No newline at end of file
@@ -1923,9 +2034,10 @@
         global_settings["inhibit"] = int(data[1])
     db.close()
     return global_settings
-    
-#def monitor_alert(ip_list, monitor_id, condition, msg):
-#def monitor_alert(target_type, target_list, monitor_id, user_info):
+
+
+# def monitor_alert(ip_list, monitor_id, condition, msg):
+# def monitor_alert(target_type, target_list, monitor_id, user_info):
 def monitor_alert(target_type, target_list, monitor_id):
     monitor_info = get_monitor_info(monitor_id)
     global_settings = get_global_settings()
\ No newline at end of file
@@ -1933,34 +2045,36 @@
     if len(monitor_info['type_id']) == 1 and int(monitor_info['type_id'][0]) == 4000:
         decistion = SSLCertDecision()
     else:
-        decistion = Decision(target_type, target_list, monitor_info["continued"] * 60, global_settings["inhibit"] * 60, global_settings['interval'])
-        
+        decistion = Decision(target_type, target_list, monitor_info["continued"] * 60, global_settings["inhibit"] * 60,
+                             global_settings['interval'])
+
     dec_list = decistion.determine(monitor_info)
-    #decistion.alert_msg(dec_list, monitor_info, user_info)
+    # decistion.alert_msg(dec_list, monitor_info, user_info)
     if dec_list:
         decistion.alert_msg(dec_list, monitor_info)
 
     mark_expire_all(get_model('cm', ['monitor', 'MonitorMsg']))
 
+
 '''
 end monitor_alert
 '''
 
 Scheduler_func_list = {
-    'upload_job' : upload_job,
-    'upload_to_device' : upload_to_device,
-    'system_update_device' : system_update_device,
-    'waiting_for_device' : waiting_for_device,
-    'config_init_device' : config_init_device,
-    'config_batch_cli' : config_batch_cli,
-    'config_compliance_check' : config_compliance_check,
-    'config_compliance_check_all' : config_compliance_check_all,
-    'config_backup' : config_backup,
-    'config_recover' : config_recover,
-    'sync_to_device' : sync_to_device,
-    'monitor_alert' : monitor_alert,
+    'upload_job': upload_job,
+    'upload_to_device': upload_to_device,
+    'system_update_device': system_update_device,
+    'waiting_for_device': waiting_for_device,
+    'config_init_device': config_init_device,
+    'config_batch_cli': config_batch_cli,
+    'config_compliance_check': config_compliance_check,
+    'config_compliance_check_all': config_compliance_check_all,
+    'config_backup': config_backup,
+    'config_recover': config_recover,
+    'sync_to_device': sync_to_device,
+    'monitor_alert': monitor_alert,
     'config_custom': config_custom,
-    'config_clone' : config_clone,
+    'config_clone': config_clone,
     'config_compliance_check_custom': config_compliance_check_custom
 }
 
\ No newline at end of file
@@ -1971,9 +2085,10 @@
 from hive.model.query import mark_expire_one
 from hive.model.loading import get_model
 
+
 def set_device_version(device_id, version):
     db = DB.get_connected_db()
-    
+
     # Check if it's device2
     is_device2 = db.fetchone("SELECT id FROM device2 WHERE id='%s'" % device_id)
     if is_device2:
\ No newline at end of file
@@ -1988,15 +2103,16 @@
         update_sql = "UPDATE device SET version='%s' WHERE id='%s'" % (version, device_id)
         db.execute_sql(update_sql)
         try:
-             mark_expire_one(get_model('cm', ['device_mgmt', 'device', 'Device']), device_id)
+            mark_expire_one(get_model('cm', ['device_mgmt', 'device', 'Device']), device_id)
         except Exception, e:
-             logger.error('Failed to expire cache for Device %s: %s' % (device_id, str(e)))
-             
+            logger.error('Failed to expire cache for Device %s: %s' % (device_id, str(e)))
+
     db.close()
 
+
 def set_device_status(device_id, status):
     db = DB.get_connected_db()
-    
+
     is_device2 = db.fetchone("SELECT id FROM device2 WHERE id='%s'" % device_id)
     if is_device2:
         update_sql = "UPDATE device2 SET connection='%s' WHERE id='%s'" % (status, device_id)
\ No newline at end of file
@@ -2006,21 +2122,23 @@
         except Exception, e:
             logger.error('Failed to expire cache for CMDevice %s: %s' % (device_id, str(e)))
     else:
-         update_sql = "UPDATE device SET connection='%s' WHERE id='%s'" % (status, device_id)
-         db.execute_sql(update_sql)
-         try:
-             mark_expire_one(get_model('cm', ['device_mgmt', 'device', 'Device']), device_id)
-         except Exception, e:
-             logger.error('Failed to expire cache for Device %s: %s' % (device_id, str(e)))
+        update_sql = "UPDATE device SET connection='%s' WHERE id='%s'" % (status, device_id)
+        db.execute_sql(update_sql)
+        try:
+            mark_expire_one(get_model('cm', ['device_mgmt', 'device', 'Device']), device_id)
+        except Exception, e:
+            logger.error('Failed to expire cache for Device %s: %s' % (device_id, str(e)))
 
     db.close()
 
+
 def set_device_snmp(device_id, snmp_general):
     db = DB.get_connected_db()
     update_sql = "UPDATE device SET snmp_general='%s' WHERE id='%s'" % (snmp_general, device_id)
     db.execute_sql(update_sql)
     db.close()
 
+
 def check_one_device_status(data):
     # begin_time = time.time()
     failed_time = 3
\ No newline at end of file
@@ -2033,7 +2151,8 @@
                 break
             else:
                 failed_time -= 1
-                logger.error('Get device<%s>(%s) version failed. %s' % (data['name'], data['ip'], response_data['data']))
+                logger.error(
+                    'Get device<%s>(%s) version failed. %s' % (data['name'], data['ip'], response_data['data']))
                 time.sleep(2)
         else:
             try:
\ No newline at end of file
@@ -2042,9 +2161,12 @@
                 new_url = modify_url(generic_url, data['type'])
                 if not new_url:
                     # Fallback to apv if modify_url returns None (shouldn't happen for valid device types)
-                    logger.warning('Device<%s>(%s) type "%s" not recognized, falling back to APV URL' % (data['name'], data['ip'], data['type']))
+                    logger.warning('Device<%s>(%s) type "%s" not recognized, falling back to APV URL' % (
+                    data['name'], data['ip'], data['type']))
                     new_url = '/rest/apv/system/SystemInfo/version'
-                rest_response_data = send_https_rest_request('GET', new_url, '', data['ip'], data['restapi_port'], data['restapi_username'], data['restapi_password'], lock=False)
+                rest_response_data = send_https_rest_request('GET', new_url, '', data['ip'], data['restapi_port'],
+                                                             data['restapi_username'], data['restapi_password'],
+                                                             lock=False)
             except Exception, e:
                 failed_time -= 1
                 logger.error('Get device<%s>(%s) version failed. Exception: %s' % (data['name'], data['ip'], str(e)))
\ No newline at end of file
@@ -2052,7 +2174,8 @@
             else:
                 if rest_response_data['status'] == 200:
                     if data['connection'] != 'connected':
-                        logger.info('Device<%s>(%s) connection recovered. Setting status to connected.' % (data['name'], data['ip']))
+                        logger.info('Device<%s>(%s) connection recovered. Setting status to connected.' % (
+                        data['name'], data['ip']))
                         set_device_status(data['id'], 'connected')
 
                         # Re-register device if it is managed by license server (in device2 table)
\ No newline at end of file
@@ -2060,34 +2183,46 @@
                             check_db = DB.get_connected_db()
                             is_license_device = check_db.fetchone("SELECT id FROM device2 WHERE ip='%s'" % data['ip'])
                             check_db.close()
-                            
+
                             if is_license_device:
-                                logger.info('Device<%s>(%s) is a license managed device. Re-registering to restore activationserver config.' % (data['name'], data['ip']))
+                                logger.info(
+                                    'Device<%s>(%s) is a license managed device. Re-registering to restore activationserver config.' % (
+                                    data['name'], data['ip']))
                                 reg_url = '/rest/cm/Registration'
                                 reg_body = json.dumps({'port': 8888})
                                 # Use timeout=10 to match usage in some places or default
-                                reg_response = send_https_rest_request('POST', reg_url, reg_body, data['ip'], data['restapi_port'], data['restapi_username'], data['restapi_password'], lock=False)
-                                
+                                reg_response = send_https_rest_request('POST', reg_url, reg_body, data['ip'],
+                                                                       data['restapi_port'], data['restapi_username'],
+                                                                       data['restapi_password'], lock=False)
+
                                 if reg_response.get('status') == 200:
-                                    logger.info('Device<%s>(%s) re-registered successfully.' % (data['name'], data['ip']))
+                                    logger.info(
+                                        'Device<%s>(%s) re-registered successfully.' % (data['name'], data['ip']))
                                 else:
-                                    logger.error('Device<%s>(%s) re-registration failed. HTTP %s. Body: %s' % (data['name'], data['ip'], reg_response.get('status'), reg_response.get('body', '')[:100]))
+                                    logger.error('Device<%s>(%s) re-registration failed. HTTP %s. Body: %s' % (
+                                    data['name'], data['ip'], reg_response.get('status'),
+                                    reg_response.get('body', '')[:100]))
                         except Exception, e:
-                            logger.error('Device<%s>(%s) re-registration exception: %s' % (data['name'], data['ip'], str(e)))
+                            logger.error(
+                                'Device<%s>(%s) re-registration exception: %s' % (data['name'], data['ip'], str(e)))
 
                     try:
                         rest_body = json.loads(rest_response_data['body'])
                         set_device_version(data['id'], rest_body['SystemInfo']['version'])
                     except Exception, e:
-                        logger.error('Get device<%s>(%s) version failed. JSON parse error. Body: %s' % (data['name'], data['ip'], rest_response_data['body'][:100]))
+                        logger.error('Get device<%s>(%s) version failed. JSON parse error. Body: %s' % (
+                        data['name'], data['ip'], rest_response_data['body'][:100]))
                     break
                 else:
-                    logger.error('Get device<%s>(%s) version failed. HTTP Status: %s, Body: %s' % (data['name'], data['ip'], rest_response_data.get('status'), rest_response_data.get('body', '')[:100]))
+                    logger.error('Get device<%s>(%s) version failed. HTTP Status: %s, Body: %s' % (
+                    data['name'], data['ip'], rest_response_data.get('status'),
+                    rest_response_data.get('body', '')[:100]))
                     failed_time -= 1
                     time.sleep(2)
     if not failed_time:
         if data['connection'] != 'unconnected':
-            logger.warning('Device<%s>(%s) connection lost. Setting status to unconnected.' % (data['name'], data['ip']))
+            logger.warning(
+                'Device<%s>(%s) connection lost. Setting status to unconnected.' % (data['name'], data['ip']))
             set_device_status(data['id'], 'unconnected')
     else:
         if os.path.exists(os.path.join(extension_path, "monitoring")):
\ No newline at end of file
@@ -2123,7 +2258,9 @@
                 if data['type'].lower() in ADC_TYPE_LIST + WAF_TYPE_LIST:
                     try:
                         new_url = modify_url('/rest/device_type/admintools/snmp/SNMPGeneral/general', data['type'])
-                        rest_response_data = send_https_rest_request('GET', new_url, '', data['ip'], data['restapi_port'], data['restapi_username'], data['restapi_password'])
+                        rest_response_data = send_https_rest_request('GET', new_url, '', data['ip'],
+                                                                     data['restapi_port'], data['restapi_username'],
+                                                                     data['restapi_password'])
                     except Exception, e:
                         logger.error('Get device<%s>(%s) SNMPGeneral failed. %s' % (data['name'], data['ip'], str(e)))
                     else:
\ No newline at end of file
@@ -2143,9 +2280,10 @@
                                     if 'v3_user' in rest_body['SNMPGeneral']:
                                         for user in rest_body['SNMPGeneral']['v3_user']:
                                             if 'username' in user and 'auth_password' in user and 'level' in user:
-                                                v3user = user['username']+' '+user['auth_password']+' '+user['level']
+                                                v3user = user['username'] + ' ' + user['auth_password'] + ' ' + user[
+                                                    'level']
                                                 if 'priv_password' in user:
-                                                    v3user += ' '+user['priv_password']
+                                                    v3user += ' ' + user['priv_password']
                                                 snmp_general['v3user'] = v3user
                                             break
                                     set_device_snmp(data['id'], json.dumps(snmp_general))
\ No newline at end of file
@@ -2158,7 +2296,9 @@
                     snmp_general = {}
                     try:
                         new_url = modify_url('/rest/device_type/admintools/snmp/SNMPGeneral/snmp_version', data['type'])
-                        rest_response_data = send_https_rest_request('GET', new_url, '', data['ip'], data['restapi_port'], data['restapi_username'], data['restapi_password'])
+                        rest_response_data = send_https_rest_request('GET', new_url, '', data['ip'],
+                                                                     data['restapi_port'], data['restapi_username'],
+                                                                     data['restapi_password'])
                     except Exception, e:
                         logger.error('Get device<%s>(%s) SNMP version failed. %s' % (data['name'], data['ip'], str(e)))
                     else:
\ No newline at end of file
@@ -2169,11 +2309,14 @@
                                     if 'snmp_version' in rest_body['SNMPGeneral']:
                                         snmp_general['snmp_version'] = rest_body['SNMPGeneral']['snmp_version']
                             except Exception, e:
-                                logger.error('Set device<%s>(%s) snmp version failed. %s' % (data['name'], data['ip'], str(e)))
+                                logger.error(
+                                    'Set device<%s>(%s) snmp version failed. %s' % (data['name'], data['ip'], str(e)))
 
                     try:
                         new_url = modify_url('/rest/device_type/admintools/snmp/SNMPGeneral/snmp_enable', data['type'])
-                        rest_response_data = send_https_rest_request('GET', new_url, '', data['ip'], data['restapi_port'], data['restapi_username'], data['restapi_password'])
+                        rest_response_data = send_https_rest_request('GET', new_url, '', data['ip'],
+                                                                     data['restapi_port'], data['restapi_username'],
+                                                                     data['restapi_password'])
                     except Exception, e:
                         logger.error('Get device<%s>(%s) SNMP enable failed. %s' % (data['name'], data['ip'], str(e)))
                     else:
\ No newline at end of file
@@ -2184,13 +2327,17 @@
                                     if 'snmp_enable' in rest_body['SNMPGeneral']:
                                         snmp_general['snmp_enable'] = rest_body['SNMPGeneral']['snmp_enable']
                             except Exception, e:
-                                logger.error('Set device<%s>(%s) snmp enable failed. %s' % (data['name'], data['ip'], str(e)))
+                                logger.error(
+                                    'Set device<%s>(%s) snmp enable failed. %s' % (data['name'], data['ip'], str(e)))
 
                     try:
                         new_url = modify_url('/rest/device_type/admintools/snmp/SNMPGeneral/community', data['type'])
-                        rest_response_data = send_https_rest_request('GET', new_url, '', data['ip'], data['restapi_port'], data['restapi_username'], data['restapi_password'])
+                        rest_response_data = send_https_rest_request('GET', new_url, '', data['ip'],
+                                                                     data['restapi_port'], data['restapi_username'],
+                                                                     data['restapi_password'])
                     except Exception, e:
-                        logger.error('Get device<%s>(%s) SNMP community failed. %s' % (data['name'], data['ip'], str(e)))
+                        logger.error(
+                            'Get device<%s>(%s) SNMP community failed. %s' % (data['name'], data['ip'], str(e)))
                     else:
                         if rest_response_data['status'] == 200:
                             rest_body = json.loads(rest_response_data['body'])
\ No newline at end of file
@@ -2199,10 +2346,13 @@
                                     if 'community' in rest_body['SNMPGeneral']:
                                         snmp_general['community'] = rest_body['SNMPGeneral']['community']
                             except Exception, e:
-                                logger.error('Set device<%s>(%s) snmp community failed. %s' % (data['name'], data['ip'], str(e)))
+                                logger.error(
+                                    'Set device<%s>(%s) snmp community failed. %s' % (data['name'], data['ip'], str(e)))
                     try:
                         new_url = modify_url('/rest/device_type/admintools/snmp/SNMPGeneral/v3_user', data['type'])
-                        rest_response_data = send_https_rest_request('GET', new_url, '', data['ip'], data['restapi_port'], data['restapi_username'], data['restapi_password'])
+                        rest_response_data = send_https_rest_request('GET', new_url, '', data['ip'],
+                                                                     data['restapi_port'], data['restapi_username'],
+                                                                     data['restapi_password'])
                     except Exception, e:
                         logger.error('Get device<%s>(%s) SNMP v3_user failed. %s' % (data['name'], data['ip'], str(e)))
                     else:
\ No newline at end of file
@@ -2213,26 +2363,29 @@
                                     if 'v3_user' in rest_body['SNMPGeneral']:
                                         for user in rest_body['SNMPGeneral']['v3_user']:
                                             if 'username' in user and 'auth_password' in user and 'level' in user:
-                                                v3user = user['username']+' '+user['auth_password']+' '+user['level']
+                                                v3user = user['username'] + ' ' + user['auth_password'] + ' ' + user[
+                                                    'level']
                                                 if 'priv_password' in user:
-                                                    v3user += ' '+user['priv_password']
+                                                    v3user += ' ' + user['priv_password']
                                                 snmp_general['v3user'] = v3user
                                             break
                             except Exception, e:
-                                logger.error('Set device<%s>(%s) snmp v3_user failed. %s' % (data['name'], data['ip'], str(e)))
+                                logger.error(
+                                    'Set device<%s>(%s) snmp v3_user failed. %s' % (data['name'], data['ip'], str(e)))
                     try:
                         set_device_snmp(data['id'], json.dumps(snmp_general))
                     except Exception, e:
                         logger.error('Set device<%s>(%s) snmp failed. %s' % (data['name'], data['ip'], str(e)))
 
+
 def check_device_status():
     db = None
     try:
         try:
-            db = DB.get_connected_db()        
+            db = DB.get_connected_db()
             select_sql = "SELECT id, name, ip_address, protocol, restapi_port, restapi_username, restapi_password, type, enable_password, connection FROM device"
             data = db.fetchall(select_sql)
-            
+
             # Also check device2 (License Managed Devices) - only active ones
             select_sql2 = "SELECT id, ip, restapi_protocol, restapi_port, restapi_username, restapi_password, connection FROM device2 WHERE status='active'"
             data2 = db.fetchall(select_sql2)
\ No newline at end of file
@@ -2242,24 +2395,29 @@
     except Exception, e:
         # select from db failed, mostly because avx extension is not installed.
         return
-    key = ['id', 'name', 'ip', 'protocol', 'restapi_port', 'restapi_username', 'restapi_password', 'type', 'enable_password', 'connection']
+    key = ['id', 'name', 'ip', 'protocol', 'restapi_port', 'restapi_username', 'restapi_password', 'type',
+           'enable_password', 'connection']
     result = [dict(zip(key, each)) for each in data]
-    
+
     # Process device2 data
     if data2:
         key2 = ['id', 'ip', 'protocol', 'restapi_port', 'restapi_username', 'restapi_password', 'connection']
         result2 = [dict(zip(key2, each)) for each in data2]
         for each in result2:
             each['name'] = each['ip']
-            each['type'] = 'Array APV' # Default type for license managed devices
+            each['type'] = 'Array APV'  # Default type for license managed devices
             each['enable_password'] = ''
             result.append(each)
 
     for each in result:
         device_check_pool.apply_async(check_one_device_status, (each,))
+
+
 '''
 end check device status
 '''
+
+
 def get_device_ip(device_list):
     db = DB.get_connected_db()
     device_ip = []
\ No newline at end of file
@@ -2273,18 +2431,21 @@
     db.close()
     return device_ip
 
+
 def set_host_version(host_id, version):
     db = DB.get_connected_db()
     update_sql = "UPDATE host SET version='%s' WHERE id='%s'" % (version, host_id)
     db.execute_sql(update_sql)
     db.close()
 
+
 def set_host_connection(host_id, connection):
     db = DB.get_connected_db()
     update_sql = "UPDATE host SET connection='%s' WHERE id='%s'" % (connection, host_id)
     db.execute_sql(update_sql)
     db.close()
 
+
 def set_host_sys_status(host_id, data):
     regex = re.compile(r'\\(?![/u"])')
     data['cpu_usage'] = json.loads(regex.sub(r"\\\\", data['cpu_usage']).replace("'", '"'))
\ No newline at end of file
@@ -2301,15 +2462,19 @@
     db.execute_sql(update_sql)
     db.close()
 
+
 def set_host_vm_number(host_id, number):
     db = DB.get_connected_db()
     update_sql = "UPDATE host SET vm_number=%s WHERE id='%s'" % (number, host_id)
     db.execute_sql(update_sql)
     db.close()
 
+
 def check_one_host_status(data):
     try:
-        rest_response_data = call_restapi('GET', '/rest/avx/system/SystemInfo/version', '', data['ip'], data['restapi_port'], data['restapi_username'], data['restapi_password'], 'https')      
+        rest_response_data = call_restapi('GET', '/rest/avx/system/SystemInfo/version', '', data['ip'],
+                                          data['restapi_port'], data['restapi_username'], data['restapi_password'],
+                                          'https')
     except Exception, e:
         logger.error('Get host<%s> version failed. %s' % (data['ip'], str(e)))
         set_host_connection(data['id'], 'Unconnected')
\ No newline at end of file
@@ -2320,7 +2485,9 @@
             set_host_version(data['id'], json.loads(rest_response_data['body'])['SystemInfo']['version'])
             # get cpu/memory/disk data
             try:
-                rest_response_data = call_restapi('GET', '/rest/avx/system/SystemStatus/status', '', data['ip'], data['restapi_port'], data['restapi_username'], data['restapi_password'], 'https')
+                rest_response_data = call_restapi('GET', '/rest/avx/system/SystemStatus/status', '', data['ip'],
+                                                  data['restapi_port'], data['restapi_username'],
+                                                  data['restapi_password'], 'https')
             except Exception, e:
                 logger.error('Get host<%s> cpu/memory/disk usage failed. %s' % (data['ip'], str(e)))
                 mark_expire_all(get_model('cm', ['virtualization', 'Host']))
\ No newline at end of file
@@ -2328,9 +2495,11 @@
                 if rest_response_data['status'] == 200:
                     set_host_sys_status(data['id'], json.loads(rest_response_data['body'])['SystemStatus'])
                 mark_expire_all(get_model('cm', ['virtualization', 'Host']))
-            #get vm numbers
+            # get vm numbers
             try:
-                rest_response_data = call_restapi('GET', '/rest/avx/va/instance/VAInstance/_get_list_data', '', data['ip'], data['restapi_port'], data['restapi_username'], data['restapi_password'], 'https')
+                rest_response_data = call_restapi('GET', '/rest/avx/va/instance/VAInstance/_get_list_data', '',
+                                                  data['ip'], data['restapi_port'], data['restapi_username'],
+                                                  data['restapi_password'], 'https')
             except Exception, e:
                 logger.error('Get host<%s> vm numbers failed. %s' % (data['ip'], str(e)))
                 mark_expire_all(get_model('cm', ['virtualization', 'Host']))
\ No newline at end of file
@@ -2345,7 +2514,7 @@
     db = None
     try:
         try:
-            db = DB.get_connected_db()        
+            db = DB.get_connected_db()
             select_sql = "SELECT id, name, ip, restapi_port, restapi_username, restapi_password, connection FROM host"
             data = db.fetchall(select_sql)
         finally:
\ No newline at end of file
@@ -2367,7 +2536,7 @@
 class TaskScheduler(object):
     def __init__(self):
         self.data = {}
-        #start a task to check devices's connection every 600 seconds
+        # start a task to check devices's connection every 600 seconds
         sched = BackgroundScheduler()
         sched.add_job(check_host_status, 'interval', seconds=30)
         sched.add_job(check_device_status, 'interval', seconds=30)
\ No newline at end of file
@@ -2375,13 +2544,13 @@
 
         # check_device_status()
 
-        #init log
+        # init log
         self.init_log()
-        #when GenReportSchedules object init, we should restart the schedule by configuration in db
+        # when GenReportSchedules object init, we should restart the schedule by configuration in db
         db = DB.get_connected_db()
         sql = '''SELECT name, type, trigger, next_run_time, scheduler, state, custom_fields FROM task'''
         data = db.fetchall(sql)
-        #db.close()
+        # db.close()
         key = ['name', 'type', 'trigger', 'next_run_time', 'scheduler', 'state', 'custom_fields']
         result = [dict(zip(key, each)) for each in data]
         for item in result:
\ No newline at end of file
@@ -2393,17 +2562,23 @@
                     unit = item['next_run_time'].split()[1]
                     interval = int(item['next_run_time'].split()[0])
                     if unit == "seconds":
-                        sched.add_job(Scheduler_func_list[new_scheduler['func']], item['trigger'], seconds=interval, args=new_scheduler['args'], id=item['name'])
+                        sched.add_job(Scheduler_func_list[new_scheduler['func']], item['trigger'], seconds=interval,
+                                      args=new_scheduler['args'], id=item['name'])
                     elif unit == "minutes":
-                        sched.add_job(Scheduler_func_list[new_scheduler['func']], item['trigger'], minutes=interval, args=new_scheduler['args'], id=item['name'])
+                        sched.add_job(Scheduler_func_list[new_scheduler['func']], item['trigger'], minutes=interval,
+                                      args=new_scheduler['args'], id=item['name'])
                     elif unit == "hours":
-                        sched.add_job(Scheduler_func_list[new_scheduler['func']], item['trigger'], hours=interval, args=new_scheduler['args'], id=item['name'])
+                        sched.add_job(Scheduler_func_list[new_scheduler['func']], item['trigger'], hours=interval,
+                                      args=new_scheduler['args'], id=item['name'])
                     elif unit == "days":
-                        sched.add_job(Scheduler_func_list[new_scheduler['func']], item['trigger'], days=interval, args=new_scheduler['args'], id=item['name'])
+                        sched.add_job(Scheduler_func_list[new_scheduler['func']], item['trigger'], days=interval,
+                                      args=new_scheduler['args'], id=item['name'])
                     elif unit == "weeks":
-                        sched.add_job(Scheduler_func_list[new_scheduler['func']], item['trigger'], weeks=interval, args=new_scheduler['args'], id=item['name'])
+                        sched.add_job(Scheduler_func_list[new_scheduler['func']], item['trigger'], weeks=interval,
+                                      args=new_scheduler['args'], id=item['name'])
                 else:
-                    sched.add_job(Scheduler_func_list[new_scheduler['func']], item['trigger'], hours=int(item['next_run_time']), args=new_scheduler['args'], id=item['name'])
+                    sched.add_job(Scheduler_func_list[new_scheduler['func']], item['trigger'],
+                                  hours=int(item['next_run_time']), args=new_scheduler['args'], id=item['name'])
             else:
                 date = datetime.datetime.strptime(item['next_run_time'], '%Y-%m-%d %H:%M:%S')
                 now = datetime.datetime.now()
\ No newline at end of file
@@ -2413,7 +2588,8 @@
                     else:
                         run_time = now + datetime.timedelta(seconds=3)
                     sched = BackgroundScheduler()
-                    sched.add_job(Scheduler_func_list[new_scheduler['func']], item['trigger'], next_run_time=run_time, args=new_scheduler['args'], id=item['name'])                    
+                    sched.add_job(Scheduler_func_list[new_scheduler['func']], item['trigger'], next_run_time=run_time,
+                                  args=new_scheduler['args'], id=item['name'])
                     # if self.get_schedule(item['name']):
                     #     self.remove(item['name'])
                     self.add(item['name'], sched)
\ No newline at end of file
@@ -2428,7 +2604,9 @@
                         func_str = new_scheduler['func']
                         args_scheduler = new_scheduler['args']
                     sched = BackgroundScheduler()
-                    sched.add_job(Scheduler_func_list[func_str], item['trigger'], next_run_time=now + datetime.timedelta(seconds=3), args=args_scheduler, id=item['name'])
+                    sched.add_job(Scheduler_func_list[func_str], item['trigger'],
+                                  next_run_time=now + datetime.timedelta(seconds=3), args=args_scheduler,
+                                  id=item['name'])
                     self.add(item['name'], sched)
                 else:
                     continue
\ No newline at end of file
@@ -2442,28 +2620,27 @@
         logger.setLevel(logging.INFO)
         fmt = "%(asctime)s - %(name)s - %(filename)s[line:%(lineno)d] - %(levelname)s - %(message)s"
         log_format = logging.Formatter(fmt)
-        loghandler = logging.handlers.RotatingFileHandler('/var/log/schedule.log', maxBytes=1024 * 1024 * 100, backupCount=3)
+        loghandler = logging.handlers.RotatingFileHandler('/var/log/schedule.log', maxBytes=1024 * 1024 * 100,
+                                                          backupCount=3)
         loghandler.setFormatter(log_format)
         logger.addHandler(loghandler)
-        
 
     def get_schedule(self, title):
         if title in self.data:
-            return self.data[title] 
+            return self.data[title]
         return None
 
     def add(self, title, schedule):
         if title not in self.data:
             self.data[title] = schedule
-            #we should start the schedule after adding
+            # we should start the schedule after adding
             send_notification('info', 'Task <%s> is starting.' % title)
             schedule.add_listener(scheduler_error_listener, EVENT_JOB_ERROR)
             schedule.start()
-            
 
     def remove(self, title):
         if title in self.data:
-            #we should shutdown the schedule before removing
+            # we should shutdown the schedule before removing
             self.data[title].shutdown(wait=False)
             del self.data[title]
 
\ No newline at end of file
@@ -2473,4 +2650,4 @@
 
     def resume(self, title):
         if title in self.data:
-            self.data[title].resume()
+            self.data[title].resume()
\ No newline at end of file
Index: /branches/amp_3_7_2/src/webui/webui/htdocs/new/src/cm/models/configuration/update/__init__.py
===================================================================
--- /branches/amp_3_7_2/src/webui/webui/htdocs/new/src/cm/models/configuration/update/__init__.py	(revision 2934)
+++ /branches/amp_3_7_2/src/webui/webui/htdocs/new/src/cm/models/configuration/update/__init__.py	(working copy)
@@ -1,20 +1,29 @@
+import datetime
+import json
+import time
+
 from hive.imports.model import *
 from django.utils.translation import ugettext_lazy as _
-#from cm.lib.sqlite_db import DB
+# from cm.lib.sqlite_db import DB
 from cm.lib.postgres_db import DB
 from djproject.settings import FILE_UPLOAD_DIR
 import hashlib, time
 import os, shutil, requests, datetime, json
-from cm.lib.task_scheduler import add_job_into_database, remove_job_from_database, get_file_md5, compare_md5, mv_file, upload_job, update_updatelist, upload_to_device
+from cm.lib.task_scheduler import add_job_into_database, remove_job_from_database, get_file_md5, compare_md5, mv_file, \
+    upload_job, update_updatelist, upload_to_device
 from cm.models.tasking import GLOBAL_TASK
 from apscheduler.schedulers.background import BackgroundScheduler
-__=_
+from hive.model.loading import get_model
+from hive.utils import andebug
 
+__ = _
+
+
 class Update(ANModel):
     id = IntegerField(primary_key=True, configurable=False)
     app_name = CharField(verbose_name='APP Name', length='1..32', primary_key=True)
     build_version = CharField(verbose_name='Build Version', length='1..64', primary_key=True)
-    #file_size = IntegerField(verbose_name='File Size', optional=True, configurable=False)
+    # file_size = IntegerField(verbose_name='File Size', optional=True, configurable=False)
     file_size = CharField(verbose_name='File Size', optional=True, configurable=False)
     md5_value = CharField(verbose_name='MD5', length='1..64', editable=True)
     download_link = CharField(verbose_name='Download Link', optional=True, length='1..128', configurable=False)
\ No newline at end of file
@@ -23,15 +32,15 @@
     class Meta:
         verbose_name = _('Upgrade')
         show_im_export_button = False
-        list_config_options={
-            'columns':[
-                {'name':'app_name'},
-                {'name':'build_version'},
-                {'name':'md5_value'},
-                {'name':'file_size'},
-                {'name':'download_link'},
-                {'name':'location'}
-                ]}
+        list_config_options = {
+            'columns': [
+                {'name': 'app_name'},
+                {'name': 'build_version'},
+                {'name': 'md5_value'},
+                {'name': 'file_size'},
+                {'name': 'download_link'},
+                {'name': 'location'}
+            ]}
 
     class Upload(Action):
         verbose_name = _('Build Upload')
\ No newline at end of file
@@ -42,10 +51,12 @@
         instance_mul = '1'
         option_fields = (
             UnionField(name='using', verbose_name='Using', optional=True, fields={
-                'local':ImportLocalFileField(verbose_name=_('Local File'), button_text=__('Choose Update File'), maxFileSize=500000000, acceptFileSuffix='^.*?\\.(array|click)$'),
-                'URL':ImportURLField(verbose_name=_('URL'))
+                'local': ImportLocalFileField(verbose_name=_('Local File'), button_text=__('Choose Update File'),
+                                              maxFileSize=500000000, acceptFileSuffix='^.*?\\.(array|click)$'),
+                'URL': ImportURLField(verbose_name=_('URL'))
             }),
         )
+
     class System_update(Action):
         verbose_name = _('Update')
         action_name = _('Update')
\ No newline at end of file
@@ -57,16 +68,18 @@
             # DynamicMultiEnumField(name='device', verbose_name='devices', 
             #     values=['__value:---Please Select---', '__field:cm.device_mgmt.device.Device.name']),
             UnionField(name='devices', verbose_name='devices', optional=True, fields={
-                    'group':DynamicMultiEnumField(verbose_name='device_group', values=['__value:---Please Select---', '__field:cm.device_mgmt.device_group.DeviceGroup.name']),
-                    'device':DynamicMultiEnumField(verbose_name='devices', values=['__value:---Please Select---', '__field:cm.device_mgmt.device.Device.name'])
-                }),
+                'group': DynamicMultiEnumField(verbose_name='device_group', values=['__value:---Please Select---',
+                                                                                    '__field:cm.device_mgmt.device_group.DeviceGroup.name']),
+                'device': DynamicMultiEnumField(verbose_name='devices', values=['__value:---Please Select---',
+                                                                                '__field:cm.device_mgmt.device.Device.name'])
+            }),
             EnumField(verbose_name='Option', default='immediate', name='option', values=(
                 ('immediate', 'Immediate'), ('deferred', 'Deferred'),
             )),
-            DateTimeField(verbose_name='Expire Time', name='expire_time', condition=ValueCondition('option', ['deferred'])),
+            DateTimeField(verbose_name='Expire Time', name='expire_time',
+                          condition=ValueCondition('option', ['deferred'])),
             BooleanField(verbose_name='Backup Database', name='backup_db', default=False, optional=True),
         )
-        
 
     class Manager(CLIManager):
         def _get_query_set(self):
\ No newline at end of file
@@ -80,13 +93,14 @@
                 file_size = item['file_size']
                 if file_size < 1024:
                     item['file_size'] = str(file_size)
-                elif file_size < 1024*1024:
-                    item['file_size'] = str(file_size/1024) + 'KB'
-                elif file_size < 1024*1024*1024:
-                    item['file_size'] = str(file_size/1024/1024) + 'MB'
+                elif file_size < 1024 * 1024:
+                    item['file_size'] = str(file_size / 1024) + 'KB'
+                elif file_size < 1024 * 1024 * 1024:
+                    item['file_size'] = str(file_size / 1024 / 1024) + 'MB'
                 else:
-                    item['file_size'] = str(file_size/1024/1024/1024) + 'GB'
+                    item['file_size'] = str(file_size / 1024 / 1024 / 1024) + 'GB'
             return QuerySet(self._model, result)
+
         def _update_md5_value(self, instance):
             data = instance.get_field_dict()
             update_sql = "UPDATE update_list SET md5_value='%s' WHERE id='%d'" % (data['md5_value'], data['id'])
\ No newline at end of file
@@ -99,10 +113,12 @@
             db.execute_sql(update_sql)
             db.close()
             return
+
         def _insert(self, instance):
             data = instance.get_field_dict()
             db = DB.get_connected_db()
-            save_sql = "INSERT INTO update_list(app_name, build_version, md5_value) values('%s', '%s', '%s')" % (data['app_name'], data['build_version'], data['md5_value'])
+            save_sql = "INSERT INTO update_list(app_name, build_version, md5_value) values('%s', '%s', '%s')" % (
+            data['app_name'], data['build_version'], data['md5_value'])
             count_sql = "select count(id) from update_list;"
             select_sql = "SELECT id FROM update_list WHERE md5_value='%s'" % (data['md5_value'])
 
\ No newline at end of file
@@ -118,12 +134,14 @@
             db.execute_sql(save_sql)
             db.close()
             return
+
         def _delete_instance(self, instance):
             data = instance.get_field_dict()
             db = DB.get_connected_db()
 
-            #check the status of upload
-            select_sql = "SELECT state FROM task where name='%s' and type='upload'" % (data['app_name']+'-'+data['build_version'])
+            # check the status of upload
+            select_sql = "SELECT state FROM task where name='%s' and type='upload'" % (
+                        data['app_name'] + '-' + data['build_version'])
             result = db.fetchall(select_sql)
             if result and (result[0][0] == 'ongoing' or result[0][0] == 'waiting'):
                 raise ModelQueryException(CLICmdError(__('This build is uploading.')))
\ No newline at end of file
@@ -138,6 +156,7 @@
 
             db.close()
             return
+
         def _perform_Upload(self, options):
             def update_download_link(id, url):
                 update_sql = "UPDATE update_list SET download_link='%s' WHERE id='%d'" % (url, id)
\ No newline at end of file
@@ -145,8 +164,9 @@
                 db.execute_sql(update_sql)
                 db.close()
                 mark_expire_all(Update)
-            #print 'action update ', options, options['using'].values()[0], options['__pk_list'][0]['id']
-            #print options['using']
+
+            # print 'action update ', options, options['using'].values()[0], options['__pk_list'][0]['id']
+            # print options['using']
             if 'local' in options['using']:
                 tmp_file = options['using'].values()[0]
                 tmp_file = FILE_UPLOAD_DIR + os.path.basename(tmp_file)
\ No newline at end of file
@@ -162,10 +182,11 @@
 
                 update_download_link(int(options['__pk_list'][0]['id']), tmp_url)
 
-                #name = 'upload-' + options['__pk_list'][0]['app_name'] + '-' + options['__pk_list'][0]['build_version']
-                date_now = datetime.datetime.now() #.strftime('%Y-%m-%d %H:%M:%S')
+                # name = 'upload-' + options['__pk_list'][0]['app_name'] + '-' + options['__pk_list'][0]['build_version']
+                date_now = datetime.datetime.now()  # .strftime('%Y-%m-%d %H:%M:%S')
                 date = date_now + datetime.timedelta(seconds=3)
-                name = options['__pk_list'][0]['app_name'] + '-' + options['__pk_list'][0]['build_version'] + '-' + date_now.strftime('%Y_%m_%d_%H:%M:%S')
+                name = options['__pk_list'][0]['app_name'] + '-' + options['__pk_list'][0][
+                    'build_version'] + '-' + date_now.strftime('%Y_%m_%d_%H:%M:%S')
                 if GLOBAL_TASK.get_schedule(name):
                     GLOBAL_TASK.remove(name)
 
\ No newline at end of file
@@ -176,14 +197,19 @@
                 new_schedule['func'] = 'upload_job'
                 new_schedule['args'] = [int(options['__pk_list'][0]['id']), name, tmp_url]
                 new_schedule_str = json.dumps(new_schedule)
-                add_job_into_database(name, 'upload', description, 'date', 'waiting', date.strftime('%Y-%m-%d %H:%M:%S'), new_schedule_str)
+                add_job_into_database(name, 'upload', description, 'date', 'waiting',
+                                      date.strftime('%Y-%m-%d %H:%M:%S'), new_schedule_str)
 
                 sched = BackgroundScheduler()
-                sched.add_job(upload_job, 'date', next_run_time=date, args=[int(options['__pk_list'][0]['id']), name, tmp_url], id=name)
+                sched.add_job(upload_job, 'date', next_run_time=date,
+                              args=[int(options['__pk_list'][0]['id']), name, tmp_url], id=name)
                 GLOBAL_TASK.add(name, sched)
                 mark_expire_all(get_model('cm', ['tasking', 'Tasks']))
             return
+
         def _perform_System_update(self, options):
+            import re
+
             def get_data_from_device(device_name):
                 db = DB.get_connected_db()
                 fetchall_sql = '''SELECT ip_address, restapi_port, restapi_username, restapi_password, type FROM device where name='%s' ''' % device_name
\ No newline at end of file
@@ -192,12 +218,19 @@
                 key = ['ip_address', 'restapi_port', 'restapi_username', 'restapi_password', 'type']
                 result = [dict(zip(key, each)) for each in data]
                 return result
+
+            def extract_mgmt_ip(cli_output):
+                match = re.search(r'ip address\s+"[^"]+"\s+(\d+\.\d+\.\d+\.\d+)', cli_output)
+                if match:
+                    return match.group(1)
+                return None
+
             pk_list = options['__pk_list'][0]
-            #print 'options', options
-            #options {u'__pk_list': [{u'build_version': u'9.4.0.138.7', u'app_name': u'AG', u'id': 1}],
+            # print 'options', options
+            # options {u'__pk_list': [{u'build_version': u'9.4.0.138.7', u'app_name': u'AG', u'id': 1}],
             # u'devices': {u'group': [u'BeiJing']}, u'option': u'immediate'}
             # u'devices': {u'device': [u'10.8.2.55', u'10.8.2.54']}
-            #devices = options['devices']
+            # devices = options['devices']
             device_list = []
             if 'group' in options['devices']:
                 group_list = options['devices'].values()[0]
\ No newline at end of file
@@ -213,11 +246,15 @@
                 db.close()
             elif 'device' in options['devices']:
                 device_list = options['devices'].values()[0]
-                
+
             for device in device_list:
                 time.sleep(1)
                 data_now = datetime.datetime.now()
-                task_name = pk_list['app_name'] + '-' + pk_list['build_version'] + '-' + data_now.strftime('%Y_%m_%d_%H:%M:%S')
+                task_name = (
+                        pk_list['app_name'] + '-' +
+                        pk_list['build_version'] + '-' +
+                        data_now.strftime('%Y_%m_%d_%H:%M:%S')
+                )
                 device_info = get_data_from_device(device)[0]
                 if not device_info:
                     continue
\ No newline at end of file
@@ -225,26 +262,36 @@
                     GLOBAL_TASK.remove(task_name)
 
                 remove_job_from_database(task_name)
-                #data_now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
-                
+                # data_now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
+
                 if options['option'] == 'immediate':
                     date = data_now + datetime.timedelta(seconds=3)
                 elif options['option'] == 'deferred':
-                    time_str = options['expire_time'][:19] #'2014-03-28T06:59:52'
+                    time_str = options['expire_time'][:19]  # '2014-03-28T06:59:52'
                     date = datetime.datetime.strptime(time_str, '%Y-%m-%dT%H:%M:%S')
 
-                if date < data_now: 
-                    raise ModelQueryException(CLICmdError(__('Time(%s) has passed, Now %s' % (date.strftime('%Y-%m-%d %H:%M:%S'), data_now.strftime('%Y-%m-%d %H:%M:%S')))))
-                
+                if date < data_now:
+                    raise ModelQueryException(CLICmdError(__('Time(%s) has passed, Now %s' % (
+                    date.strftime('%Y-%m-%d %H:%M:%S'), data_now.strftime('%Y-%m-%d %H:%M:%S')))))
+
                 new_schedule = {}
+                self.cli.set_enable()
+                result = self.cli.cmd('show ip address')
+                mgmt_ip_addr = extract_mgmt_ip(result)
                 description = " UPDATE create time: " + data_now.strftime('%Y-%m-%d %H:%M:%S') + '\n'
                 new_schedule['func'] = 'upload_to_device'
-                new_schedule['args'] = [int(pk_list['id']), task_name, device_info['ip_address'], device_info['restapi_port'], device_info['restapi_username'], device_info['restapi_password'], [device_info['type'],options['backup_db']], device]
+                new_schedule['args'] = [int(pk_list['id']), task_name, device_info['ip_address'],
+                                        device_info['restapi_port'], device_info['restapi_username'],
+                                        device_info['restapi_password'], [device_info['type'], options['backup_db']],
+                                        device, mgmt_ip_addr]
                 new_schedule_str = json.dumps(new_schedule)
-                add_job_into_database(task_name, 'update', description, 'date', 'waiting', date.strftime('%Y-%m-%d %H:%M:%S'), new_schedule_str, device_list=device)
+                add_job_into_database(task_name, 'update', description, 'date', 'waiting',
+                                      date.strftime('%Y-%m-%d %H:%M:%S'), new_schedule_str, device_list=device)
+                self.cli.set_enable()
+                reslt = self.cli.cmd('show ip address')
 
                 sched = BackgroundScheduler()
                 sched.add_job(upload_to_device, 'date', next_run_time=date, args=new_schedule['args'], id=task_name)
                 GLOBAL_TASK.add(task_name, sched)
                 mark_expire_all(get_model('cm', ['tasking', 'Tasks']))
-            return           
+            return
\ No newline at end of file
