def __init__(self):
     self.param_valid = True
     self.tmc_url = os.getenv("tmc_url")
     if self.tmc_url is None:
         print(
             "No tmc_url provided, please set environment variable tmc_url")
         self.param_valid = False
         return
     self.api_token = os.getenv("api_token")
     if self.api_token is None:
         print(
             "No api_token provided, please set environment variable api_token"
         )
         self.param_valid = False
         return
     self.org_id = os.getenv("org_id")
     if self.org_id is None:
         print("No org_id provided, please set environment variable org_id")
         self.param_valid = False
         return
     self.lcp_name = os.getenv("lcp_name")
     if self.lcp_name is None:
         print(
             "No lcp_name provided, please set environment variable lcp_name"
         )
         self.param_valid = False
         return
     self.tmc = TMC(self.tmc_url, self.api_token, self.org_id)
Ejemplo n.º 2
0
 def __init__(self,
              vc,
              username,
              password,
              tmc_url,
              api_token,
              org_id,
              lcp_prefix,
              yaml_action,
              skiplist=[]):
     self.vc = vc
     self.username = username
     self.password = password
     self.tmc_url = tmc_url
     self.api_token = api_token
     self.org_id = org_id
     self.lcp_prefix = lcp_prefix
     self.yaml_action = yaml_action
     self.tmc_handler = TMC(self.tmc_url, self.api_token, self.org_id)
     self.wcp_fetcher = WCPFetcher(self.vc, self.username, self.password)
     self.wcp_info = self.wcp_fetcher.wcp_info
     self.skipIPList = skiplist
     print("Skip List: " + str(self.skipIPList))
     mywcp_info = self.wcp_info.copy()
     for w in mywcp_info:
         if mywcp_info[w]["IP"] in self.skipIPList:
             self.wcp_info.pop(w, None)
     print("WCP Clusters: ")
     print(self.wcp_info)
     print("Initialized successfully")
class Test:
    def __init__(self):
        self.param_valid = True
        self.tmc_url = os.getenv("tmc_url")
        if self.tmc_url is None:
            print(
                "No tmc_url provided, please set environment variable tmc_url")
            self.param_valid = False
            return
        self.api_token = os.getenv("api_token")
        if self.api_token is None:
            print(
                "No api_token provided, please set environment variable api_token"
            )
            self.param_valid = False
            return
        self.org_id = os.getenv("org_id")
        if self.org_id is None:
            print("No org_id provided, please set environment variable org_id")
            self.param_valid = False
            return
        self.lcp_name = os.getenv("lcp_name")
        if self.lcp_name is None:
            print(
                "No lcp_name provided, please set environment variable lcp_name"
            )
            self.param_valid = False
            return
        self.tmc = TMC(self.tmc_url, self.api_token, self.org_id)

    def isLCPHealthy(self):
        myresp = self.tmc.get_local_control_plane(self.lcp_name)
        print(myresp)
        isHealthy = True
        try:
            lcp_info = myresp.json()
            if ("healthy" in lcp_info["localcontrolplane"]["status"]
                ["health"].lower()):
                print("LCP: " + self.lcp_name + " seems to be healthy.")
                isHealthy = True
            else:
                print("LCP: " + self.lcp_name + " seems to be unhealthy.")
                isHealthy = False

        except Exception as e:
            print("Health check for " + self.lcp_name + " failed with " +
                  str(e))
            isHealthy = False
        return isHealthy

    def testSetup(self):
        if self.param_valid:
            return True
        else:
            print("Some parameters are not valid.")
            return False

    def testTask(self):
        success = True
        try:
            success = self.isLCPHealthy()
            return success
        except Exception as e:
            return False

    def testCleanup(self):
        return True
