Authored by root

Enable retry when failed

@@ -4,6 +4,7 @@ @@ -4,6 +4,7 @@
4 import smtplib 4 import smtplib
5 from email.mime.text import MIMEText 5 from email.mime.text import MIMEText
6 from email.header import Header 6 from email.header import Header
  7 +import time
7 8
8 9
9 class mailman(object): 10 class mailman(object):
@@ -16,11 +17,21 @@ class mailman(object): @@ -16,11 +17,21 @@ class mailman(object):
16 message['From'] = Header(From, 'utf-8') 17 message['From'] = Header(From, 'utf-8')
17 message['To'] = Header(To, 'utf-8') 18 message['To'] = Header(To, 'utf-8')
18 message['Subject'] = Header(Title, 'utf-8') 19 message['Subject'] = Header(Title, 'utf-8')
19 -  
20 - try:  
21 - self.smtpObj.sendmail(self.sender, receivers, message.as_string())  
22 - print "邮件发送成功"  
23 - except smtplib.SMTPException:  
24 - print "Error: 无法发送邮件" 20 +
  21 + retry = 5
  22 + while retry>0:
  23 + try:
  24 + self.smtpObj.sendmail(self.sender, receivers, message.as_string())
  25 + print "邮件发送成功"
  26 + return True
  27 + except Exception as e:
  28 + retry-=1
  29 + print e
  30 + print "Error: 无法发送邮件"
  31 + print "重新发送邮件...还剩{0}次".format(retry)
  32 + time.sleep(10)
  33 + return False
25 34
26 - return 0 35 +if __name__ == '__main__':
  36 + demo = mailman()
  37 + demo.mail(['tiexin.yang@yoho.cn'])
@@ -30,7 +30,7 @@ class EMRClusterScanner(object): @@ -30,7 +30,7 @@ class EMRClusterScanner(object):
30 self.inventoryFile = '{0}inventories/bigdata/hosts'.format(self.workDir) 30 self.inventoryFile = '{0}inventories/bigdata/hosts'.format(self.workDir)
31 self.playbook_resolv = '{0}playbooks/resolv.yml'.format(self.workDir) 31 self.playbook_resolv = '{0}playbooks/resolv.yml'.format(self.workDir)
32 self.mailman = mailman() 32 self.mailman = mailman()
33 - self.receivers = ['tiexin.yang@yoho.cn','chunhua.zhang@yoho.cn'] 33 + self.receivers = ['tiexin.yang@yoho.cn']#,'chunhua.zhang@yoho.cn']
34 self.emrNameMap = { 34 self.emrNameMap = {
35 "emr-rt": "emr-r6bhtb5v", 35 "emr-rt": "emr-r6bhtb5v",
36 "emr-ops": "emr-iaeloyc2", 36 "emr-ops": "emr-iaeloyc2",
@@ -128,11 +128,17 @@ class EMRClusterScanner(object): @@ -128,11 +128,17 @@ class EMRClusterScanner(object):
128 def syncDNS(self,target='emr-recom'): 128 def syncDNS(self,target='emr-recom'):
129 if target == 'emr-recom': #目前只负责同步emr-recom的DNS 129 if target == 'emr-recom': #目前只负责同步emr-recom的DNS
130 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)
131 - output = os.popen(cmd).read()  
132 - if 'changed=1' not in output or 'unreachable=1' in output:  
133 - return False  
134 - else:  
135 - return True 131 + retry = 3
  132 + while retry > 0:
  133 + output = os.popen(cmd).read()
  134 + if 'changed=1' not in output or 'unreachable=1' in output:
  135 + retry-=1
  136 + print "重新同步DNS...还剩{0}次".format(retry)
  137 + time.sleep(300)
  138 + continue
  139 + else:
  140 + return True
  141 + return False
136 else: 142 else:
137 return True 143 return True
138 144