import os import requests import pymysql import subprocess import time, datetime import json host = "tidb.e23bdccc.cb17aaf2.us-west-2.prod.aws.tidbcloud.com" passwd = os.getenv('passwd') webhookUrl = "https://open.feishu.cn/open-apis/bot/v2/hook/bf4efba8-16ae-438f-bafe-dee982923c6c" instanceIDs = {} metaClusters = {} def post_bot_webhook(clusters): post_infos = [] post_infos.append( { "is_short": False, "text": { "tag": "lark_md", "content": "" } }, ) post_infos.append( { "is_short": True, "text": { "tag": "lark_md", "content": "Stagling Clusters:\n {}".format(",\n".join( clusters )) } }, ) url = webhookUrl payload_message = { "timestamp": str(int(time.time())), "msg_type": "interactive", "card": { "i18n_elements": { "en_us": [ { "tag": "div", "text": { "tag": "lark_md", "content": "** Stagling Clusters ** {}\n".format( datetime.datetime.today().strftime("%Y-%m-%d")), }, "fields": post_infos, }, ] } } } headers = { 'Content-Type': 'application/json' } response = requests.request( "POST", url, headers=headers, data=json.dumps(payload_message)) print(response.text) def fetch_clusters( env ): if env == 'nightly': env = 'dev' dbName = env + '_central' db = pymysql.connect(host=host, port=4000, user='root', password=passwd, db=dbName, charset='utf8' ) cursor = db.cursor() cursor.execute("select id from clusters where status in ('launching', 'created'," "'initializing','preparing'," "'launching','normal','scaling'," "'upgrading','recovering') ") results = cursor.fetchall() for row in results: id = row[0] print("running", str(id) ) metaClusters[str(id)] = env infraRunningClusters = {} def fetch_staging_clusters_json(): cmd = "KUBECONFIG=staging-infra-apiserver /usr/local/bin/kubectl get clusters -A -o json" inputDomain = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) jsonS = json.dumps(inputDomain.communicate()[0].decode('utf-8')) return json.loads(jsonS) def fetch_dev_clusters_json(): cmd = "KUBECONFIG=dev-infra-apiserver /usr/local/bin/kubectl get clusters -A -o json" inputDomain = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) jsonS = json.dumps(inputDomain.communicate()[0].decode('utf-8')) return json.loads(jsonS) def process_infra_clusters(): rawData = fetch_dev_clusters_json() clusters = json.loads(rawData) for cluster in clusters['items']: if 'status' not in cluster: continue if 'phase' not in cluster['status']: continue status = cluster['status']['phase'] clusterKind = cluster['spec']['cluster']['tidb']['instanceType'] if 'freetier' in clusterKind: continue name = cluster['metadata']['name'] if status == 'Running': namespaceSep = str(cluster['metadata']['namespace']).split('-') infraRunningClusters[name] = namespaceSep[0] rawData = fetch_staging_clusters_json() clusters = json.loads(rawData) for cluster in clusters['items']: if 'status' not in cluster: continue if 'phase' not in cluster['status']: continue status = cluster['status']['phase'] clusterKind = cluster['spec']['cluster']['tidb']['instanceType'] if 'freetier' in clusterKind: continue name = cluster['metadata']['name'] if status == 'Running': infraRunningClusters[name] = "staging" envs = [ "nightly", "staging", "yinliang3", "ouyan", 'yunqing', 'nizheming', 'jyzdev10', 'hanlins', 'gweber', 'baoling2', 'louis2' ] def lambda_handler(event, context): process_infra_clusters() for env in envs: print(" process env {}".format( env ) ) try: fetch_clusters( env ) except: print("bypass env {}".format( env )) staglingClusters = [] print(infraRunningClusters) print("------------------") print(metaClusters) for id in infraRunningClusters.keys(): if id not in metaClusters.keys(): # print( clusters[id] ) found = False for env in envs: if env in infraRunningClusters[id]: found = True break if found: staglingClusters.append(infraRunningClusters[id] + "/" + id) post_bot_webhook(staglingClusters) return { 'statusCode': 200, 'body': json.dumps('Hello from Lambda!') } lambda_handler("", "")