コード例 #1
0
def do_ls(ns):
    response = requests.get(ns.api_url / 'autocert', json=jsonify(ns, destinations=dictify(ns.destinations)))
    if response.status_code == 200:
        json = response.json()
        output(json)
        return
    else:
        print(response)
        print(response.text)
    raise Exception('wtf do_ls')
コード例 #2
0
def main():
    parser = ArgumentParser(add_help=False)
    parser.add_argument('-C',
                        '--config',
                        action='store_true',
                        help='show loaded CFG values')
    parser.add_argument('-D',
                        '--debug',
                        action='store_true',
                        help='turn on debug mode')
    parser.add_argument('-V',
                        '--version',
                        choices=VERSIONS,
                        const=VERSIONS[0],
                        nargs='?',
                        help='default=%(const)s; show the version')
    parser.add_argument('-U',
                        '--api-url',
                        metavar='URL',
                        default='http://0.0.0.0',
                        type=URL,
                        help='default=%(default)s; set the api url to use')
    parser.add_argument(
        '-S',
        '--sort',
        dest='sorting',
        metavar='SORT',
        default=SORTING[0],
        choices=SORTING,
        help=
        'default="%(default)s"; set the sorting method; choices=[%(choices)s]')

    parser.set_defaults(**CFG)
    ns, rem = parser.parse_known_args()
    if not any([h in rem for h in ('-h', '--help')]):
        version_check(ns)

    if ns.config:
        output(dict(CFG=dict(CFG)))
        sys.exit(0)

    parser = ArgumentParser(parents=[parser],
                            description=__doc__,
                            formatter_class=RawDescriptionHelpFormatter)

    add_subparsers(parser)
    parser.set_defaults(**CFG)

    ns = parser.parse_args()
    if ns.debug:
        print('ns =', ns)
    ns.func(ns)
コード例 #3
0
def version_check(ns):
    if ns.version not in VERSIONS:
        version = api_version(ns)
        version = version.split('-')[0]
        if version_parse(version) >= version_parse(cli_version()):
            logging.debug('version_check: PASSED')
            return
        raise VersionCheckFailedError(version, cli_version())
    if ns.version == VERSIONS[0]:
        output({'cli-version': cli_version()})
    elif ns.version == VERSIONS[1]:
        output({'api-version': api_version(ns)})
    sys.exit(0)
コード例 #4
0
def do_deploy(ns):
    response = requests.put(ns.api_url / 'autocert',
                            json=jsonify(ns,
                                         destinations=dictify(
                                             ns.destinations)))
    if response.status_code == 200:
        certs = response.json().get('certs', [])
        output(certs)
    elif response.status_code == 201:
        certs = response.json().get('certs', [])
        output(certs)
    else:
        print(response)
        print(response.text)
        raise Exception('wtf do_deploy')
コード例 #5
0
ファイル: cli.py プロジェクト: fox2mike/autocert
def do_request(ns):
    method = METHODS[ns.command]
    destinations = dictify(ns.destinations) if hasattr(
        ns, 'destinations') else None
    json = jsonify(ns, destinations=destinations if destinations else None)
    validate(ns.api_url, throw=True)
    response = requests.request(method, ns.api_url / 'autocert', json=json)
    status = response.status_code
    try:
        json = response.json()
        output(json)
    except JSONDecodeError as jde:
        print('status =', status)
        print('JSONDecodeError =', jde)
        print('text =', response.text)
        return -1
    return status
コード例 #6
0
def do_renew(ns):
    json = {
        'common_name': ns.common_name,
        'authority': ns.authority,
        'verbosity': ns.verbosity,
    }
    response = requests.put(ns.api_url / 'autocert', json=json)
    if response.status_code == 201:
        certs = response.json()['certs']
        if not ns.verbose:
            pass
        output(certs)
        return
    else:
        print(response)
        print(response.text)
    raise Exception('wtf do_renew')
コード例 #7
0
ファイル: cli.py プロジェクト: fox2mike/autocert
def main():
    parser = ArgumentParser(add_help=False)
    parser.add_argument('-C',
                        '--config',
                        choices=LOC,
                        const=LOC[0],
                        nargs='?',
                        help='default="%(const)s"; show the config')
    parser.add_argument('-V',
                        '--version',
                        choices=LOC,
                        const=LOC[0],
                        nargs='?',
                        help='default="%(const)s"; show the version')
    parser.add_argument('-U',
                        '--api-url',
                        metavar='URL',
                        default='http://0.0.0.0',
                        type=URL,
                        help='default="%(default)s"; set the api url to use')
    parser.add_argument(
        '--sort',
        dest='sorting',
        metavar='SORT',
        default=SORTING[0],
        choices=SORTING,
        help=
        'default="%(default)s"; set the sorting method; choices=[%(choices)s]')
    add_argument(parser, '-n', '--nerf')

    parser.set_defaults(**CFG)
    ns, rem = parser.parse_known_args()

    config = AttrDict(cli=dict(CFG), api=fetch_api_config(ns))
    version = AttrDict(cli=get_cli_version().split('-')[0],
                       api=fetch_api_version(ns))
    version_check(version)

    if ns.version:
        output(
            {'{version}-version'.format(**ns.__dict__): version[ns.version]})
        sys.exit(0)
    if ns.config:
        output({'{config}-config'.format(**ns.__dict__): config[ns.config]})
        sys.exit(0)

    parser = ArgumentParser(parents=[parser],
                            description=__doc__,
                            formatter_class=RawDescriptionHelpFormatter)

    add_subparsers(parser, config.api)
    parser.set_defaults(**CFG)

    ns = parser.parse_args()
    if ns.nerf:
        output(dict(ns=ns.__dict__))
        sys.exit(0)

    status = do_request(ns)

    if status not in (200, 201, 202, 203, 204, 205):
        return status