Example #1
0
def notify_by_email(event, obj):
    spug_key, u_ids = _parse_args(obj.grp)
    users = set(
        x.email
        for x in Contact.objects.filter(id__in=u_ids, email__isnull=False))
    if users:
        mail_service = json.loads(AppSetting.get_default('mail_service', '{}'))
        body = [
            '告警名称:' + obj.name, '告警时间:' + human_datetime(), '告警描述:' + obj.out
        ]
        if event == '2':
            body.append('故障持续:' + obj.duration)
        if mail_service.get('server'):
            event_map = {'1': '告警发生', '2': '告警恢复'}
            subject = f'{event_map[event]}-{obj.name}'
            mail = Mail(**mail_service)
            mail.send_text_mail(users, subject,
                                '\r\n'.join(body) + '\r\n\r\n自动发送,请勿回复。')
        elif spug_key:
            data = {
                'token': spug_key,
                'event': event,
                'subject': obj.name,
                'body': '\r\n'.join(body),
                'users': list(users)
            }
            requests.post(f'{spug_server}/apis/notify/mail/', json=data)
        else:
            Notify.make_notify(notify_source, '1', '发送报警信息失败',
                               '未配置报警服务调用凭据,请在系统管理/系统设置/报警服务设置中配置。')
    else:
        Notify.make_notify(notify_source, '1', '发送报警信息失败',
                           '未找到可用的通知对象,请确保设置了相关报警联系人的邮件地址。')
Example #2
0
File: utils.py Project: zyayun/spug
 def send_deploy_notify(req):
     rst_notify = json.loads(req.deploy.rst_notify)
     host_ids = json.loads(req.host_ids)
     if rst_notify['mode'] != '0' and rst_notify.get('value'):
         extra = json.loads(req.extra)
         if req.deploy.extend == '1':
             mode, extra1, extra2 = extra
             if mode == 'branch':
                 version = f'{extra1}#{extra2[:6]}'
             else:
                 version = extra1
         else:
             version = extra[0]
         hosts = [{
             'id': x.id,
             'name': x.name
         } for x in Host.objects.filter(id__in=host_ids)]
         if rst_notify['mode'] == '1':
             color, text = ('#8ece60',
                            '成功') if req.status == '3' else ('#f90202',
                                                             '失败')
             texts = [
                 '## %s ## ' % '发布结果通知',
                 f'**应用名称:** {req.deploy.app.name} ',
                 f'**应用版本:** {version} ',
                 f'**发布环境:** {req.deploy.env.name} ',
                 f'**发布主机:** {",".join(x["name"] for x in hosts)} ',
                 f'**发布结果:** <font color="{color}">{text}</font>',
                 f'**发布时间:** {human_datetime()} ', '> 来自 Spug运维平台'
             ]
             data = {
                 'msgtype': 'markdown',
                 'markdown': {
                     'title': '发布结果通知',
                     'text': '\n\n'.join(texts)
                 }
             }
             res = requests.post(rst_notify['value'], json=data)
         elif rst_notify['mode'] == '2':
             data = {
                 'req_id': req.id,
                 'app_id': req.deploy.app_id,
                 'app_name': req.deploy.app.name,
                 'env_id': req.deploy.env_id,
                 'env_name': req.deploy.env.name,
                 'version': version,
                 'targets': hosts,
                 'is_success': req.status == '3',
                 'deploy_at': human_datetime()
             }
             requests.post(rst_notify['value'], json=data)
Example #3
0
 def send_deploy_notify(cls, req, action=None):
     rst_notify = json.loads(req.deploy.rst_notify)
     host_ids = json.loads(req.host_ids)
     if rst_notify['mode'] != '0' and rst_notify.get('value'):
         extra = json.loads(req.extra)
         if req.deploy.extend == '1':
             mode, extra1, extra2 = extra
             if mode == 'branch':
                 version = f'{extra1}#{extra2[:6]}'
             else:
                 version = extra1
         else:
             version = extra[0] or ''
         hosts = [{
             'id': x.id,
             'name': x.name
         } for x in Host.objects.filter(id__in=host_ids)]
         host_str = ', '.join(x['name'] for x in hosts[:2])
         if len(hosts) > 2:
             host_str += f'等{len(hosts)}台主机'
         if rst_notify['mode'] == '1':
             data = cls._make_dd_notify(action, req, version, host_str)
         elif rst_notify['mode'] == '2':
             data = {
                 'action': action,
                 'req_id': req.id,
                 'req_name': req.name,
                 'app_id': req.deploy.app_id,
                 'app_name': req.deploy.app.name,
                 'env_id': req.deploy.env_id,
                 'env_name': req.deploy.env.name,
                 'status': req.status,
                 'reason': req.reason,
                 'version': version,
                 'targets': hosts,
                 'is_success': req.status == '3',
                 'created_at': human_datetime()
             }
         elif rst_notify['mode'] == '3':
             data = cls._make_wx_notify(action, req, version, host_str)
         else:
             raise NotImplementedError
         res = requests.post(rst_notify['value'], json=data)
         if res.status_code != 200:
             Notify.make_notify(
                 'flag', '1', '发布通知发送失败',
                 f'返回状态码:{res.status_code}, 请求URL:{res.url}')
         if rst_notify['mode'] in ['1', '3']:
             res = res.json()
             if res.get('errcode') != 0:
                 Notify.make_notify('flag', '1', '发布通知发送失败', f'返回数据:{res}')
