Class: Vtasks::Docker

Inherits:
Rake::TaskLib
  • Object
show all
Includes:
Utils::Output, Utils::System
Defined in:
lib/vtasks/docker.rb,
lib/vtasks/docker/image.rb,
lib/vtasks/docker/image/tag.rb,
lib/vtasks/docker/image/push.rb,
lib/vtasks/docker/image/build.rb

Overview

Docker tasks

Defined Under Namespace

Classes: Image

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::System

#command?

Methods included from Utils::Output

#debug, #error, #info, #warn

Constructor Details

#initialize(args = {}) ⇒ Docker

Returns a new instance of Docker.



17
18
19
20
21
22
23
# File 'lib/vtasks/docker.rb', line 17

def initialize(args = {})
  @args ||= args
  @repo ||= args.fetch(:repo)

  check_docker
  define_tasks
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



15
16
17
# File 'lib/vtasks/docker.rb', line 15

def args
  @args
end

#repoObject (readonly)

Returns the value of attribute repo.



15
16
17
# File 'lib/vtasks/docker.rb', line 15

def repo
  @repo
end

Instance Method Details

#add_namespace(image, path) ⇒ Object

Image namespace



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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_dockerObject

Check Docker is installed



92
93
94
95
96
# 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_tasksObject



25
26
27
28
29
30
31
32
33
34
35
36
# 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

#dockerfilesObject

List all folders containing Dockerfiles



85
86
87
88
89
# 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_collectObject

Garbage collect



117
118
119
120
121
122
# 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) ⇒ Object

Lint image



107
108
109
110
111
112
113
114
# 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_imagesObject

List all images



99
100
101
102
103
104
# 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) ⇒ Object

Run a task for all images



71
72
73
74
75
# 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) ⇒ Object

Run a task for all images in parallel



78
79
80
81
82
# 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

#tasksObject

Tasks



63
64
65
66
67
68
# 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