From 640ff61422bc0ee3966941b93d9889ddbbd38d77 Mon Sep 17 00:00:00 2001 From: ckonstanski Date: Thu, 30 Jul 2026 18:17:14 -0600 Subject: more files --- tools/audit-vmb.sh | 4 + tools/check-images.sh | 12 ++ tools/collect-logs.py | 524 +++++++++++++++++++++++++++++++++++++++++++++ tools/diff.py | 37 ++++ tools/find-image-status.sh | 23 ++ tools/get-ss-kubeconfig.py | 92 ++++++++ 6 files changed, 692 insertions(+) create mode 100755 tools/audit-vmb.sh create mode 100755 tools/check-images.sh create mode 100644 tools/collect-logs.py create mode 100644 tools/diff.py create mode 100755 tools/find-image-status.sh create mode 100644 tools/get-ss-kubeconfig.py (limited to 'tools') diff --git a/tools/audit-vmb.sh b/tools/audit-vmb.sh new file mode 100755 index 0000000..f8439f3 --- /dev/null +++ b/tools/audit-vmb.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +site_name=$1 +grep $site_name /var/log/apache2/far-edge-ops-api_error_log | grep reportName diff --git a/tools/check-images.sh b/tools/check-images.sh new file mode 100755 index 0000000..130cc68 --- /dev/null +++ b/tools/check-images.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +site_ip=$1 +password=$2 +echo $site_ip +python3 collect-logs.py command imagetest $site_ip $password "system registry-image-tags vzw-adpf-cmp" +python3 collect-logs.py command imagetest $site_ip $password "system registry-image-tags vzw-adpf-dip" +python3 collect-logs.py command imagetest $site_ip $password "system registry-image-tags vzw-adpf-dmp" +python3 collect-logs.py command imagetest $site_ip $password "system registry-image-tags vzw-adpf-dpp" +python3 collect-logs.py command imagetest $site_ip $password "system registry-image-tags vzw-adpf-pmp" +python3 collect-logs.py command imagetest $site_ip $password "system registry-image-tags vzw-adpf-rmp" + diff --git a/tools/collect-logs.py b/tools/collect-logs.py new file mode 100644 index 0000000..8ec3ce5 --- /dev/null +++ b/tools/collect-logs.py @@ -0,0 +1,524 @@ +import datetime +import pexpect +import os +import logging +import sys +import tarfile +import os.path +import gzip +import psycopg2 + +class LogCollector: + + def __init__(self): + pass + self.logger = logging.getLogger() + + def get_connection(self): + conn = psycopg2.connect( + host="", + database="", + user="", + password="") + return conn + + def run_command_and_collect_output(self, site_name, site_ip, password, command): + #print(site_name) + #print(site_ip) + #print(password) + #print(command) + #conn = self.get_connection() + # create a cursor + #try: + # cur = conn.cursor() + + # execute a statement + # print('PostgreSQL database version:') + # cur.execute('SELECT version()') + + # display the PostgreSQL database server version + # db_version = cur.fetchone() + # print(db_version) + + # close the communication with the PostgreSQL + # cur.close() + #except (Exception, psycopg2.DatabaseError) as error: + # print(error) + #finally: + # if conn is not None: + # conn.close() + # print('Database connection closed.') + + source_dir = site_name + if not os.path.exists(source_dir): + os.makedirs(source_dir) + + ssh_prefix = 'ssh -o StrictHostKeyChecking=no -t sysadmin@' + site_ip + debugLog = site_name + "/cmd-output.log" + fp = open(debugLog, "a") + + parts = command.split(" ") + + which_cmd = ssh_prefix + " which " + parts[0] + cmds = [] + cmds.append(which_cmd) + all_lines = self.run_command_get_all_lines(cmds, password, self.logger) + raw_cmd = '' + for line in all_lines.split("\n"): + line = line.rstrip().lstrip() + if line and 'Connection' not in line: + raw_cmd = line + break + + #kubectl get ns --kubeconfig=/etc/kubernetes/admin.conf | grep wsbomagj | awk '{print $1}' | xargs kubectl delete ns --kubeconfig=/etc/kubernetes/admin.conf + + #cmd = ssh_prefix + " /usr/sbin/" + raw_cmd + raw_cmd = raw_cmd + " " + ' '.join(parts[1:]) + if 'system' in raw_cmd: + cmd = ssh_prefix + " source /etc/platform/openrc; " + raw_cmd + elif 'kubectl' in raw_cmd: + cmd = ssh_prefix + " " + raw_cmd + " --kubeconfig=/etc/kubernetes/admin.conf" + else: + cmd = ssh_prefix + " " + raw_cmd + print(cmd) + self._execute_cmd_and_save_op(cmd, raw_cmd, password, fp) + fp.close() + + def collect_samsung_logs(self, site_name, site_ip, password): + source_dir = site_name + if not os.path.exists(source_dir): + os.makedirs(source_dir) + + ssh_prefix = 'ssh -o StrictHostKeyChecking=no -t sysadmin@' + site_ip + debugLog = site_name + "/DebugLog" + fp = open(debugLog, "w") + + # 1. uptime + raw_cmd = " uptime " + uptime = ssh_prefix + raw_cmd + self._execute_cmd_and_save_op(uptime, raw_cmd, password, fp) + + # 2. ls -l /var/lib/systemd/coredump + raw_cmd = " ls -l /var/lib/systemd/coredump " + coredump_ls = ssh_prefix + raw_cmd + self._execute_cmd_and_save_op(coredump_ls, raw_cmd, password, fp) + + # 3. fm alarm-list + raw_cmd = " fm alarm-list" + fm_alarm_list = ssh_prefix + " source /etc/platform/openrc; " + raw_cmd + self._execute_cmd_and_save_op(fm_alarm_list, raw_cmd, password, fp) + + # 4. date + raw_cmd = " date " + date = ssh_prefix + raw_cmd + self._execute_cmd_and_save_op(date, raw_cmd, password, fp) + + # 5. build.info + raw_cmd = " cat /etc/build.info " + build_info = ssh_prefix + raw_cmd + self._execute_cmd_and_save_op(build_info, raw_cmd, password, fp) + + # 6. ip link + raw_cmd = " /usr/sbin/ip link " + ip_link = ssh_prefix + raw_cmd + self._execute_cmd_and_save_op(ip_link, raw_cmd, password, fp) + fp.close() + + # 7. pmond.log + pmond_log = '/var/log/pmond.log' + cmds = [] + cmd = 'scp -o StrictHostKeyChecking=no sysadmin@[' + site_ip + ']:' + pmond_log + " " + site_name + "/." + cmds.append(cmd) + self.run_commands_scp(cmds, password, self.logger) + + # 8. /var/log/kern.log + kern_logs_cmd = ssh_prefix + " ls /var/log/kern.log*" + cmds = [kern_logs_cmd] + kern_logs = self.run_command_get_all_lines(cmds, password, self.logger) + print(kern_logs) + for kern_log in kern_logs.split('\n'): + if 'Connection to' not in kern_log: + kern_log = kern_log.rstrip().lstrip() + kern_parts = kern_log.split("\t") + print("Kern parts:" + str(len(kern_parts))) + if len(kern_parts) == 1: + kern_parts = kern_log.split(" ") + for p in kern_parts: + if p != '' and p != '\n' and p != '\t' and 'Connection' not in p: + cmds = [] + print(p) + cmd = 'scp -o StrictHostKeyChecking=no sysadmin@[' + site_ip + ']:' + p + " " + site_name + "/." + print(cmd) + cmds.append(cmd) + self.run_commands_scp(cmds, password, self.logger) + + # 6. /var/log/user.log* + user_logs_cmd = ssh_prefix + " ls /var/log/user.log*" + cmds = [user_logs_cmd] + user_logs = self.run_command_get_all_lines(cmds, password, self.logger) + print(user_logs) + for user_log in user_logs.split('\n'): + if 'Connection to' not in user_log: + cmds = [] + user_log = user_log.rstrip().lstrip() + user_parts = user_log.split("\t") + print("User parts:" + str(len(user_parts))) + if len(user_parts) == 1: + user_parts = user_log.split(" ") + for p in user_parts: + if p != '' and p != '\n' and p != '\t' and 'Connection' not in p: + print(p) + cmd = 'scp -o StrictHostKeyChecking=no sysadmin@[' + site_ip + ']:' + p + " " + site_name + "/." + print(cmd) + cmds.append(cmd) + self.run_commands_scp(cmds, password, self.logger) + + # 7. /var/log/pods/*adpf* + pod_logs_cmd = ssh_prefix + " ls /var/log/pods/ | grep adpf" + cmds = [pod_logs_cmd] + pod_logs = self.run_command_get_all_lines(cmds, password, self.logger) + print(pod_logs) + for pod_log in pod_logs.split('\n'): + cmds = [] + if ((pod_log is not None) and (pod_log != '')): + cmd = 'scp -o StrictHostKeyChecking=no -r sysadmin@[' + site_ip + ']:/var/log/pods/' + pod_log + " " + site_name + "/." + cmds.append(cmd) + self.run_commands_scp(cmds, password, self.logger) + + # 8. /var/lib/systemd/coredump/* + core_dump_cmd = ssh_prefix + " ls /var/lib/systemd/coredump " + cmds = [core_dump_cmd] + core_dump_logs = self.run_command_get_all_lines(cmds, password, self.logger) + print(core_dump_logs) + #for core_dump_log in core_dump_logs.split('\n'): + cmds = [] + cmd = 'scp -o StrictHostKeyChecking=no -r sysadmin@[' + site_ip + ']:/var/lib/systemd/coredump/' + " " + site_name + "/." + cmds.append(cmd) + self.run_commands_scp(cmds, password, self.logger) + + # create tar ball + date = datetime.datetime.now().strftime("%b-%d-%Y") + tar_file_name = site_name + '-' + date + '.tar' + gzip_file_name = tar_file_name + '.gz' + os.system('tar -cvf ' + tar_file_name + ' ./' + site_name) + os.system('gzip ' + tar_file_name) + print(gzip_file_name + " created.") + print("Upload it to Samsung FTP site and send a note to Samsung team. FTP coordinates:") + #print(" url: https://zone.samsungtelecom.com/") + #print(" username:vzw_femto_ndbm_jumpbox") + #print(" password:S@msung123") + suser = "verizon_snap" + shost = "zone.samsungtelecom.com" + spasswd = "VZWSn@p786" + print(" ur: "+shost+"/ 65.169.250.6") + print(" username:"+suser) + print(" password:"+spasswd) + # sftp the file up to samsung + remotepath = '/VZW_4G_5G/Controller\ Logs' + p = pexpect.spawn('sftp %s@%s' %(suser,shost)) + p.logfile = sys.stdout.buffer + try: + p.expect('(?i)password:') + x = p.sendline(spasswd) + x = p.expect(['Permission denied','sftp>']) + if x == 0: + print ('Permission denied for password:') + print (spasswd) + p.kill(0) + else: + x = p.sendline('cd ' + remotepath) + x = p.expect('sftp>') + x = p.sendline('put ' + gzip_file_name) + x = p.expect('sftp>') + x = p.isalive() + x = p.close() + retval = p.exitstatus + except EOF: + print (str(p)) + print ('SFTP file transfer failed due to premature end of file.') + except TIMEOUT: + print (str(p)) + print ('SFTP file transfer failed due to timeout.') + + def _handle_collect(self, site_name, site_ip, password): + ssh_prefix = 'ssh -o StrictHostKeyChecking=no -t sysadmin@' + site_ip + collect_cmd = ssh_prefix + " collect all" + cmds = [] + cmds.append(collect_cmd) + all_lines = self._run_collect(cmds, password) + for line in all_lines.split("\n"): + if 'creating' in line: + parts = line.split(" ") + collect_file_location = parts[3] + print("Collect file location:" + collect_file_location) + + cmds = [] + cmd = 'scp -o StrictHostKeyChecking=no sysadmin@[' + site_ip + ']:' + collect_file_location + " " + site_name + "/." + cmds.append(cmd) + self.run_commands_scp(cmds, password, self.logger) + + def collect_windriver_logs_steps(self, site_name, site_ip, password): + source_dir = site_name + if not os.path.exists(source_dir): + os.makedirs(source_dir) + + # 1. collect + self._handle_collect(site_name, site_ip, password) + + # 2. network interface details + ssh_prefix = 'ssh -o StrictHostKeyChecking=no -t sysadmin@' + site_ip + port_list_cmd = ssh_prefix + " \"source /etc/platform/openrc; system host-port-list controller-0 | grep -v name | grep -v '+' | cut -d ' ' -f 4\"" + cmds = [] + cmds.append(port_list_cmd) + all_lines = self.run_command_get_all_lines(cmds, password, self.logger) + + for interface in all_lines.split("\n"): + if 'Connection' not in interface: + print("Interface..." + interface) + interface_file = site_name + "/" + interface + ".txt" + fp = open(interface_file, "a") + + raw_cmd = " /usr/sbin/ethtool -i " + interface + cmd1 = ssh_prefix + raw_cmd + print(cmd1) + self._execute_cmd_and_save_op(cmd1, raw_cmd, password, fp) + + raw_cmd = " /usr/sbin/ethtool -l " + interface + cmd1 = ssh_prefix + raw_cmd + self._execute_cmd_and_save_op(cmd1, raw_cmd, password, fp) + + raw_cmd = " /usr/sbin/ethtool -S " + interface + cmd1 = ssh_prefix + raw_cmd + self._execute_cmd_and_save_op(cmd1, raw_cmd, password, fp) + + fp.close() + + # 3. interrupts + interrupt_file = site_name + "/interrupts.txt" + fp = open(interrupt_file, "a") + raw_cmd = " cat /proc/interrupts" + cmd1 = ssh_prefix + raw_cmd + self._execute_cmd_and_save_op(cmd1, raw_cmd, password, fp) + fp.close() + + debugLog = site_name + "/wr-debug.log" + fp = open(debugLog, "w") + + # 4. date + raw_cmd = " date " + date = ssh_prefix + raw_cmd + self._execute_cmd_and_save_op(date, raw_cmd, password, fp) + + # 5. ipmitool + raw_cmd = " sudo ipmitool sel time get " + cmd1 = ssh_prefix + raw_cmd + print(cmd1) + self._execute_cmd_and_save_op(cmd1, raw_cmd, password, fp, additional_password=True) + + # 6. uptime + raw_cmd = " uptime " + uptime = ssh_prefix + raw_cmd + self._execute_cmd_and_save_op(uptime, raw_cmd, password, fp) + + # 7. host_if_list + raw_cmd = "system host-if-list controller-0" + host_if_list = ssh_prefix + " source /etc/platform/openrc; " + raw_cmd + self._execute_cmd_and_save_op(host_if_list, raw_cmd, password, fp) + + # 8. interface_network_list + raw_cmd = "system interface-network-list controller-0" + interface_network_list = ssh_prefix + " source /etc/platform/openrc; " + raw_cmd + self._execute_cmd_and_save_op(interface_network_list, raw_cmd, password, fp) + + # 9. fm alarm-list + raw_cmd = " fm alarm-list" + fm_alarm_list = ssh_prefix + " source /etc/platform/openrc; " + raw_cmd + self._execute_cmd_and_save_op(fm_alarm_list, raw_cmd, password, fp) + + # 5. fm event-list + #raw_cmd = " fm event-list" + #fm_event_list = ssh_prefix + " source /etc/platform/openrc; " + raw_cmd + #self._execute_cmd_and_save_op(fm_event_list, raw_cmd, password, fp) + + # 10. sw-patch query + raw_cmd = " /usr/sbin/sw-patch query" + sw_patch_query = ssh_prefix + " source /etc/platform/openrc; " + raw_cmd + self._execute_cmd_and_save_op(sw_patch_query, raw_cmd, password, fp) + fp.close() + + # 11. drive details + drive_list_cmd = ssh_prefix + " \"source /etc/platform/openrc; system host-disk-list controller-0 | grep \"/dev/disk\" | awk '{print $4}' \"" + cmds = [] + cmds.append(drive_list_cmd) + all_lines = self.run_command_get_all_lines(cmds, password, self.logger) + + if not os.path.exists(site_name + "/dev"): + os.makedirs(site_name + "/dev") + + for drive in all_lines.split("\n"): + if 'Connection' not in drive: + print("Drive..." + drive) + drive_file = site_name + drive + ".txt" + fp = open(drive_file, "w") + raw_cmd = " sudo smartctl -a " + drive + cmd1 = ssh_prefix + raw_cmd + print(cmd1) + self._execute_cmd_and_save_op(cmd1, raw_cmd, password, fp, additional_password=True) + fp.close() + + # create tar ball + tar_file_name = source_dir + '.tar' + gzip_file_name = tar_file_name + '.gz' + os.system('tar -cvf ' + tar_file_name + ' ./' + source_dir) + os.system('gzip ' + tar_file_name) + #with tarfile.open(tar_file_name, "w:gz") as tar: + # tar.add(source_dir, arcname=os.path.basename(source_dir)) + print(gzip_file_name + " created.") + + def collect_windriver_logs(self, site_name, site_ip, password): + source_dir = site_name + if not os.path.exists(source_dir): + os.makedirs(source_dir) + + ssh_prefix = 'ssh -o StrictHostKeyChecking=no -t sysadmin@' + site_ip + + # Running collect + cmds = [] + cmd = ssh_prefix + " collect" + cmds.append(cmd) + all_lines = self._run_collect(cmds, password) + for line in all_lines.split("\n"): + if 'creating' in line: + parts = line.split(" ") + collect_file_location = parts[3] + print("Collect file location:" + collect_file_location) + + def _execute_cmd_and_save_op(self, cmd, raw_cmd, password, fp, additional_password=False): + cmds = [] + #print(cmd) + cmds.append(cmd) + all_lines = self.run_command_get_all_lines(cmds, password, self.logger, additional_password=additional_password) + #print(all_lines) + fp.write(raw_cmd + ":\n") + fp.write("-------------------------\n") + for line in all_lines.split("\n"): + if 'Connection' not in line: + fp.write(line) + fp.write("\n") + fp.write("\n") + + def _run_collect(self, commands, host_password, cmd_timeout=None): + all_lines = [] + for command in commands: + print(command) + child = pexpect.spawn(command) + child.timeout=cmd_timeout + try: + child.expect_exact(['password: '], timeout=cmd_timeout) + child.sendline(host_password) + child.expect_exact(['[sudo] password for sysadmin:'], timeout=cmd_timeout) + child.sendline(host_password) + #child.sendline("\n") + line = child.readline() + print(line) + line = child.readline() + print(line) + all_lines = child.read() + all_lines = all_lines.rstrip().lstrip() + all_lines = all_lines.decode('utf-8').replace('\r\n', '\n') + print(all_lines) + except: + print(str(child)) + return all_lines + + def run_command_get_all_lines(self, commands, host_password, logger, block=False, timeout=None, additional_password=False): + #print("Inside _run_command_get_all_lines") + all_lines = [] + for command in commands: + print("Executing.." + command) + child = pexpect.spawn(command) + child.timeout=timeout + try: + i = child.expect(['password: ','Connection refused\r\r\n'], timeout=timeout) + if i == 0: + child.sendline(host_password) + if additional_password: + child.expect_exact("Password:") + child.sendline(host_password) + all_lines = child.read() + all_lines = all_lines.rstrip().lstrip() + all_lines = all_lines.decode('utf-8').replace('\r\n', '\n') + #logger.info(all_lines) + if i == 1: + print("Connection refused") + except: + print(str(child)) + return all_lines + + def run_commands_scp(self, commands, host_password, logger, block=False, timeout=None): + successful = False + #print("Inside run_command_scp") + #print(commands) + for command in commands: + print("Executing.." + command) + try: + child = pexpect.spawn(str(command)) + except: + print(str(child)) + child.timeout = None + try: + i = child.expect([b'', 'Are you .*(yes/no)? ', pexpect.EOF], timeout=None) + if i == 0: + child.sendline('') + if i == 1: + child.sendline('yes') + child.expect(['password: '], timeout=timeout) + child.sendline(host_password) + all_lines = child.read() + all_lines = all_lines.rstrip().lstrip() + all_lines = all_lines.decode('utf-8').replace('\r\n', '\n') + successful = True # Tentative + for line in all_lines.split("\n"): + #print(line) + if re.search('error', line, re.IGNORECASE): + successful = False + if re.search('unable', line, re.IGNORECASE): + successful = False + except: + pass + #print(str(child)) + #print("Status:" + str(successful)) + return successful + +if __name__ == '__main__': + + if len(sys.argv) < 5: + print("Samsung logs:") + print(" python collect-logs.py samsung ") + print("WindRiver logs:") + print(" python collect-logs.py windriver ") + print("One-off command o/p:") + print(" python collect-logs.py command \"\"") + print(" python3 collect-logs.py command longmeadow-oct8 2001:4888:2a10:e0ad:122:40a:0:f400 password \"ifconfig\"") + print(" python3 collect-logs.py command longmeadow-oct8 2001:4888:2a10:e0ad:122:40a:0:f400 password \"ip link\"") + exit(0) + + logs_for = sys.argv[1] + site_name = sys.argv[2] + site_ip = sys.argv[3] + password = sys.argv[4] + + print("Option chosen:" + logs_for) + print("Site:" + site_name) + print("Site IP:" + site_ip) + print("Site Password:" + password) + log_collector = LogCollector() + + if logs_for == 'samsung': + log_collector.collect_samsung_logs(site_name, site_ip, password) + elif logs_for == 'windriver': + log_collector.collect_windriver_logs_steps(site_name, site_ip, password) + else: + command = sys.argv[5] + #print(command) + log_collector.run_command_and_collect_output(site_name, site_ip, password, command) diff --git a/tools/diff.py b/tools/diff.py new file mode 100644 index 0000000..667010b --- /dev/null +++ b/tools/diff.py @@ -0,0 +1,37 @@ +import sys + +if len(sys.argv) < 3: + print("Find the items that are in file1 and not in file2") + print(" Usage:") + print(" python diff.py ") + exit(0) + +f1 = sys.argv[1] +f2 = sys.argv[2] + +f1_list = [] +f1_fp = open(f1, "r") +f1_lines = f1_fp.readlines() + +for line in f1_lines: + line1 = line.rstrip().lstrip() + f1_list.append(line1) + +f2_list = [] +f2_fp = open(f2, "r") +f2_lines = f2_fp.readlines() + +for line in f2_lines: + line1 = line.rstrip().lstrip() + f2_list.append(line1) + +diff = [] +for i in f1_list: + present = False + for j in f2_list: + if i == j: + present = True + if not present: + diff.append(i) + print(i) + diff --git a/tools/find-image-status.sh b/tools/find-image-status.sh new file mode 100755 index 0000000..6a71ab4 --- /dev/null +++ b/tools/find-image-status.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +if (( $# < 1 )); then + echo "./find-image-status " + exit 0 +fi + +site=$1 +printf "Cluster: $site\n" + +printf "Checking cmp..." +curl -k https://middleware2.vcpfe.vzwops.com/orchestration/remote-regions/$site/images/vzw-adpf-cmp/status +printf "\nChecking pmp..." +curl -k https://middleware2.vcpfe.vzwops.com/orchestration/remote-regions/$site/images/vzw-adpf-pmp/status +printf "\nChecking rmp..." +curl -k https://middleware2.vcpfe.vzwops.com/orchestration/remote-regions/$site/images/vzw-adpf-rmp/status +printf "\nChecking dmp..." +curl -k https://middleware2.vcpfe.vzwops.com/orchestration/remote-regions/$site/images/vzw-adpf-dmp/status +printf "\nChecking dpp..." +curl -k https://middleware2.vcpfe.vzwops.com/orchestration/remote-regions/$site/images/vzw-adpf-dpp/status +printf "\nChecking dip..." +curl -k https://middleware2.vcpfe.vzwops.com/orchestration/remote-regions/$site/images/vzw-adpf-dip/status +printf "\n" diff --git a/tools/get-ss-kubeconfig.py b/tools/get-ss-kubeconfig.py new file mode 100644 index 0000000..b04db8a --- /dev/null +++ b/tools/get-ss-kubeconfig.py @@ -0,0 +1,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 ") + 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") -- cgit v1.3