summaryrefslogtreecommitdiff
path: root/tools/get-ss-kubeconfig.py
blob: b04db8a5b5dff4f82bbf37191c493cd384e0997c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import json
import requests
import os
import os.path
import sys
import time
import psycopg2

class KubeconfigGetter():

    def _get_db_connection(self):
        conn = None
        try:
            conn = psycopg2.connect(
                host='xxxxx',
                port='5432',
                database="xxxxx",
                user="xxxxx",
                password='xxxxx')
        except Exception as e:
            print(e)
        return conn

    def get_cluster_name(self, fuze_spm_site_id):
        cluster_name = ''
        try:
            conn = self._get_db_connection()

            # create a cursor
            cur = conn.cursor()

            # execute a statement
            #print('select cluster_name, fuze_spm_site_id, fuze_spm_site_name, oam_vip_address from caas_cluster join caas_location on caas_cluster.location_id=caas_location.id where fuze_spm_site_id = ' + fuze_spm_site_id)
            sql = 'select cluster_name from caas_cluster join caas_location on caas_cluster.location_id=caas_location.id where fuze_spm_site_id=' + '\'' + fuze_spm_site_id + '\''
            cur.execute(sql)

            # display the PostgreSQL database server version
            cluster_name = cur.fetchone()[0]
            #print(cluster_name)

            # close the communication with the PostgreSQL
            cur.close()
        except (Exception, psycopg2.DatabaseError) as error:
            print(error)
            if conn is not None:
                conn.close()
                print('Database connection closed.')
        return str(cluster_name)

    def get_kubeconfig(self, site_name):
        #url = "http://192.168.31.20/orchestration/remote-regions/" + site_name + "/kubeconfig/vendor/samsung"
        url = "https://middleware.vcpfe.vzwops.com/orchestration/remote-regions/" + site_name + "/connection-details?team=samsung"
        url1 = "https://middleware.vcpfe.vzwops.com/orchestration/remote-regions/" + site_name + "/connection-details"
        resp = requests.get(url, verify=False)
        #print(resp.json())
        transaction_id = resp.json()['transaction_id']
        #print(transaction_id)
        url2 = url1 + "/" + transaction_id
        #print(url2)
        time.sleep(10)
        resp1 = requests.get(url2, verify=False)
        #print(resp1.json())
        kubeconfig = resp1.json()['remote_region_setup'][0]["kube_config"]
        #print(kubeconfig)
        if not os.path.exists("samsung-kubeconfigs"):
            os.makedirs("samsung-kubeconfigs")
        fp = open("samsung-kubeconfigs/" + site_name + ".conf", "w")
        fp.write(kubeconfig)
        fp.close()

if __name__ == '__main__':

    if len(sys.argv) < 2:
        print("python get-ss-kubeconfig.py <site-list-file>")
        print("  Make sure to populate database creds in this file.")
        print("  Use it from a server from which database is accessible (e.g.: app1 or app2)")
        exit(0)

    site_list_file = sys.argv[1]
    fp = open(site_list_file, "r")
    lines = fp.read()
    kubeconfigGetter = KubeconfigGetter()
    for fuze_site_id in lines.split("\n"):
        fuze_site_id = fuze_site_id.lstrip().rstrip()
        if fuze_site_id != '':
            #print(fuze_site_id)
            cluster_name = kubeconfigGetter.get_cluster_name(fuze_site_id)
            kubeconfigGetter.get_kubeconfig(cluster_name) 
    #fuze_site_id = sys.argv[1]
    #print(lines)
    #print(fuze_site_id + " " + cluster_name)
    print("Done")