Showing
1 changed file
with
18 additions
and
5 deletions
@@ -11,10 +11,16 @@ import os,shutil | @@ -11,10 +11,16 @@ import os,shutil | ||
11 | import time | 11 | import time |
12 | import re | 12 | import re |
13 | 13 | ||
14 | +""" | ||
15 | + 作用:由于EMR集群不定时会发生伸缩变化,该脚本可以定时维护inventories/bigdata/hosts文件,如果发生变更会同步更新该文件并推送到git.yoho.cn,并执行resolv的playbook同步DNS | ||
16 | + 依赖:zabbix.py(注销zabbix host) | ||
17 | + Demo: | ||
18 | + python server_decomission.py 1.2.3.4 | ||
19 | +""" | ||
14 | 20 | ||
15 | class EMRClusterScanner(object): | 21 | class EMRClusterScanner(object): |
16 | def __init__(self,interval,secretId='',secretKey=''): | 22 | def __init__(self,interval,secretId='',secretKey=''): |
17 | - self.interval = int(interval) if int(interval)> 10 else 10 #扫描时间间隔须大于10秒,避免浪费cpu资源以及api访问过于频繁 单位:秒 | 23 | + self.interval = int(interval) #扫描时间间隔 若为0则只扫描一次 单位:秒 |
18 | if not secretId or not secretKey: | 24 | if not secretId or not secretKey: |
19 | self.secretId,self.secretKey = open('/home/ansible/.qcloud_config').read().strip('\n').split('\n') | 25 | self.secretId,self.secretKey = open('/home/ansible/.qcloud_config').read().strip('\n').split('\n') |
20 | else: | 26 | else: |
@@ -24,7 +30,7 @@ class EMRClusterScanner(object): | @@ -24,7 +30,7 @@ class EMRClusterScanner(object): | ||
24 | self.inventoryFile = '{0}inventories/bigdata/hosts'.format(self.workDir) | 30 | self.inventoryFile = '{0}inventories/bigdata/hosts'.format(self.workDir) |
25 | self.playbook_resolv = '{0}playbooks/resolv.yml'.format(self.workDir) | 31 | self.playbook_resolv = '{0}playbooks/resolv.yml'.format(self.workDir) |
26 | self.mailman = mailman() | 32 | self.mailman = mailman() |
27 | - self.receivers = ['tiexin.yang@yoho.cn']#,'chunhua.zhang@yoho.cn'] | 33 | + self.receivers = ['tiexin.yang@yoho.cn','chunhua.zhang@yoho.cn'] |
28 | self.emrNameMap = { | 34 | self.emrNameMap = { |
29 | "emr-rt": "emr-r6bhtb5v", | 35 | "emr-rt": "emr-r6bhtb5v", |
30 | "emr-ops": "emr-iaeloyc2", | 36 | "emr-ops": "emr-iaeloyc2", |
@@ -122,7 +128,8 @@ class EMRClusterScanner(object): | @@ -122,7 +128,8 @@ class EMRClusterScanner(object): | ||
122 | def syncDNS(self,target='emr-recom'): | 128 | def syncDNS(self,target='emr-recom'): |
123 | if target == 'emr-recom': #目前只负责同步emr-recom的DNS | 129 | if target == 'emr-recom': #目前只负责同步emr-recom的DNS |
124 | cmd = 'ansible-playbook -i {0} {1}'.format(self.inventoryFile,self.playbook_resolv) | 130 | cmd = 'ansible-playbook -i {0} {1}'.format(self.inventoryFile,self.playbook_resolv) |
125 | - if os.system(cmd) != 0: | 131 | + output = os.popen(cmd).read() |
132 | + if 'changed=1' not in output or 'unreachable=1' in output: | ||
126 | return False | 133 | return False |
127 | else: | 134 | else: |
128 | return True | 135 | return True |
@@ -139,7 +146,7 @@ class EMRClusterScanner(object): | @@ -139,7 +146,7 @@ class EMRClusterScanner(object): | ||
139 | print 'Inventory already up to date' | 146 | print 'Inventory already up to date' |
140 | return 'Passed' | 147 | return 'Passed' |
141 | else: | 148 | else: |
142 | - print '\n'.join(currentNodes) | 149 | + #print '\n'.join(currentNodes) |
143 | N_toAdd = len(set(currentNodes) - set(hostsInFile)) | 150 | N_toAdd = len(set(currentNodes) - set(hostsInFile)) |
144 | N_toDel = len(set(hostsInFile) - set(currentNodes)) | 151 | N_toDel = len(set(hostsInFile) - set(currentNodes)) |
145 | print '{0} to add,{1} to del'.format(N_toAdd,N_toDel) | 152 | print '{0} to add,{1} to del'.format(N_toAdd,N_toDel) |
@@ -178,10 +185,13 @@ class EMRClusterScanner(object): | @@ -178,10 +185,13 @@ class EMRClusterScanner(object): | ||
178 | result = self.updateEMRInventory(emrName) | 185 | result = self.updateEMRInventory(emrName) |
179 | if result: | 186 | if result: |
180 | if result.upper() == 'PASSED': | 187 | if result.upper() == 'PASSED': |
188 | + print 'Nothing changed, passed' | ||
181 | continue | 189 | continue |
182 | else: | 190 | else: |
191 | + print 'Sending mail' | ||
183 | self.mailman.mail(receivers=self.receivers,Content='EMR大数据Inventory [{0}] 已更新\n{1}\n详见 http://git.yoho.cn/ops/yoho-ansible-roles/blob/master/inventories/bigdata/hosts'.format(emrName,result),Title='EMR Inventory 更新结果') | 192 | self.mailman.mail(receivers=self.receivers,Content='EMR大数据Inventory [{0}] 已更新\n{1}\n详见 http://git.yoho.cn/ops/yoho-ansible-roles/blob/master/inventories/bigdata/hosts'.format(emrName,result),Title='EMR Inventory 更新结果') |
184 | else: | 193 | else: |
194 | + print 'Sending mail' | ||
185 | self.mailman.mail(receivers=self.receivers,Content='EMR大数据Inventory更新失败\n请检查本地git缓存和ansible连接\n5分钟后重试',Title='EMR Inventory 更新结果') | 195 | self.mailman.mail(receivers=self.receivers,Content='EMR大数据Inventory更新失败\n请检查本地git缓存和ansible连接\n5分钟后重试',Title='EMR Inventory 更新结果') |
186 | time.sleep(300) | 196 | time.sleep(300) |
187 | continue | 197 | continue |
@@ -190,7 +200,10 @@ class EMRClusterScanner(object): | @@ -190,7 +200,10 @@ class EMRClusterScanner(object): | ||
190 | self.mailman.mail(receivers=self.receivers,Content='EMR大数据Inventory更新失败\n{0}\n5分钟后重试'.format(str(e)),Title='EMR Inventory 更新结果') | 200 | self.mailman.mail(receivers=self.receivers,Content='EMR大数据Inventory更新失败\n{0}\n5分钟后重试'.format(str(e)),Title='EMR Inventory 更新结果') |
191 | time.sleep(300) | 201 | time.sleep(300) |
192 | pass | 202 | pass |
193 | - time.sleep(self.interval) | 203 | + if self.interval > 0: |
204 | + time.sleep(self.interval) | ||
205 | + else: | ||
206 | + break | ||
194 | 207 | ||
195 | 208 | ||
196 | if __name__ == '__main__': | 209 | if __name__ == '__main__': |
-
Please register or login to post a comment