| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- #!/usr/bin/python3
- # --*-- coding:utf-8 --*--
- import requests, json, os, time
- import pymysql
- import time, datetime
- import logging
- import logging.handlers
- logger = logging.getLogger('/root/xcloudclean/app_info.log')
- logger.setLevel(logging.DEBUG)
- rf_handler = logging.handlers.TimedRotatingFileHandler('/root/xcloudclean/app_clear.log', when='midnight', interval=1, backupCount=7, atTime=datetime.time(0, 0, 0, 0))
- rf_handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s"))
- f_handler = logging.FileHandler('/root/xcloudclean/app_error.log')
- f_handler.setLevel(logging.ERROR)
- f_handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(filename)s[:%(lineno)d] - %(message)s"))
- logger.addHandler(rf_handler)
- logger.addHandler(f_handler)
- # logging.basicConfig(filename='./clear_info.log', level=logging.INFO)
- CLEAR_ID = []
- CLEAN_ERROR_ID = []
- CLEAN_SUCCESS_ID = []
- CLEAN_PENDING_ID = []
- STRATE_TIME = datetime.datetime.now()
- # clear_status : 1.清理完成 2.待清理 3.清理中 4.清理失败
- def get_data():
- conn1 = pymysql.connect(
- host='10.99.1.85',
- port=3306,
- user='gluttony',
- passwd='gluttony',
- db='awsregistry',
- charset='utf8mb4')
- cs1 = conn1.cursor()
- select_sql = "SELECT id FROM r_aws_account_info WHERE clear_status=2;"
- cs1.execute(select_sql)
- response = cs1.fetchall()
- cs1.close()
- conn1.close()
- yield response
- def updata_db(info_sql):
- conn2 = pymysql.connect(
- host='10.99.1.85',
- port=3306,
- user='gluttony',
- passwd='gluttony',
- db='awsregistry',
- charset='utf8mb4')
- cs2 = conn2.cursor()
- try:
- cs2.execute(info_sql)
- conn2.commit()
- logger.info(f" Exec_SQL_Syntax: {info_sql}")
- except Exception as e:
- conn2.rollback()
- logger.error(f" Exec_SQL_Syntax_ERROR: {e}\t,{info_sql}")
- finally:
- conn2.close()
- def send_email(error_id):
- import smtplib
- from email.mime.text import MIMEText
- from email.header import Header
- mail_host="smtp.qiye.163.com"
- mail_user="cloud@cstor.cn"
- mail_pass="Cstor0820"
- sender = 'cloud@cstor.cn'
- receivers = ['xclouds@qq.com','xiechao@cstor.cn',
- 'suzeyue@cstor.cn','taozhongyuan@cstor.cn','panhaonan@cstor.cn',
- 'xuchong@cstor.cn','2214395275@qq.com']
- # receivers = ['xclouds@qq.com']
- message = MIMEText('gluttony.cstor.cn Resource 清理异常......\n\n请及时处理。', 'plain', 'utf-8')
- message['From'] = Header("gluttony", 'utf-8')
- message['To'] = Header("平台部云计算项目组", 'utf-8')
- subject = f'AWS清理失败(ACCOUNT ID: {error_id})'
- message['Subject'] = Header(subject, 'utf-8')
- try:
- smtpObj = smtplib.SMTP()
- smtpObj.connect(mail_host, 25)
- smtpObj.login(mail_user,mail_pass)
- smtpObj.sendmail(sender, receivers, message.as_string())
- logger.info("邮件发送成功")
- except smtplib.SMTPException as e:
- logger.error(f"Error: 无法发送邮件\t {e}")
- def auth_post():
- url_auth = "http://localhost:8000/api/user/auth"
- auth_body = {"username": "admin", "password": "admin"}
- auth_headers = {
- "Content-Type": "application/json, charset=UTF-8"
- }
- auth_original = requests.request('post', url_auth, json=auth_body, headers=auth_headers)
- access_token_original = auth_original.json()
- access_token = access_token_original['access_token']
- return access_token
- def get_status():
- CLEAR_ID_orginal = get_data()
- for i in CLEAR_ID_orginal:
- id_list = list(i)
- for a in id_list:
- b = list(a)
- for c in b:
- CLEAR_ID.append(c)
- return CLEAR_ID
- def clear_post():
- url_clear = "http://localhost:8000/api/resource/clear/range"
- data_headers = {
- 'Content-Type': 'application/json',
- 'Authorization': 'Bearer ' + auth_post()
- }
- json_id = {'ID': CLEAR_ID}
- data_original = requests.post(url_clear, json=json_id, headers=data_headers)
- try:
- if "message" in data_original.text:
- raise TypeError(data_original.text)
- except TypeError as e:
- logger.error(f" ERROR {e}")
- logger.info(f" Clear ID = {json_id}")
- logger.info(f" HTTP Response {data_original.text}")
- # return data_original
- def init_post(init_id):
- init_headers = {'content-type': 'application/x-www-form-urlencoded'}
- init_url = "http://10.99.1.85:8081/account/init"
- params = {"account_ids":init_id}
- resp = requests.post(init_url, data=params, headers=init_headers)
- logger.info(f"init_id: {init_id}, Status:{resp.text}")
- def dbs_pending_status(sql_clear_id):
- id_tup = tuple(sql_clear_id)
- if len(id_tup) == 1:
- sql_old = f"UPDATE r_aws_account_info SET clear_status=3 WHERE clear_status=2 AND id IN {id_tup};"
- sql_exec = sql_old.replace(",", "")
- updata_db(sql_exec)
- else:
- sql_exec = f"UPDATE r_aws_account_info SET clear_status=3 WHERE clear_status=2 AND id IN {id_tup};"
- updata_db(sql_exec)
- def updata_db_pending(sql_clear_id):
- sql_old = f"UPDATE r_aws_account_info SET clear_status=3 WHERE clear_status=2 AND id IN ({sql_clear_id});"
- updata_db(sql_old)
- def updata_db_success(sql_clear_id):
- sql_old = f"UPDATE r_aws_account_info SET clear_status=1 WHERE clear_status=3 AND id IN ({sql_clear_id});"
- updata_db(sql_old)
- def updata_db_error(sql_clear_id):
- # id_tup = tuple(sql_clear_id)
- # if len(id_tup) == 1:
- sql_old = f"UPDATE r_aws_account_info SET clear_status=4 WHERE clear_status=3 AND id IN ({sql_clear_id});"
- # sql_exec = sql_old.replace(",", "")
- updata_db(sql_old)
- # else:
- # sql_exec = f"UPDATE r_aws_account_info SET clear_status=4 WHERE clear_status=3 AND id IN {id_tup};"
- # updata_db(sql_exec)
- CLEAN_STATUS_GET_HEADER = {
- 'Content-Type': 'application/json',
- 'Authorization': 'Bearer ' + auth_post()}
- def clean_status_get(r_id):
- status_url = f"http://127.0.0.1:8000/api/status/account/state/{r_id}"
- resp = requests.get(status_url,headers=CLEAN_STATUS_GET_HEADER)
- logger.info(f"Clean_Status:{resp.text}")
- if "SUCCESS" in json.loads(resp.text)["account_clean_status"]:
- # {"account_clean_status":"SUCCESS","id":"8","start_time":"2021-01-06 16:02:00.018024","state":"200"}
- CLEAN_SUCCESS_ID.append(r_id)
- updata_db_success(r_id)
- if r_id in CLEAN_PENDING_ID:
- CLEAN_PENDING_ID.remove(r_id)
- elif "PENDING" in json.loads(resp.text)["account_clean_status"]:
- # {"account_clean_status":"PENDING","id":"102","start_time":"2021-01-06 17:42:10.059127"}
- if r_id in CLEAN_PENDING_ID:
- pass
- else:
- CLEAN_PENDING_ID.append(r_id)
- else:
- CLEAN_ERROR_ID.append(r_id)
- updata_db_error(r_id)
- if r_id in CLEAN_PENDING_ID:
- CLEAN_PENDING_ID.remove(r_id)
- def while_pending_status(pending_id):
- while len(CLEAN_PENDING_ID) > 0:
- time.sleep(180)
- current_time = datetime.datetime.now()
- process_time = (current_time - STRATE_TIME).seconds
- if process_time < 2400:
- for i in pending_id:
- clean_status_get(i)
- elif process_time > 2400:
- for err_id in pending_id:
- CLEAN_ERROR_ID.append(err_id)
- CLEAN_PENDING_ID.remove(err_id)
-
- # updata_db_error(err_id)
- # return
- def start_now():
- global CLEAR_ID
- global CLEAN_ERROR_ID
- global CLEAN_SUCCESS_ID
- global CLEAN_PENDING_ID
- get_status()
- logger.info(f" Start: NOW")
- logger.info(f" Id: {CLEAR_ID}")
- if CLEAR_ID == []:
- logger.info("ID: None ")
- logger.info("None exit cleanapp")
- os._exit(0)
- else:
- clear_post()
- # clear_status : 1.清理完成 2.待清理 3.清理中 4.清理失败
- dbs_pending_status(CLEAR_ID) # status 2 --> 3
- time.sleep(600)
- for i in CLEAR_ID:
- clean_status_get(i)
- if CLEAN_PENDING_ID == [] and CLEAN_ERROR_ID == []:
- pass
- elif CLEAN_PENDING_ID != []:
- while_pending_status(CLEAN_PENDING_ID)
- elif CLEAN_ERROR_ID != []:
- send_email(CLEAN_ERROR_ID)
- for i in CLEAN_SUCCESS_ID:
- init_post(i)
- logger.info(f"clean SUCCESS ID: {CLEAN_SUCCESS_ID}")
- logger.info(f"clean PENDING ID: {CLEAN_PENDING_ID}")
- if CLEAN_ERROR_ID == []:
- pass
- else:
- logger.error(f"clean ERROR ID: {CLEAN_ERROR_ID}")
- logger.info("End: end")
- logger.info("\n")
- if __name__ == '__main__':
- start_now()ls
|