def deploy_by_gitlab(ips, code_path, branch, deploy_type): result_list = [] prefix = "http://oms.yihaodian.com.cn/itil/api/?action=check&method=isDeploy&deploy=" healthcheck_close_url = prefix + "1" healthcheck_open_url = prefix + "0" for ip in ips: healthcheck_close_url += "&ip[]=" + ip healthcheck_open_url += "&ip[]=" + ip # close healthcheck logging.info(__name__ + ": prepare close healthcheck url -> " + healthcheck_close_url) status_code, response = httpcall2(healthcheck_close_url) logging.info(__name__ + ": healthcheck close request complet -> " + str(status_code) + ", response -> " + response) for item in ips: if deploy_type == 'python': status, cmd, output = ssh('/depot/boot.sh', item) result_list.append( [item, status, '<br>'.join(output.splitlines())]) time.sleep(10) elif deploy_type == 'manual-docs': command = 'cd %s;git pull origin %s:%s;rm -rf /data/manual/manual/ledao/;/data/python-virtualenv/manual/bin/sphinx-build /data/manual/_source/docs /data/manual/manual/ledao' % ( code_path, branch, branch) ssh(command, item) elif deploy_type == 'manual-docs-office': command = 'cd %s;git pull origin %s:%s;/data/python-virtualenv/manual/bin/sphinx-build /data/manual/_source/docs-office /data/manual/manual/ledao-office' % ( code_path, branch, branch) ssh(command, item) elif deploy_type == 'ledao_app': command = 'cd %s;git pull origin %s:%s' % (code_path, branch, branch) ssh(command, item) else: command = 'cd %s;git pull origin %s:%s' % (code_path, branch, branch) ssh(command, item) time.sleep(10) if deploy_type == 'python': title = u'代码发布' + ('成功' if all([result[1] for result in result_list]) else '失败') send_email(subject=title, content=loader.render_to_string( 'deploy/assetv2_result.html', {'result_list': result_list}), recipient_list=GitLab_Deploy_Mail) # open healthcheck. time.sleep(400) logging.info(__name__ + ": prepare open healthcheck url -> " + healthcheck_open_url) status_code, response = httpcall2(healthcheck_open_url) logging.info(__name__ + ": healthcheck open request complete -> " + str(status_code) + ", response -> " + response) return True
def all_auto_publish(jiraid, pool, deploy_dict, recipient_list, deploy_interval): from deploy.utils.Publish import Publish config_depid_list = deploy_dict.get("config", []) static_depid_list = deploy_dict.get("static", []) webapps_depid_list = deploy_dict.get("webapps", []) event_dict = { "sendType": "email", "receivers": '*****@*****.**', "warningMessage": None, "warningTopic": None, "warningType": 1 } #静态发布 if static_depid_list: error = False try: publish = Publish(depid=static_depid_list[0], interval=deploy_interval) publish.auto_publish() except Exception, e: print e.message error = True static_status = DeployMain.objects.get( depid=static_depid_list[0]).status if error or static_status != 4: subject = u"无人发布|{0}|{1}|静态发布异常".format(jiraid, pool) body = loader.render_to_string( 'mail/deploy/auto/static/error_report.html', { 'jiraid': jiraid, 'pool': cgi.escape(pool), 'static_depid_url': get_url_by_depid(static_depid_list), }) # body = list() # body.append(u"<span style='color: red'>静态发布异常的发布单:{0}</span>".format(get_url_by_depid(static_depid_list))) # body.append(u"YCC待发布的发布单:{0}".format(get_url_by_depid(config_depid_list))) # body.append(u"程序待发布的发布单:{0}".format(get_url_by_depid(webapps_depid_list))) # body.append(u"请正确处理已发布的发布单和待发布的发布单") # send_email(subject=subject, content=body.encode("utf8"), recipient_list=recipient_list) sendmail_v2(subject, body.encode("utf8"), recipient_list, None) print httpcall2(EVENT['PREFIX'] + EVENT['API'], 'POST', body={ 'apiType': 'Deploy', 'param': json.dumps( dict( event_dict, **{ 'warningMessage': ','.join(static_depid_list), 'warningTopic': subject })) }) return False
def change(user=None, action=None, index=None, message=None, url=CHANGE_API['URL'], type="release", level="change"): happen_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") body = { "user": user, "type": type, "action": action, "index": index, "message": message, "level": level, "happen_time": happen_time } httpcall2(url, method="POST", body=body, username=CHANGE_API["USERNAME"], password=CHANGE_API["PASSWORD"])
def web_hook_puppet(request): web_hook_dict = request.data puppet_dict = dict() for git_app_obj in GitApp.objects.filter(valid=True): app = git_app_obj.app type = git_app_obj.type room = git_app_obj.room app_id = app.id if app else 0 if type.room_property: type_name = '_'.join([room.name, type.name]) git_path = '/'.join([app.site.name, app.name, type_name ]) if app else type_name puppet_dict[type.name] = puppet_dict.get(type.name, dict()) puppet_dict[type.name][app_id] = puppet_dict[type.name].get( app_id, dict()) puppet_dict[type.name][app_id][room.name] = git_path else: git_path = '/'.join([app.site.name, app.name, type.name ]) if app else type.name puppet_dict[type.name] = puppet_dict.get(type.name, dict()) puppet_dict[type.name][app_id] = git_path for puppet_master in GIT['COMMON']['PUPPET_MASTER_LIST']: url = urlunparse( ('http', puppet_master, GIT['COMMON']['PUPPET_URL'], '', '', '')) code, response = httpcall2(url, method='POST', body={'data': json.dumps(puppet_dict)}) web_hook_dict['puppet'] = web_hook_dict.get('puppet', dict()) web_hook_dict['puppet'][puppet_master] = json.loads( response) if code is not None and code < 400 else dict() web_hook_dict['puppet'][puppet_master]['code'] = code r = redis.Redis(host=REDIS["HOST"], port=REDIS["PORT"], db=3) r.set(web_hook_dict['after'], json.dumps(web_hook_dict)) return Response(status=status.HTTP_200_OK, data=web_hook_dict)
def detector_check(self, ip): start_time = datetime.datetime.now()-datetime.timedelta(seconds=int(self.deploy.gray_stage_interval*60*0.7)) start_timestamp = int(time.mktime(start_time.timetuple())) * 1000 url = DETECTOR['PREFIX']+DETECTOR['HEALTH_API'] % (self.deploy.app.site.name, self.deploy.app.name, ip, start_timestamp) code, result = httpcall2(url) self.i('detector|%s|%s|%s' % (url, code, result)) try: health_detail_dict = json.loads(result) except Exception, e: health_detail_dict = {}
def all_auto_publish_v2(jiraid, pool, deploy_dict, recipient_list): # 异常事件处理 event_url_v2 = EVENT['PREFIX'] + EVENT['API_V2'] from deploy.utils.PublishV2 import Publish config_depid_list = deploy_dict.get("config", []) static_depid_list = deploy_dict.get("static", []) webapps_depid_list = deploy_dict.get("webapps", []) #静态发布 if static_depid_list: error = False try: publish = Publish(depid=static_depid_list[0]) publish.auto_publish() except Exception, e: print e.message error = True static_status = DeployMain.objects.get( depid=static_depid_list[0]).status if error or static_status != 4: subject = u"无人发布|{0}|{1}|静态发布异常".format(jiraid, pool) body = loader.render_to_string( 'mail/deploy/auto/static/error_report.html', { 'jiraid': jiraid, 'pool': cgi.escape(pool), 'static_depid_url': get_url_by_depid(static_depid_list), }) # send_email(subject=subject, content=body.encode("utf8"), recipient_list=recipient_list) sendmail_v2(subject, body.encode("utf8"), recipient_list, None) event_dict_v2 = { 'title': subject, 'level_id': 300, 'type_id': 3, 'source_id': 16, 'pool_id': LEDAO_POOL_ID, 'message': '静态程序发布异常: ' + ','.join(static_depid_list), 'send_to': ','.join(recipient_list) } print httpcall2(event_url_v2, 'POST', body=event_dict_v2) return False
def ycc_validate(deploy): url = YCC['PREFIX']+YCC['VALIDATE_API'].format(Room.objects.get(id=deploy.zone_id).ycc_code, deploy.app.site.name, deploy.app.name) code, response = httpcall2(url) i(deploy.depid, "{0}|{1}|{2}".format(url, code, response)) response = json.loads(response) if response else {} if response.get("result"): i(deploy.depid, "有待发配置,可以继续发布") return True else: i(deploy.depid, '无待发配置,停止发布,原因为{0}'.format(response.get("msg"))) trident_callback(deploy, 6) return False
def ycc_deploy(deploy, true_list, false_list): i(deploy.depid, 'ycc发布开始') url = YCC['PREFIX']+YCC['DEPLOY_API'].format(Room.objects.get(id=deploy.zone_id).ycc_code, deploy.app.site.name, deploy.app.name) if true_list or false_list: body = { 'ipList': ','.join(['{0}:true'.format(ip) for ip in true_list] + ['{0}:false'.format(ip) for ip in false_list]) } code, response = httpcall2(url, method='POST', body=body) i(deploy.depid, "{0}|{1}|{2}|{3}".format(url, body['ipList'], code, response)) else: code, response = httpcall2(url) i(deploy.depid, "{0}|{1}|{2}".format(url, code, response)) response = json.loads(response) if response else {} if response.get("result"): i(deploy.depid, 'ycc发布结束') trident_callback(deploy, 2) return True else: i(deploy.depid, 'ycc发布失败,失败原因为{0},请联系liushuansheng'.format(response.get("msg"))) trident_callback(deploy, 4) return False
def ycc_rmvpublish(deploy): ycc_code = TRIDENT_YCC_IDC_MAPPING.get(deploy.idc) if deploy.idc else Room.objects.get(id=deploy.zone_id).ycc_code url = YCC2['PREFIX']+YCC2['RMVPUBLISH_API'].format(ycc_code, deploy.app.site.name, deploy.app.name) code, response = httpcall2(url) i(deploy.depid, "{0}|{1}|{2}".format(url, code, response)) response = json.loads(response) if response else {} if response.get("result"): i(deploy.depid, "成功删除配置组待发布状态") return True else: i(deploy.depid, '无法删除配置组待发布状态,原因为{0}'.format(response.get("detail"))) return False
def detector_method(server_obj, method): room = get_room_obj_by_server_obj(server_obj) log_list = [] for i in range(3): code, response = httpcall2(DETECTOR['PREFIX']+DETECTOR['METHOD_API'] % (CMDB_DETECTOR_IDC_MAPPING.get(room.id), DETECTOR['SECRET'], DETECTOR['SECRET'], server_obj.ip, method)) log_list.append('服务%s:detector|%s|%s|%s' % (method, DETECTOR['PREFIX']+DETECTOR['METHOD_API'] % (CMDB_DETECTOR_IDC_MAPPING.get(room.id), '*' * 32, '*' * 32, server_obj.ip, method), code, response)) try: response = json.loads(response) except: response = dict() if isinstance(response, dict) and response.get('result') != '-3': break return log_list
def boot_sh_web_hook_puppet(request): web_hook_dict = request.data for puppet_master in GIT['BOOT_SH']['PUPPET_MASTER_LIST']: code, response = httpcall2( 'http://%s%s' % (puppet_master, GIT['BOOT_SH']['PUPPET_URL']), method='POST') web_hook_dict['puppet'] = web_hook_dict.get('puppet', dict()) web_hook_dict['puppet'][puppet_master] = json.loads( response) if code is not None and code < 400 else dict() web_hook_dict['puppet'][puppet_master]['code'] = code r = redis.Redis(host=REDIS["HOST"], port=REDIS["PORT"], db=3) r.set(web_hook_dict['after'], json.dumps(web_hook_dict)) return Response(status=status.HTTP_200_OK, data=True)
def ycc_rollback(deploy): i(deploy.depid, 'ycc回滚开始') url = YCC['PREFIX']+YCC['ROLLBACK_API'].format(Room.objects.get(id=deploy.zone_id).ycc_code, deploy.app.site.name, deploy.app.name) code, response = httpcall2(url) i(deploy.depid, "{0}|{1}|{2}".format(url, code, response)) response = json.loads(response) if response else {} if response.get("result"): i(deploy.depid, 'ycc回滚结束') trident_callback(deploy, 3) return True else: i(deploy.depid, 'ycc回滚失败,失败原因为{0},请联系请联系liushuansheng'.format(response.get("msg"))) trident_callback(deploy, 5) return False
def detector_method(self, ip, method): time.sleep(OFFLINE_DELAY) room = self.get_room_by_ip(ip) code, response = httpcall2(DETECTOR['PREFIX']+DETECTOR['METHOD_API'] % (CMDB_DETECTOR_IDC_MAPPING.get(room.id), DETECTOR['SECRET'], DETECTOR['SECRET'], ip, method)) if code is not None and code >= 400: response = None self.i(u'服务%s:detector|%s|%s|%s' % (method, DETECTOR['PREFIX']+DETECTOR['METHOD_API'] % (CMDB_DETECTOR_IDC_MAPPING.get(room.id), '*' * 32, '*' * 32, ip, method), code, response)) if response: try: response = json.loads(response) except: response = dict() if isinstance(response, dict) and response.get('result') == '0': return True return False
def ycc_deploy2(deploy): i(deploy.depid, 'ycc发布开始') ycc_code = TRIDENT_YCC_IDC_MAPPING.get(deploy.idc) if deploy.idc else Room.objects.get(id=deploy.zone_id).ycc_code url = YCC2['PREFIX']+YCC2['DEPLOY_API'].format(ycc_code, deploy.app.site.name, deploy.app.name) code, response = httpcall2(url) i(deploy.depid, "{0}|{1}|{2}".format(url, code, response)) response = json.loads(response) if response else {} if response.get("result"): i(deploy.depid, 'ycc发布结束') trident_callback(deploy, 2) return True else: i(deploy.depid, 'ycc发布失败,失败原因为{0}'.format(response.get("detail"))) trident_callback(deploy, 4) return False
def health_check(self): health = False for retry in range(ONLINE_REPORT['TRIES']): url = HC['PREFIX'] + HC['PATH_API'].format(self.ip) status, data = httpcall2(url) self.i('%s|%s|%s' % (url, status, data)) try: result = json.loads(data) except: return None, data if result['success'] and result['data']['status'] in (-1, 1): self.i('通过healthcheck检查,或者没有设置healthcheck') health = True break time.sleep(ONLINE_REPORT['WAIT']) return health, None
def ycc_black(deploy, black_list, white_list): i(deploy.depid, '设置YCC黑白名单') url = YCC2['PREFIX']+YCC2['BLACK_API'] body = { 'iplist': json.dumps({'black': black_list, 'white': white_list}) } code, response = httpcall2(url, method='POST', body=body) i(deploy.depid, "{0}|{1}|{2}|{3}".format(url, code, response, str(body))) response = json.loads(response) if response else {} if response.get("result"): i(deploy.depid, "设置黑白名单成功") return True else: i(deploy.depid, '设置黑白名单失败,原因为{0}'.format(response.get("detail"))) trident_callback(deploy, 4) return False
def hedwig(self, method, action): code, response = httpcall2( settings.DETECTOR['PREFIX'] + settings.DETECTOR['METHOD_API'] % (settings.CMDB_DETECTOR_IDC_MAPPING.get( self.server_obj.rack.room_id), settings.DETECTOR['SECRET'], settings.DETECTOR['SECRET'], self.ip, method)) if code == 200: print code, response response = json.loads(response) if response.get('result') == '0': msg = 'hedwig%s成功' % action else: msg = 'hedwig%s失败,原因为%s' % (action, response.get('warn')) self.i(msg) return msg else: msg = 'hedwig%s失败,原因%s|%s' % (action, code, response) self.ie(msg)
def hudson_hook(self): items = HudsonJob.objects.filter(app_id=self.deploy.app_id) if items and self.deploy.packtype == 0: if self.deploy.deptype in [1, 2]: jobs = items.filter(jobtype=2) elif self.deploy.deptype == 0: jobs = items.filter(jobtype=1) if not jobs: jobs = items.filter(jobtype=0) job = jobs[0] if jobs else None if job: url = '%s?token=%s' % (job.url, job.token) code, data = httpcall2(url) if code is None: self.ie('hudson错误:执行命令:【%s|%s】' % (url, data)) elif code >= 400: self.ie('hudson错误:执行命令:【%s|%s】' % (url, code)) self.i('hudson执行命令:【%s|%s】' % (url, code)) else: self.i('没有对应的hudson_job')
def ticket(uniq_id, ip, task_id, status=1): if uniq_id is None: return url = TICKET['PREFIX'] + TICKET['EXPAND_API'] body = { 'uniq_id': uniq_id, 'ip': ip, 'status': status, 'url': 'http://%s/deploy/single/detail/?task_id=%s&ip=%s' % (OMS_HOST, task_id, ip) } code, response = httpcall2(url, method='POST', body=body) # cache = get_cache('deploy', **{'LOCATION': CACHES['deploy']['LOCATION'] + '2'}) log = '|'.join([url, str(code), str(response)]) # i2(cache, task_id, log) write_redis_for_log(task_id, log)
def handle(self, *args, **options): for app_obj in App.objects.filter(status=0, type=0): print app_obj.name url = settings.CMIS[ 'PREFIX'] + settings.CMIS['JENKINS_API'] % app_obj.cmis_id code, result = httpcall2(url) if 200 <= code < 400: response = json.loads(result)['response'] url_list = [] for job in response: if job['jobStatus'] == 0 and job['jobTypeId'] == 6: url = job['jobUrl'] url_list.append(url) owner = job['updateUserVo']['adAccount'] deploy_jenkins_job, created = DeployJenkinsJob.objects.get_or_create( app=app_obj, url=url, defaults={'owner': owner}) if not created: deploy_jenkins_job.owner = owner deploy_jenkins_job.save() DeployJenkinsJob.objects.filter(app=app_obj).exclude( url__in=url_list).delete()
def trident_callback(deploy, status): is_webapp_deploy = False is_config_deploy = False if deploy.__class__.__name__ == 'DeployMain': is_webapp_deploy = True elif deploy.__class__.__name__ == 'DeployMainConfig': is_config_deploy = True deploy.status = status deploy.last_modified = int(time.time()) deploy.save() username = User.objects.get(pk=deploy.uid).username index = deploy.depid app_id = deploy.app_id deploy_detail_url = DEPLOY_DETAIL_URL % (OMS_HOST, deploy.depid) if is_webapp_deploy else DEPLOY_YCC_DETAIL_URL % (OMS_HOST, deploy.depid) if is_webapp_deploy: deploy_type_msg = '-'.join(["程序发布", deploy.deptype_name, deploy.packtype_name]) elif is_config_deploy: deploy_type_msg = '-'.join(['配置发布', deploy.zone.name_ch]) message = """ 单号: <a href="%s" target="_blank">%s</a> 发布类型为: %s 更改状态为: %s """ % (deploy_detail_url, deploy.depid, deploy_type_msg, deploy.status_name) RL_all_deploy(username, index, message, app_id) if deploy.uid != 523: return depid = deploy.depid # depResult = TRIDENT['STATUS_MAPPING'].get(deploy.__class__.__name__, dict()).get(deploy.status, 0) # depResult = deploy.status if is_webapp_deploy: depResult = deploy.status elif is_config_deploy: depResult = {2: 4, 3: 5, 7: 7}.get(deploy.status, 0) checkcode = md5('{0}{1}OJIMRAS'.format(depid, depResult)).hexdigest() url = TRIDENT['PREFIX']+TRIDENT['CALLBACK_API'].format(depid, depResult, checkcode) code, data = httpcall2(url) i(deploy.depid, '调用trident接口的结果:%s|%s|%s' % (url, code, data))
def haproxy(self, in_use, method, action): body = { 'site': self.app_obj.site.name, 'app': self.app_obj.name, 'idc': self.haproxy_room, 'group': self.haproxy_group, 'ips': [{ self.ip: { 'inter': '10000', 'maxconn': '500', 'port': '8080', 'weight': 1, 'check': True, 'in_use': in_use } }], 'method': method } code, response = httpcall2(settings.HAPROXY_REGISTRATION_API, method='POST', body=body) if code == 200: msg = 'haproxy%s成功(%s:%s)' % (action, self.haproxy_room, self.haproxy_group) self.i(msg) return msg elif code == 503: msg = 'haproxy%s失败(%s:%s),原因为%s' % ( action, self.haproxy_room, self.haproxy_group, json.loads(response).get('output')) self.ie(msg) else: msg = 'haproxy%s失败(%s:%s),原因%s|%s' % ( action, self.haproxy_room, self.haproxy_group, code, response) self.ie(msg)
def ycc_rollback2(deploy): room_ids = [] if deploy.idc: room_ids = TRIDENT_CMDB_IDC_MAPPING[deploy.idc] else: room_ids = [deploy.zone_id] # white_list = [obj.ip for obj in Server.objects.filter(app_id=deploy.app_id) ] white_list = [obj.server.ip for obj in DeployDetailConfig.objects.filter(depid=deploy.depid) if obj.room.id in room_ids] if not ycc_black(deploy, None, white_list): return False i(deploy.depid, 'ycc回滚开始') ycc_code = TRIDENT_YCC_IDC_MAPPING.get(deploy.idc) if deploy.idc else Room.objects.get(id=deploy.zone_id).ycc_code url = YCC2['PREFIX']+YCC2['ROLLBACK_API'].format(ycc_code, deploy.app.site.name, deploy.app.name) code, response = httpcall2(url) i(deploy.depid, "{0}|{1}|{2}".format(url, code, response)) response = json.loads(response) if response else {} if response.get("result"): i(deploy.depid, 'ycc回滚结束') trident_callback(deploy, 3) return True else: i(deploy.depid, 'ycc回滚失败,失败原因为{0}'.format(response.get("detail"))) trident_callback(deploy, 5) return False
def handle(self, *args, **options): now = datetime.datetime.now() st = datetime.datetime.strptime(now.strftime('%Y-%m-%d %H'), '%Y-%m-%d %H') # en = st + datetime.timedelta(hours=1) - datetime.timedelta(seconds=1) st_unixtime = long(time.mktime(st.timetuple())) mail_dict = dict() jiraid_dict = dict() packtype_dict = {0: 'webapps', 3: 'static'} app_id_list = [] filters = dict() filters['is_auto_published'] = 0 filters['publishdatetimefrom'] = st_unixtime # filters['publishdatetimeto'] = en_unixtime filters['publishtimetype__in'] = (1, 2, 3, 4) for deploy in DeployMainConfig.objects.filter( **dict(filters, **{ 'status': 1, 'gray_release_info__isnull': False })): jiraid = str(deploy.jiraid) app = deploy.app site = app.site if app else None pool = '{0}/{1}'.format(site.name if site else '', app.name if app else '') mail_dict[jiraid] = mail_dict.get(jiraid, dict()) mail_dict[jiraid][pool] = mail_dict[jiraid].get(pool, dict()) mail_dict[jiraid][pool]['config'] = mail_dict[jiraid][pool].get( 'config', []) mail_dict[jiraid][pool]['config'].append(str(deploy.depid)) # deploy.is_auto_published = 1 # deploy.save() jiraid_dict[jiraid] = deploy.create_time app_id_list.append(deploy.app_id) # 拉取webapps和static发布申请单 for deploy in DeployMain.objects.filter(**dict( filters, **{ 'status__in': (1, 2, 3), 'packtype__in': (0, 3), 'is_gray_release': 1 })): jiraid = str(deploy.jiraid) packtype = deploy.packtype app = deploy.app site = app.site if app else None pool = '{0}/{1}'.format(site.name if site else '', app.name if app else '') mail_dict[jiraid] = mail_dict.get(jiraid, dict()) mail_dict[jiraid][pool] = mail_dict[jiraid].get(pool, dict()) mail_dict[jiraid][pool][ packtype_dict[packtype]] = mail_dict[jiraid][pool].get( packtype_dict[packtype], []) mail_dict[jiraid][pool][packtype_dict[packtype]].append( str(deploy.depid)) # deploy.is_auto_published = 1 # deploy.save() jiraid_dict[jiraid] = deploy.create_time app_id_list.append(deploy.app_id) # # 拉取webapps和static发布申请单 print mail_dict if mail_dict: # email_info = self.format_table(mail_dict) # print "sending email..." # self.deploy_email(email_info, st.hour, app_id_list) # 语音告警 event_dict_v2 = { 'title': '灰度发布提醒', 'level_id': 300, 'type_id': 3, 'source_id': 16, 'pool_id': LEDAO_POOL_ID, 'message': '当前时间段内有灰度发布,请检查邮件!', 'send_to': ','.join(U_MAIL_RECIPIENT), 'ignore': 1 } print httpcall2(event_url_v2, 'POST', body=event_dict_v2) else: print "Can not found any gray publish task."
# body = list() # body.append(u"静态已发布的发布单:{0}".format(get_url_by_depid(static_depid_list))) # body.append(u"<span style='color: red'>YCC发布异常的发布单:{0}</span>".format(get_url_by_depid(config_failure_list))) # body.append(u"YCC已发布的发布单:{0}".format(get_url_by_depid(config_success_list))) # body.append(u"程序待发布的发布单:{0}".format(get_url_by_depid(webapps_depid_list))) # body.append(u"请正确处理已发布的发布单和待发布的发布单") # send_email(subject=subject, content=body.encode("utf8"), recipient_list=recipient_list) sendmail_v2(subject, body.encode("utf8"), recipient_list, None) print httpcall2(EVENT['PREFIX'] + EVENT['API'], 'POST', body={ 'apiType': 'Deploy', 'param': json.dumps( dict( event_dict, **{ 'warningMessage': ','.join(config_failure_list), 'warningTopic': subject })) }) return False #程序发布 if webapps_depid_list: error = False try: publish = Publish(depid=webapps_depid_list[0], interval=deploy_interval) publish.auto_publish()
def assetlist(request): room_list = Room.objects.all() asset_type = AssetType.objects.all() asset_model = AssetModel.objects.all() current_date = stamp2str(time.time() + 3 * 365 * 24 * 60 * 60, '%Y-%m-%d') rack_info = [] rack = Rack.objects.all() roomlist = Room.objects.all() server_os_template = ServerOsTemplate.objects.filter(status=1, type=1) server_app_template = ServerAppTemplate.objects.filter(status=1, type=1) server_env = ServerEnv.objects.exclude(id=0) ipsegments = IpSegment.objects.filter(status=1) mgips = [] ips = [] for item in ipsegments: if item.type == 2: mgips.append(item.id) if item.type == 3: ips.append(item.id) mgip = IpTotal.objects.filter(ip_segment_id__in=mgips, status=1, is_used=0)[0:10] ip = IpTotal.objects.filter(ip_segment_id__in=ips, status=1, is_used=0)[0:10] for item in rack: rack_info.append({ "id": item.id, "cname": "%s-%s" % (item.room.name, item.name) }) cur_session = str(int(time.time())) if request.FILES.has_key('filename'): new_session = request.POST['cur_session'] if new_session == request.session['cur_post']: fp = request.FILES.get('filename') reader = csv.reader(fp) header = reader.next() errors = [] lists = [] result = [] for row in reader: row = [item.decode("gbk") for item in row] if not len(row) == 8: errors.append({'assetid': row, 'info': '该记录内容填写缺失,请检查'}) continue assetid, asset_type_id, sn, room_n, rack_n, rackno_n, asset_model1, exp_time = row if not assetid: errors.append({'assetid': row, 'info': '该记录未填写设备编号,请检查'}) continue if not sn: errors.append({'assetid': row, 'info': '该记录未填写序列号,请检查'}) continue try: room = Room.objects.get(name=room_n) rack = Rack.objects.get(name=rack_n, room_id=room.id) model = AssetModel.objects.get(name=asset_model1) except Room.DoesNotExist: errors.append({ 'assetid': row, 'info': '该记录机房信息在CMDB中不存在,请检查' }) continue except Rack.DoesNotExist: errors.append({ 'assetid': row, 'info': '该记录机架信息在CMDB中不存在,请检查' }) continue except AssetModel.DoesNotExist: errors.append({ 'assetid': row, 'info': '该记录型号信息在CMDB中不存在,请检查' }) continue try: exp_time = str2stamp(exp_time, '%Y/%m/%d') or 0 if exp_time else 0 except Exception, e: errors.append({ 'assetid': row, 'info': '过保时间格式错误%s,请检查' % e.message }) continue rackno = RackSpace.objects.filter(rack_id=rack.id, assetid="") if rackno is None: errors.append({ 'assetid': row, 'info': '该记录所在机架的无剩余机位,请检查' }) continue if len(rackno) < int(rackno_n): errors.append({ 'assetid': row, 'info': '该记录所在机架的剩余机位号不够,请检查' }) continue rackno = rackno[0:int(rackno_n)] asset, created = Asset.objects.get_or_create( assetid=assetid, defaults={ 'service_tag': sn, 'asset_model_id': model.id, 'asset_type_id': asset_type_id, 'rack_id': rack.id, 'create_time': int(time.time()), 'last_modified': int(time.time()), 'expiration_time': exp_time, 'come_from': 1 }) if created: for item1 in rackno: item1.assetid = assetid item1.save() if asset_type_id == 9: rack, created = Rack.objects.get_or_create( name=assetid, room_id=room.id, defaults={ 'height': 20, 'valid': 0, 'ctime': int(time.time()), }) lists.append(row) CB_asset = { 'assetid': asset.assetid, 'service_tag': asset.service_tag, 'asset_type_id': asset.asset_type_id, 'asset_model_id': asset.asset_model_id, 'create_time': asset.create_time, 'last_modified': asset.last_modified, 'expiration_time': asset.expiration_time, } happen_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") body = { "user": request.user.username, "type": "asset", "action": "import", "index": asset.assetid, "message": json.dumps(CB_asset, ensure_ascii=False), "level": "change", "happen_time": happen_time } url = CMDBAPI_URL + 'change/' httpcall2(url, method="POST", body=body, username=CMDBAPI_USER, password=CMDBAPI_PASS) else: errors.append({ 'assetid': row, 'info': '该记录已存在,无需重新录入。请检查' }) for item in errors: result.append('%s - %s' % (item['assetid'], item['info'])) info = " 成功录入%s台, %s台有问题, 请检查" % (len(lists), len(errors)) result_str = "<br />".join(result)
event_url_v2 = settings.EVENT['PREFIX'] + settings.EVENT['API_V2'] try: from deploy import tasks except Exception, e: event_dict_v2 = { 'title': '无人发布脚本异常', 'level_id': 300, 'type_id': 3, 'source_id': 16, 'pool_id': LEDAO_POOL_ID, 'message': '发布系统无人发布脚本异常: ' + json.dumps(e.args), 'send_to': ','.join(U_MAIL_RECIPIENT) } print httpcall2(event_url_v2, 'POST', body=event_dict_v2) print e.args exit(1) class Command(BaseCommand): args = '' help = 'auto deploy' def handle(self, *args, **options): try: if len(args) != 1 or args[0] not in ('1', '2', '3'): print 'Usage: /opt/ENV/bin/python /data/asset/manage.py AutoDeploy <1-3>' exit(1) # 判断传入发布的时间区间 now = datetime.datetime.now()
def handle(self, *args, **options): try: if len(args) != 1 or args[0] not in ('1', '2', '3'): print 'Usage: /opt/ENV/bin/python /data/asset/manage.py AutoDeploy <1-3>' exit(1) # 判断传入发布的时间区间 now = datetime.datetime.now() st = datetime.datetime.strptime(now.strftime('%Y-%m-%d %H'), '%Y-%m-%d %H') # en = st + datetime.timedelta(hours=1) - datetime.timedelta(seconds=1) st_unixtime = long(time.mktime(st.timetuple())) # en_unixtime = long(time.mktime(en.timetuple())) deploy_interval = DEPLOY_INTERVAL if st.hour != 2 else 0 mail_dict = dict() jiraid_dict = dict() packtype_dict = {0: 'webapps', 3: 'static'} app_id_list = [] filters = dict() filters['is_auto_published'] = 0 filters['publishdatetimefrom'] = st_unixtime # filters['publishdatetimeto'] = en_unixtime filters['publishtimetype__in'] = (1, 2, 4) # filters['jiraid__in'] = ('TRIDENT-161217',) # 拉取ycc发布申请单 if args[0] in ('1', '3'): for deploy in DeployMainConfig.objects.filter(**dict( filters, **{ 'status': 1, 'gray_release_info__isnull': True })): jiraid = str(deploy.jiraid) app = deploy.app site = app.site if app else None pool = '{0}/{1}'.format(site.name if site else '', app.name if app else '') mail_dict[jiraid] = mail_dict.get(jiraid, dict()) mail_dict[jiraid][pool] = mail_dict[jiraid].get( pool, dict()) mail_dict[jiraid][pool]['config'] = mail_dict[jiraid][ pool].get('config', []) mail_dict[jiraid][pool]['config'].append(str(deploy.depid)) deploy.is_auto_published = 1 deploy.save() jiraid_dict[jiraid] = deploy.create_time app_id_list.append(deploy.app_id) # 拉取webapps和static发布申请单 if args[0] in ('2', '3'): for deploy in DeployMain.objects.filter(**dict( filters, **{ 'status__in': (1, 2, 3), 'packtype__in': (0, 3), 'is_gray_release': 0 })): jiraid = str(deploy.jiraid) packtype = deploy.packtype app = deploy.app site = app.site if app else None pool = '{0}/{1}'.format(site.name if site else '', app.name if app else '') mail_dict[jiraid] = mail_dict.get(jiraid, dict()) mail_dict[jiraid][pool] = mail_dict[jiraid].get( pool, dict()) mail_dict[jiraid][pool][ packtype_dict[packtype]] = mail_dict[jiraid][pool].get( packtype_dict[packtype], []) mail_dict[jiraid][pool][packtype_dict[packtype]].append( str(deploy.depid)) deploy.is_auto_published = 1 deploy.save() jiraid_dict[jiraid] = deploy.create_time app_id_list.append(deploy.app_id) print mail_dict # 通知Monitor并等待10分钟 if not mail_dict: return email_info = self.format_table(mail_dict) self.deploy_email(email_info, st.hour, app_id_list) time.sleep(U_DELAY) # 开始对POOL进行逐个发布,先对Trident进行排序 for jiraid, create_time in sorted(jiraid_dict.items(), key=lambda e: e[1]): for pool in mail_dict[jiraid]: site_name, app_name = pool.split('/') app_obj = App.objects.get( name=app_name, site_id=Site.objects.get(name=site_name).id, status=0) # if app_obj.id in settings.APP_ID_LIST: # tasks.all_auto_publish_v2.apply_async((jiraid, pool, mail_dict[jiraid][pool], U_MAIL_RECIPIENT)) # else: # tasks.all_auto_publish.apply_async((jiraid, pool, mail_dict[jiraid][pool], U_MAIL_RECIPIENT, deploy_interval)) tasks.all_auto_publish_v2.apply_async( (jiraid, pool, mail_dict[jiraid][pool], U_MAIL_RECIPIENT)) print 'success' except Exception, e: event_dict_v2 = { 'title': '无人发布脚本异常', 'level_id': 300, 'type_id': 3, 'source_id': 16, 'pool_id': LEDAO_POOL_ID, 'message': '无人发布脚本异常: ' + json.dumps(e.args), 'send_to': ','.join(U_MAIL_RECIPIENT_FOR_PUBLISH_ERROR) } print httpcall2(event_url_v2, 'POST', body=event_dict_v2) print e.args