0%

Python-调用阿里云API实现Swarm集群应用的更新或重启

需求

调用阿里云API实现 根据docker compose文件进行Swarm集群容器应用的更新、重启

脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#调用阿里容器api
'''
alics.py
参数1 操作 update restart
参数2 集群名称 pre test pro
参数2 应用名称
参数3 dockercompose文件的绝对路径
参数4 时间戳
'''
import requests
import sys
import json
import time
import sys
reload(sys)
sys.setdefaultencoding("utf-8")

Action=sys.argv[1]
TimeStamp=str(time.time())

def UpdataService(ClusterName,Service,File,TimeStamp):
with open(File) as file:
contents = file.read().replace('\n', '\r\n')
data = {
"template": contents,
"version": TimeStamp,
}
if ClusterName == "pre":
print '预发环境的 %s 应用进行更新' % Service
url = 'https://master2g11.cs.cn-beijing.aliyuncs.com:20060/projects/' + Service + '/update'
verify = '/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/key.pem')
elif ClusterName == "pro":
print '生产环境的 %s 应用进行更新' % Service
url = 'https://master4g5.cs-cn-beijing.aliyun.com:20045/projects/' + Service + '/update'
verify = '/jenkins/vcgapp/ops/sdk/swarm/vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/vcg-com/key.pem')
elif ClusterName == "test":
print '测试环境的 %s 应用进行更新' % Service
url = 'https://master1g8.cs-cn-hangzhou.aliyun.com:20034/projects/' + Service + '/update'
verify = '/jenkins/vcgapp/ops/sdk/swarm/test-vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/test-vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/test-vcg-com/key.pem')
res = requests.post(url,data=json.dumps(data),verify=verify,cert=cert)
print res
print res.text
if '202' in str(res):
print '\n应用更新请求已发送到阿里云容器服务!\n'
sys.exit(0)
else:
print '更新失败,请查看compose文件是否缺少参数'
sys.exit(1)

def CreateService(ClusterName,Service,File,TimeStamp):
with open(File) as file:
contents = file.read().replace('\n', '\r\n')
data = {
"name": Service,
"template": contents,
"version": TimeStamp,
}
if ClusterName == "pre":
print '创建预发环境的 %s 应用' % Service
url = 'https://master2g11.cs.cn-beijing.aliyuncs.com:20060/projects/'
verify = '/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/key.pem')
elif ClusterName == "pro":
print '创建生产环境的 %s 应用' % Service
url = 'https://master4g5.cs-cn-beijing.aliyun.com:20045/projects/'
verify = '/jenkins/vcgapp/ops/sdk/swarm/vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/vcg-com/key.pem')
elif ClusterName == "test":
print '创建测试环境的 %s 应用' % Service
url = 'https://master1g8.cs-cn-hangzhou.aliyun.com:20034/projects/'
verify = '/jenkins/vcgapp/ops/sdk/swarm/test-vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/test-vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/test-vcg-com/key.pem')
res = requests.post(url,data=json.dumps(data),verify=verify,cert=cert)
print res
print res.text
if '201' in str(res):
print '\n应用更新请求已发送到阿里云容器服务!\n'
sys.exit(0)
else:
print '更新失败,请查看compose文件是否缺少参数'
sys.exit(1)


def RestartService(ClusterName,Service):
if ClusterName == "pre":
print '预发环境的 %s 应用进行重启' % Service
url = 'https://master2g11.cs.cn-beijing.aliyuncs.com:20060/projects/' + Service + '/restart'
verify = '/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/key.pem')
elif ClusterName == "pro":
print '生产环境的 %s 应用进行重启' % Service
url = 'https://master4g5.cs-cn-beijing.aliyun.com:20045/projects/' + Service + '/restart'
verify = '/jenkins/vcgapp/ops/sdk/swarm/vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/vcg-com/key.pem')
elif ClusterName == "test":
print '测试环境的 %s 应用进行重启' % Service
url = 'https://master1g8.cs-cn-hangzhou.aliyun.com:20034/projects/' + Service + '/restart'
verify = '/jenkins/vcgapp/ops/sdk/swarm/test-vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/test-vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/test-vcg-com/key.pem')
while True:
res = requests.post(url,verify=verify,cert=cert)
print res
print res.text
if '200' in str(res):
print '\n应用重启完成!\n'
break
else:
print '程序正在更新,10秒后重试!'
time.sleep( 10 )

def GetService(ClusterName,Service):
if ClusterName == "pre":
print '获取预发环境的 %s 应用的信息' % Service
url = 'https://master2g11.cs.cn-beijing.aliyuncs.com:20060/projects/' + Service
verify = '/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/key.pem')
elif ClusterName == "pro":
print '获取生产环境的 %s 应用的信息' % Service
url = 'https://master4g5.cs-cn-beijing.aliyun.com:20045/projects/' + Service
verify = '/jenkins/vcgapp/ops/sdk/swarm/vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/vcg-com/key.pem')
elif ClusterName == "test":
print '获取测试环境的 %s 应用的信息' % Service
url = 'https://master1g8.cs-cn-hangzhou.aliyun.com:20034/projects/' + Service
verify = '/jenkins/vcgapp/ops/sdk/swarm/test-vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/test-vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/test-vcg-com/key.pem')
res = requests.get(url,verify=verify,cert=cert)
print res
print json.loads(res.text)

