Authored by root

Add emr scanner

... ... @@ -2,8 +2,8 @@
# -*- coding: UTF-8 -*-
# author tiexin.yang@yoho.cn
from qcloud_api import QcloudApi
from mailer import mailman
from qcloud.qcloud_api import QcloudApi
from qcloud.mailer import mailman
import datetime
import argparse
import os,shutil
... ... @@ -20,6 +20,7 @@ class EMRClusterScanner(object):
self.secretId,self.secretKey = secretId,secretKey
self.inventoryFile = '/opt/projects/yoho-ansible-roles/inventories/bigdata/hosts'
self.playbook_resolv = '/opt/projects/yoho-ansible-roles/playbooks/resolv.yml'
self.mailman = mailman()
self.receivers = ['tiexin.yang@yoho.cn','chunhua.zhang@yoho.cn']
self.emrNameMap = {
... ... @@ -112,6 +113,14 @@ class EMRClusterScanner(object):
cmd = 'cd /opt/projects/yoho-ansible-roles/;git pull;git add {0};git commit -m "Update bigdata inventory file"; git push'.format(self.inventoryFile)
os.popen(cmd)
return True
def syncDNS(self,target='emr-recom'):
if target == 'emr-recom': #目前只负责同步emr-recom的DNS
cmd = 'sudo ansible-playbook -i {0} {1}'.format(self.inventoryFile,self.playbook_resolv)
os.popen(cmd)
return True
else:
return False
def updateEMRInventory(self, emrName):
#比对当前与最新的Inventory ip列表,相同则pass不同则更新
... ... @@ -137,23 +146,28 @@ class EMRClusterScanner(object):
print 'Pushing changes to git server...'
self.gitUpdate()
print 'Inventory file updated success'
if N_toAdd > 0:
print 'Running playbook to sync DNS...'
self.syncDNS(emrName)
print 'DNS synced'
return 'Inventory 新增{0}条,删除{1}条'.format(N_toAdd,N_toDel)
def start_daemon(self):
#以守护进程的状态运行此EMR扫描程序
while True:
for emrName in self.emrNameMap:
#try:
try:
result = self.updateEMRInventory(emrName)
if result:
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 更新结果')
else:
pass
#except Exception as e:
# print e
# self.mailman.mail(receivers=self.receivers,Content='EMR大数据Inventory更新失败\n{0}\n5分钟后重试'.format(str(e)),Title='EMR Inventory 更新结果')
# time.sleep(300)
# pass
except Exception as e:
print e
self.mailman.mail(receivers=self.receivers,Content='EMR大数据Inventory更新失败\n{0}\n5分钟后重试'.format(str(e)),Title='EMR Inventory 更新结果')
time.sleep(300)
pass
time.sleep(self.interval)
... ...