Authored by chunhua.zhang

Merge branch 'master' of git.yoho.cn:ops/yoho-ansible-roles

@@ -2,8 +2,8 @@ @@ -2,8 +2,8 @@
2 # -*- coding: UTF-8 -*- 2 # -*- coding: UTF-8 -*-
3 # author tiexin.yang@yoho.cn 3 # author tiexin.yang@yoho.cn
4 4
5 -from qcloud_api import QcloudApi  
6 -from mailer import mailman 5 +from qcloud.qcloud_api import QcloudApi
  6 +from qcloud.mailer import mailman
7 import datetime 7 import datetime
8 import argparse 8 import argparse
9 import os,shutil 9 import os,shutil
@@ -20,6 +20,7 @@ class EMRClusterScanner(object): @@ -20,6 +20,7 @@ class EMRClusterScanner(object):
20 self.secretId,self.secretKey = secretId,secretKey 20 self.secretId,self.secretKey = secretId,secretKey
21 21
22 self.inventoryFile = '/opt/projects/yoho-ansible-roles/inventories/bigdata/hosts' 22 self.inventoryFile = '/opt/projects/yoho-ansible-roles/inventories/bigdata/hosts'
  23 + self.playbook_resolv = '/opt/projects/yoho-ansible-roles/playbooks/resolv.yml'
23 self.mailman = mailman() 24 self.mailman = mailman()
24 self.receivers = ['tiexin.yang@yoho.cn','chunhua.zhang@yoho.cn'] 25 self.receivers = ['tiexin.yang@yoho.cn','chunhua.zhang@yoho.cn']
25 self.emrNameMap = { 26 self.emrNameMap = {
@@ -112,6 +113,14 @@ class EMRClusterScanner(object): @@ -112,6 +113,14 @@ class EMRClusterScanner(object):
112 cmd = 'cd /opt/projects/yoho-ansible-roles/;git pull;git add {0};git commit -m "Update bigdata inventory file"; git push'.format(self.inventoryFile) 113 cmd = 'cd /opt/projects/yoho-ansible-roles/;git pull;git add {0};git commit -m "Update bigdata inventory file"; git push'.format(self.inventoryFile)
113 os.popen(cmd) 114 os.popen(cmd)
114 return True 115 return True
  116 +
  117 + def syncDNS(self,target='emr-recom'):
  118 + if target == 'emr-recom': #目前只负责同步emr-recom的DNS
  119 + cmd = 'sudo ansible-playbook -i {0} {1}'.format(self.inventoryFile,self.playbook_resolv)
  120 + os.popen(cmd)
  121 + return True
  122 + else:
  123 + return False
115 124
116 def updateEMRInventory(self, emrName): 125 def updateEMRInventory(self, emrName):
117 #比对当前与最新的Inventory ip列表,相同则pass不同则更新 126 #比对当前与最新的Inventory ip列表,相同则pass不同则更新
@@ -137,23 +146,28 @@ class EMRClusterScanner(object): @@ -137,23 +146,28 @@ class EMRClusterScanner(object):
137 print 'Pushing changes to git server...' 146 print 'Pushing changes to git server...'
138 self.gitUpdate() 147 self.gitUpdate()
139 print 'Inventory file updated success' 148 print 'Inventory file updated success'
  149 + if N_toAdd > 0:
  150 + print 'Running playbook to sync DNS...'
  151 + self.syncDNS(emrName)
  152 + print 'DNS synced'
  153 +
140 return 'Inventory 新增{0}条,删除{1}条'.format(N_toAdd,N_toDel) 154 return 'Inventory 新增{0}条,删除{1}条'.format(N_toAdd,N_toDel)
141 155
142 def start_daemon(self): 156 def start_daemon(self):
143 #以守护进程的状态运行此EMR扫描程序 157 #以守护进程的状态运行此EMR扫描程序
144 while True: 158 while True:
145 for emrName in self.emrNameMap: 159 for emrName in self.emrNameMap:
146 - #try: 160 + try:
147 result = self.updateEMRInventory(emrName) 161 result = self.updateEMRInventory(emrName)
148 if result: 162 if result:
149 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 更新结果') 163 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 更新结果')
150 else: 164 else:
151 pass 165 pass
152 - #except Exception as e:  
153 - # print e  
154 - # self.mailman.mail(receivers=self.receivers,Content='EMR大数据Inventory更新失败\n{0}\n5分钟后重试'.format(str(e)),Title='EMR Inventory 更新结果')  
155 - # time.sleep(300)  
156 - # pass 166 + except Exception as e:
  167 + print e
  168 + self.mailman.mail(receivers=self.receivers,Content='EMR大数据Inventory更新失败\n{0}\n5分钟后重试'.format(str(e)),Title='EMR Inventory 更新结果')
  169 + time.sleep(300)
  170 + pass
157 time.sleep(self.interval) 171 time.sleep(self.interval)
158 172
159 173