def RedeployService(ClusterName,Service):
if ClusterName == "pre":
print '预发环境的 %s 应用进行重新部署' % Service
url = 'https://master2g11.cs.cn-beijing.aliyuncs.com:20060/projects/' + Service + '/redeploy'
verify = '/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/key.pem')
elif ClusterName == "pro":
print '生产环境的 %s 应用进行重新部署' % Service
url = 'https://master4g5.cs-cn-beijing.aliyun.com:20045/projects/' + Service + '/redeploy'
verify = '/vcgapp/ops/sdk/swarm/vcg-com/ca.pem'
cert = ('/vcgapp/ops/sdk/swarm/vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/vcg-com/key.pem')
elif ClusterName == "test":
print '测试环境的 %s 应用进行重新部署' % Service
url = 'https://master1g8.cs-cn-hangzhou.aliyun.com:20034/projects/' + Service + '/redeploy'
verify = '/jenkins/vcgapp/ops/sdk/swarm/test-vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/test-vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/test-vcg-com/key.pem')
res = requests.post(url,verify=verify,cert=cert)
print res
print res.text

def GetComposeService(ClusterName,Service):
if ClusterName == "pre":
File = '/jenkins/vcgapp/compose-back/pre/' + Service
print '获取预发环境的 %s 应用的Compose文件,保存本地后的文件为 %s' % (Service,File)
url = 'https://master2g11.cs.cn-beijing.aliyuncs.com:20060/projects/' + Service
verify = '/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/cert.pem', '/vcgapp/ops/sdk/swarm/pre-vcg-com/key.pem')
elif ClusterName == "pro":
File = '/jenkins/vcgapp/compose-back/pro/' + Service
print '获取生产环境的 %s 应用的Compose文件,保存本地后的文件为 %s' % (Service,File)
url = 'https://master4g5.cs-cn-beijing.aliyun.com:20045/projects/' + Service
verify = '/jenkins/vcgapp/ops/sdk/swarm/vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/vcg-com/cert.pem', '/vcgapp/ops/sdk/swarm/vcg-com/key.pem')
res = requests.get(url,verify=verify,cert=cert)
with open(File,'w') as file:
file.write(json.loads(res.text).get('template').replace('\r\n','\n'))

def GetAllService(ClusterName):
if ClusterName == "pre":
print '获取预发环境所有应用的信息'
url = 'https://master2g11.cs.cn-beijing.aliyuncs.com:20060/projects/'
verify = '/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/pre-vcg-com/key.pem')
elif ClusterName == "pro":
print '获取生产环境所有应用的信息'
url = 'https://master4g5.cs-cn-beijing.aliyun.com:20045/projects/'
verify = '/jenkins/vcgapp/ops/sdk/swarm/vcg-com/ca.pem'
cert = ('/jenkins/vcgapp/ops/sdk/swarm/vcg-com/cert.pem', '/jenkins/vcgapp/ops/sdk/swarm/vcg-com/key.pem')
res = requests.get(url,verify=verify,cert=cert)
print res
#print res.text
for i in json.loads(res.text):
#print i.get('template').replace('\r\n','\n')
if ClusterName == "pre":
File = '/jenkins/vcgapp/compose-back/pre/' + i.get('name')
elif ClusterName == "pro":
File = '/jenkins/vcgapp/compose-back/pro/' + i.get('name')
composefile = i.get('template').replace('\r\n','\n')
with open(File,'w') as file:
file.write(composefile)
print "%s 的 compose 文件已备份到 %s " % (i.get('name'),File)

if Action == "update":
ClusterName = sys.argv[2]
Service = sys.argv[3]
File = sys.argv[4]
UpdataService(ClusterName,Service,File,TimeStamp)
elif Action == "create":
ClusterName = sys.argv[2]
Service = sys.argv[3]
File = sys.argv[4]
CreateService(ClusterName,Service,File,TimeStamp)
elif Action == "restart":
ClusterName = sys.argv[2]
Service = sys.argv[3]
RestartService(ClusterName, Service)
elif Action == "getinfo":
ClusterName = sys.argv[2]
Service = sys.argv[3]
GetService(ClusterName, Service)
elif Action == "redeploy":
ClusterName = sys.argv[2]
Service = sys.argv[3]
RedeployService(ClusterName, Service)
elif Action == "getcompose":
ClusterName = sys.argv[2]
Service = sys.argv[3]
GetComposeService(ClusterName, Service)
elif Action == "getallservice":
ClusterName = sys.argv[2]
GetAllService(ClusterName)