qcloudDeploy.py
4.45 KB
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
#!/usr/bin/python
import hmac
import hashlib
import urllib
import random
import time
import requests
import sys
def push_param_to_tencent_ccs(paraMap):
print ("begin add common para ,the request para is %s" % paraMap)
secretKey = 'ACJkH9mg0DBA1PYpf0E7f3g534wBsQaW'
getaction = 'GET'
url = 'ccs.api.qcloud.com/v2/index.php?'
paraMap["Nonce"] = str(random.randint(1000, 9999))
paraMap["Region"] = "bj"
paraMap["SecretId"] = "AKID6dwpKadiQgbDpXDtyNhppIHPO5qPv5GK"
paraMap["Timestamp"] = str("%.0f" % time.time())
listkey = sorted(paraMap.keys())
request_string_buffer = ""
for key in listkey:
request_string_buffer = request_string_buffer + "&" + key + "=" + paraMap[key]
get_request_url = request_string_buffer[1:]
request_url = url + get_request_url
my_sign = hmac.new(secretKey, getaction + request_url, hashlib.sha1).digest().encode('base64').rstrip()
str_encode = my_sign.encode('gb2312')
d = {'Signature': str_encode}
requestUrlLast = "https://" + request_url + "&" + urllib.urlencode(d)
# print ("last push to tencent request url is %s" % requestUrlLast)
result_json = requests.get(requestUrlLast)
# print ("request result from qcloud is %s" + str(result_json.json()))
return result_json.json()
def check_service_image_version(clusterId, serviceName):
check_service_image_version_dirct = {"clusterId": clusterId, "serviceName": serviceName, "namespace": "default",
"Action": "DescribeClusterServiceInfo"}
r = push_param_to_tencent_ccs(check_service_image_version_dirct)
return r['data']['service']['containers'][0]['image']
def check_service_status(clusterId, serviceName):
check_service_image_version_dirct = {"clusterId": clusterId, "serviceName": serviceName, "namespace": "default",
"Action": "DescribeClusterServiceInfo"}
r = push_param_to_tencent_ccs(check_service_image_version_dirct)
return r['data']['service']['status']
def change_service_image(clusterId, serviceName, containername, imageversion):
para_check_service = {"clusterId": clusterId, "serviceName": serviceName, "namespace": "default",
"Action": "ModifyClusterServiceImage", "containers.0.containerName": containername,
"containers.0.image": imageversion}
r = push_param_to_tencent_ccs(para_check_service)
return r['codeDesc']
def restart_service_image(clusterId, serviceName):
para_check_service = {"clusterId": clusterId, "serviceName": serviceName, "namespace": "default",
"Action": "RedeployClusterService"}
r = push_param_to_tencent_ccs(para_check_service)
return r['codeDesc']
def get_qcloud_cluster(var):
return {
'yoho-ops': 'cls-gscif1o5',
'yoho-web-gray': 'cls-hclninkp',
'yoho-web': 'cls-ro6kl3cp',
'yoho-media': 'cls-qzkxrhg7',
}.get(var, 'error')
def main(clusterid, servicename, containername, imageversion):
print 'begin update the image info clusterid : %s , servicename: %s ,containername :%s ,imageversion :%s' % (
clusterid, servicename, containername, imageversion)
change_service_image_status = change_service_image(clusterid, servicename, containername, imageversion)
if change_service_image_status != 'Success':
print 'update the image info clusterid failure,please check the clusterid'
sys.exit(1)
print 'begin check the result clusterid : %s , servicename: %s' % (clusterid, servicename)
count = 0
time.sleep(20)
while count <= 60:
if count == 60:
sys.exit(1)
count = count + 1
container_status = check_service_status(clusterid, servicename)
print 'search number : %i ,clusterid : %s , servicename: %s,container_status :%s' % (
count, clusterid, servicename, container_status)
if container_status == "Normal":
break
time.sleep(10)
print "end with the image change"
if __name__ == '__main__':
cluster_id = get_qcloud_cluster(sys.argv[1])
service_name = sys.argv[2]
container_name = sys.argv[3]
image_version = 'ccr.ccs.tencentyun.com/' + sys.argv[4]
print 'request para cluster_id is ', cluster_id
print 'request para service_name is ', service_name
print 'request para container_name is ', container_name
print 'request para image_version is ', image_version
main(cluster_id, service_name, container_name, image_version)