Example #1
0
def notifyByEmail(config, data):
    prefix = 'error_' if 'error' in data else ''
    subject = config[prefix + 'subject'].format(**data)
    body = config[prefix + 'body'].format(**data)
    APP.debug(f'邮件 数据===> {subject} ; {body}')
    #p('邮件===>', subject, '; ', body)
    if not CONFIG['dry']:
        res = APP.send_email(subject, body)
        if res:
            APP.error(f'邮件推送失败:{res}')
            #p('邮件推送失败:', res, force=True)
        else:
            APP.debug('邮件发送成功。')
Example #2
0
def run(version):
    assert (version == 4 or version == 6)
    dnsType = 'AAAA' if version == 6 else 'A'
    domains = CONFIG[f'ipv{version}']
    if not domains:
        APP.debug(f'未配置IPv{version},不需要更新。')
        return
    try:
        APP.debug('*' * 40 + f'IPv{version}' + '*' * 40)
        newIP = getIp(version)
        APP.debug(f'解析IPv{version}结果为:{newIP}')
        changedDomains = []
        for subDomain in domains:
            oldIp = getOldIP(version, subDomain)
            if newIP != oldIp or CONFIG['force']:
                APP.info(f'域名{subDomain}的IPv{version}已发生改变,上次地址为{oldIp}')

                result = refreshRecord(subDomain, newIP, version)
                status = result['status']
                if status['code'] != '1':
                    raise RuntimeError('{}-{}'.format(status['code'],
                                                      status['message']))

                APP.info(f'域名{subDomain}的{dnsType}纪录已经更新为{newIP}')
                changedDomains.append(subDomain)
            else:
                APP.debug(f'域名{subDomain}的{dnsType}纪录未发生改变')
        if not CONFIG['dry']:
            saveIP(version, newIP, domains)
        if changedDomains:
            notify({
                'version': version,
                'dnsType': dnsType,
                'ip': newIP,
                'domains': ','.join(changedDomains)
            })
    except Exception as ex:
        APP.error(f'!!!运行失败,原因:{ex}')
        notify({
            'version': version,
            'dnsType': dnsType,
            'domains': ','.join(domains),
            'error': ex
        })

    if CONFIG['dry']:
        print('\n\n' + '!' * 20 + f'-这是在dry模式下运行,实际IPv{version}域名未作更新-' +
              '!' * 20)
Example #3
0
def notify_by_email(data):
    mail_data = data.copy()
    #APP.debug(f'邮件 数据===> {subject} ; {body}')
    subject = MAIL_SUBJECT.format(**data)

    mail_data['comment_li'] = ''.join(
        (f'<li>{c}</li>' for c in data['comments']))
    mail_data['command_li'] = ''.join(
        (f'<li>{c}</li>' for c in data['commands']))
    mail_data['stdout_li'] = ''.join(
        (f'<li>{c}</li>' for c in data['stdout_list']))
    mail_data['stderr_li'] = ''.join(
        (f'<li>{c}</li>' for c in data['stderr_list']))

    body = MAIL_BODY.format(**mail_data)
    res = APP.send_email(subject, html_body=body)
    if res:
        APP.error(f'邮件推送失败:{res}')
    else:
        APP.debug(f'邮件发送成功,数据===> {subject}')
Example #4
0
def notifyByDingTail(config, data):
    """发消息给钉钉机器人
    """
    token = config['token']
    if not token:
        APP.error('没有钉钉token')
        #p('APP.error: 没有钉钉token')
        return
    prefix = 'error_' if 'error' in data else ''
    data = {
        "msgtype": "text",
        "text": {
            "content":
            config['keyword'] + config[prefix + 'message'].format(**data)
        },
        "at": config['at']
    }
    APP.debug(f'钉钉机器人 数据===> {data}')
    #p('钉钉机器人===>', data)
    if not CONFIG['dry']:
        res = requests.post(url="https://oapi.dingtalk.com/robot/send?access_token={}".format(token), \
            headers = {'Content-Type': 'application/json'}, data=json.dumps(data))
        #p('钉钉推送结果:', res.json())
        APP.debug(f'钉钉推送结果:{res.json()}')