Example #4
0
def auto_run_by_minute():
    try:
        now = datetime.now()
        for req in DeployRequest.objects.filter(status='2'):
            if (now - parse_time(req.do_at)).seconds > 3600:
                req.status = '-3'
                req.save()
        for req in DeployRequest.objects.filter(status='1', plan__lte=now):
            req.status = '2'
            req.do_at = human_datetime()
            req.do_by = req.created_by
            req.save()
            Thread(target=dispatch, args=(req,)).start()
    finally:
        connections.close_all()
Example #5
0
def _sync_host_extend(host, private_key=None, public_key=None, password=None, ssh=None):
    if not ssh:
        kwargs = host.to_dict(selects=('hostname', 'port', 'username'))
        ssh = _get_ssh(kwargs, host.pkey, private_key, public_key, password)
    form = AttrDict(fetch_host_extend(ssh))
    form.disk = json.dumps(form.disk)
    form.public_ip_address = json.dumps(form.public_ip_address)
    form.private_ip_address = json.dumps(form.private_ip_address)
    form.updated_at = human_datetime()
    form.os_type = check_os_type(form.os_name)
    if hasattr(host, 'hostextend'):
        extend = host.hostextend
        extend.update_by_dict(form)
    else:
        extend = HostExtend.objects.create(host=host, **form)
    return extend
Example #6
0
def _do_notify(task, mode, url, msg):
    if mode == '1':
        texts = [
            '## <font color="#f90202">任务执行失败通知</font> ## ',
            f'**任务名称:** {task.name} ', f'**任务类型:** {task.type} ',
            f'**描述信息:** {msg or "请在任务计划执行历史中查看详情"} ',
            f'**发生时间:** {human_datetime()} ', '> 来自 Spug运维平台'
        ]
        data = {
            'msgtype': 'markdown',
            'markdown': {
                'title': '任务执行失败通知',
                'text': '\n\n'.join(texts)
            }
        }
        requests.post(url, json=data)
    elif mode == '2':
        data = {
            'task_id': task.id,
            'task_name': task.name,
            'task_type': task.type,
            'message': msg or '请在任务计划执行历史中查看详情',
            'created_at': human_datetime()
        }
        requests.post(url, json=data)
    elif mode == '3':
        texts = [
            '## <font color="warning">任务执行失败通知</font>', f'任务名称: {task.name}',
            f'任务类型: {task.type}', f'描述信息: {msg or "请在任务计划执行历史中查看详情"}',
            f'发生时间: {human_datetime()}', '> 来自 Spug运维平台'
        ]
        data = {
            'msgtype': 'markdown',
            'markdown': {
                'content': '\n'.join(texts)
            }
        }
        res = requests.post(url, json=data)
        if res.status_code != 200:
            Notify.make_notify('schedule', '1', '任务执行通知发送失败',
                               f'返回状态码:{res.status_code}, 请求URL:{url}')
        if mode in ['1', '3']:
            res = res.json()
            if res.get('errcode') != 0:
                Notify.make_notify('schedule', '1', '任务执行通知发送失败',
                                   f'返回数据:{res}')
