目录
create_deployment_yaml.py
#创建deployment,service脚本,用于创建新项目
create_deployment_yaml.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#########
BASE_YAML='''
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: %(JOB_NAME)s
namespace: apm
labels:
app: %(JOB_NAME)s
spec:
minReadySeconds: 10
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
replicas: %(POD_NUMBER)s
selector:
matchLabels:
app: %(JOB_NAME)s
template:
metadata:
name: %(JOB_NAME)s
labels:
app: %(JOB_NAME)s
version: v1
spec:
terminationGracePeriodSeconds: 30
imagePullSecrets:
- name: qcloudregistrykey
containers:
- name: %(JOB_NAME)s
image: %(IMAGE)s
imagePullPolicy: IfNotPresent
env:
- name: PATH
value: /usr/local/tomcat/bin:/opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
- name: JAVA_TOOL_OPTIONS
value: %(JAVA_TOOL_OPTIONS)s
- name: CATALINA_OPTS
value: %(CATALINA_OPTS)s
ports:
- containerPort: %(PORT)s
resources:
limits:
cpu: %(CPU_LIMITS)s
memory: %(MEM_LIMITS)s
requests:
cpu: %(CPU_REQUEST)s
memory: %(MEM_REQUEST)s
livenessProbe:
failureThreshold: 1
initialDelaySeconds: 60
periodSeconds: 3
successThreshold: 1
tcpSocket:
port: %(PORT)s
timeoutSeconds: 2
readinessProbe:
failureThreshold: 1
initialDelaySeconds: 60
periodSeconds: 3
successThreshold: 1
tcpSocket:
port: %(PORT)s
timeoutSeconds: 2
---
apiVersion: v1
kind: Service
metadata:
name: %(JOB_NAME)s
namespace: apm
labels:
app: %(JOB_NAME)s
spec:
selector:
app: %(JOB_NAME)s
type: ClusterIP
ports:
- name: tomcat
port: %(PORT)s
targetPort: %(PORT)s
'''
import os,sys
def create_yaml(BASE_YAML,FILE_PATH,JOB_NAME,POD_NUMBER,IMAGE,PORT,CPU_LIMITS,MEM_LIMITS,CPU_REQUEST,MEM_REQUEST,JAVA_TOOL_OPTIONS,CATALINA_OPTS):
#接收到的将参数传入到BASE_YAML的str中
BASE_YAML = BASE_YAML % {\
"JOB_NAME":str(JOB_NAME),\
"POD_NUMBER":str(POD_NUMBER),\
"IMAGE":str(IMAGE),"PORT":str(PORT),\
"CPU_LIMITS":str(CPU_LIMITS),\
"MEM_LIMITS":str(MEM_LIMITS),\
"CPU_REQUEST":str(CPU_REQUEST),\
"MEM_REQUEST":str(MEM_REQUEST),\
"JAVA_TOOL_OPTIONS":str(JAVA_TOOL_OPTIONS),\
"CATALINA_OPTS":str(CATALINA_OPTS)\
}
#拼接文件路径
FILE_PATH = FILE_PATH+"/"+JOB_NAME+".yaml"
#写入文件
file = open(FILE_PATH,"w")
file.write(BASE_YAML)
file.close()
print(BASE_YAML)
if __name__ == '__main__':
try:
FILE_PATH=sys.argv[1]
JOB_NAME=sys.argv[2]+"-online"
POD_NUMBER=sys.argv[3]
IMAGE=sys.argv[4]
PORT=sys.argv[5]
CPU_LIMITS=sys.argv[6]
MEM_LIMITS=sys.argv[7]
CPU_REQUEST=sys.argv[8]
MEM_REQUEST=sys.argv[9]
JAVA_TOOL_OPTIONS=sys.argv[10]
CATALINA_OPTS=sys.argv[11]
######下面注释为测试用#####
#FILE_PATH="/export"
#JOB_NAME="test"+"-online"
#POD_NUMBER="2"
#IMAGE="ccr.ccs.tencentyun.com/ywf-online/line:2020-0"
#PORT="8080"
#CPU_LIMITS="1000m"
#MEM_LIMITS="1Gi"
#CPU_REQUEST="250m"
#MEM_REQUEST="250Mi"
#JAVA_TOOL_OPTIONS="-XX:+IgnoreUnrecognizedVMOptions -XX:+UseContainerSupport -XX:+IdleTuningCompactOnIdle -XX:+IdleTuningGcOnIdle"
#CATALINA_OPTS="-server -XX:+UseContainerSupport -XX:MaxRAMPercentage=70 -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -Djava.security.egd=file:/dev/./urandom -Duser.timezone=GMT+08 -Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8 -Duser.timezone=Asia/Shangha"
create_yaml(BASE_YAML,FILE_PATH,JOB_NAME,POD_NUMBER,IMAGE,PORT,CPU_LIMITS,MEM_LIMITS,CPU_REQUEST,MEM_REQUEST,JAVA_TOOL_OPTIONS,CATALINA_OPTS)
except IndexError as e:
print("缺少参数")
exit()
jenkins_upload.py
#用于给Deployment更新镜像,用作项目上线
jenkins_upload.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#==================================================#
#使用到的三方库PyYaml,pip install PyYaml
#用法python3 [脚本] [job-name] [file-path] [new-image] [env-name] [new-env-value]
import yaml,json,os,sys
class Change_yaml:
''' '''
def __init__(self,job_name,file_path,namespace):
self.job_name=job_name
self.file_path=file_path+"/"+job_name+"-online.yaml"
self.namespace=namespace
def get_yaml_file(self):
#通过kubectl将yaml文件输出到指定文件
command="kubectl get deployment/"+job_name+"-online"+" -n "+self.namespace+" -o yaml "+"> "+str(self.file_path)
try:
os.system(command)
result = True
except:
result = False
return result
def file_content(self):
#打开yaml文件读取
file = open(self.file_path,"r",encoding='UTF-8')
file_content = file.read()
file.close()
return file_content
def file_write(self,dict_yaml):
#写入文件
file = open(self.file_path,"w")
file.write(dict_yaml)
file.close()
def yaml_dict(self,yaml_content):
#将读取到的Yaml文件转换为字典格式
yaml_dict = yaml.load(yaml_content,Loader=yaml.FullLoader)
return yaml_dict
def change_dict_image(self,yaml_dict,new_image):
#修改镜像
yaml_dict["spec"]["template"]["spec"]["containers"][0]["image"]=new_image
#print(yaml_dict["spec"]["template"]["spec"]["containers"][0]["image"])
return yaml_dict
def change_dict_env(self,yaml_dict,env_name,new_env_value):
#修改变量
number=0
yaml_dict_env=yaml_dict["spec"]["template"]["spec"]["containers"][0]["env"]
for env in yaml_dict_env:
if env["name"] == env_name:
old_env_value = yaml_dict["spec"]["template"]["spec"]["containers"][0]["env"][number]["value"]
if old_env_value == new_env_value:
pass
else:
yaml_dict["spec"]["template"]["spec"]["containers"][0]["env"][number]["value"] = new_env_value
number+=1
return yaml_dict
def dict_yaml(self,yaml_dict):
#将dict转换为yaml文件
new_yaml_str = yaml.dump(yaml_dict)
return new_yaml_str
if __name__ == '__main__':
namespace = "apm"
job_name = sys.argv[1]
file_path = sys.argv[2]
new_image = sys.argv[3]
env_name = sys.argv[4]
new_env_value = sys.argv[5]
class_func=Change_yaml(job_name,file_path,namespace)
#拉取YAML文件
class_func.get_yaml_file()
#定义类
yaml_content = class_func.file_content()
#将YAML转换为dict
yaml_dict = class_func.yaml_dict(yaml_content)
#修改image
yaml_dict = class_func.change_dict_image(yaml_dict,new_image)
#修改ENV
yaml_dict = class_func.change_dict_env(yaml_dict,env_name,new_env_value)
#将YAML转换为dict
dict_yaml = class_func.dict_yaml(yaml_dict)
#将修改后的字符写入到文件中
class_func.file_write(dict_yaml)
get_image_tags.py
#用于从腾讯云上下拉镜像标签,配合Jenkins完成回滚
get_image_tags.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#用法python3 [this_file] [image_warehouse] [file_path]
#该脚本实现对腾讯云上镜像仓库中的镜像标签进行抓取,以“$key$=[tag1],[tag2]”方式存储到文件内,配合Jenkins实现参数化构建
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.tcr.v20190924 import tcr_client, models
import json,sys
class Update_image_tag_file:
def __init__(self,image_job,file_path):
self.image_job=image_job
self.file_path=file_path
#调用腾讯云镜像仓库的接口获取某一镜像中所有tag信息,输出list
def get_image_info(self):
try:
#下面是腾讯云许可的账号密码
cred = credential.Credential("###################", "@@@@@@@@@@@")
httpProfile = HttpProfile()
httpProfile.endpoint = "tcr.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = tcr_client.TcrClient(cred, "ap-beijing", clientProfile)
req = models.DescribeImagePersonalRequest()
params = '{"RepoName":"%s"}' % self.image_job
req.from_json_string(params)
resp = client.DescribeImagePersonal(req)
output = json.loads(resp.to_json_string())
return output
except TencentCloudSDKException as err:
return False
#将腾讯云接口获取的镜像tag信息作为参数传递,对数据进行处理,输出str
def get_image_tags(self,image_info):
image_str = ""
image_list = list(image_info["Data"]["TagInfo"])
number=0
for image in image_list:
if number < 10:
tag = image["TagName"]
image_str = image_str+tag+","
number+=1
image_str = "$key$="+image_str[:-1]
return image_str
#将处理好的数据作为参数传递,写入到指定的文件内
def rwrite_file(self,image_tags):
file = open(self.file_path,'w')
file.write(image_tags)
file.close()
return True
image_job=sys.argv[1]
file_path=sys.argv[2]
if __name__ == '__main__':
obj_class = Update_image_tag_file(image_job,file_path)
image_info = obj_class.get_image_info()
#判断是否获取到镜像信息,无则返回False
if image_info != False:
image_tags = obj_class.get_image_tags(image_info)
obj_class.rwrite_file(image_tags)
print("Success update image_tags_file!")
else:
obj_class.rwrite_file("$key$=")
print("Have no image_warehouse!")