コード例 #1
0
ファイル: dns.py プロジェクト: stoensin/sa-tools-core
    def add_monitors(self, sub_domain, port, monitor_interval, monitor_schema_type, monitor_uri_path, points,
                     bak_ip_mode, send_to_dns_monitor_cb=True):
        records = [r for r in self.get_records(sub_domain=sub_domain) if r['enabled'] == '1' and
                   (r['type'] == 'A' or r['type'] == 'CNAME')]

        if self.verbose:
            for r in records:
                logger.info('add_monitor({sub_domain} {line} {type} {value})'.format(
                            sub_domain=r['name'], line=to_str(r['line']),
                            type=r['type'], value=r['value']))

        if self.dry_run:
            return

        params = {
            'domain_id': self.domain_id,
            'port': port,
            'monitor_interval': monitor_interval,
            'host': '{}.{}'.format(sub_domain, self.domain_str),
            'monitor_type': monitor_schema_type,
            'monitor_path': monitor_uri_path,
            'points': points,
            'bak_ip': bak_ip_mode,
            'email_notice': 'me',
            'less_notice': 'yes'
        }

        if send_to_dns_monitor_cb:
            params.update({'callback_url': DNS_MONITOR_CALLBACK_URL.format(cb_token=self.dns_monitor_cb_key),
                          'callback_key': self.dns_monitor_cb_key})

        for r in records:
            params['record_id'] = r['id']
            self.monitor.create(**params)
コード例 #2
0
ファイル: dns.py プロジェクト: stoensin/sa-tools-core
    def remove_records(self, sub_domain, type=None, value=None, line=DEFAULT_LINE):
        """remove all records of the sub_domain, type, value"""
        line = line if line else DEFAULT_LINE
        records = self.get_records(sub_domain=sub_domain)
        # NOTE: only deal with default line type records by default
        exist_records = [r for r in records
                         if (to_str(r['line']) == line)
                         and (not type or r['type'] == type)
                         and (not value or r['value'].rstrip('.') == value.rstrip('.'))]

        if self.verbose:
            logger.info('remove_records({sub_domain}, {type}, {value}, {line})'
                        .format(sub_domain=sub_domain,
                                type=type, value=value, line=line))

        if exist_records:
            if self.verbose:
                [logger.info('self.record.remove({domain_id}, {record_id})'
                             .format(domain_id=self.domain_id, record_id=r['id']))
                 for r in exist_records]
            if self.dry_run:
                return [], ''

            results = [json.loads(self.record.remove(domain_id=self.domain_id, record_id=r['id']))
                       for r in exist_records]

            successes = [int(r['status']['code']) == 1 for r in results]
            msg = 'remove records failed: {msgs}'.format(msgs=' || '.join([r['status']['message']
                                                         for r in results]))
        else:
            return [], ''

        return exist_records, '' if all(successes) else msg
コード例 #3
0
def render(template_name, strip_empty_lines=True, **data):
    out = lookup.get_template(template_name).render(**data)
    # When using Python 3, the render() method will return a bytes object, if output_encoding is set. Otherwise it returns
    # a string.
    out = to_str(out)

    if strip_empty_lines:
        return '\n'.join([('' if i.strip() == MAGIC_EMPTY_LINE_MARK else i)
                          for i in out.splitlines() if i.strip()]) + '\n'
    else:
        return out
コード例 #4
0
ファイル: dns.py プロジェクト: stoensin/sa-tools-core
    def remove_monitors(self, sub_domain):
        monitors = [m for m in self.get_monitors() if m['domain_id'] == self.domain_id and m['sub_domain'] == sub_domain]

        if self.verbose:
            for m in monitors:
                logger.info('remove_monitors({sub_domain} {line} {ip} {type} {path})'.format(
                            sub_domain=sub_domain, line=to_str(m['record_line']), ip=m['ip'], type=m['monitor_type'],
                            path=m['monitor_path']))

        if self.dry_run:
            return

        for m in monitors:
            self.monitor.remove(monitor_id=m['monitor_id'])