Example #7
0
def _do_notify(task, mode, url, msg):
    if mode == '1':
        texts = [
            '## <font color="#f90202">任务执行失败通知</font> ## ',
            f'**任务名称:** {task.name} ', f'**任务类型:** {task.type} ',
            f'**描述信息:** {msg or "请在任务计划执行历史中查看详情"} ',
            f'**发生时间:** {human_datetime()} ', '> 来自 Spug运维平台'
        ]
        data = {
            'msgtype': 'markdown',
            'markdown': {
                'title': '任务执行失败通知',
                'text': '\n\n'.join(texts)
            }
        }
        requests.post(url, json=data)
    elif mode == '2':
        data = {
            'task_id': task.id,
            'task_name': task.name,
            'task_type': task.type,
            'message': msg or '请在任务计划执行历史中查看详情',
            'created_at': human_datetime()
        }
        requests.post(url, json=data)
    elif mode == '3':
        texts = [
            '## <font color="warning">任务执行失败通知</font>',
            f'**任务名称:** {task.name} ', f'**任务类型:** {task.type} ',
            f'**描述信息:** {msg or "请在任务计划执行历史中查看详情"} ',
            f'**发生时间:** {human_datetime()} ', '> 来自 Spug运维平台'
        ]
        data = {
            'msgtype': 'markdown',
            'markdown': {
                'content': '\n'.join(texts)
            }
        }
        requests.post(url, json=data)
Example #8
0
 def send_deploy_notify(cls, req, action=None):
     rst_notify = json.loads(req.deploy.rst_notify)
     host_ids = json.loads(req.host_ids)
     if rst_notify['mode'] != '0' and rst_notify.get('value'):
         url = rst_notify['value']
         version = req.version
         hosts = [{
             'id': x.id,
             'name': x.name
         } for x in Host.objects.filter(id__in=host_ids)]
         host_str = ', '.join(x['name'] for x in hosts[:2])
         if len(hosts) > 2:
             host_str += f'等{len(hosts)}台主机'
         if rst_notify['mode'] == '1':
             cls._make_dd_notify(url, action, req, version, host_str)
         elif rst_notify['mode'] == '2':
             data = {
                 'action': action,
                 'req_id': req.id,
                 'req_name': req.name,
                 'app_id': req.deploy.app_id,
                 'app_name': req.deploy.app.name,
                 'env_id': req.deploy.env_id,
                 'env_name': req.deploy.env.name,
                 'status': req.status,
                 'reason': req.reason,
                 'version': version,
                 'targets': hosts,
                 'is_success': req.status == '3',
                 'created_at': human_datetime()
             }
             Notification.handle_request(url, data)
         elif rst_notify['mode'] == '3':
             cls._make_wx_notify(url, action, req, version, host_str)
         elif rst_notify['mode'] == '4':
             cls._make_fs_notify(url, action, req, version, host_str)
         else:
             raise NotImplementedError
Example #9
0
def _do_notify(task, mode, url, msg):
    if mode == '1':
        texts = [
            '## <font color="#f90202">任务执行失败通知</font> ## ',
            f'**任务名称:** {task.name} ', f'**任务类型:** {task.type} ',
            f'**描述信息:** {msg or "请在任务计划执行历史中查看详情"} ',
            f'**发生时间:** {human_datetime()} ', '> 来自 Spug运维平台'
        ]
        data = {
            'msgtype': 'markdown',
            'markdown': {
                'title': '任务执行失败通知',
                'text': '\n\n'.join(texts)
            },
            'at': {
                'isAtAll': True
            }
        }
        Notification.handle_request(url, data, 'dd')
    elif mode == '2':
        data = {
            'task_id': task.id,
            'task_name': task.name,
            'task_type': task.type,
            'message': msg or '请在任务计划执行历史中查看详情',
            'created_at': human_datetime()
        }
        Notification.handle_request(url, data)
    elif mode == '3':
        texts = [
            '## <font color="warning">任务执行失败通知</font>', f'任务名称: {task.name}',
            f'任务类型: {task.type}', f'描述信息: {msg or "请在任务计划执行历史中查看详情"}',
            f'发生时间: {human_datetime()}', '> 来自 Spug运维平台'
        ]
        data = {
            'msgtype': 'markdown',
            'markdown': {
                'content': '\n'.join(texts)
            }
        }
        Notification.handle_request(url, data, 'wx')
    elif mode == '4':
        data = {
            'msg_type': 'post',
            'content': {
                'post': {
                    'zh_cn': {
                        'title':
                        '任务执行失败通知',
                        'content': [
                            [{
                                'tag': 'text',
                                'text': f'任务名称: {task.name}'
                            }],
                            [{
                                'tag': 'text',
                                'text': f'任务类型: {task.type}'
                            }],
                            [{
                                'tag': 'text',
                                'text': f'描述信息: {msg or "请在任务计划执行历史中查看详情"}'
                            }],
                            [{
                                'tag': 'text',
                                'text': f'发生时间: {human_datetime()}'
                            }],
                            [{
                                'tag': 'at',
                                'user_id': 'all'
                            }],
                        ]
                    }
                }
            }
        }
        Notification.handle_request(url, data, 'fs')