Sha256: 1ca5963e779f949548c32da66415c9543240124134df431754dd4bc3884fc020

Contents?: true

Size: 1.38 KB

Versions: 6

Compression:

Stored size: 1.38 KB

Contents

module Kontena::Cli::Apps
  module DockerHelper

    def process_docker_images(services, force_build = false)
      services.each do |name, service|
        if service['build'] && (!image_exist?(service['image']) || force_build)
          abort("'#{service['image']}' is not valid Docker image name") unless validate_image_name(service['image'])
          abort("'#{service['build']}' does not have Dockerfile") unless dockerfile_exist?(service['build'])
          puts "Building image #{service['image'].colorize(:cyan)}"
          build_docker_image(service['image'], service['build'])

          puts "Pushing image #{service['image'].colorize(:cyan)} to registry"
          push_docker_image(service['image'])
        end
      end
    end

    def validate_image_name(name)
      !(/^[\w.\/\-]+:?+[\w+.]+$/ =~ name).nil?
    end


    def build_docker_image(name, path)
      ret = system("docker build -t #{name} #{path}")
      abort("Failed to build image #{name.colorize(:cyan)}") unless ret
      ret
    end

    def push_docker_image(image)
      ret = system("docker push #{image}")
      abort("Failed to push image #{image.colorize(:cyan)}") unless ret
      ret
    end

    def image_exist?(image)
      `docker history #{image} 2>&1` ; $?.success?
    end

    def dockerfile_exist?(path)
      file = File.join(File.expand_path(path), 'Dockerfile')
      File.exist?(file)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
kontena-cli-0.10.3 lib/kontena/cli/apps/docker_helper.rb
kontena-cli-0.10.2 lib/kontena/cli/apps/docker_helper.rb
kontena-cli-0.10.1 lib/kontena/cli/apps/docker_helper.rb
kontena-cli-0.10.0 lib/kontena/cli/apps/docker_helper.rb
kontena-cli-0.9.3 lib/kontena/cli/apps/docker_helper.rb
kontena-cli-0.9.2 lib/kontena/cli/apps/docker_helper.rb