コード例 #5
0
def list_monitor(args):
    dnspod = DNSPod(domain=args.domain)

    monitors = dnspod.get_monitors()

    monitors = [m for m in monitors if m['domain'] == args.domain]
    if not args.all:
        monitors = [m for m in monitors if m['monitor_status'] == 'enabled']
    if args.sub_domain:
        monitors = [m for m in monitors if m['sub_domain'] == args.sub_domain]
    output('{:>10} {:<12} {:>15} {:>4} {:>5} {:<20} {:<5}'.format(
        'sub_domain', 'line', 'ip', 'port', 'type', 'path', 'status'))
    output('-' * 80)
    for monitor in monitors:
        output('{:>10} {:<12} {:>15} {:>4} {:>5} {:<20} {:<5}'.format(
            monitor['sub_domain'], to_str(monitor['record_line']),
            monitor['ip'], monitor['port'], monitor['monitor_type'],
            monitor['monitor_path'], monitor['monitor_status']))
コード例 #6
0
ファイル: disk.py プロジェクト: zeus911/sa-tools-core
def read_data(args):
    data_file_path = args.data_file
    read_log_cmd = 'zcat %s | ncdu -f-' % data_file_path  # Security Problem
    if not args.topn:
        logger.info("About to open ncdu ui, read datafile %s" % data_file_path)
        prompt_input("Press any key to continue...")
        process.call(read_log_cmd, shell=True)
    if args.topn:
        logger.info("About to calc top %d huge items in depth %s." %
                    (args.topn, args.max_depth))
        stdout = process.call('zcat %s' % data_file_path)['stdout']
        rawfscontent = json.loads(to_str(stdout, errors='ignore'))[3]
        logger.debug('calc top_huge_dirs_from_ncdu...')
        ret = top_huge_dirs_from_ncdu(args.topn,
                                      rawfscontent,
                                      max_depth=args.max_depth)
        logger.debug('done top_huge_dirs_from_ncdu')

        print("Top %d huge items in depth %s:" % (args.topn, args.max_depth))
        for k, v in sorted(ret.items(), key=lambda kv: kv[1], reverse=True):
            humanize_size = humanize.naturalsize(v, binary=True)
            print('%s  %s' % (humanize_size, k))
