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
def push(ctx, namespace, force, image): """ Push a container """ utils.logger.debug("Executing push command") _validate_global_params(ctx, 'registry') tag = git.get_hash() image_name = image + ':' + tag ret = _push(ctx, force, image, image_name, namespace, tag) if ret != 0: return ret return ret
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
def cli(ctx, registry, build_container_image, build_container_tag, build_container_net, verbose): """ Easily dockerize your Git repository """ logging_level = logging.DEBUG if verbose else logging.INFO utils.configure_logging(name='skipper', level=logging_level) ctx.obj['registry'] = registry ctx.obj['build_container_image'] = build_container_image ctx.obj['build_container_net'] = build_container_net ctx.obj['git_revision'] = build_container_tag == 'git:revision' ctx.obj['build_container_tag'] = git.get_hash() if ctx.obj['git_revision'] else build_container_tag ctx.obj['env'] = ctx.default_map.get('env', {}) ctx.obj['containers'] = ctx.default_map.get('containers') ctx.obj['volumes'] = ctx.default_map.get('volumes') ctx.obj['workdir'] = ctx.default_map.get('workdir') ctx.obj['container_context'] = ctx.default_map.get('container_context') utils.login_remote_registry(registry, ctx.obj)
def push(ctx, namespace, force, pbr, image): """ Push a container """ utils.logger.debug("Executing push command") _validate_global_params(ctx, 'registry') tag = git.get_hash() tag_to_push = tag if pbr: # Format = pbr_version.short_hash tag_to_push = "{}.{}".format(packaging._get_version_from_git().replace('dev', ''), tag[:8]) image_name = image + ':' + tag ret = _push(ctx, force, image, image_name, namespace, tag_to_push) if ret != 0: return ret return ret
def push(ctx, image): ''' Push a container ''' utils.logger.debug("Executing push command") _validate_global_params(ctx, 'registry') tag = git.get_hash() image_name = image + ':' + tag fqdn_image = utils.generate_fqdn_image(ctx.obj['registry'], image, tag) utils.logger.debug("Adding tag %(tag)s", dict(tag=fqdn_image)) command = ['docker', 'tag', image_name, fqdn_image] runner.run(command) utils.logger.debug("Pushing to registry %(registry)s", dict(registry=ctx.obj['registry'])) command = ['docker', 'push', fqdn_image] runner.run(command) utils.logger.debug("Removing tag %(tag)s", dict(tag=fqdn_image)) command = ['docker', 'rmi', fqdn_image] runner.run(command)
def test_get_short_hash(self, check_output_mock): git_hash = git.get_hash(short=True) check_output_mock.assert_called_once_with( ['git', 'rev-parse', '--short', 'HEAD']) self.assertEqual(git_hash, GIT_HASH_SHORT)
def test_get_full_hash(self, check_output_mock): git_hash = git.get_hash(short=False) check_output_mock.assert_called_once_with(['git', 'rev-parse', 'HEAD']) self.assertEqual(git_hash, GIT_HASH_FULL)
def test_get_hash_with_default_argument(self, check_output_mock): git_hash = git.get_hash() check_output_mock.assert_called_once_with(['git', 'rev-parse', 'HEAD']) self.assertEqual(git_hash, GIT_HASH_FULL)
def test_not_in_git_project(self, exists_mock, check_output_mock): git_hash = git.get_hash() exists_mock.assert_called_once_with('.git') check_output_mock.assert_not_called() self.assertEqual(git_hash, 'none')