Exemple #1
0
def build_command(upstream_job_name, upstream_git_commit):
    # This is kinda dumb since we just cleaned the 'services-' off of the
    # service so we could validate it, but the Docker image will have the full
    # name with 'services-' so add it back.
    tag = build_docker_tag(upstream_job_name, upstream_git_commit)
    cmd = f"docker push {tag}"
    return cmd
def build_command(upstream_job_name, upstream_git_commit):
    # This is kinda dumb since we just cleaned the 'services-' off of the
    # service so we could validate it, but the Docker image will have the full
    # name with 'services-' so add it back.
    tag = build_docker_tag(upstream_job_name, upstream_git_commit)
    cmd = "docker push %s" % (tag,)
    return cmd
Exemple #3
0
def test_build_docker_tag(mock_build_docker_image_name):
    upstream_job_name = 'foo'
    upstream_git_commit = 'bar'
    mock_build_docker_image_name.return_value = 'fake-registry/services-foo'
    expected = 'fake-registry/services-foo:paasta-%s' % (upstream_git_commit, )
    actual = utils.build_docker_tag(upstream_job_name, upstream_git_commit)
    assert actual == expected
Exemple #4
0
def test_build_docker_tag(mock_build_docker_image_name):
    upstream_job_name = "foo"
    upstream_git_commit = "bar"
    mock_build_docker_image_name.return_value = "fake-registry/services-foo"
    expected = "fake-registry/services-foo:paasta-%s" % (upstream_git_commit,)
    actual = utils.build_docker_tag(upstream_job_name, upstream_git_commit)
    assert actual == expected
Exemple #5
0
def paasta_itest(args):
    """Build and test a docker image"""
    service = args.service
    if service and service.startswith("services-"):
        service = service.split("services-", 1)[1]
    validate_service_name(service)

    tag = build_docker_tag(service, args.commit)
    run_env = os.environ.copy()
    run_env["DOCKER_TAG"] = tag
    cmd = "make itest"
    loglines = []

    _log(service=service, line="starting itest for %s." % args.commit, component="build", level="event")
    returncode, output = _run(
        cmd, env=run_env, timeout=3600, log=True, component="build", service=service, loglevel="debug"
    )
    if returncode != 0:
        loglines.append("ERROR: itest failed for %s." % args.commit)
        output = get_jenkins_build_output_url()
        if output:
            loglines.append("See output: %s" % output)
    else:
        loglines.append("itest passed for %s." % args.commit)
        if not check_docker_image(service, args.commit):
            loglines.append("ERROR: itest has not created %s" % tag)
            returncode = 1
    for logline in loglines:
        _log(service=service, line=logline, component="build", level="event")
    sys.exit(returncode)
Exemple #6
0
def paasta_itest(args):
    """Build and test a docker image"""
    service = args.service
    soa_dir = args.soa_dir
    if service and service.startswith('services-'):
        service = service.split('services-', 1)[1]
    validate_service_name(service, soa_dir=soa_dir)

    tag = build_docker_tag(service, args.commit)
    run_env = os.environ.copy()
    run_env['DOCKER_TAG'] = tag
    cmd = "make itest"
    loglines = []

    _log(
        service=service,
        line='starting itest for %s.' % args.commit,
        component='build',
        level='event'
    )
    returncode, output = _run(
        cmd,
        env=run_env,
        timeout=3600,
        log=True,
        component='build',
        service=service,
        loglevel='debug',
        stream=True,
    )
    if returncode != 0:
        loglines.append(
            'ERROR: itest failed for %s.' % args.commit
        )
        output = get_jenkins_build_output_url()
        if output:
            loglines.append('See output: %s' % output)
    else:
        loglines.append('itest passed for %s.' % args.commit)
        if not check_docker_image(service, args.commit):
            loglines.append('ERROR: itest has not created %s' % tag)
            returncode = 1
    for logline in loglines:
        _log(
            service=service,
            line=logline,
            component='build',
            level='event',
        )
    return returncode
Exemple #7
0
def paasta_itest(args):
    """Build and test a docker image"""
    service = args.service
    soa_dir = args.soa_dir
    if service and service.startswith('services-'):
        service = service.split('services-', 1)[1]
    validate_service_name(service, soa_dir=soa_dir)

    tag = build_docker_tag(service, args.commit)
    run_env = os.environ.copy()
    run_env['DOCKER_TAG'] = tag
    cmd = "make itest"
    loglines = []

    _log(
        service=service,
        line='starting itest for %s.' % args.commit,
        component='build',
        level='event'
    )
    returncode, output = _run(
        cmd,
        env=run_env,
        timeout=3600,
        log=True,
        component='build',
        service=service,
        loglevel='debug'
    )
    if returncode != 0:
        loglines.append(
            'ERROR: itest failed for %s.' % args.commit
        )
        output = get_jenkins_build_output_url()
        if output:
            loglines.append('See output: %s' % output)
    else:
        loglines.append('itest passed for %s.' % args.commit)
        if not check_docker_image(service, args.commit):
            loglines.append('ERROR: itest has not created %s' % tag)
            returncode = 1
    for logline in loglines:
        _log(
            service=service,
            line=logline,
            component='build',
            level='event',
        )
    return returncode