コード例 #7
0
    def add_or_modify_record(self,
                             sub_domain,
                             type,
                             value,
                             ttl=DEFAULT_TTL,
                             mx=DEFAULT_MX,
                             line=DEFAULT_LINE,
                             force_enable=None):
        """
        - add or modify 一个 记录
            - 若存在,则 modify
                - 若没差,则直接返回
                - 若有差,
                    - 若 enable,则先把对立的 type 记录都设 disable
                        (若为 CNAME,则把其他 CNAME 都设为 disable),再真正调 modify
                    - 若 disable,真正调 modify
            - 若不存在,则 add
                - 若 enable,则先把对立的 type 记录都设 disable
                    (若为 CNAME,则把其他 CNAME 都设为 disable),再真正调 add
                - 若 disable,真正调 add
        """
        ttl = ttl if ttl else DEFAULT_TTL
        line = line if line else DEFAULT_LINE
        mx = mx if mx else DEFAULT_MX
        mx = "0" if type != 'MX' else mx
        status = 'enable' if force_enable else 'disable'

        # @.example.com.h1.aqb.so. -> example.com.h1.aqb.so.
        value = value.replace('@', '').lstrip('.')

        records = self.get_records(sub_domain=sub_domain)
        # NOTE: only deal with default line type records by default
        exist_records = [
            r for r in records
            if to_str(r['line']) == line and r['type'] == type
            and r['value'].rstrip('.') == value.rstrip('.')
        ]

        if self.verbose:
            logger.info(
                'add_or_modify_record({sub_domain}, {type}, {value}, ttl={ttl}, '
                'mx={mx}, line={line}, force_enable={force_enable})'.format(
                    sub_domain=sub_domain,
                    type=type,
                    value=value,
                    ttl=ttl,
                    mx=mx,
                    line=line,
                    force_enable=force_enable))

        if exist_records:

            # if no changes
            if all([
                    r['ttl'] == str(ttl) and r['mx'] == str(mx) and
                ('enabled' if int(r['enabled']) else 'disabled') == status
                    for r in exist_records
            ]):
                return exist_records, ''

            if force_enable:
                if type == 'A':
                    self.set_status(sub_domain, 'CNAME', enable=False)
                elif type == 'CNAME':
                    self.set_status(sub_domain, 'A', enable=False)
                    self.set_status(sub_domain,
                                    'CNAME',
                                    enable=False,
                                    except_values=[value])

            # if only change status
            only_status_change_exist_records = [
                r for r in exist_records
                if (r['ttl'] == str(ttl) and r['mx'] == str(mx) and
                    ('enabled' if int(r['enabled']) else 'disabled') != status)
            ]
            rest_exist_records = [
                r for r in exist_records
                if not (r['ttl'] == str(ttl) and r['mx'] == str(mx) and (
                    'enabled' if int(r['enabled']) else 'disabled') != status)
            ]
            self.set_status_by_records(only_status_change_exist_records,
                                       enable=force_enable)

            if self.verbose:
                [
                    logger.info(
                        'record.modify({domain_id}, {record_id}, {sub_domain}, '
                        '{record_type}, {value}, {record_line}, {ttl}, {mx}, {status})'
                        .format(domain_id=self.domain_id,
                                record_id=r['id'],
                                sub_domain=r['name'],
                                record_type=type,
                                value=value,
                                record_line=line,
                                ttl=ttl,
                                mx=mx,
                                status=status)) for r in rest_exist_records
                ]
            if self.dry_run:
                return [], ''

            # NOTE: 一小时超过五次没有任何变动的修改会导致记录被锁定一小时,会报 API usage is limited
            results = [
                json.loads(
                    self.record.modify(domain_id=self.domain_id,
                                       record_id=r['id'],
                                       sub_domain=r['name'],
                                       record_type=type,
                                       value=value,
                                       record_line=line,
                                       ttl=ttl,
                                       mx=mx,
                                       status=status))
                for r in rest_exist_records
            ]

            successes = [int(r['status']['code']) == 1 for r in results]
            msg = 'modify records failed: {msgs}'.format(
                msgs=' || '.join([r['status']['message'] for r in results]))
            return exist_records, '' if all(successes) else msg
        else:
            if force_enable:
                if type == 'A':
                    self.set_status(sub_domain, 'CNAME', enable=False)
                elif type == 'CNAME':
                    self.set_status(sub_domain, 'A', enable=False)
                    self.set_status(sub_domain, 'CNAME', enable=False)

            if self.verbose:
                logger.info(
                    'record.create({domain_id}, {sub_domain}, {record_type}, {value}, '
                    '{record_line}, {ttl}, {mx}, {status})'.format(
                        domain_id=self.domain_id,
                        sub_domain=sub_domain,
                        record_type=type,
                        value=value,
                        record_line=line,
                        ttl=ttl,
                        mx=mx,
                        status=status))
            if self.dry_run:
                return [], ''

            result = json.loads(
                self.record.create(domain_id=self.domain_id,
                                   sub_domain=sub_domain,
                                   record_type=type,
                                   value=value,
                                   record_line=line,
                                   ttl=ttl,
                                   mx=mx,
                                   status=status))
            success = int(result['status']['code']) == 1

            msg = 'add record failed: {msg}'.format(
                msg=result['status']['message'])
            return [result['record']
                    ] if success else [], '' if success else msg