本文共 5259 字,大约阅读时间需要 17 分钟。
腾讯云平台绑定域名端口是要一个个绑,比较麻烦,之前看了一下API,简单写了一个绑域名端口的脚本:
ApiLogger.py(这个是输出日志用的)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #!/usr/bin/python #coding:utf-8 import logging #日志模块 logger = logging.getLogger( "sy_tools" ) logger.setLevel(logging.DEBUG) ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) fh = logging.FileHandler( "sy_tools.log" ) fh.setLevel(logging.DEBUG) formatter = logging.Formatter( "%(asctime)s - %(levelname)s - %(message)s" ) ch.setFormatter(formatter) fh.setFormatter(formatter) logger.addHandler(ch) logger.addHandler(fh) |
ApiRequest.py (这个API提交信息的基础方法)
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 | #!/usr/bin/python #coding:utf-8 import urllib2 import base64 import json import time import random import hmac import hashlib import ApiLogger logger = ApiLogger.logger class TX_API: def __init__( self ): self .accessId = 'accessid' self .secretKey = 'secretKey' self .endPoint = 'http://api.yun.qq.com' self .uri = '' self .body = '' self .method = '' def set_uri( self , value): self .uri = value return self .uri def set_body( self , value): self .body = value return self .body def set_method( self , value): self .method = value return self .method def do_request( self ): if self .body: data = json.dumps( self .body) else : data = self .body self .nonce = random.randint( 1 , 2 * * 31 - 1 ) self .timestamp = int (time.time()) self .orignal = '''body=%s&method=%s&uri=%s&x-txc-cloud-secretid=%s&x-txc-cloud-nonce=%s&x-txc-cloud-timestamp=%s''' % (data, self .method, self .uri, self .accessId, self .nonce, self .timestamp) self .signature = base64.b64encode(hmac.new( self .secretKey, self .orignal,digestmod = hashlib.sha1).digest()) self .header = { "Content-type" : "application/json; charset=utf-8" , "x-txc-cloud-secretid" : self .accessId, "x-txc-cloud-nonce" : self .nonce, "x-txc-cloud-timestamp" : self .timestamp, "x-txc-cloud-signature" : self .signature, } if self .body: self .request = urllib2.Request( self .endPoint + self .uri, json.dumps( self .body)) else : self .request = urllib2.Request( self .endPoint + self .uri) for key in self .header: self .request.add_header(key, self .header[key]) try : result = urllib2.urlopen( self .request) except urllib2.HTTPError as http_error: print http_error else : response = json.loads(result.read()) result.close() return responsep jfd |
这里是做一些接口认证,比较麻烦,详细要求看下API文档
self.accessId 和 self.secretKey 这两个KEY是要自己在云平台生成的
ApiDomain.py (调用绑定域名API及返回相关状态)
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 | #!/usr/bin/python #coding:utf-8 import ApiRequest import ApiLogger logger = ApiLogger.logger def get_domain_id(domain): sy = ApiRequest.TX_API() sy.set_method( 'GET' ) sy.set_uri( '/v1/domains/query_instance_id?domains=%s' % domain) result = sy.do_request() if result: return result[ 'instanceIds' ][domain] # return result['instances'][0]['instanceId'] def bind_domain_port(domain_id, cvm_ip, cvm_port): sy = ApiRequest.TX_API() sy.set_method( 'POST' ) sy.set_uri( '/v1/domains/%s/cvm_bind' % domain_id) body = { 'lanIps' :[cvm_ip], 'port' :cvm_port} sy.set_body(body) result = sy.do_request() if result: errorCode = result[ 'errorCode' ] httpCode = result[ 'httpCode' ] logger.debug( "errorCode = %s and httpCode = %s" % (errorCode, httpCode)) else : logger.error( "Request Error! Please try again!" ) def check_domain_info(domain_id): sy = ApiRequest.TX_API() sy.set_method( 'GET' ) sy.set_uri( '/v1/domains/%s' % domain_id) result = sy.do_request() if result: logger.debug( "Domain '%s' already bind:" % result[ 'instanceInfo' ][ 'domain' ]) for i in result[ 'instanceInfo' ][ 'devicesList' ]: logger.debug( "host : %s,\tport: %s" % (i[ 'lanIp' ], i[ 'port' ])) else : logger.error( "Request Error! Please try again!" ) if __name__ = = "__main__" : print get_domain_id( 's233.app100670828.qqopenapp.com' ) #bind_domain_port('100000041073', "10.204.153.56", 8008) #check_domain_info(100000041073) |
get_domain_id : 根据域名获取它的ID
bind_domain_port: 把CVM绑定到该域名的某个端口
check_domain_info: 获取该域名绑定了哪些CVM及端口
sy_tools.py (实际工作,调用上面的接口)
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 | #!/usr/bin/python #coding:utf-8 import ApiDomain import time import ApiLogger logger = ApiLogger.logger domains = [ 's233.app100670828.qqopenapp.com' ] hosts = [ '10.207.252.117' ] ports = [ '80' , '8000' , '8001' , '8002' , '8003' , '8004' , '8005' ] logger.debug( "This scripts will bind domain ports!" ) raw_input ( "Please Enter any key to start!" ) num = len (domains) for i in range ( 0 ,num): domain = domains[i] host = hosts[i] domain_id = ApiDomain.get_domain_id(domain) print domain, domain_id, host for port in ports: logger.debug( "bind %s port ING~~~" % port) ApiDomain.bind_domain_port(domain_id, host, int (port)) time.sleep( 20 ) ApiDomain.check_domain_info(domain_id) time.sleep( 20 ) logger.debug( "Done!" ) raw_input ( "Please Enter any key to exit!" ) |
domains = ['s233.app100670828.qqopenapp.com']
hosts = ['10.207.252.117']
这里应该用字典比较好,当时写的时候就不知道怎么想的了。。后来就没改了。。
写得比较弱,不过能用就好了。。
本文转自运维笔记博客51CTO博客,原文链接http://blog.51cto.com/lihuipeng/1285389如需转载请自行联系原作者
lihuipeng