def main()

in tools/build/build.py [0:0]


def main():
    parser = argparse.ArgumentParser(
        epilog="With the exception of the --skip-nexus and --skip-ingester options, the user will be "
               "prompted to set options at runtime."
    )

    parser.add_argument(
        '-t', '--tag',
        dest='tag',
        help='Tag for built docker images',
    )

    parser.add_argument(
        '--docker-registry',
        dest='registry',
        help='Docker registry to tag images with. Important if you want to push the images.'
    )

    cache = parser.add_mutually_exclusive_group(required=False)

    cache.add_argument(
        '--no-cache',
        dest='cache',
        action='store_false',
        help='Don\'t use build cache'
    )

    cache.add_argument(
        '--cache',
        dest='cache',
        action='store_true',
        help='Use build cache'
    )

    push = parser.add_mutually_exclusive_group(required=False)

    push.add_argument(
        '--push',
        dest='push',
        action='store_true',
        help='Push images after building'
    )

    push.add_argument(
        '--no-push',
        dest='push',
        action='store_false',
        help='Don\'t push images after building'
    )

    parser.add_argument(
        '--dry-run',
        dest='dry',
        action='store_true',
        help="Don't execute build/push commands, but print them"
    )

    parser.add_argument(
        '--skip-nexus',
        dest='skip_nexus',
        action='store_true',
        help='Don\'t build Nexus webapp, Solr cloud & Solr cloud init images'
    )

    parser.add_argument(
        '--skip-ingester',
        dest='skip_ingester',
        action='store_true',
        help='Don\'t build Collection Manager & Granule Ingester images'
    )

    parser.add_argument(
        '--skip',
        dest='skip',
        nargs='*',
        choices=['webapp', 'solr', 'solr-init', 'gi', 'cm'],
        help='List of individual images to not build',
        default=[],
    )

    parser.add_argument(
        '--nexusproto',
        dest='proto_src',
        choices=['pip', 'git', None],
        default=None,
        help='Source for nexusproto build. \'pip\' to use the latest published version from PyPi; \'git\' to build '
             'from a git repo (see --nexusproto-repo and --nexusproto-branch). Omit to be prompted'
    )

    parser.add_argument(
        '--nexusproto-repo',
        dest='proto_repo',
        default=None,
        help='Repository URL for nexusproto build. Omit to be prompted if --nexusproto=git'
    )

    parser.add_argument(
        '--nexusproto-branch',
        dest='proto_branch',
        default=None,
        help='Repository branch name for nexusproto build. Omit to be prompted if --nexusproto=git'
    )

    parser.set_defaults(cache=None, push=None)

    args = parser.parse_args()

    tag, registry, cache, push = args.tag, args.registry, args.cache, args.push

    proto, proto_repo, proto_branch = args.proto_src, args.proto_repo, args.proto_branch

    build = {key: key not in args.skip for key in SKIP_KEYS}

    if args.skip_ingester:
        build['cm'] = False
        build['gi'] = False

    if args.skip_nexus:
        build['webapp'] = False
        build['solr'] = False
        build['solr-init'] = False

    # TODO: Prompting is a bit cumbersome. Maybe do all prompts then ask for confirmation for all entries

    if tag is None:
        tag = basic_prompt('Enter the tag to use for built images')

    if registry is None:
        registry = basic_prompt('Enter Docker image registry')

    if cache is None:
        cache = yes_no_prompt('Use Docker build cache? [Y]/N: ')

    if push is None:
        push = yes_no_prompt('Push built images? [Y]/N: ')

    if proto is None:
        proto = 'git' if yes_no_prompt('Custom build nexusproto? Y/[N]: ', default=False) else 'pip'

    if proto == 'git':
        if proto_repo is None:
            proto_repo = basic_prompt('Enter nexusproto repository URL', default=ASF_NEXUSPROTO_REPO)

        if proto_branch is None:
            proto_branch = basic_prompt('Enter nexusproto repository branch', default=ASF_NEXUSPROTO_BRANCH)

    extract_dir = tempfile.TemporaryDirectory()

    pull_source(extract_dir, build)

    os.environ['DOCKER_DEFAULT_PLATFORM'] = 'linux/amd64'

    built_images = []

    if any([build['cm'], build['gi']]):
        print('Building ingester images...')

        cm_tag = f'{registry}/sdap-collection-manager:{tag}'

        if build['cm']:
            run_subprocess(build_cmd(
                cm_tag,
                os.path.join(extract_dir.name, 'ingester'),
                dockerfile='collection_manager/docker/Dockerfile',
                cache=cache
            ), dryrun=args.dry)

            built_images.append(cm_tag)

        gi_tag = f'{registry}/sdap-granule-ingester:{tag}'

        if build['gi']:
            run_subprocess(build_cmd(
                gi_tag,
                os.path.join(extract_dir.name, 'ingester'),
                dockerfile='granule_ingester/docker/Dockerfile',
                cache=cache,
                proto=proto,
                proto_repo=proto_repo,
                proto_branch=proto_branch
            ), dryrun=args.dry)

            built_images.append(gi_tag)

    if any([build['webapp'], build['solr'], build['solr-init']]):
        solr_tag = f'{registry}/sdap-solr-cloud:{tag}'

        if build['solr']:
            run_subprocess(build_cmd(
                solr_tag,
                os.path.join(extract_dir.name, 'nexus/docker/solr'),
                cache=cache
            ), dryrun=args.dry)

            built_images.append(solr_tag)

        solr_init_tag = f'{registry}/sdap-solr-cloud-init:{tag}'

        if build['solr-init']:
            run_subprocess(build_cmd(
                solr_init_tag,
                os.path.join(extract_dir.name, 'nexus/docker/solr'),
                dockerfile='cloud-init/Dockerfile',
                cache=cache
            ), dryrun=args.dry)

            built_images.append(solr_init_tag)

        webapp_tag = f'{registry}/sdap-nexus-webapp:{tag}'

        if build['webapp']:
            run_subprocess(build_cmd(
                webapp_tag,
                os.path.join(extract_dir.name, 'nexus'),
                dockerfile='docker/nexus-webapp/Dockerfile',
                cache=cache,
                proto=proto,
                proto_repo=proto_repo,
                proto_branch=proto_branch
            ), dryrun=args.dry)

            built_images.append(webapp_tag)

    if not args.dry:
        print('Image builds completed')

    if push:
        for image in built_images:
            run_subprocess(
                [DOCKER, 'push', image], dryrun=args.dry
            )

    print('done')