Exemple #1
0
def build(images_to_build):
    '''
    Build a container
    '''
    utils.logger.debug("Executing build command")
    images_to_build = images_to_build or utils.get_images_from_dockerfiles()
    tag = git.get_hash()
    for image in images_to_build:
        utils.logger.info('Building image: %(image)s', dict(image=image))
        dockerfile = utils.image_to_dockerfile(image)

        if not os.path.exists(dockerfile):
            utils.logger.warning(
                'Image %(image)s is not valid for this project! Skipping...',
                dict(image=image))
            continue

        fqdn_image = image + ':' + tag

        command = ['docker', 'build', '-f', dockerfile, '-t', fqdn_image, '.']

        ret = runner.run(command)
        if ret != 0:
            utils.logger.error('Failed to build image: %(image)s',
                               dict(image=image))
            return ret

    return 0
Exemple #2
0
def images(ctx, remote):
    """
    List images
    """
    utils.logger.debug("Executing images command")

    valid_images = ctx.obj.get(
        'containers') or utils.get_images_from_dockerfiles()
    images_names = valid_images.keys()
    utils.logger.info("Expected images: %(images)s\n",
                      dict(images=", ".join(images_names)))
    images_info = utils.get_local_images_info(images_names)
    if remote:
        _validate_global_params(ctx, 'registry')
        try:
            images_info += utils.get_remote_images_info(
                images_names, ctx.obj['registry'])
        except Exception as exp:
            raise click.exceptions.ClickException(
                'Got unknow error from remote registry %(error)s' %
                dict(error=exp.message))

    print(
        tabulate.tabulate(images_info,
                          headers=['REGISTRY', 'IMAGE', 'TAG'],
                          tablefmt='grid'))
Exemple #3
0
def _validate_project_image(image):
    project_images = utils.get_images_from_dockerfiles()
    if image not in project_images:
        raise click.BadParameter(
            "'%s' is not an image of this project, try %s" %
            (image, project_images),
            param_hint='image')
Exemple #4
0
def build(ctx, images_to_build, container_context, cache):
    """
    Build a container
    """
    utils.logger.debug("Executing build command")

    valid_images = ctx.obj.get('containers') or utils.get_images_from_dockerfiles()
    valid_images = {image: os.path.abspath(dockerfile) for image, dockerfile in six.iteritems(valid_images)}
    valid_images_to_build = {}
    if not images_to_build:
        valid_images_to_build = valid_images
    else:
        for image in images_to_build:
            if image not in valid_images:
                utils.logger.warning('Image %(image)s is not valid for this project! Skipping...', dict(image=image))
                continue
            valid_images_to_build[image] = valid_images[image]

    tag = git.get_hash()
    for image, dockerfile in six.iteritems(valid_images_to_build):
        utils.logger.info('Building image: %(image)s', dict(image=image))

        if not os.path.exists(dockerfile):
            utils.logger.warning('Dockerfile %(dockerfile)s does not exist! Skipping...', dict(dockerfile=dockerfile))
            continue

        fqdn_image = image + ':' + tag
        if container_context is not None:
            build_context = container_context
        elif ctx.obj['container_context']:
            build_context = ctx.obj['container_context']
        else:
            build_context = os.path.dirname(dockerfile)
        command = ['docker', 'build', '--network=host', '--build-arg', 'TAG={}'.format(tag),
                   '-f', dockerfile, '-t', fqdn_image, build_context]
        if cache:
            cache_image = utils.generate_fqdn_image(ctx.obj['registry'], namespace=None, image=image, tag=DOCKER_TAG_FOR_CACHE)
            runner.run(['docker', 'pull', cache_image])
            command.extend(['--cache-from', cache_image])
        ret = runner.run(command)

        if ret != 0:
            utils.logger.error('Failed to build image: %(image)s', dict(image=image))
            return ret

        if cache:
            cache_image = utils.generate_fqdn_image(ctx.obj['registry'], namespace=None, image=image, tag=DOCKER_TAG_FOR_CACHE)
            runner.run(['docker', 'tag', fqdn_image, cache_image])
            runner.run(['docker', 'push', cache_image])

    return 0