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') instanceIDs = {} metaClusters = {} envs = [ "nightly", "staging", "yinliang3", "ouyan", 'yunqing', 'nizheming', 'jyzdev10', 'hanlins', 'gweber', 'baoling2', 'louis2' ] 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" def find_leaked_clusters(): 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(): staglingClusters.append( { "environment": infraRunningClusters[id] , "cluster_id": id }) return staglingClusters