#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2021/12/22 16:58 import os ServiceName = raw_input('输入服务名,例:Assess.Service :') def Deployment_name(value): result = value.replace('.', '-').lower() return result def LogPath(): Deploy = Deployment_name(ServiceName) Service_type = str(Deploy.split('-')[-1]) if Service_type == 'service' or Service_type == 'web': PodLogDir = "var/www/logs" return PodLogDir else: PodLogDir = "var/www/bin/Debug/logs" return PodLogDir def search_Svrname(ServiceName): DeployName = Deployment_name(ServiceName) PodLogDir = LogPath() PodName = "".join(os.popen('kubectl get pod -A | grep -i %s'%(ServiceName)).readlines()) print PodName Select_Ns = raw_input("输入服务所在命名空间cbc/cbe/common/zebra/ants:") Select_PodName = "".join(os.popen("kubectl get pod -n %s | grep -i %s |awk '{print $1}'"%(Select_Ns,ServiceName)).readlines()) Pod_list = [] for i in Select_PodName.split('\n'): if i == '': continue Pod_list.append(i) print Pod_list PodLogName = raw_input("输入查询日志的PodName :") # PodLogMsg = "".join(os.popen('kubectl -n %s exec -c %s %s ls /var/www/bin/Debug/logs '%(Select_Ns,DeployName,PodLogName)).readlines()) # if PodLogMsg in "No such file or directory": # print "/var/www/bin/Debug/logs 容器此目录不存在" #PodLogDir = "var/www/bin/Debug/logs" if os.path.exists("/root/log/K8sPod_log/%s"%PodLogName) == False: #print "LocalPath no exists /root/log/K8sPod_log/%s"%PodLogName os.mkdir("/root/log/K8sPod_log/%s"%PodLogName) LocalLogDir = "/root/log/K8sPod_log/%s"%PodLogName result = "".join(os.popen('kubectl cp -n %s %s:%s /%s'%(Select_Ns,PodLogName,PodLogDir,LocalLogDir))) print (Select_Ns,Select_PodName,PodLogDir,LocalLogDir) print "运行完毕%s"%(result) else: LocalLogDir = "/root/log/K8sPod_log/%s" % PodLogName result = "".join(os.popen('kubectl cp -n %s %s:%s /%s'%(Select_Ns,PodLogName,PodLogDir,LocalLogDir))) print (Select_Ns, Select_PodName, PodLogDir, LocalLogDir) print "运行完毕%s"%(result) search_Svrname(ServiceName)