def sudo(c, cmd, nodename): # 初始化远程工具对象 robj = Remote(getEnv(nodename)) robj.sudo(cmd) print("Exec Success.")
def installWeaveNodeAfter(self): admin_env = self.getEnv("admin") # 初始化远程工具对象 robj_admin = Remote(admin_env) weaveConfigPath = self.getLocalPath('weaveConfigPath') remotePluginPath = self.getRemotePath('remotePluginPath') remoteBinPath = self.getRemotePath('remoteBinPath') tmpPath = self.getLocalPath('tmpPath') cmdRemote = remoteBinPath + "/kubectl delete -f " + remotePluginPath + "/weave/weave.yaml" robj_admin.sudo(cmdRemote) #upload yaml file ips_data = self.config_object.get_node_by_attr("name", "ips") podClusterIpRange = ips_data.find('podClusterIpRange').text.replace( "/", "\/") sedRegex = "sed \"s/{podClusterIpRange}/%s/g\"" % podClusterIpRange file_tmp = tmpPath + "/weave.yaml" file_config = weaveConfigPath + "/weave.yaml" cmd_local = "cat %s | %s > %s" % (file_config, sedRegex, file_tmp) robj_admin.local(cmd_local) # 上传文件到远程主机 robj_admin.upload(file_tmp, remotePluginPath + "/weave/", True) #install weave plugin # yamlWeave="https://cloud.weave.works/k8s/net?k8s-version=\$(${remoteBinPath}/kubectl version | base64 | tr -d '\\\n')" cmdRemote = remoteBinPath + "/kubectl apply -f " + remotePluginPath + "/weave/weave.yaml" robj_admin.sudo(cmdRemote)
def validIngress(self): admin_env = self.getEnv("admin") # 初始化远程工具对象 robj_admin = Remote(admin_env) remoteBinPath = self.getRemotePath('remoteBinPath') # remotePluginPath = self.getRemotePath('remotePluginPath') # corednsConfigPath = self.getLocalPath('corednsConfigPath') #get namespaces cmdRemote = remoteBinPath + "/kubectl get ns" robj_admin.sudo(cmdRemote) #get pod list in namespace ingress-nginx cmdRemote = remoteBinPath + "/kubectl get rs,pods,svc -n ingress-nginx -o wide" robj_admin.sudo(cmdRemote) #get pod list in namespace default cmdRemote = remoteBinPath + "/kubectl get rs,pods,svc -n default -o wide" robj_admin.sudo(cmdRemote) #get process list listenning on 80 and 443 on target node # cmdRemote = "netstat -tnlp | egrep \"80|443\"" #test myapp service cluterip in deploy node #curl {clusterIp} #get ingress cmdRemote = remoteBinPath + "/kubectl get ingress" robj_admin.sudo(cmdRemote)
def installWeaveNodeNormal(self, nodename): node_env = self.getEnv(nodename) # 初始化远程工具对象 robj_node = Remote(node_env) #install bridge tools cmdRemote = "yum install -y bridge-utils" robj_node.sudo(cmdRemote)
def validCniNodeNormal(self, nodename): node_env = self.getEnv(nodename) # 初始化远程工具对象 robj_node = Remote(node_env) print("check kubelet service ...") cmdRemote = "systemctl status kube-kubelet.service" robj_node.sudo(cmdRemote)
def deleteWeave(self): admin_env = self.getEnv("admin") # 初始化远程工具对象 robj_admin = Remote(admin_env) remoteBinPath = self.getRemotePath('remoteBinPath') remotePluginPath = self.getRemotePath('remotePluginPath') cmdRemote = remoteBinPath + "/kubectl delete -f " + remotePluginPath + "/weave/weave.yaml" robj_admin.sudo(cmdRemote)
def deleteDockerPrivate(self): admin_env = self.getEnv("admin") # 初始化远程工具对象 robj_admin = Remote(admin_env) remoteBinPath = self.getRemotePath('remoteBinPath') remoteCfgPath = self.getRemotePath('remoteCfgPath') cmdRemote = remoteBinPath + "/kubectl delete -f " + remoteCfgPath + "/registry-pull-secret.yaml" robj_admin.sudo(cmdRemote)
def validDemoDockerPrivate(self): admin_env = self.getEnv("admin") # 初始化远程工具对象 robj_admin = Remote(admin_env) remoteBinPath = self.getRemotePath('remoteBinPath') #view cmdRemote = remoteBinPath + "/kubectl get pods busybox-demo -o wide" robj_admin.sudo(cmdRemote)
def deleteMetricsServer(self): admin_env = self.getEnv("admin") # 初始化远程工具对象 robj_admin = Remote(admin_env) remoteBinPath = self.getRemotePath('remoteBinPath') remotePluginPath = self.getRemotePath('remotePluginPath') cmdRemote = remoteBinPath + "/kubectl delete -f " cmdRemote = cmdRemote + remotePluginPath + "/metrics/" robj_admin.sudo(cmdRemote)
def deleteDashboard(self): admin_env = self.getEnv("admin") # 初始化远程工具对象 robj_admin = Remote(admin_env) remoteBinPath = self.getRemotePath('remoteBinPath') remotePluginPath = self.getRemotePath('remotePluginPath') cmdRemote = remoteBinPath + "/kubectl delete -f " cmdRemote = cmdRemote + remotePluginPath + "/dashboard/kubernetes-dashboard.yaml" robj_admin.sudo(cmdRemote)
def upload(c, dirlocal, dirdes, nodename): print(dirlocal) print(nodename) # 初始化远程工具对象 robj = Remote(getEnv(nodename)) # 上传文件到远程主机 robj.upload(dirlocal, dirdes) print("Upload Success.")
def deleteIngress(self): admin_env = self.getEnv("admin") # 初始化远程工具对象 robj_admin = Remote(admin_env) remotePluginPath = self.getRemotePath('remotePluginPath') remoteBinPath = self.getRemotePath('remoteBinPath') #delete ingress print("delete ingress ...") cmdRemote = remoteBinPath + "/kubectl delete -f " + remotePluginPath + "/ingress/mandatory.yaml" robj_admin.sudo(cmdRemote)
def deleteDemoIngressHttps(self): admin_env = self.getEnv("admin") # 初始化远程工具对象 robj_admin = Remote(admin_env) remotePluginPath = self.getRemotePath('remotePluginPath') remoteBinPath = self.getRemotePath('remoteBinPath') # remoteSslPath = self.getRemotePath('remoteSslPath') #delete ingress rules of https print("delete ingress rules of https ...") cmdRemote = remoteBinPath + "/kubectl delete -f "+remotePluginPath+"/ingress/nginx-ingress-rules-https.yaml" robj_admin.sudo(cmdRemote)
def installNginx(self): admin_env = self.getEnv("admin") # 初始化远程工具对象 robj_admin = Remote(admin_env) remoteBinPath = self.getRemotePath('remoteBinPath') #uninstall old nginx pod and service # cmdRemote = remoteBinPath + "/kubectl delete -f "+remoteDemoPath+"/nginx-demo-deployment.yaml" # robj_master1.sudo(cmdRemote) #upload nginx-demo-deployment.yaml to master # robj_master1.upload(kbsConfigPath+"/nginx-demo-deployment.yaml", remoteDemoPath+"/") # create pod # cmdRemote = remoteBinPath + "/kubectl apply -f "+remoteDemoPath+"/nginx-demo-deployment.yaml" # robj_master1.sudo(cmdRemote) cmdRemote = remoteBinPath + "/kubectl run nginx --image=nginx --replicas=3" robj_admin.sudo(cmdRemote) cmdRemote = remoteBinPath + "/kubectl expose deployment nginx --port=88 --target-port=80 --type=NodePort" robj_admin.sudo(cmdRemote) # list pod and service cmdRemote = remoteBinPath + "/kubectl get pod,svc -o wide" robj_admin.sudo(cmdRemote)
def rmRemoteFile(c, filename, nodename): # 初始化远程工具对象 robj = Remote(getEnv(nodename)) # 删除远程文件 result = robj.rmfile(filename) if result == False: print("failed to delete remote file.") if not confirm("failed to delete remote file, Continue[Y/N]?"): return True else: print("succeed to delete remote file.") return True
def download(c, dirremote, dirlocal, nodename): # 初始化远程工具对象 robj = Remote(getEnv(nodename)) # 下载文件到本地 result = robj.download(dirremote, dirlocal) if result == False: print("Download Failed.") # if not confirm("Failed to download file, Continue[Y/N]?"): # return True else: print("Download Success.") return True
def checkRemotePath(c, path, nodename): # 初始化远程工具对象 robj = Remote(getEnv(nodename)) # 下载文件到本地 result = robj.checkpath(path) if result == False: print("Remote Path not exists.") if not confirm("Path not exists in remote machine, Continue[Y/N]?"): return True else: print("Remote Path exists.") return True
def deleteDemoDockerPrivate(self): admin_env = self.getEnv("admin") # 初始化远程工具对象 robj_admin = Remote(admin_env) # docker_data = self.config_object.get_node_by_attr("name", "docker") # dockerUsername = docker_data.find('username').text # dockerPassword = docker_data.find('password').text # dockerDomain = docker_data.find('domain').text remoteBinPath = self.getRemotePath('remoteBinPath') remotePluginPath = self.getRemotePath('remotePluginPath') # dockerConfigPath = self.getLocalPath('dockerConfigPath') #uninstall old service cmdRemote = remoteBinPath + "/kubectl delete -f "+remotePluginPath+"/docker/busybox-demo.yaml" print(cmdRemote) robj_admin.sudo(cmdRemote)
def installDemoIngress(self): admin_env = self.getEnv("admin") # 初始化远程工具对象 robj_admin = Remote(admin_env) remoteBinPath = self.getRemotePath('remoteBinPath') remotePluginPath = self.getRemotePath('remotePluginPath') ingressConfigPath = self.getLocalPath('ingressConfigPath') #upload ingress backend robj_admin.upload(ingressConfigPath+"/nginx-ingress-backend.yaml", remotePluginPath+"/ingress/", True) #install ingress backend cmdRemote = remoteBinPath + "/kubectl apply -f "+remotePluginPath+"/ingress/nginx-ingress-backend.yaml" robj_admin.sudo(cmdRemote) #upload ingress rules robj_admin.upload(ingressConfigPath+"/nginx-ingress-rules.yaml", remotePluginPath+"/ingress/", True) #install ingress rules cmdRemote = remoteBinPath + "/kubectl apply -f "+remotePluginPath+"/ingress/nginx-ingress-rules.yaml" robj_admin.sudo(cmdRemote)
def installDashboard(self): admin_env = self.getEnv("admin") # 初始化远程工具对象 robj_admin = Remote(admin_env) dashboardConfigPath = self.getLocalPath('dashboardConfigPath') remoteBinPath = self.getRemotePath('remoteBinPath') remotePluginPath = self.getRemotePath('remotePluginPath') #label admin node for install dashboard cmdRemote = remoteBinPath + "/kubectl label node " + admin_env.host + " role=admin" robj_admin.sudo(cmdRemote) cmdRemote = remoteBinPath + "/kubectl delete -f " cmdRemote = cmdRemote + remotePluginPath + "/dashboard/kubernetes-dashboard.yaml" robj_admin.sudo(cmdRemote) robj_admin.upload(dashboardConfigPath + "/kubernetes-dashboard.yaml", remotePluginPath + "/dashboard/") cmdRemote = remoteBinPath + "/kubectl apply -f " cmdRemote = cmdRemote + remotePluginPath + "/dashboard/kubernetes-dashboard.yaml" robj_admin.sudo(cmdRemote)
def deleteNginx(self): admin_env = self.getEnv("admin") # 初始化远程工具对象 robj_admin = Remote(admin_env) remoteBinPath = self.getRemotePath('remoteBinPath') cmdRemote = remoteBinPath + "/kubectl delete deployment nginx" robj_admin.sudo(cmdRemote) cmdRemote = remoteBinPath + "/kubectl delete service nginx" robj_admin.sudo(cmdRemote)
def validWeaveNodeAll(self): admin_env = self.getEnv("admin") # 初始化远程工具对象 robj_admin = Remote(admin_env) remoteBinPath = self.getRemotePath('remoteBinPath') # 在远端主机执行命令 print("list weave net pods: ") cmdRemote = remoteBinPath + "/kubectl get pods,services -n kube-system -l name=weave-net -o wide" robj_admin.sudo(cmdRemote) cmdRemote = remoteBinPath + "/kubectl get pods -n kube-system -l name=weave-net" cmdRemote = cmdRemote + " | awk 'NR==2 {print $1}'" robj_admin.sudo(cmdRemote) # kubectl exec -n kube-system {podname} -c weave -- /home/weave/weave --local status cmdRemote = remoteBinPath + "/kubectl exec -c weave -n kube-system $(%s)" % cmdRemote cmdRemote = cmdRemote + " -- /home/weave/weave --local status" print(cmdRemote) robj_admin.sudo(cmdRemote)
def validMetricsServer(self): admin_env = self.getEnv("admin") # 初始化远程工具对象 robj_admin = Remote(admin_env) remoteBinPath = self.getRemotePath('remoteBinPath') #list pod and service cmdRemote = remoteBinPath + "/kubectl get pods -l k8s-app=metrics-server -n kube-system -o wide" robj_admin.sudo(cmdRemote) cmdRemote = remoteBinPath + "/kubectl get apiservice v1beta1.metrics.k8s.io -o yaml " robj_admin.sudo(cmdRemote)
def installIngress(self): admin_env = self.getEnv("admin") # 初始化远程工具对象 robj_admin = Remote(admin_env) remotePluginPath = self.getRemotePath('remotePluginPath') remoteBinPath = self.getRemotePath('remoteBinPath') # tmpPath = self.getLocalPath('tmpPath') ingressConfigPath = self.getLocalPath('ingressConfigPath') #remove cmdRemote = remoteBinPath + "/kubectl delete -f " + remotePluginPath + "/ingress/mandatory.yaml" robj_admin.sudo(cmdRemote) #upload yaml file robj_admin.upload(ingressConfigPath + "/mandatory.yaml", remotePluginPath + "/ingress/", True) #install cmdRemote = remoteBinPath + "/kubectl apply -f " + remotePluginPath + "/ingress/mandatory.yaml" robj_admin.sudo(cmdRemote)
def deleteDemoBusybox(self): admin_env = self.getEnv("admin") # 初始化远程工具对象 robj_admin = Remote(admin_env) remotePluginPath = self.getRemotePath('remotePluginPath') remoteBinPath = self.getRemotePath('remoteBinPath') # tmpPath = self.getLocalPath('tmpPath') # corednsConfigPath = self.getLocalPath('corednsConfigPath') #uninstall old service print("remove busybox container ...") cmdRemote = remoteBinPath + "/kubectl delete -f "+remotePluginPath+"/coredns/busybox.yaml" robj_admin.sudo(cmdRemote) print("sleep 5 seconds ...") robj_admin.sudo("sleep 5s") #view print("list pods of busybox ...") cmdRemote = remoteBinPath + "/kubectl get pods -o wide" robj_admin.sudo(cmdRemote)
def deleteDemoIngress(self): admin_env = self.getEnv("admin") # 初始化远程工具对象 robj_admin = Remote(admin_env) remoteBinPath = self.getRemotePath('remoteBinPath') remotePluginPath = self.getRemotePath('remotePluginPath') # ingressConfigPath = self.getLocalPath('ingressConfigPath') # remove ingress rules cmdRemote = remoteBinPath + "/kubectl delete -f "+remotePluginPath+"/ingress/nginx-ingress-rules.yaml" robj_admin.sudo(cmdRemote) # remove ingress backend cmdRemote = remoteBinPath + "/kubectl delete -f "+remotePluginPath+"/ingress/nginx-ingress-backend.yaml" robj_admin.sudo(cmdRemote)
def validDemoIngress(self): admin_env = self.getEnv("admin") # 初始化远程工具对象 robj_admin = Remote(admin_env) # remotePluginPath = self.getRemotePath('remotePluginPath') remoteBinPath = self.getRemotePath('remoteBinPath') # remoteSslPath = self.getRemotePath('remoteSslPath') # ingressConfigPath = self.getLocalPath('ingressConfigPath') #get ingress cmdRemote = remoteBinPath + "/kubectl get ingress" robj_admin.sudo(cmdRemote) #describe inress cmdRemote = remoteBinPath + "/kubectl describe ingress simple-fanout-example" robj_admin.sudo(cmdRemote)
def validKbsMaster(self): admin_env = self.getEnv("admin") # 初始化远程工具对象 robj_admin = Remote(admin_env) remoteBinPath = self.getRemotePath('remoteBinPath') # 在远端主机执行命令 print("show apiserver info in port 6443: ") cmdRemote = "curl -L --cacert /opt/kubernetes/ssl/ca.pem" cmdRemote = cmdRemote + " https://" + self.proxy_host + ":" + self.proxy_port + "/api" robj_admin.sudo(cmdRemote) # print("show apiserver info in port 8080: ") # cmdValid="curl -L http://"+proxy_host+":8080/api" # robj_admin.sudo(cmdValid) print("show cluster info by kubectl: ") cmdValid = remoteBinPath + "/kubectl get cs" robj_admin.sudo(cmdValid)
def installDemoDockerPrivate(self): admin_env = self.getEnv("admin") # 初始化远程工具对象 robj_admin = Remote(admin_env) docker_data = self.config_object.get_node_by_attr("name", "docker") dockerUsername = docker_data.find('username').text dockerPassword = docker_data.find('password').text dockerDomain = docker_data.find('domain').text remoteBinPath = self.getRemotePath('remoteBinPath') remotePluginPath = self.getRemotePath('remotePluginPath') dockerConfigPath = self.getLocalPath('dockerConfigPath') #get docker secret cmdRemote = remoteBinPath + "/kubectl get secret" robj_admin.sudo(cmdRemote) #valid busyboxAddress = "registry.cn-beijing.aliyuncs.com/ducafe/busybox" cmdLocal = "docker images | grep %s | awk '{print $3}'" % busyboxAddress robj_admin.local(cmdLocal) imageId = robj_admin.getResult().stdout.rstrip() # print(robj_admin.getResult().exited) if imageId == "": print("upload busybox image to aliyun repository ...") cmdLocal = "docker pull registry.cn-beijing.aliyuncs.com/ducafe/busybox:1.24" robj_admin.local(cmdLocal) cmdLocal = "docker images | grep %s | awk '{print $3}'" % busyboxAddress robj_admin.local(cmdLocal) imageId = robj_admin.getResult().stdout.rstrip() #login docker registry cmdLocal = "docker login --username=%s --password=%s %s" % (dockerUsername, dockerPassword, dockerDomain) robj_admin.local(cmdLocal) cmdLocal = "docker tag %s %s/kube-systems/busybox-demo:1.24" % (imageId, dockerDomain) robj_admin.local(cmdLocal) cmdLocal = "docker images" robj_admin.local(cmdLocal) cmdLocal = "docker push %s/kube-systems/busybox-demo:1.24" % dockerDomain robj_admin.local(cmdLocal) #upload robj_admin.upload(dockerConfigPath+"/busybox-demo.yaml", remotePluginPath+"/docker/") #install cmdRemote = remoteBinPath + "/kubectl create -f "+remotePluginPath+"/docker/busybox-demo.yaml" robj_admin.sudo(cmdRemote) cmdRemote = "sleep 2s" robj_admin.sudo(cmdRemote)
def installDemoIngressHttps(self): admin_env = self.getEnv("admin") # 初始化远程工具对象 robj_admin = Remote(admin_env) remotePluginPath = self.getRemotePath('remotePluginPath') remoteBinPath = self.getRemotePath('remoteBinPath') remoteSslPath = self.getRemotePath('remoteSslPath') ingressConfigPath = self.getLocalPath('ingressConfigPath') # tls support domainName = "sslexample.foo.com" secretName = domainName.replace(".", "-") print("generate tls key file ...") keyFile = remoteSslPath + "/tls-ingress-"+secretName+".key" certFile = remoteSslPath + "/tls-ingress-"+secretName+".crt" #generate tls key cmdRemote = "openssl genrsa -out "+keyFile+" 2048" robj_admin.sudo(cmdRemote) #generate tls cert print("generate tls cert file ...") cmdRemote = "openssl req -new -x509 -key "+keyFile+" -out "+certFile cmdRemote = cmdRemote + " -subj /C=CN/ST=Beijing/L=Beijing/O=DevOps/CN="+domainName robj_admin.sudo(cmdRemote) #generate secret print("generate secret ...") cmdRemote = remoteBinPath + "/kubectl create secret tls "+secretName cmdRemote = cmdRemote + " --cert="+certFile+" --key="+keyFile robj_admin.sudo(cmdRemote) #get secret print("get secret ...") cmdRemote = remoteBinPath + "/kubectl get secret" robj_admin.sudo(cmdRemote) #describe secret print("describe secret ...") cmdRemote = remoteBinPath + "/kubectl describe secret "+secretName robj_admin.sudo(cmdRemote) # remove ingress rules of https print("remove ingress rules of https ...") cmdRemote = remoteBinPath + "/kubectl delete -f " cmdRemote = cmdRemote + remotePluginPath + "/ingress/nginx-ingress-rules-https.yaml" robj_admin.sudo(cmdRemote) #upload ingress rules of https print("upload ingress rules yaml ...") robj_admin.upload(ingressConfigPath+"/nginx-ingress-rules-https.yaml", remotePluginPath+"/ingress/") #install ingress rules of https print("install ingress rules ...") cmdRemote = remoteBinPath + "/kubectl apply -f " cmdRemote = cmdRemote + remotePluginPath+"/ingress/nginx-ingress-rules-https.yaml" robj_admin.sudo(cmdRemote)