Exemplo n.º 1
0
def is_docker_image_already_in_registry(service, soa_dir, sha):
    """Verifies that docker image exists in the paasta registry.

    :param service: name of the service
    :param sha: git sha
    :returns: True, False or raises requests.exceptions.RequestException
    """
    registry_uri = get_service_docker_registry(service, soa_dir)
    repository, tag = build_docker_image_name(service, sha).split(":")

    creds = read_docker_registry_creds(registry_uri)
    uri = f"{registry_uri}/v2/{repository}/manifests/paasta-{sha}"

    with requests.Session() as s:
        try:
            url = "https://" + uri
            r = (s.head(url, timeout=30)
                 if creds[0] is None else s.head(url, auth=creds, timeout=30))
        except SSLError:
            # If no auth creds, fallback to trying http
            if creds[0] is not None:
                raise
            url = "http://" + uri
            r = s.head(url, timeout=30)

        if r.status_code == 200:
            return True
        elif r.status_code == 404:
            return False  # No Such Repository Error
        r.raise_for_status()
Exemplo n.º 2
0
def is_docker_image_already_in_registry(service, soa_dir, sha):
    """Verifies that docker image exists in the paasta registry.

    :param service: name of the service
    :param sha: git sha
    :returns: True, False or raises requests.exceptions.RequestException
    """
    registry_uri = get_service_docker_registry(service, soa_dir)
    repository, tag = build_docker_image_name(service, sha).split(':')

    creds = read_docker_registy_creds(registry_uri)
    uri = '%s/v2/%s/tags/list' % (registry_uri, repository)

    with requests.Session() as s:
        try:
            url = 'https://' + uri
            r = s.get(url, timeout=30) if creds[0] is None else s.get(
                url, auth=creds, timeout=30)
        except SSLError:
            # If no auth creds, fallback to trying http
            if creds[0] is not None:
                raise
            url = 'http://' + uri
            r = s.get(url, timeout=30)

        if r.status_code == 200:
            tags_resp = r.json()
            if tags_resp['tags']:
                return tag in tags_resp['tags']
            else:
                return False
        elif r.status_code == 404:
            return False  # No Such Repository Error
        r.raise_for_status()
Exemplo n.º 3
0
def paasta_push_to_registry(args: argparse.Namespace) -> int:
    """Upload a docker image to a registry"""
    service = args.service
    if service and service.startswith("services-"):
        service = service.split("services-", 1)[1]
    validate_service_name(service, args.soa_dir)
    image_identifier = build_image_identifier(args.commit, None,
                                              args.image_version)

    if not args.force:
        try:
            if is_docker_image_already_in_registry(service, args.soa_dir,
                                                   args.commit,
                                                   args.image_version):
                print(
                    "The docker image is already in the PaaSTA docker registry. "
                    "I'm NOT overriding the existing image. "
                    "Add --force to override the image in the registry if you are sure what you are doing."
                )
                return 0
        except RequestException as e:
            registry_uri = get_service_docker_registry(service, args.soa_dir)
            print(
                "Can not connect to the PaaSTA docker registry '%s' to verify if this image exists.\n"
                "%s" % (registry_uri, str(e)))
            return 1

    cmd = build_command(service, args.commit, args.image_version)
    loglines = []
    returncode, output = _run(
        cmd,
        timeout=3600,
        log=True,
        component="build",
        service=service,
        loglevel="debug",
    )
    if returncode != 0:
        loglines.append("ERROR: Failed to promote image for %s." %
                        image_identifier)
        output = get_jenkins_build_output_url()
        if output:
            loglines.append("See output: %s" % output)
    else:
        loglines.append("Successfully pushed image for %s to registry" %
                        image_identifier)
        _log_audit(
            action="push-to-registry",
            action_details={"commit": args.commit},
            service=service,
        )
    for logline in loglines:
        _log(service=service, line=logline, component="build", level="event")
    return returncode
Exemplo n.º 4
0
def paasta_push_to_registry(args):
    """Upload a docker image to a registry"""
    service = args.service
    if service and service.startswith('services-'):
        service = service.split('services-', 1)[1]
    validate_service_name(service, args.soa_dir)

    if not args.force:
        try:
            if is_docker_image_already_in_registry(service, args.soa_dir, args.commit):
                paasta_print(
                    "The docker image is already in the PaaSTA docker registry. "
                    "I'm NOT overriding the existing image. "
                    "Add --force to override the image in the registry if you are sure what you are doing.",
                )
                return 0
        except RequestException as e:
            registry_uri = get_service_docker_registry(service, args.soa_dir)
            paasta_print("Can not connect to the PaaSTA docker registry '%s' to verify if this image exists.\n"
                         "%s" % (registry_uri, str(e)))
            return 1

    cmd = build_command(service, args.commit)
    loglines = []
    returncode, output = _run(
        cmd,
        timeout=3600,
        log=True,
        component='build',
        service=service,
        loglevel='debug',
    )
    if returncode != 0:
        loglines.append('ERROR: Failed to promote image for %s.' % args.commit)
        output = get_jenkins_build_output_url()
        if output:
            loglines.append('See output: %s' % output)
    else:
        loglines.append('Successfully pushed image for %s to registry' % args.commit)
        _log_audit(
            action='push-to-registry',
            action_details={'commit': args.commit},
            service=service,
        )
    for logline in loglines:
        _log(
            service=service,
            line=logline,
            component='build',
            level='event',
        )
    return returncode
Exemplo n.º 5
0
def paasta_get_docker_image(args):
    service = args.service
    deploy_group = args.deploy_group
    soa_dir = args.soa_dir
    validate_service_name(service, soa_dir)

    deployments = load_v2_deployments_json(service=service, soa_dir=soa_dir)
    docker_image = deployments.get_docker_image_for_deploy_group(deploy_group)

    if not docker_image:
        print(
            PaastaColors.red(
                f"There is no {service} docker_image for {deploy_group}. Has it been deployed yet?"
            ),
            file=sys.stderr,
        )
        return 1
    else:
        registry_uri = get_service_docker_registry(service=service,
                                                   soa_dir=soa_dir)
        docker_url = f"{registry_uri}/{docker_image}"
        print(docker_url)
        return 0