qcloudDeploy.py 4.45 KB
#!/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(10)
    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)