class Vtasks::Docker

Docker tasks

Attributes

args[R]
repo[R]

Public Class Methods

new(args = {}) click to toggle source
# File lib/vtasks/docker.rb, line 17
def initialize(args = {})
  @args ||= args
  @repo ||= args.fetch(:repo)

  check_docker
  define_tasks
end

Public Instance Methods

add_namespace(image, path) click to toggle source

Image namespace

# File lib/vtasks/docker.rb, line 39
def add_namespace(image, path)
  namespace path.to_sym do |_args|
    require 'rspec/core/rake_task'
    ::RSpec::Core::RakeTask.new(:spec) do |task|
      task.pattern = "#{path}/spec/*_spec.rb"
    end

    docker_image = Vtasks::Docker::Image.new(image, path, args)

    lint_image(path)

    desc 'Build and tag docker image'
    task :build do
      docker_image.build_with_tags
    end

    desc 'Publish docker image'
    task :push do
      docker_image.push
    end
  end
end
check_docker() click to toggle source

Check Docker is installed

# File lib/vtasks/docker.rb, line 92
def check_docker
  task :docker do
    raise 'These tasks require docker to be installed' unless command? 'docker'
  end
end
define_tasks() click to toggle source
# File lib/vtasks/docker.rb, line 25
def define_tasks
  namespace :docker do
    list_images
    garbage_collect
    tasks

    dockerfiles.each do |dockerfile|
      path = File.basename(dockerfile)
      add_namespace("#{repo}/#{path}", path)
    end # dockerfiles.each
  end # namespace :docker
end
dockerfiles() click to toggle source

List all folders containing Dockerfiles

# File lib/vtasks/docker.rb, line 85
def dockerfiles
  @dockerfiles = Dir.glob('*').select do |dir|
    File.directory?(dir) && File.exist?("#{dir}/Dockerfile")
  end
end
garbage_collect() click to toggle source

Garbage collect

# File lib/vtasks/docker.rb, line 117
def garbage_collect
  desc 'Garbage collect unused docker data'
  task :gc do
    system 'docker system prune --all --force'
  end
end
lint_image(path) click to toggle source

Lint image

# File lib/vtasks/docker.rb, line 107
def lint_image(path)
  desc 'Run Hadolint against the Dockerfile'
  task :lint do
    dockerfile = "#{path}/Dockerfile"
    info "Running Hadolint to check the style of #{dockerfile}"
    system "docker container run --rm -i lukasmartinelli/hadolint hadolint --ignore DL3008 --ignore DL3013 - < #{dockerfile}"
  end
end
list_images() click to toggle source

List all images

# File lib/vtasks/docker.rb, line 99
def list_images
  desc 'List all Docker images'
  task :list do
    info dockerfiles.map { |image| File.basename(image) }
  end
end
run_task(name) click to toggle source

Run a task for all images

# File lib/vtasks/docker.rb, line 71
def run_task(name)
  desc "Run #{name} for all images in repository"
  task name => dockerfiles
    .collect { |image| "docker:#{File.basename(image)}:#{name}" }
end
run_task_parallel(name) click to toggle source

Run a task for all images in parallel

# File lib/vtasks/docker.rb, line 78
def run_task_parallel(name)
  desc "Run #{name} for all images in repository in parallel"
  multitask name => dockerfiles
    .collect { |image| "docker:#{File.basename(image)}:#{name}" }
end
tasks() click to toggle source

Tasks

# File lib/vtasks/docker.rb, line 63
def tasks
  # Run tasks one by one for all images
  [:spec, :lint].each { |task_name| run_task(task_name) }
  # Run tasks in parallel for all images
  [:build, :push].each { |task_name| run_task_parallel(task_name) }
end