Skip to content

Commit

Permalink
增加容器日志查看功能
Browse files Browse the repository at this point in the history
  • Loading branch information
gujiwork committed Apr 1, 2021
1 parent 6b9b809 commit ef5c1c3
Show file tree
Hide file tree
Showing 46 changed files with 300 additions and 260 deletions.
3 changes: 2 additions & 1 deletion api/admin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.utils.translation import gettext_lazy
from api.models import InnerAccount, AccountType, DatabaseRecord, GrayDomain
# Register your models here.
from api.models import InnerAccount, DatabaseRecord, GrayDomain
from api.models import Project
from api.models import Ticket
from api.models import DeployTask
Expand Down
32 changes: 16 additions & 16 deletions api/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import requests
from api.utils.dingtalk_notice import DingTalkSendMsg
from django.core.mail import EmailMultiAlternatives
from api.utils.check_jenkins import JenkinsStauts
from api.utils.check_jenkins import JenkinsStatus
from django_redis import get_redis_connection
from jenkinsapi.jenkins import Jenkins
from decouple import config
Expand Down Expand Up @@ -52,7 +52,7 @@ def dashboard_deploy_chart_count():
DeployStatusChart.objects.update_or_create(defaults={'count': success_count}, days=str_today,deploy_status='部署成功')

except BaseException as e:
logger.error('部署成功状态统计结转失败, 异常原因: %s' % str(traceback.format_exc()))
logger.error('部署成功状态统计结转失败, 异常原因: %s' % str(traceback.format_exc()), e)