Ejemplo n.º 4
0
class TMCWorkFlow:
    def __init__(self,
                 vc,
                 username,
                 password,
                 tmc_url,
                 api_token,
                 org_id,
                 lcp_prefix,
                 yaml_action,
                 skiplist=[]):
        self.vc = vc
        self.username = username
        self.password = password
        self.tmc_url = tmc_url
        self.api_token = api_token
        self.org_id = org_id
        self.lcp_prefix = lcp_prefix
        self.yaml_action = yaml_action
        self.tmc_handler = TMC(self.tmc_url, self.api_token, self.org_id)
        self.wcp_fetcher = WCPFetcher(self.vc, self.username, self.password)
        self.wcp_info = self.wcp_fetcher.wcp_info
        self.skipIPList = skiplist
        print("Skip List: " + str(self.skipIPList))
        mywcp_info = self.wcp_info.copy()
        for w in mywcp_info:
            if mywcp_info[w]["IP"] in self.skipIPList:
                self.wcp_info.pop(w, None)
        print("WCP Clusters: ")
        print(self.wcp_info)
        print("Initialized successfully")

    def fillInfo(self):
        for w in self.wcp_info:
            print("Cluster: " + w)
            try:
                lcp_name = self.lcp_prefix + "-vc-" + self.vc.replace(
                    ".", "-") + "-w-" + self.wcp_info[w]["IP"].replace(
                        ".", "-") + "lcp"
                self.wcp_info[w]["lcp_name"] = lcp_name
                print("LCP: " + lcp_name)

            except Exception as e:
                print(str(e))

        print("Completed")

    def delete_lcp(self, force=True):
        for w in self.wcp_info:
            print("Cluster: " + w)
            try:
                print("Deleting LCP for " + self.wcp_info[w]["IP"])
                print("")
                lcp_name = self.lcp_prefix + "-vc-" + self.vc.replace(
                    ".", "-") + "-w-" + self.wcp_info[w]["IP"].replace(
                        ".", "-") + "lcp"
                myinfo = self.tmc_handler.delete_local_control_plane(
                    lcp_name, force)
                self.wcp_info[w]["lcp"] = myinfo
                self.wcp_info[w]["lcp_name"] = lcp_name
                print("Completed")
            except Exception as e:
                print(str(e))

    def deregister_cluster(self):
        for w in self.wcp_info:
            print("Cluster: " + w)
            try:
                print("Deregistering for " + self.wcp_info[w]["IP"])
                print("Get Domain: ")
                domain = w.split(":")[0].split("domain-")[1]
                print(domain)
                if 'apply' in self.yaml_action:
                    cmd0 = 'kubectl delete agentinstall tmc-agent-installer-config -n svc-tmc-' + domain
                    self.wcp_fetcher.run_command_on_wcp(w, cmd0)
                    print("Sleeping for 5 sec.")
                    time.sleep(5)

                cmd1 = 'curl -k -X GET "https://raw.githubusercontent.com/yogeshbendre/ytmc/master/tmc_deregistration_template.yaml" -o /root/tmc_deregistration_template.yaml'
                self.wcp_fetcher.run_command_on_wcp(w, cmd1)
                time.sleep(1)

                cmd2 = "cat /root/tmc_deregistration_template.yaml | sed 's/<namespace>/svc-tmc-" + domain + "/g' > /root/tmc_deregister.yaml"
                self.wcp_fetcher.run_command_on_wcp(w, cmd2)
                time.sleep(1)
                print("Generated YAML for TMC Registration")

                cmd3 = "cat  /root/tmc_deregister.yaml"
                self.wcp_fetcher.run_command_on_wcp(w, cmd3)
                time.sleep(1)
                if 'apply' in self.yaml_action:
                    cmd4 = "kubectl apply -f  /root/tmc_deregister.yaml"
                    self.wcp_fetcher.run_command_on_wcp(w, cmd4)
                    time.sleep(1)
                print("Completed")
            except Exception as e:
                print(str(e))

    def is_lcp_healthy(self, lcp_name):
        myresp = self.tmc_handler.get_local_control_plane(lcp_name)
        try:
            lcp_info = myresp.json()
            if ("healthy" in lcp_info["localcontrolplane"]["status"]
                ["health"].lower()):
                print("LCP: " + lcp_name + " seems to be healthy.")
                return True
            else:
                print("LCP: " + lcp_name + " seems to be unhealthy.")
                return False
        except Exception as e:
            print("Health check for " + lcp_name + " failed with " + str(e))
            return False

    def monitor_deregistration(self, monitor_time_in_min=5):
        print("Monitoring deregistration for " + str(monitor_time_in_min) +
              " minutes...")
        t = 0
        areAllHealthy = False
        healthStates = {}
        while t <= monitor_time_in_min:
            areAllHealthy = False
            for w in self.wcp_info:
                try:
                    print("Check disconnection of " +
                          self.wcp_info[w]["lcp_name"])
                    myhealth = self.is_lcp_healthy(
                        self.wcp_info[w]["lcp_name"])
                    areAllHealthy = areAllHealthy or myhealth
                    healthStates[self.wcp_info[w]["lcp_name"]] = not myhealth
                except Exception as e:
                    healthStates[self.wcp_info[w]["lcp_name"]] = False
            if (not areAllHealthy):
                print("All the control planes are in disconnected states.")
                for lcp in healthStates.keys():
                    print("LCP: " + lcp + " Disconnected: " +
                          str(healthStates[lcp]))
                break
            else:
                print(
                    "Some LCP are still healthy. Sleeping for 1 min. Remaining Time: "
                    + str(monitor_time_in_min - t) + " min")
                time.sleep(60)
                t = t + 1

        if (not areAllHealthy):
            return True
        print("Monitoring Time Out and still few LCPs are healthy.")
        for lcp in healthStates.keys():
            print("LCP: " + lcp + " Disconnected: " + str(healthStates[lcp]))
        return False
