Authored by root

Enable retry when failed

... ... @@ -4,6 +4,7 @@
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import time
class mailman(object):
... ... @@ -16,11 +17,21 @@ class mailman(object):
message['From'] = Header(From, 'utf-8')
message['To'] = Header(To, 'utf-8')
message['Subject'] = Header(Title, 'utf-8')
try:
self.smtpObj.sendmail(self.sender, receivers, message.as_string())
print "邮件发送成功"
except smtplib.SMTPException:
print "Error: 无法发送邮件"
retry = 5
while retry>0:
try:
self.smtpObj.sendmail(self.sender, receivers, message.as_string())
print "邮件发送成功"
return True
except Exception as e:
retry-=1
print e
print "Error: 无法发送邮件"
print "重新发送邮件...还剩{0}次".format(retry)
time.sleep(10)
return False
return 0
if __name__ == '__main__':
demo = mailman()
demo.mail(['tiexin.yang@yoho.cn'])
... ...
... ... @@ -30,7 +30,7 @@ class EMRClusterScanner(object):
self.inventoryFile = '{0}inventories/bigdata/hosts'.format(self.workDir)
self.playbook_resolv = '{0}playbooks/resolv.yml'.format(self.workDir)
self.mailman = mailman()
self.receivers = ['tiexin.yang@yoho.cn','chunhua.zhang@yoho.cn']
self.receivers = ['tiexin.yang@yoho.cn']#,'chunhua.zhang@yoho.cn']
self.emrNameMap = {
"emr-rt": "emr-r6bhtb5v",
"emr-ops": "emr-iaeloyc2",
... ... @@ -128,11 +128,17 @@ class EMRClusterScanner(object):
def syncDNS(self,target='emr-recom'):
if target == 'emr-recom': #目前只负责同步emr-recom的DNS
cmd = 'ansible-playbook -i {0} {1}'.format(self.inventoryFile,self.playbook_resolv)
output = os.popen(cmd).read()
if 'changed=1' not in output or 'unreachable=1' in output:
return False
else:
return True
retry = 3
while retry > 0:
output = os.popen(cmd).read()
if 'changed=1' not in output or 'unreachable=1' in output:
retry-=1
print "重新同步DNS...还剩{0}次".format(retry)
time.sleep(300)
continue
else:
return True
return False
else:
return True
... ...