try:
failed_count = DeployTask.objects.filter(
Expand All @@ -63,10 +63,10 @@ def dashboard_deploy_chart_count():
DeployStatusChart.objects.update_or_create(defaults={'count': failed_count}, days=str_today,deploy_status='部署失败')

except BaseException as e:
logger.error('部署成功状态统计结转失败, 异常原因: %s' % str(traceback.format_exc()))
logger.error('部署成功状态统计结转失败, 异常原因: %s' % str(traceback.format_exc()), e)

alarm_user = '1508870xxxx,1508870xxxx'
notice_url = 'https://oapi.dingtalk.com/robot/send?access_token={填写ding token}'
notice_url = 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxxxxxx'
cache_redis = get_redis_connection('default')


Expand Down Expand Up @@ -491,7 +491,7 @@ def send_deploy_email(email, name, title, msg, msg_en, url, subject='小飞猪
<table align="left" width="100%" border="0" cellpadding="0" cellspacing="0" class="mcnImageContentContainer" style="min-width:100%;">
<tbody><tr>
<td class="mcnImageContent" valign="top" style="padding-right: 9px; padding-left: 9px; padding-top: 0; padding-bottom: 0; text-align:center;">
<img align="center" alt="" src="{填写你的Logo url}/images/login/loginlogo.png" width="196" style="max-width:196px; padding-bottom: 0; display: inline !important; vertical-align: bottom;" class="mcnImage">
<img align="center" alt="" src="https://www.pigs.com/images/login/loginlogo.png" width="196" style="max-width:196px; padding-bottom: 0; display: inline !important; vertical-align: bottom;" class="mcnImage">
</td>
</tr>
</tbody></table>
Expand Down Expand Up @@ -622,7 +622,7 @@ def send_deploy_email(email, name, title, msg, msg_en, url, subject='小飞猪
<td valign="top" class="mcnTextContent" style="padding: 0px 18px 9px; font-family: Arvo, Courier, Georgia, serif; font-style: normal; font-weight: normal;">
<em>Copyright © 飞巴运维平台 All rights reserved.</em>
<em>Copyright © 小飞猪运维平台 All rights reserved.</em>
</td>
</tr>
</tbody></table>
Expand Down Expand Up @@ -657,7 +657,7 @@ def send_deploy_email(email, name, title, msg, msg_en, url, subject='小飞猪
</html>
'''

message = EmailMultiAlternatives(subject, text_content, '运维平台', [email])
message = EmailMultiAlternatives(subject, text_content, '运维平台<system@pigs.com>', [email])
message.attach_alternative(html_content.format(css=css, title=title, name=name, msg=msg, msg_en=msg_en, url=url),
"text/html")
message.send()
Expand Down Expand Up @@ -707,7 +707,7 @@ def deploy_send_develop_dingtalk_group(data="异步钉钉通知", at_develop_use
:param data:
:return:
"""
develop_group_notice_url = 'https://oapi.dingtalk.com/robot/send?access_token={填写ding token}'
develop_group_notice_url = 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxxxxxxx'
headers = {
'Content-Type': 'application/json;charset=utf-8',
}
Expand Down Expand Up @@ -760,7 +760,7 @@ def check_status(project_name, build_id, task_id):
:return:
"""

deploy_status = JenkinsStauts.jenkins_task_status(project_name=project_name, build_id=int(build_id),task_id=task_id)
deploy_status = JenkinsStatus.jenkins_task_status(project_name=project_name, build_id=int(build_id),task_id=task_id)
return deploy_status


Expand Down Expand Up @@ -798,7 +798,7 @@ def check_rollback_status(project_name, build_id):
:param build_id:
:return:
"""
rollback_status = JenkinsStauts.jenkins_rollback_status(project_name=project_name, build_id=int(build_id))
rollback_status = JenkinsStatus.jenkins_rollback_status(project_name=project_name, build_id=int(build_id))


@shared_task
Expand All @@ -819,7 +819,7 @@ def sync_code_job_cluster(project_name, task_id):
"开始同步集群代码:params={'sync_project': %s, 'code': 'class', 'slb': True, 'is_restart_tomcat': True}" % project_name)
server_2.build_job('trip-sbox-sync', params={'sync_project': project_name, 'code': 'class', 'slb': True,
'is_restart_tomcat': True})
build_id = JenkinsStauts.jenkins_task_id('trip-sbox-sync')
build_id = JenkinsStatus.jenkins_task_id('trip-sbox-sync')
Sync_Model = SyncJobHistory()
Sync_Model.sync_id = build_id
Sync_Model.sync_project = project_name
Expand All @@ -829,10 +829,10 @@ def sync_code_job_cluster(project_name, task_id):
Sync_Model.off_slb = True
Sync_Model.is_restart_tomcat = True
Sync_Model.save()
sync_cluster_result = JenkinsStauts.jenkins_sync_status(project_name='trip-sbox-sync', build_id=build_id,
sync_cluster_result = JenkinsStatus.jenkins_sync_status(project_name='trip-sbox-sync', build_id=build_id,
task_id=task_id)
else:
logger.info('任务:%s, 属于单机项目无集群机器, 跳过同步。' % (project_name))
logger.info('任务:%s, 属于单机项目无集群机器, 跳过同步。' % project_name)


@shared_task
Expand All @@ -855,11 +855,11 @@ def rollback_sync_code_job_cluster(project_name):
'code': 'class',
'slb': True,
'is_restart_tomcat': True})
build_id = JenkinsStauts.jenkins_task_id('trip-sbox-sync')
sync_cluster_result = JenkinsStauts.jenkins_rollback_status(project_name='trip-sbox-sync', build_id=build_id)
build_id = JenkinsStatus.jenkins_task_id('trip-sbox-sync')
sync_cluster_result = JenkinsStatus.jenkins_rollback_status(project_name='trip-sbox-sync', build_id=build_id)

else:
logger.info('回滚任务:%s, 属于单机项目无集群机器, 跳过同步。' % (project_name))
logger.info('回滚任务:%s, 属于单机项目无集群机器, 跳过同步。' % project_name)


@shared_task
Expand Down
6 changes: 1 addition & 5 deletions api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from api.views.approval import ApprovalUserView, ApprovalListView, ApprovalAllView
from api.views.deploy import DeployView, DeployDetailView, DeployAuditLogsView, ApprovalDeployStatus, DeployCode
from api.views.deploy_chart import DashboardChart, DeployChart
from api.views.flight_order_dump import FlightOrderDumpView
from api.views.rollback import DeployRollBackView, CreateDeployBackupView
from api.views.upload import UploadView, CaseUploadView
from api.views.user import UserInfoView, CheckEmailExistView, \
Expand All @@ -12,16 +11,14 @@
from api.views.project import QueryProjectView
from api.views.dingtalk import DingConf, DingCallBack
from api.views.ticket import TicketRecordView, TicketDetailView, TicketTypelView, TicketView
from rest_framework_jwt.views import obtain_jwt_token, refresh_jwt_token
from rest_framework_jwt.views import refresh_jwt_token
from api.views.gray import GrayServerView, GrayDomainView
from api.views.websocket_bak import QueryDeployLogs
from api.views.database import DatabaseView
from api.views.es_manage import ElasticSearchView
app_name = 'api'


urlpatterns = [
url(r'websocket$', QueryDeployLogs.as_view(), name='websocket'),
url(r'^(?P<version>[v1|v2]+)/user/login$', UserLoginView.as_view(), name='login'),
url(r'^(?P<version>[v1|v2]+)/user/getUserInfo$', UserInfoView.as_view(), name='user_info'),
# 更新用户角色
Expand Down Expand Up @@ -74,7 +71,6 @@
url(r'^(?P<version>[v1|v2]+)/database$', DatabaseView.as_view(), name='database'),
# ElasticSearch
url(r'^(?P<version>[v1|v2]+)/es/search$', ElasticSearchView.as_view(), name='search'),
url(r'^(?P<version>[v1|v2]+)/flightOrder/download$', FlightOrderDumpView.as_view(), name='flight_order_dump'),



Expand Down
3 changes: 0 additions & 3 deletions api/utils/authorization.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,13 @@ def authenticate(self, request):
token = auth[1]
result = parse_payload(token)


if not result['status']:
raise AuthenticationFailed(result)

try:
user = UserInfo.objects.get(username=result['data'].get('username'))

except UserInfo.DoesNotExist:
raise AuthenticationFailed('No such user')

return user, token


Expand Down
52 changes: 27 additions & 25 deletions api/utils/check_jenkins.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
server = jenkins.Jenkins(config('JENKINS_URL'), username=config('JENKINS_USER'), password=config('JENKINS_PASS'))


class JenkinsStauts(object):
class JenkinsStatus(object):

@classmethod
def jenkins_task_status(self, project_name: str, build_id: int, task_id: str) -> dict:
Expand All @@ -39,14 +39,14 @@ def jenkins_task_status(self, project_name: str, build_id: int, task_id: str) ->
next_id = build_id
time.sleep(10)
while True:
if server.get_build_info(project_name, next_id)['building'] == True:
if server.get_build_info(project_name, next_id)['building']:
time.sleep(10)
else:
break

result = {}
result['url'] = "{0}{1}".format(job_url, next_id)
result['result'] = server.get_build_info(project_name, next_id)['result']
result = {'url': "{0}{1}".format(job_url, next_id),
'result': server.get_build_info(project_name, next_id)['result']}

queryset = DeployTask.objects.filter(task_id=task_id).first()
if result.get("result") == 'SUCCESS':
logger.info("任务:%s构建成功, 任务编号:%d" % (project_name, next_id))
Expand All @@ -57,11 +57,12 @@ def jenkins_task_status(self, project_name: str, build_id: int, task_id: str) ->
if project_name in sync_job_name:
logger.info("2分钟后开始将:%s代码从A1机器同步集群A2机器" % project_name)
DeployLogs.objects.create(task_id=task_id, status=3, message='A组机器已部署完成, 等待同步集群节点 ')
time.sleep(random.randint(5,15))
time.sleep(random.randint(5, 15))
DeployLogs.objects.create(task_id=task_id, status=3, message='正在同步集群数据, 此过程大约耗时5~15分钟! ')
# 任务构建成功后调用异步sync_job_code_cluster方法自动同步集群项目
rsyc_result = tasks.sync_code_job_cluster.delay(project_name, task_id)
logger.info('项目: %s 集群同步构建状态异步任务返回ID: %s, 状态: %s' % (project_name, str(rsyc_result.id), str(rsyc_result.state)))
logger.info('项目: %s 集群同步构建状态异步任务返回ID: %s, 状态: %s' % (
project_name, str(rsyc_result.id), str(rsyc_result.state)))
else:
DeployLogs.objects.create(task_id=task_id, status=4, message='项目部署成功 ')
queryset = DeployTask.objects.filter(task_id=task_id).first()
Expand All @@ -79,14 +80,14 @@ def jenkins_task_status(self, project_name: str, build_id: int, task_id: str) ->
UserInfo.objects.filter(username=_user).values('mobile')[0]['mobile'])
# 调用异步通知
data = "您负责的应用【%s】已部署上线, 部署版本: %s \n 提交人: %s \n上线原因: %s, 请及时关注! " % (
project_name, queryset.version, queryset.submit_people, queryset.title)
project_name, queryset.version, queryset.submit_people, queryset.title)
tasks.deploy_send_develop_dingtalk_group.delay(data, at_develop_user_list)
except BaseException as e:
print(e)
logger.error("发送研发人通知失败, 原因: %s" % str(traceback.format_exc()))

else:
logger.info('释放任务锁,%s' %(project_name,))
logger.info('释放任务锁,%s' % (project_name,))
conn_redis.delete(project_name + "_lock")
DeployLogs.objects.create(
task_id=task_id,
Expand All @@ -106,7 +107,7 @@ def jenkins_task_status(self, project_name: str, build_id: int, task_id: str) ->
UserInfo.objects.filter(username=_user).values('mobile')[0]['mobile'])
# 调用异步通知
data = "您负责的应用【%s】已部署上线, 部署版本: %s \n 提交人: %s \n上线原因: %s, 请及时关注! " % (
project_name, queryset.version, queryset.submit_people, queryset.title)
project_name, queryset.version, queryset.submit_people, queryset.title)
tasks.deploy_send_develop_dingtalk_group.delay(data, at_develop_user_list)
except BaseException as e:
print(e)
Expand All @@ -120,7 +121,7 @@ def jenkins_task_status(self, project_name: str, build_id: int, task_id: str) ->
status=6,
message='部署失败, 请通过日志查看失败原因! ')

logger.info("任务: %s构建失败,任务编号:%d 释放锁:%s" % (project_name, next_id, project_name+"_lock"))
logger.info("任务: %s构建失败,任务编号:%d 释放锁:%s" % (project_name, next_id, project_name + "_lock"))
queryset.status = 5
queryset.save()

Expand All @@ -138,7 +139,6 @@ def jenkins_task_id(self, job_name: str) -> int:

return next_id


@classmethod
def jenkins_sync_status(self, project_name: str, build_id: int, task_id: str) -> dict:
"""
Expand All @@ -152,14 +152,14 @@ def jenkins_sync_status(self, project_name: str, build_id: int, task_id: str) ->
job_url = server.get_job_info(project_name)['url']
time.sleep(10)
while True:
if server.get_build_info(project_name, build_id)['building'] == True:
if server.get_build_info(project_name, build_id)['building']:
time.sleep(10)
else:
break

result = {}
result['url'] = "{0}{1}".format(job_url, build_id)
result['result'] = server.get_build_info(project_name, build_id)['result']
result = {'url': "{0}{1}".format(job_url, build_id),
'result': server.get_build_info(project_name, build_id)['result']}

queryset_2 = SyncJobHistory.objects.filter(sync_id=build_id).first()

if result.get("result") == 'SUCCESS':
Expand All @@ -184,7 +184,9 @@ def jenkins_sync_status(self, project_name: str, build_id: int, task_id: str) ->
_user = user.split('@')[1]
at_develop_user_list.append(UserInfo.objects.filter(username=_user).values('mobile')[0]['mobile'])
# 调用异步通知
data = "您负责的应用【%s】已部署上线, 部署版本: %s \n 提交人: %s \n上线原因: %s, 请及时关注! " % (lock.title, queryset.version, queryset.submit_people, queryset.title)
data = "您负责的应用【%s】已部署上线, 部署版本: %s \n 提交人: %s \n上线原因: %s, 请及时关注! " % (
lock.title, queryset.version, queryset.submit_people, queryset.title
)
tasks.deploy_send_develop_dingtalk_group.delay(data, at_develop_user_list)
except BaseException as e:
print(e)
Expand Down Expand Up @@ -221,31 +223,31 @@ def jenkins_rollback_status(self, project_name: str, build_id: int) -> dict:
next_id = build_id
time.sleep(10)
while True:
if server.get_build_info(project_name, next_id)['building'] == True:
if server.get_build_info(project_name, next_id)['building']:
time.sleep(10)
else:
break
result = {}
result['url'] = "{0}{1}".format(job_url, next_id)
result['result'] = server.get_build_info(project_name, next_id)['result']
result = {'url': "{0}{1}".format(job_url, next_id),
'result': server.get_build_info(project_name, next_id)['result']}

if result.get("result") == 'SUCCESS':
logger.info("回滚-> 任务:%s构建成功, 任务编号:%d" % (project_name, next_id))
sync_job_name = config('SYNC_JOB_NAME').split(',')

if project_name in sync_job_name:
logger.info("2分钟后开始将:%s代码从A1机器同步集群A2机器" % project_name)
time.sleep(random.randint(3,10))
time.sleep(random.randint(3, 10))
# DeployLogs.objects.create(task_id=task_id, status=3, message='正在同步集群数据, 此过程大约耗时5~15分钟! ')
# 任务构建成功后调用异步sync_job_code_cluster方法自动同步集群项目
rsyc_result = tasks.rollback_sync_code_job_cluster.delay(project_name, build_id)
logger.info('回滚-> 项目: %s 集群同步构建状态异步任务返回ID: %s, 状态: %s' % (project_name, str(rsyc_result.id), str(rsyc_result.state)))
logger.info('回滚-> 项目: %s 集群同步构建状态异步任务返回ID: %s, 状态: %s' % (
project_name, str(rsyc_result.id), str(rsyc_result.state)))
else:
# todo 发送回滚部署成功dingtalk通知
logger.info('回滚任务:%s, 属于单机项目无集群机器, 跳过同步。' % (project_name))
logger.info('回滚任务:%s, 属于单机项目无集群机器, 跳过同步。' % project_name)
else:
# TODO 部署失败钉钉通知
conn_redis.delete(project_name + "_lock")
logger.info("回滚-> 任务: %s回滚失败, 任务编号:%d 释放锁:%s" % (project_name, next_id, project_name+"_lock"))
logger.info("回滚-> 任务: %s回滚失败, 任务编号:%d 释放锁:%s" % (project_name, next_id, project_name + "_lock"))

return result
4 changes: 3 additions & 1 deletion api/utils/dingtalk_notice.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ def get_token(self):
access_token = res.get('access_token')
return access_token
else:
logger.error("获取企业凭证失败,%s" %(res))
logger.error("获取企业凭证失败,%s" % res)
except BaseException as e:
logger.error(e)
logger.error('获取企业凭证失败: %s' % str(traceback.format_exc()))

@classmethod
Expand Down Expand Up @@ -68,4 +69,5 @@ def send_msg(self, title, msg, user_id):
logger.info('发送钉钉工作通知消息完成,请求body:{%s}' %(str(data)))

except BaseException as e:
logger.error(e)
logger.error('发送钉钉工作通知消息异常: %s' % str(traceback.format_exc()))
Loading

0 comments on commit ef5c1c3

Please sign in to comment.