停止某个机房所有机器上包的脚本 pack_idc_stop.py

一、初衷:

  鉴于公司的进程包package都是冗余多点部署的,一般一个idc机房有多台机器部署同一个package。当机房网络出问题的时候,我们不得不查到本机房部署了哪些package,并在包发布系统中一个个查询并停掉。这样就很费时费力。

  利用包发布系统提供的一些接口stop.do等,做了个停掉某个机房所有package的脚本。只需之前把所有包名,机房名写入配置文件pack_idc.json,执行脚本时通过带不同的配置文件做为参数,即可实现批量停掉包的目的。

二、原理:

  因为包发布系统的接口返回都是json,所以本脚本主要就是用post/get方法调用接口,处理json,利用dict、list做一些匹配、过滤、组合的操作。

  主要用到:

  python的urllib模块的urlopen(): http://www.cnblogs.com/langdashu/p/4963053.html

  python解析json : http://www.cnblogs.com/langdashu/p/4963098.html

  python判断内网IP : http://www.cnblogs.com/langdashu/p/4962657.html

  python的字典、列表的应用

  python脚本传参数、处理文件、json注释方法

三、实例:

  pack_idc.json  pack_idc_start.py  pack_idc_stop.py

  执行方法 python   pack_idc_stop.py  pack_idc.json 

1、配置文件pack_idc.json

  

{
"pack_name":["yc_agent","duang_agent"],
"idc_name":"北京鲁谷BGP-01",
"comment":"这是注释"
}

 

  pack_name可以是任意多个,idc_name必须唯一。若pack_name有一个不存在本idc上会自动过滤掉。

  通过事先配置好不同的json文件,当需要停某个机房的package的时候,只需执行脚本带上相应的配置文件即可。

2、脚本pack_idc_stop.py

 

  1 #!/usr/bin/python
  2 #-*-coding:utf8-*-
  3 
  4 import urllib2
  5 import json
  6 import os
  7 import sys
  8 
  9 vid = []
 10 real_host = {}
 11 real_match = {}
 12 #判断内网IP
 13 def ip_into_int(ip):
 14         return reduce(lambda x,y:(x<<8)+y,map(int,ip.split('.')))
 15 
 16 def is_internal_ip(ip):
 17         ip = ip_into_int(ip)
 18         net_a = ip_into_int('10.255.255.255') >> 24
 19         net_b = ip_into_int('172.31.255.255') >> 20
 20         net_c = ip_into_int('192.168.255.255') >> 16
 21         return ip >> 24 == net_a or ip >>20 == net_b or ip >> 16 == net_c
 22 
 23 
 24 def match_idc(real_host, hname):
 25 
 26         for key in real_host.keys():
 27                 match_ip = []
 28                 t3 = len(real_host[key])
 29 
 30                 for n in range(t3):
 31                         name_url = 'http://www.google.com/getIp.do?ip=%s' % real_host[key][n]
 32                         name = urllib2.urlopen(name_url)
 33                         name_json = json.loads(name.read())
 34                         if name_json['object']['name'] == hname:
 35                                 match_ip.append(real_host[key][n])
 36 
 37                 if len(match_ip) != 0:
 38                         real_match[key]=match_ip
 39 
 40         name.close()
 41         return real_match
 42 
 43 def get_ips(vid):
 44 
 45         t = len(vid)
 46         for i in range(t):
 47                 host_ip = []
 48                 ips_url = 'http://www.google.com/IP.do?versionId=%d ' % vid[i]
 49                 ips =urllib2.urlopen(ips_url)
 50                 ips_json = json.loads(ips.read())
 51 
 52                 t2 = len(ips_json['object'])
 53                 for k in range(t2):
 54                         flag = 0
 55                         ip_list = ips_json['object'][k].split(',')
 56 
 57                         t3 =len(ip_list)
 58                         for m in range(t3):
 59                                 if flag ==0 and is_internal_ip(ip_list[m]) == False:
 60                                         host_ip.append(ip_list[m])
 61                                         flag = 1
 62 
 63                 real_host[vid[i]] = host_ip
 64 
 65         ips.close()
 66         return real_host
 67 
 68 
 69 def get_vid(pname):
 70 
 71         vid_url = 'http://www.google.com/Version.do?name=%s' % pname
 72         html = urllib2.urlopen(vid_url)
 73         hjson = json.loads(html.read())
 74 
 75         t = len(hjson['object'])
 76         if t == 0:
 77                 print "...................."
 78                 print "%s is not exist,please check the pack_name !!!" % pname
 79                 print "all pack start faild !!"
 80                 sys.exit()
 81         for i in range(t):
 82                 count = hjson['object'][i]['count']
 83                 if count > 0:
 84                         vid.append(hjson['object'][i]['versionId'])
 85         html.close()
 86 
 87         return vid
 88 
 89 def start_pack(real_match):
 90 
 91         for x in real_match.keys():
 92                 start_url = 'http://www.google.com/start.do?ips=%s&versionId=%s&operator=dw_%s' % (",".join(real_match[x]), x,os.getlogin())
 93                 start_html = urllib2.urlopen(start_url)
 94                 start_json = json.loads(start_html.read())
 95 
 96                 task_url = 'http://www.google.com/TaskId.do?task_id=%s' % start_json['object']['taskId']
 97                 task_html = urllib2.urlopen(task_url)
 98                 task_json = json.loads(task_html.read())
 99 
100                 if start_json['code'] == 0:
101                         print "package %s start succees!" % task_json['object'][0]['package_name']
102                 else:
103                         print "package %s start error!" % task_json['object'][0]['package_name']
104 
105                 start_html.close()
106                 task_html.close()
107 
108 
109 if __name__=='__main__':
110         if len(sys.argv) != 2:
111                 print "Usage:  python   pack_idc_stop.py    xxxx.json"
112                 sys.exit()
113 
114         with open(sys.argv[1]) as f:
115                 data = f.read()
116         pack_idc = json.loads(data)
117 
118         t = len(pack_idc['pack_name'])
119         hname = pack_idc['idc_name']
120         for n in range(t):
121                 pname = pack_idc['pack_name'][n]
122                 print "starting %s on %s ...... " % (pname, hname)
123                 vid = get_vid(pname)
124         print '..............'
125         real_host = get_ips(vid)
126         real_match = match_idc(real_host, hname)
127         start_pack(real_match)

 

posted @ 2015-11-13 16:08  Devops达人  阅读(458)  评论(0编辑  收藏  举报