def gitlabHelmInstall(self): # helm 安装 gitlab Print("Staring helm install gitlab", colour="green") gitlab_helm_path = os.path.join(BASE_DIR, "package/helm/gitlab-ce") gitlab_helm_values = os.path.join(gitlab_helm_path, "values.yaml") gitlab_helm_ingress = os.path.join(gitlab_helm_path, "gitlab-ingress.yaml") gitlab_helm_ingress_tmp = os.path.join(gitlab_helm_path, "gitlab-ingress.yaml.tmpl") # 替换 gitlab-ce 中 template.content(src=gitlab_helm_ingress_tmp, dest=gitlab_helm_ingress, externalUrl=cfg.get("gitlab", "externalUrl").replace( 'http://', '')) gitlab_helm_cmd = "helm install --name {} -f {} {} " \ "--set gitlabRootPassword={} " \ "--set externalUrl={} " \ "--namespace {}".format(self.gitlab_config['gitlab_name'],gitlab_helm_values, gitlab_helm_path, self.gitlab_config['gitlabRootPassword'], self.gitlab_config['externalUrl'],self.gitlab_config['gitlab_namespace']) print gitlab_helm_cmd gitlab_status = local(cmd=gitlab_helm_cmd) if gitlab_status: Print("Gitlab install Success.", colour="green") gitlab_ingress_cmd = "kubectl apply -f {}".format( gitlab_helm_ingress) local(cmd=gitlab_ingress_cmd) add_url_hosts(self.gitlab_config['externalUrl'].split('//')[1]) Print("Please run: 'kubectl get pod -n {}' to view.".format( self.gitlab_config['gitlab_namespace']), colour="yellow") else: print("\033[1;31mError: gitlab install Failed.\033[0m")
def rbdInstall(self): Print("Staring YAML install rbd provisioner", colour="green") ceph_rbd_tmp_path = os.path.join(self.ceph_rbd_path, "tmp") template.content( src=os.path.join(ceph_rbd_tmp_path, "rbd-deployment-tmp.yaml"), dest=self.ceph_yaml_rbd_deploymemt, ceph_rbd_namespace=self.cephRBD_config['ceph_rbd_namespace']) template.content( src=os.path.join(ceph_rbd_tmp_path, "rbd-sa-tmp.yaml"), dest=self.ceph_yaml_rbd_sa, ceph_rbd_namespace=self.cephRBD_config['ceph_rbd_namespace']) ceph_yaml_rbd_cmd = "kubectl apply -f {},{}".format( self.ceph_yaml_rbd_sa, self.ceph_yaml_rbd_deploymemt) ceph_rbd_status = local(cmd=ceph_yaml_rbd_cmd) if ceph_rbd_status: Print("rbd-provisioner YAML install Success.") Print("Please run: 'kubectl get pod -n {}' to view.".format( self.cephRBD_config['ceph_rbd_namespace']), colour="yellow") else: print( "\033[1;31mError: rbd-provisioner YAML install Failed.\033[0m")
def ingressInstall(): # 安装 ingress Print("Staring install ingress", colour="green") time.sleep(3) ingress_helm_path = os.path.join(BASE_DIR, "package/helm/nginx-ingress") ingress_helm_values = os.path.join(ingress_helm_path, "values.yaml") ingress_config = DefaultOption( cfg, "ingress", ingress_name="ingress-nginx", ingress_namespace="kube-system").configDict() ingress_cmd = "helm install --name {} -f {} {} --namespace {}".format( ingress_config['ingress_name'], ingress_helm_values, ingress_helm_path, ingress_config['ingress_namespace']) try: res_status = local(ingress_cmd) if res_status: Print("Ingress install Success.", colour="green") Print("Please run: 'kubectl get pod -n {}' to view.".format( ingress_config['ingress_namespace']), colour="yellow") else: print( "\033[1;31mError: ingress-nginx already exists or install failed..\033[0m" ) except: print( "\033[1;31mError: Executing {} Failed,Ingress install Failed.\033[0m" .format(ingress_cmd))
def update_linux_kernel(self, hostname): # 升级Linux内核 print("{} staring update linux kernel".format(hostname)) ip_result = Utils.localCMPHostname(hostname) if ip_result: local_update_linux_script = os.path.join(kernel_dir, "update_linux_kernel.sh") result_script = local("bash {}".format(local_update_linux_script)) if result_script: Print("Please restart {} later".format(hostname), colour="yellow") else: Print("update kernel failed", colour="red") else: remote_update_linux_script = os.path.join( K8S_DEST_DIR, 'update_linux_kernel.sh') sftp_upload(hostname, kernel_dir, K8S_DEST_DIR) Print("Kernel is being upgraded, please wait a moment.", hostname=hostname, colour="green") result_script = sudo(hostname, "bash {}".format(remote_update_linux_script)) print(result_script[hostname]['stdout']) if result_script['status']: # reboot_status = raw_input("[execute [reboot machine] ?( yes/no)] ") # if reboot_status == "yes": Print("{} will be restarted soon".format(hostname), colour="yellow") return True # sys.exit(1) else: Print("{} exec update_linux_kernel.sh False.", colour="red") return False
def rbdRemove(self): remove_ceph_rbd_cmd = "kubectl delete -f {},{}".format( self.ceph_yaml_rbd_sa, self.ceph_yaml_rbd_deploymemt) remove_ceph_rbd_status = local(cmd=remove_ceph_rbd_cmd) if remove_ceph_rbd_status: Print("rbd-provisioner YAML remove Success.", colour="green") Print("Please run: 'kubectl get pod -n {}' to view.".format( self.cephRBD_config['ceph_rbd_namespace']), colour="yellow") else: print( "\033[1;31mError: rbd-provisioner YAML remove Failed.\033[0m")
def nfsInstall(): # 安装 nfs-client-provisioner Print("Staring install nfs storageClass.", colour="green") nfs_helmPath = os.path.join(BASE_DIR, "package/helm/nfs-client-provisioner") nfs_helmValues = os.path.join(nfs_helmPath, "values.yaml") nfs_install_script = os.path.join( BASE_DIR, "package/script/nfs_install/nfs_install.sh") nfs_config = DefaultOption(cfg, "nfs", nfs_name="nfs-client-provisioner", nfs_namespace="kube-system", storageClass_name="nfs-provisioner", nfsServer=GetBaseconfig.NODE_HOST[-1], nfsPath="/nfs/k8s").configDict() if re.match(r"^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$", nfs_config['nfsServer']): pass else: Print("The config.ini nfsServer {} must modify IP Invaild.".format( nfs_config['nfsServer']), colour="red") nfsServer = Utils.getHostnameIp(nfs_config['nfsServer']) nfs_helm_cmd = "helm install --name {} -f {} {} " \ "--set storageClass.name={} " \ "--set persistence.nfsServer={} " \ "--set persistence.nfsPath={} " \ "--namespace {}".format(nfs_config['nfs_name'],nfs_helmValues,nfs_helmPath,nfs_config['storageClass_name'], nfsServer,nfs_config['nfsPath'],nfs_config['nfs_namespace']) print nfs_helm_cmd try: result = exec_script(hostname=nfs_config['nfsServer'], script=nfs_install_script, args="{}".format(nfs_config['nfsPath']), sudo=True) if result['status']: Print(result[nfs_config['nfsServer']]['stdout']) except Exception as error: print(error) nfs_status = local(cmd=nfs_helm_cmd) if nfs_status: Print("nfs-client-provisioner install Success.", colour="green") Print("Please run: 'kubectl get pod -n {}' to view.".format( nfs_config['nfs_namespace']), colour="yellow") else: print("\033[1;31mError: nfs-client-provisioner install Failed.\033[0m")
def helmInstall(): # 安装 helm Print("Staring install helm", colour="green") helm_client_package_file = os.path.join( BASE_DIR, "package/helm/helm-v2.14.2-linux-amd64.tar.gz") helm_install_script = os.path.join( BASE_DIR, "package/script/helm_install/helm_install.sh") helm_install_cmd = "bash {} {}".format(helm_install_script, helm_client_package_file) res_status = local(helm_install_cmd) if res_status: Print("helm install Success.", colour="green") else: Print("helm install Failed.")
def local_kernel(self): res = subprocess.Popen("uname -r", shell=True, stdout=subprocess.PIPE) kernel = res.stdout.read().strip() kernel_short = float(kernel[0:3]) if kernel_short < float(4.19): Print("{} kernel version {},Need to upgrade the kernel".format( self.local_master, kernel), colour="green") self.update_linux_kernel(self.local_master) reboot_status = raw_input("[execute [reboot machine] ?( yes/no)] ") if reboot_status == "yes": local(cmd="reboot") else: Print("{} kernel version {}".format(self.local_master, kernel), colour="green")
def gitlabYamlInstall(self): # yaml 安装 gitlab Print("Staring YAML install gitlab", colour="green") self.check.check_nfs_client(self.nfs_name, display=False) self.check.check_ingress(self.ingress_name, display=False) gitlab_yaml_tmp_path = os.path.join(self.gitlab_yaml_path, "tmp") gitlab_yaml_gitlab_ce = os.path.join(self.gitlab_yaml_path, "gitlab-ce.yaml") gitlab_yaml_gitlab_postgresql = os.path.join(self.gitlab_yaml_path, "gitlab-postgresql.yaml") gitlab_yaml_gitlab_redis = os.path.join(self.gitlab_yaml_path, "gitlab-redis.yaml") # 替换 gitlab-ce 中 template.content( src=os.path.join(gitlab_yaml_tmp_path, "gitlab-ce-tmp.yaml"), dest=gitlab_yaml_gitlab_ce, gitlab_namespace=self.gitlab_config['gitlab_namespace'], GITLAB_ROOT_PASSWORD=self.gitlab_config['gitlabRootPassword'], GITLAB_HOST=self.gitlab_config['externalUrl'].split('//')[1], ) template.content( src=os.path.join(gitlab_yaml_tmp_path, "gitlab-postgresql-tmp.yaml"), dest=gitlab_yaml_gitlab_postgresql, gitlab_namespace=self.gitlab_config['gitlab_namespace']) template.content( src=os.path.join(gitlab_yaml_tmp_path, "gitlab-redis-tmp.yaml"), dest=gitlab_yaml_gitlab_redis, gitlab_namespace=self.gitlab_config['gitlab_namespace']) gitlab_yaml_cmd = "kubectl apply -f {},{},{}".format( gitlab_yaml_gitlab_ce, gitlab_yaml_gitlab_postgresql, gitlab_yaml_gitlab_redis) gitlab_status = local(cmd=gitlab_yaml_cmd) if gitlab_status: Print("Gitlab YAML install Success.", colour="green") GITLAB_HOST = self.gitlab_config['externalUrl'].split('//')[1] Print("Import {} to /etc/hosts.".format(GITLAB_HOST), colour="green") add_url_hosts(GITLAB_HOST) Print("Please run: 'kubectl get pod -n {}' to view.".format( self.gitlab_config['gitlab_namespace']), colour="yellow") else: print("\033[1;31mError: gitlab install Failed.\033[0m")
def add_harbor_url_docker(self, url): Print("import insecure-registries to others node.", colour="green") add_harbor_script = os.path.join( GetBaseconfig.BASE_DIR, "package/script/add_hosts/add_harbor_url_docker.py") NODE_HOST = GetBaseconfig.NODE_HOST MASTER_HOST = GetBaseconfig.MASTER_HOST for node in NODE_HOST: node_status = exec_script(hostname=node, script=add_harbor_script, args=url, sudo=True, type="python", display=False) if node_status['status']: Print("{}".format(node_status[node]['stdout'].strip()), hostname=node, colour="green") # run(hostname=node, cmd="systemctl daemon-reload && systemctl restart docker") else: Print("{}".format(node_status[node]['stderr']), hostname=node, colour="yellow") Print( "Please import manually {} to {} /etc/docker/daemon.json". format(url, node), hostname=node, colour="yellow") for master in MASTER_HOST: master_status = exec_script(hostname=master, script=add_harbor_script, args=url, sudo=True, type="python", display=False) if master_status['status']: Print("{}".format(master_status[master]['stdout'].strip()), hostname=master, colour="green") # run(hostname=master, cmd="systemctl daemon-reload && systemctl restart docker") else: Print("{}".format(master_status[master]['stderr']), hostname=master, colour="yellow") Print( "Please import manually {} to {} /etc/docker/daemon.json". format(url, master), hostname=master, colour="yellow") tips = """ Tips: If you want to docker login {}. Please execute first systemctl daemon-reload && systemctl restart docker. """.format(url) Print(tips, colour="yellow")
def remove_harbor(self): remove_harbor_helm_cmd = "helm del --purge {} ".format( self.harbor_config['harbor_name']) harbor_status = local(cmd=remove_harbor_helm_cmd) if harbor_status: Print("helm remove harbor Success.", colour="green") remove_harbor_pvc_cmd = "kubectl delete pvc --all -n {}".format( self.harbor_config['harbor_namespace']) remove_pvc_status = local(cmd=remove_harbor_pvc_cmd) if remove_pvc_status: Print("kubectl remove harbor pvc Success.", colour="green") else: Print("Please run: 'kubectl delete pvc --all -n {}.".format( self.harbor_config['harbor_namespace']), colour="red") else: print("\033[1;31mError: helm remove harbor Failed.\033[0m")
def harborInstall(self): # 安装 harbor nfs_name = DefaultOption( cfg, "harbor", nfs_name="nfs-client-provisioner").configDict()['nfs_name'] ingress_name = DefaultOption( cfg, "ingress", ingress_name="ingress-nginx").configDict()['ingress_name'] self.check.check_nfs_client(nfs_name, display=False) self.check.check_ingress(ingress_name, display=False) Print("Staring install harbor", colour="green") harbor_helm_path = os.path.join(BASE_DIR, "package/helm/harbor-helm") harbor_helm_values = os.path.join(harbor_helm_path, "values.yaml") harbor_helm_values_tmp = os.path.join(harbor_helm_path, "values_tmp.yaml") # 替换 harbor-helm 中 StorageClass core_hosts = cfg.get("harbor", "externalURL").split('//')[1] template.content(src=harbor_helm_values_tmp, dest=harbor_helm_values, storageClass=self.harbor_config['storageClass'], externalURL=cfg.get("harbor", "externalURL"), core_hosts=core_hosts, notary_hosts=core_hosts.replace( core_hosts.split('.')[0], 'notary')) # 导入 docker harbor安装认证url harbor_helm_cmd = "helm install --name {} -f {} {} " \ "--set harborAdminPassword={} " \ "--namespace {}".format(self.harbor_config['harbor_name'],harbor_helm_values, harbor_helm_path,self.harbor_config['harborAdminPassword'], self.harbor_config['harbor_namespace']) print harbor_helm_cmd harbor_status = local(cmd=harbor_helm_cmd) if harbor_status: Print("harbor install Success.", colour="green") self.add_harbor_url_docker(cfg.get("harbor", "externalURL")) add_url_hosts(core_hosts) Print("Please run: 'kubectl get pod -n {}' to view.".format( self.harbor_config['harbor_namespace']), colour="yellow") else: print("\033[1;31mError: harbor install Failed.\033[0m")
def remove_yaml(self): gitlab_yaml_gitlab_ce = os.path.join(self.gitlab_yaml_path, "gitlab-ce.yaml") gitlab_yaml_gitlab_postgresql = os.path.join(self.gitlab_yaml_path, "gitlab-postgresql.yaml") gitlab_yaml_gitlab_redis = os.path.join(self.gitlab_yaml_path, "gitlab-redis.yaml") remove_gitlab_yaml_cmd = "kubectl delete -f {},{},{}".format( gitlab_yaml_gitlab_ce, gitlab_yaml_gitlab_postgresql, gitlab_yaml_gitlab_redis) gitlab_status = local(cmd=remove_gitlab_yaml_cmd) if gitlab_status: Print("Gitlab YAML remove Success.", colour="green") Print("Please run: 'kubectl get pod -n {}' to view.".format( self.gitlab_config['gitlab_namespace']), colour="yellow") else: print("\033[1;31mError: gitlab YAML remove Failed.\033[0m")
def auth(self): try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(self.timeout) sock.connect((self.hostname, self.port)) self.ssh_check(sock=sock) except socket.error as error: Print("{} {} {}".format(self.hostname, self.username, error), colour="red")
def ssh_check(self, sock): private_key = paramiko.RSAKey.from_private_key_file(self.private) transport = Transport(sock=sock) try: transport.start_client() except SSHException as error: Print("{} {}".format(self.username, error), colour="red") try: if self.password is None: transport.auth_publickey(self.username, private_key) else: transport.auth_password(self.username, self.password) if transport.is_authenticated(): Print("{} {} connection Successfully.".format( self.hostname, self.username), colour="green") except ssh_exception.SSHException, e: print("{} {}".format(self.hostname, e.message)) Print("{} {} Error in username or password.".format( self.hostname, self.username), colour="red") # LinuxSSHAuth(hostname='q12469v.cloud.shbt.qihoo.net',username='******').auth() # def ssh_check(hostname,username,password,port=22): # sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # sock.settimeout(3) # sock.connect((hostname, port)) # transport = Transport(sock=sock) # try: # transport.start_client() # except SSHException as error: # Print("{} {}".format(hostname, error), "red") # try: # transport.auth_password(username, password) # if transport.is_authenticated(): # Print("{} {} connection Successfully.".format(hostname, username), "green") # except ssh_exception.AuthenticationException, e: # print("{} {}".format(hostname, e.message)) # Print("{} {} Error in username or password.\nExit installation.".format(hostname, username), "red") # # ssh_check(hostname='zjtdev01v.cloud.corp.qihoo.net',username='******',password='******')
def masterJoinInfo(self): # 从日志文件中拿到 master token ca_hash self.master_join, self.token, self.ca_hash = getJoinInfo(LOG_FILE) if self.master_join is not None and self.token is not None and self.ca_hash is not None: return self.master_join, self.token, self.ca_hash else: Print( "master_join,token,ca_hash not Found.please check {} log file". format(LOG_FILE), colour="red", type=True)
def other_kernel(self): for hostname in self.other_master: res = run(hostname, "uname -r") if res['status']: kernel = res[hostname]['stdout'].strip() else: kernel = None Print("{} Failed to get the kernel version".format(hostname), colour="red") kernel_short = float(kernel[0:3]) if kernel_short < float(4.19): Print("{} kernel version {},Need to upgrade the kernel".format( hostname, kernel), colour="green") result = self.update_linux_kernel(hostname) if result: sudo(hostname=hostname, cmd="reboot") else: Print("{} kernel version {}".format(hostname, kernel), colour="green")
def ssh_check(self, sock): private_key = paramiko.RSAKey.from_private_key_file(self.private) transport = Transport(sock=sock) try: transport.start_client() except SSHException as error: Print("{} {}".format(self.username, error), colour="red") try: if self.password is None: transport.auth_publickey(self.username, private_key) else: transport.auth_password(self.username, self.password) if transport.is_authenticated(): Print("{} {} connection Successfully.".format( self.hostname, self.username), colour="green") except ssh_exception.SSHException, e: print("{} {}".format(self.hostname, e.message)) Print("{} {} Error in username or password.".format( self.hostname, self.username), colour="red")
def install_node(self): # 安装 k8s 集群 node 节点 self.masterJoinInfo() Print("Start install k8s node {} ...".format(self.hostname), colour="green") self.remove() Print("Please view install log file {}:{}".format( self.hostname, LOG_FILE), hostname=self.hostname, colour="yellow") Print( "Please wait patiently while the program is being installed. It may take several minutes.", hostname=self.hostname, colour="yellow") node_common_result = exec_script(hostname=self.hostname, script=self.node_common_script, args="{}".format(LOG_FILE)) if node_common_result['status']: node_script_result = exec_script(hostname=self.hostname, script=self.node_script, args="{} {} {} {}".format( LOG_FILE, self.master_join, self.token, self.ca_hash)) if node_script_result['status']: Print(node_script_result[self.hostname]['stdout'], hostname=self.hostname) Print("node install success", hostname=self.hostname, colour="green") else: Print(node_script_result[self.hostname]['stderr'], hostname=self.hostname) Print("node execute {} failed".format(self.node_script), hostname=self.hostname, colour="yellow") else: Print(node_common_result[self.hostname]['stderr'], hostname=self.hostname) Print("node install failed.please check {}:{} log file.".format( self.hostname, LOG_FILE), hostname=self.hostname, colour="red", type=True)
def end(): # 安装完成之后,打印提示 master_join, token, ca_hash = getJoinInfo(LOG_FILE) tips = """ Tips: Run 'kubectl get nodes' on the control-plane to see this node join the cluster. If the slave node does not join the master node, the following program can be executed. kubeadm join {} --token {} --discovery-token-ca-cert-hash {} k8s 集群已经在主节点上调度pod,如果你不需要在主节点上调度pod请执行以下命令 "kubectl taint nodes --all node-role.kubernetes.io/master=" """.format(master_join, token, ca_hash) local("kubectl taint nodes --all node-role.kubernetes.io/master-") Print(tips, colour="yellow")
def add_url_hosts(url): # 导入 harbor url 到所以节点上 Print("import {} to others node /etc/hosts.".format(url), colour="green") NODE_HOST = GetBaseconfig.NODE_HOST HOST = GetBaseconfig.MASTER_HOST HOST.extend(NODE_HOST) node_list = list() script = os.path.join(GetBaseconfig.BASE_DIR, "package/script/add_hosts/add_hosts.sh") for node in NODE_HOST: node_IP = Utils.getHostnameIp(node) node_list.append(node_IP) for i in HOST: for j in node_list: status = exec_script(hostname=i, script=script, args="{} {}".format(j, url), display=False) if status['status']: Print("{}".format(status[i]['stdout'])) else: Print("{}".format(status[i]['stderr'])) Print("Please import manually {} to {}".format(url, i), hostname=i, colour="yellow") # class InstallK8sCluster(object): # """ # 安装k8s集群 # """ # base_dir = GetBaseconfig.BASE_DIR # package_dir = os.path.join(base_dir, "package") # script_dir = os.path.join(package_dir, "script") # k8s_install_dir = os.path.join(package_dir,"script/k8s_install") # kube_dir = os.path.join(package_dir, "yaml") # common_script = os.path.join(k8s_install_dir, "commonInstall.sh") # master_script = os.path.join(k8s_install_dir, "master.sh") # remove_script = os.path.join(k8s_install_dir,"remove.sh") # flannel_yml = os.path.join(kube_dir,"kube","kube-flannel.yml") # # def __init__(self): # # # self.local_IP = Utils.getLocalIp() # self.k8s_dest_dir = Utils.getUserInstallHome() # self.node_common_script = os.path.join(self.k8s_dest_dir, "commonInstall.sh") # self.node_script = os.path.join(self.k8s_dest_dir,"node.sh") # self.node_remove_script = os.path.join(self.k8s_dest_dir,"remove.sh") # # def master(self,hostname): # # 安装 master 主节点 # self.remove(hostname) # Print("Start install k8s master {} ...".format(hostname),colour="green") # if os.path.isfile(LOG_FILE): # os.remove(LOG_FILE) # # common_status = local("bash {} {}".format(self.common_script,LOG_FILE)) # if common_status: # master_status = local("bash {} {} {}".format(self.master_script,LOG_FILE,self.flannel_yml)) # if master_status: # self.masterJoinInfo() # Print("Info: k8s cluster master {} install success.".format(hostname),colour="green") # else: # Print("Error: execute {} error,please check {} log file.".format(self.master_script,LOG_FILE),colour="red") # else: # Print("Error: execute {} error,please check {} log file".format(self.common_script,LOG_FILE),colour="red") # # def node(self,hostname): # # # 安装 k8s 集群 node 节点 # self.masterJoinInfo() # Print("Start install k8s node {} ...".format(hostname), colour="green") # self.remove(hostname) # sftp_upload(hostname=hostname, sourceFile=self.k8s_install_dir, destFile=self.k8s_dest_dir) # Print("Please view install log file {}:{}".format(hostname,LOG_FILE),hostname=hostname,colour="yellow") # Print("Please wait patiently while the program is being installed. It may take several minutes.",hostname=hostname,colour="yellow") # node_common_result = sudo(hostname, "/bin/bash {} {}".format(self.node_common_script, LOG_FILE)) # if node_common_result['status']: # node_script_result = sudo(hostname,"/bin/bash {} {} {} {} {}".format(self.node_script,LOG_FILE,self.master_join,self.token,self.ca_hash)) # if node_script_result['status']: # Print(node_script_result[hostname]['stdout'],hostname=hostname) # Print("node install success",hostname=hostname,colour="green") # else: # Print(node_script_result[hostname]['stderr'], hostname=hostname) # Print("node execute {} failed".format(self.node_script),hostname=hostname,colour="yellow") # else: # Print(node_common_result[hostname]['stderr'],hostname=hostname) # Print("node install failed.please check {}:{} log file.".format(hostname,LOG_FILE),hostname=hostname,colour="red") # # def remove(self,hostname): # # 安装之前,执行 kubeadm reset # ip_result = Utils.localCMPHostname(hostname) # if ip_result: # local("bash {}".format(self.remove_script)) # else: # run(hostname=hostname,cmd="bash {}".format(self.node_remove_script)) # # def masterJoinInfo(self): # # 从日志文件中拿到 master token ca_hash # self.master_join, self.token, self.ca_hash = getJoinInfo(LOG_FILE) # if self.master_join is not None and self.token is not None and self.ca_hash is not None: # return self.master_join, self.token, self.ca_hash # else: # Print("master_join,token,ca_hash not Found.please check {} log file".format(LOG_FILE),colour="red") # # def end(self): # # 安装完成之后,打印提示 # tips =""" # Tips: # Run 'kubectl get nodes' on the control-plane to see this node join the cluster. # If the slave node does not join the master node, the following program can be executed. # kubeadm join {} --token {} --discovery-token-ca-cert-hash {} # """.format(self.master_join, self.token, self.ca_hash) # Print(tips,colour="yellow")
def install_master(self): # 安装 ha install Print("Staring ha install Master {}".format(self.hostname), hostname=self.hostname, colour="green") Print("Please view install log file {}:{}".format( self.hostname, LOG_FILE), hostname=self.hostname, colour="yellow") commonHAMaster_script = os.path.join(ha_script, "commonHAMaster.sh") ha_master_script = os.path.join(ha_script, "ha_master.sh") add_master_script = os.path.join(ha_script, "add_master.sh") # kubeadm reset -f self.remove() if self.ip_result: result_status = local("bash {} {}".format(commonHAMaster_script, LOG_FILE)) Print( "Please wait patiently while the program is being installed. It may take several minutes.", hostname=self.hostname, colour="yellow") if result_status: self.generateHAconfig() status = local("bash {} {} {}".format(ha_master_script, kube_flannel_yml, LOG_FILE)) if status: Print("Install Master success", hostname=self.hostname, colour="green") else: Print("Install Master Failed", hostname=self.hostname, colour="red", type=True) else: Print( "{} install script Failed.".format(commonHAMaster_script), colour="red", type=True) else: Print( "Please wait patiently while the program is being installed. It may take several minutes.", hostname=self.hostname, colour="yellow") result_script = exec_script(self.hostname, script=commonHAMaster_script, args="{}".format(LOG_FILE), sudo=True) result_status = result_script['status'] if result_status: self.upload_HA_master_file() result_add_master_script = exec_script( hostname=self.hostname, script="{}".format(add_master_script)) try: if result_add_master_script['status']: Print( result_add_master_script[self.hostname]['stdout'], hostname=self.hostname) Print("{} add master success.".format( self.hostname, add_master_script), colour="green") except Exception as error: raise Exception("{} failed".format(add_master_script)) else: Print("{} exec failed".format(commonHAMaster_script), hostname=self.hostname, colour="yellow")