Exemple #8
0
def test_check_docker_image_true(mock_build_docker_image_name):
    fake_app = 'fake_app'
    fake_commit = 'fake_commit'
    mock_build_docker_image_name.return_value = 'fake-registry/services-foo'
    docker_tag = utils.build_docker_tag(fake_app, fake_commit)
    with mock.patch('docker.Client') as mock_docker:
        docker_client = mock_docker.return_value
        docker_client.images.return_value = [{
            'Created': 1425430339,
            'VirtualSize': 250344331,
            'ParentId': '1111',
            'RepoTags': [docker_tag],
            'Id': 'ef978820f195dede62e206bbd41568463ab2b79260bc63835a72154fe7e196a2',
            'Size': 0}
        ]
        assert utils.check_docker_image(fake_app, fake_commit) is True
Exemple #9
0
def test_check_docker_image_true(mock_build_docker_image_name):
    fake_app = "fake_app"
    fake_commit = "fake_commit"
    mock_build_docker_image_name.return_value = "fake-registry/services-foo"
    docker_tag = utils.build_docker_tag(fake_app, fake_commit)
    with mock.patch("docker.Client") as mock_docker:
        docker_client = mock_docker.return_value
        docker_client.images.return_value = [
            {
                "Created": 1425430339,
                "VirtualSize": 250344331,
                "ParentId": "1111",
                "RepoTags": [docker_tag],
                "Id": "ef978820f195dede62e206bbd41568463ab2b79260bc63835a72154fe7e196a2",
                "Size": 0,
            }
        ]
        assert utils.check_docker_image(fake_app, fake_commit) is True
def paasta_itest(args):
    """Build and test a docker image"""
    service = args.service
    soa_dir = args.soa_dir
    if service and service.startswith("services-"):
        service = service.split("services-", 1)[1]
    validate_service_name(service, soa_dir=soa_dir)

    tag = build_docker_tag(service, args.commit)
    run_env = os.environ.copy()
    run_env["DOCKER_TAG"] = tag
    cmd = "make itest"
    loglines = []

    _log(
        service=service,
        line="starting itest for %s." % args.commit,
        component="build",
        level="event",
    )
    returncode, output = _run(
        cmd,
        env=run_env,
        timeout=args.timeout,
        log=True,
        component="build",
        service=service,
        loglevel="debug",
    )
    if returncode != 0:
        loglines.append("ERROR: itest failed for %s." % args.commit)
        output = get_jenkins_build_output_url()
        if output:
            loglines.append("See output: %s" % output)
    else:
        loglines.append("itest passed for %s." % args.commit)
        if not check_docker_image(service, args.commit):
            loglines.append("ERROR: itest has not created %s" % tag)
            returncode = 1
    for logline in loglines:
        _log(service=service, line=logline, component="build", level="event")
    return returncode
Exemple #11
0
def paasta_cook_image(
    args: Optional[argparse.Namespace],
    service: Optional[str] = None,
    soa_dir: Optional[str] = None,
) -> int:
    """Build a docker image"""
    if not service:
        if args is None:
            print(
                "ERROR: No arguments or service passed to cook-image - unable to determine what service to cook an image for",
                file=sys.stderr,
            )
            return 1
        service = args.service
    if service and service.startswith("services-"):
        service = service.split("services-", 1)[1]
    if not soa_dir:
        if args is None:
            print(
                "ERROR: No arguments or soadir passed to cook-image - unable to determine where to look for soa-configs",
                file=sys.stderr,
            )
            return 1
        soa_dir = args.yelpsoa_config_root

    validate_service_name(service, soa_dir)

    run_env = os.environ.copy()
    if args is not None and args.commit is not None:
        # if we're given a commit, we're likely being called by Jenkins or someone
        # trying to push the cooked image to our registry - as such, we should tag
        # the cooked image as `paasta itest` would.
        tag = build_docker_tag(service, args.commit, args.image_version)
    else:
        default_tag = "paasta-cook-image-{}-{}".format(service, get_username())
        tag = run_env.get("DOCKER_TAG", default_tag)
    run_env["DOCKER_TAG"] = tag

    if not makefile_responds_to("cook-image"):
        print(
            "ERROR: local-run now requires a cook-image target to be present in the Makefile. See "
            "http://paasta.readthedocs.io/en/latest/about/contract.html.",
            file=sys.stderr,
        )
        return 1

    try:
        cmd = "make cook-image"
        returncode, output = _run(
            cmd,
            env=run_env,
            log=True,
            component="build",
            service=service,
            loglevel="debug",
        )
        if returncode != 0:
            _log(
                service=service,
                line="ERROR: make cook-image failed for %s." % service,
                component="build",
                level="event",
            )
        else:
            action_details = {"tag": tag}
            _log_audit(
                action="cook-image", action_details=action_details, service=service
            )
        return returncode

    except KeyboardInterrupt:
        print("\nProcess interrupted by the user. Cancelling.", file=sys.stderr)
        return 2