Ejemplo n.º 5
0
class TMCWorkFlow:

    def __init__(self, vc, username, password, tmc_url, api_token, org_id, lcp_prefix, yaml_action, skipIPList=[]):
        #self.logger = mylogger
        self.vc = vc
        self.username = username
        self.password = password
        self.tmc_url = tmc_url
        self.api_token = api_token
        self.org_id = org_id
        self.lcp_prefix = lcp_prefix
        self.yaml_action = yaml_action
        self.tmc_handler = TMC(self.tmc_url, self.api_token, self.org_id)
        self.wcp_fetcher = WCPFetcher(self.vc, self.username, self.password)
        self.wcp_info = self.wcp_fetcher.wcp_info
        self.skipIPList = skipIPList
        print("Skip List: "+str(self.skipIPList))
        mywcp_info = self.wcp_info.copy()
        for w in mywcp_info:
            if mywcp_info[w]["IP"] in self.skipIPList:
                self.wcp_info.pop(w,None)
        print("WCP Clusters: ")
        print(self.wcp_info)
        print("Initialized successfully")

    def create_lcp(self):
        for w in self.wcp_info:
            print("Cluster: "+w)
            try:
                print("Creating LCP for "+self.wcp_info[w]["IP"])
                print("")
                lcp_name = self.lcp_prefix + "-vc-" +self.vc.replace(".","-") + "-w-" + self.wcp_info[w]["IP"].replace(".","-") + "lcp"
                myinfo = self.tmc_handler.create_local_control_plane(lcp_name)
                self.wcp_info[w]["lcp"] = myinfo
                self.wcp_info[w]["lcp_name"] = lcp_name
                print("Completed")
            except Exception as e:
                print(str(e))

    def register_cluster(self):
        for w in self.wcp_info:
            print("Cluster: "+w)
            try:
                print("Registering for " + self.wcp_info[w]["IP"])
                print("Get Domain: ")
                domain = w.split(":")[0].split("domain-")[1]
                print(domain)

                #Enable dev_stack
                print("Enable DEV_STACK flag")
                cmd0 = 'curl -k -X GET "https://raw.githubusercontent.com/yogeshbendre/specialtools/master/yamlupdater.py" -o yamlupdater.py'
                self.wcp_fetcher.run_command_on_wcp(w, cmd0)
                time.sleep(1)

                cmd0 = 'python3 yamlupdater.py'
                self.wcp_fetcher.run_command_on_wcp(w, cmd0)
                time.sleep(1)

                cmd0 = 'kubectl apply -f /usr/lib/vmware-wcp/objects/PodVM-GuestCluster/70-tmc-agent-installer/tmc-agent-installer.yaml'
                self.wcp_fetcher.run_command_on_wcp(w, cmd0)
                time.sleep(60)


                print("Registration Link: ")
                reg_link = self.wcp_info[w]["lcp"]["managementCluster"]["status"]["registrationUrl"]
                print(reg_link)
                cmd1 = 'curl -k -X GET "https://raw.githubusercontent.com/yogeshbendre/ytmc/master/tmc_registration_template.yaml" -o /root/tmc_registration_template.yaml'
                self.wcp_fetcher.run_command_on_wcp(w,cmd1)
                time.sleep(1)
                reg_link = reg_link.replace("/","\/").replace("?","\?").replace("&","\&")
                cmd2 = "cat /root/tmc_registration_template.yaml | sed 's/<namespace>/svc-tmc-"+domain+"/g' | sed 's/<registration_link>/"+reg_link+"/g' > /root/tmc_register.yaml"
                self.wcp_fetcher.run_command_on_wcp(w,cmd2)
                time.sleep(1)
                print("Generated YAML for TMC Registration")
                cmd3 = "cat  /root/tmc_register.yaml"
                self.wcp_fetcher.run_command_on_wcp(w, cmd3)
                time.sleep(1)
                if 'apply' in self.yaml_action:
                    cmd4 = "kubectl apply -f  /root/tmc_register.yaml"
                    self.wcp_fetcher.run_command_on_wcp(w, cmd4)
                    time.sleep(1)

                print("Completed")
            except Exception as e:
                print(str(e))

    def is_lcp_healthy(self, lcp_name):
        myresp = self.tmc_handler.get_local_control_plane(lcp_name)
        try:
            lcp_info = myresp.json()
            if("healthy" in lcp_info["managementCluster"]["status"]["health"].lower()):
                print("LCP: "+lcp_name+" seems to be healthy.")
                return True
            else:
                print("LCP: " + lcp_name + " seems to be unhealthy.")
                return False
        except Exception as e:
            print("Health check for "+lcp_name+" failed with "+str(e))
            return False



    def monitor_registration(self, monitor_time_in_min = 5):
        print("Monitoring registration for "+str(monitor_time_in_min)+" minutes...")
        t = 0
        areAllHealthy = True
        healthStates = {}
        while t <= monitor_time_in_min:
            areAllHealthy = True
            for w in self.wcp_info:
                try:
                    print("Check health of "+self.wcp_info[w]["lcp_name"])
                    myhealth = self.is_lcp_healthy(self.wcp_info[w]["lcp_name"])
                    areAllHealthy = areAllHealthy and myhealth
                    healthStates[self.wcp_info[w]["lcp_name"]] = myhealth
                    try:
                        self.wcp_fetcher.run_command_on_wcp(w, "kubectl get pods -A | grep tmc")
                    except Exception as e2:
                        print(str(e2))
                except Exception as e:
                    healthStates[self.wcp_info[w]["lcp_name"]] = False
            if(areAllHealthy):
                print("All the control planes are in healthy states.")
                for lcp in healthStates.keys():
                    print("LCP: "+lcp+" Healthy: "+str(healthStates[lcp]))
                break
            else:
                print("Some LCP are still not healthy. Sleeping for 1 min. Remaining Time: "+str(monitor_time_in_min-t)+" min")
                time.sleep(60)
                t = t + 1

        if(areAllHealthy):
            return True
        print("Monitoring Time Out and still few LCPs are not healthy.")
        for lcp in healthStates.keys():
            print("LCP: " + lcp + " Healthy: " + str(healthStates[lcp]))
        return False