from django.conf import settings import os import pexpect import re import time import json from .utils import * from .remoteregionhandler import RemoteRegionWorker class Vendor(RemoteRegionWorker): def __init__(self, logger, dbCoordinationQueue, vmbCoordinationQueue, doneQueue): self.logger = logger self.doneQueue = doneQueue self.dbCoordinationQueue = dbCoordinationQueue self.vmbCoordinationQueue = vmbCoordinationQueue self.logger.info(".. created Vendor handler") def perform_vendor_setup(self, data): self.logger.info("Checking if any vendor setup needs to be done") if 'namespace' in data: namespace = data['namespace'] region_oam_ip = data['region_oam_ip'] network_setup = data['network_setup'] vendor = self._get_vendor(namespace, self.logger) self.logger.info("Vendor:" + vendor) self.logger.info("Network setup:" + network_setup) self._perform_vendor_specific_actions(vendor, namespace, region_oam_ip, network_setup, self.logger) self.logger.info("Done configuring vendor related things on the remote region") else: self.logger.info("Vendor setup not needed for this call.") self._wait_and_done() def _wait_and_done(self): if self.dbCoordinationQueue != '' and self.vmbCoordinationQueue != '' and self.doneQueue != '': self.logger.info("About to be done..waiting for cleanup") db_coordination = self.dbCoordinationQueue.get() self.logger.info(" DB coordination message:" + db_coordination) vmb_coordination = self.vmbCoordinationQueue.get() self.logger.info(" VMB coordination message:" + vmb_coordination) self.doneQueue.put("Done") self.logger.info("Done") def _get_vendor(self, namespace, logger): logger.info(" Inside _get_vendor") parts = namespace.split('-') logger.info("parts:" + str(parts)) vendor = "unknown" if len(parts) >= 5: if parts[3] == "ss" or parts[4] == "ss": #vendor_shortform = parts[3] #if vendor_shortform == "ss": vendor = "Samsung" return vendor def _perform_vendor_specific_actions(self, vendor, namespace, region_oam_ip, network_setup, logger): logger.info(" Inside _perform_vendor_specific_actions...") if vendor == "Samsung": logger.info(" Handling Samsung...") samsung_provisioner = Samsung(logger) samsung_provisioner.setup(namespace, region_oam_ip, network_setup) def setup_network(self, region, logger): logger.info(" Setting up network...") remote_region_oam_ip = self._get_remote_region_oam_ip(region, logger) logger.info(" Received OAM IP: " + region + " " + remote_region_oam_ip) self.create_host_network(remote_region_oam_ip, logger) def check_site(self, region, logger): logger.info(" Checking network...") remote_region_oam_ip = self._get_remote_region_oam_ip(region, logger) host_username, host_password, ssh_prefix = get_host_connection_details(remote_region_oam_ip) cmds = [] cmds.append(ssh_prefix + " kubectl describe nodes controller-0 --kubeconfig=/etc/kubernetes/admin.conf ") output_lines = self._run_command_get_all_lines(cmds, host_password, logger, block=True) #logger.info("Returned output lines:") #logger.info(output_lines) new_op_lines = [] if len(output_lines) > 0: for line in output_lines.split("\n"): if 'Connection to' not in line: if 'Capacity:' in line or 'Allocatable:' in line or 'Allocated' in line or 'intel.com/pci_sriov_net' in line: logger.info("LINE:" + line) new_op_lines.append(line) namespaces = self.check_namespaces(remote_region_oam_ip, logger) #new_op_lines.append("----------") #for namespace_line in namespaces.split("\n"): # new_op_lines.append(namespace_line) namespace_secrets = self.check_namespace_secrets(region, remote_region_oam_ip, logger) namespace_service_accounts = self.check_namespace_serviceaccounts(region, remote_region_oam_ip, logger) online_status = self.check_online_status(region, remote_region_oam_ip, logger) return new_op_lines, namespaces, namespace_secrets, namespace_service_accounts, online_status class Samsung(RemoteRegionWorker): def __init__(self, logger): self.logger = logger self.logger.info(".. created Samsung handler") def setup(self, namespace, remote_region_oam_ip, network_setup): self.logger.info(" Inside Samsung setup") self._create_docker_reg_secret(namespace, remote_region_oam_ip, self.logger) self._create_serviceaccount(namespace, remote_region_oam_ip, self.logger) self._add_pac_crd_annotation(remote_region_oam_ip, self.logger) self.logger.info(" Network setup:" + network_setup) if network_setup == 'true': self.create_host_network(remote_region_oam_ip, self.logger) def _add_pac_crd_annotation(self, remote_region_oam_ip, logger): logger.info("Inside _add_pac_crd_annotation") host_username, host_password, ssh_prefix = get_host_connection_details(remote_region_oam_ip) cmd = " kubectl annotate --kubeconfig=/etc/kubernetes/admin.conf --overwrite crd network-attachment-definitions.k8s.cni.cncf.io " cmd = cmd + " resource/annotation-relationship=\"on:Pod,key:k8s.v1.cni.cncf.io/networks,value:[{name:INSTANCE.metadata.name}]\"" cmd = ssh_prefix + cmd logger.info("Annotation cmd:" + cmd) cmds_annotate = [] cmds_annotate.append(cmd) run_commands(cmds_annotate, host_password, logger, block=True) def _create_host_network1(self, remote_region_oam_ip, logger): logger.info("Inside _create_host_network") #host_username = settings.HOST_CREDS['username'] #host_password = settings.HOST_CREDS['password'] #ssh_prefix = 'ssh -o StrictHostKeyChecking=no -t ' + host_username + '@' + remote_region_oam_ip host_username, host_password, ssh_prefix = get_host_connection_details(remote_region_oam_ip) cmds = [] cmds.append(ssh_prefix + " kubectl get nodes controller-0 --kubeconfig=/etc/kubernetes/admin.conf -o json") output_lines = self._run_command_get_all_lines(cmds, host_password, logger, block=True) logger.info("Returned output lines:") logger.info(output_lines) new_op_lines = "" for line in output_lines.split("\n"): if not 'Connection to' in line: new_op_lines = new_op_lines + line + "\n" allocatable_found = False capacity_found = False hugepg1G = "hugepages-1Gi" hugepg2M = "hugepages-2Mi" logger.info("New o/p lines:" + new_op_lines) if len(new_op_lines) > 0: json_op = json.loads(new_op_lines) logger.info("JSON O/P:" + str(json_op)) status = json_op["status"] logger.info("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n") logger.info("Status:" + str(status)) addresses = status["addresses"] logger.info("###############################\n") logger.info("Addresses:" + str(addresses)) allocatable = status["allocatable"] logger.info("********************************\n") logger.info("Allocatable:" + str(allocatable)) if hugepg1G in allocatable and hugepg2M in allocatable: logger.info("Allocatable found..") allocatable_found = True capacity = status["capacity"] if hugepg1G in capacity and hugepg2M in capacity: logger.info("Capacity found..") capacity_found = True result = allocatable_found and capacity_found logger.info("Create host network result:" + str(result)) return result def _verify_host_network(self, ssh_prefix, basecmd, host_password, logger): cmds = ["system host-unlock controller-0"] while True: unlock_wait = False for cmd in cmds: cmd_to_run = ssh_prefix + " " + basecmd + " " + cmd logger.info(cmd_to_run) output_lines = self._run_command_get_all_lines([cmd_to_run], host_password, logger, block=True) logger.info("Returned output lines:") logger.info(output_lines) if len(output_lines) > 0: for line in output_lines.split("\n"): logger.info("Line:" + line) if not unlock_wait: if 'retry host-unlock' in line or 'Rejected' in line: logger.info("Need to wait to call host-unlock ##### ") unlock_wait = True break if unlock_wait: time.sleep(60) else: break cmds = ["system host-show controller-0"] system_available = False while True: for cmd in cmds: cmd_to_run = ssh_prefix + " " + basecmd + " " + cmd logger.info(cmd_to_run) output_lines = self._run_command_get_all_lines([cmd_to_run], host_password, logger, block=True) logger.info("Returned output lines:") logger.info(output_lines) if len(output_lines) > 0: for line in output_lines.split("\n"): logger.info("Line:" + line) if 'available' in line: logger.info("Found available #######") system_available = True break if not system_available: time.sleep(5) else: break cmds = [] cmds.append(ssh_prefix + " kubectl get nodes controller-0 --kubeconfig=/etc/kubernetes/admin.conf -o json") output_lines = self._run_command_get_all_lines(cmds, host_password, logger, block=True) logger.info("Returned output lines:") logger.info(output_lines) new_op_lines = "" for line in output_lines.split("\n"): if not 'Connection to' in line: new_op_lines = new_op_lines + line + "\n" allocatable_found = False capacity_found = False f1c = 'intel.com/pci_sriov_net_f1c' f1u = 'intel.com/pci_sriov_net_f1u' fh0 = 'intel.com/pci_sriov_net_fh0' fh0m = 'intel.com/pci_sriov_net_fh0m' fh1 = 'intel.com/pci_sriov_net_fh1' logger.info("New o/p lines:" + new_op_lines) if len(new_op_lines) > 0: json_op = json.loads(new_op_lines) logger.info("JSON O/P:" + str(json_op)) status = json_op["status"] logger.info("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n") logger.info("Status:" + str(status)) addresses = status["addresses"] logger.info("###############################\n") logger.info("Addresses:" + str(addresses)) allocatable = status["allocatable"] logger.info("********************************\n") logger.info("Allocatable:" + str(allocatable)) if f1c in allocatable and f1u in allocatable and fh0 in allocatable and fh0m in allocatable and fh1 in allocatable: logger.info("Allocatable found..") allocatable_found = True capacity = status["capacity"] if f1c in capacity and f1u in capacity and fh0 in capacity and fh0m in capacity and fh1 in capacity: logger.info("Capacity found..") capacity_found = True result = allocatable_found and capacity_found logger.info("Create host network result:" + str(result)) return result def _create_serviceaccount(self, namespace, remote_region_oam_ip, logger): logger.info("Inside _create_serviceaccount") #host_username = settings.HOST_CREDS['username'] #host_password = settings.HOST_CREDS['password'] #ssh_prefix = 'ssh -o StrictHostKeyChecking=no -t ' + host_username + '@' + remote_region_oam_ip host_username, host_password, ssh_prefix = get_host_connection_details(remote_region_oam_ip) saName = 'vran-serviceaccount' cmds_sa = [] cmds_sa.append(ssh_prefix + ' kubectl create serviceaccount --kubeconfig=/etc/kubernetes/admin.conf ' + saName + ' -n ' + namespace) run_commands(cmds_sa, host_password, logger, block=True) def _create_docker_reg_secret(self, namespace, remote_region_oam_ip, logger): logger.info("Creating Docker registry secret in Namespace:" + namespace) #host_username = settings.HOST_CREDS['username'] #host_password = settings.HOST_CREDS['password'] #ssh_prefix = 'ssh -o StrictHostKeyChecking=no -t ' + host_username + '@' + remote_region_oam_ip host_username, host_password, ssh_prefix = get_host_connection_details(remote_region_oam_ip) dr_username, dr_password = get_docker_reg_connection_details() #dr_username = settings.CENTRAL_DR_CREDS['username'] #dr_password = settings.CENTRAL_DR_CREDS['password'] secret_name = 'admin-registry-secret' cmd = ssh_prefix + " kubectl create secret --kubeconfig=/etc/kubernetes/admin.conf docker-registry " + secret_name cmd = cmd + " --docker-server=registry.local:9001 --docker-username=" + dr_username + " --docker-password=" + dr_password cmd = cmd + " -n " + namespace cmds = [] cmds.append(cmd) run_commands(cmds, host_password, logger, block=True)