rake_docker 1.3.0 → 1.4.0.pre.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eb1abf1d8f06ff0a3db7904ba0f40e2eab9b3ede5c80a9f1e423ed2a94ce3566
4
- data.tar.gz: ebf39a86b4d844b08ba767b2724e63e7a33b3db0647fab3b2f166b3f9a58312f
3
+ metadata.gz: 650cab8353ed0ae26a7d47c29f2c55bd0ba38cb2c92812dcedaeb7df80e20c7d
4
+ data.tar.gz: ebc16ac740653db24905351ef8d159d4eed114ad962bbeb55eacecac345cfe94
5
5
  SHA512:
6
- metadata.gz: 0d67f2476415a729b79584fc0d5650065412c12b5686ee027f414f33862e70f84ad2c2b5e8c3c5dadc9798d703a8d8ca44e30c54714f81ad97bf085fca6d89e2
7
- data.tar.gz: dc371795298c6ae124665ddc2a6d6e99deceeba8edee012ae72a88c8e6e3a03e26daf77d93ebf3c54aa625bc6110891ce4f463fde0e49d1e15befc4df15172a8
6
+ metadata.gz: b3d7ee9204d7f5e93e5dd1f66f6524214cadebea69db46ce6ad067e5bc4158d584fcda5529712090cc4a8b3698e9c269b094e9748f3c7c04bd37a09dcaafcb40
7
+ data.tar.gz: 5f56e5d0166c7c679e1f14771e52c9ea3c4a93fa01eb06d8b5b637c9d909ec5c7e8ae301e993b92b58190bf36cd1502a296392988b0ea0d10fe916a0355e2da4
data/Gemfile.lock CHANGED
@@ -1,11 +1,11 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rake_docker (1.3.0)
4
+ rake_docker (1.4.0.pre.1)
5
5
  aws-sdk-ecr (~> 1.2)
6
6
  colored2 (~> 3.1)
7
7
  docker-api (~> 1.34)
8
- rake_factory (~> 0.17)
8
+ rake_factory (~> 0.23)
9
9
 
10
10
  GEM
11
11
  remote: https://rubygems.org/
@@ -16,13 +16,13 @@ GEM
16
16
  minitest (~> 5.1)
17
17
  tzinfo (~> 1.1)
18
18
  aws-eventstream (1.0.3)
19
- aws-partitions (1.268.0)
20
- aws-sdk-core (3.89.1)
19
+ aws-partitions (1.272.0)
20
+ aws-sdk-core (3.90.0)
21
21
  aws-eventstream (~> 1.0, >= 1.0.2)
22
22
  aws-partitions (~> 1, >= 1.239.0)
23
23
  aws-sigv4 (~> 1.1)
24
24
  jmespath (~> 1.0)
25
- aws-sdk-ecr (1.24.0)
25
+ aws-sdk-ecr (1.25.0)
26
26
  aws-sdk-core (~> 3, >= 3.71.0)
27
27
  aws-sigv4 (~> 1.1)
28
28
  aws-sigv4 (1.1.0)
@@ -34,7 +34,7 @@ GEM
34
34
  docker-api (1.34.2)
35
35
  excon (>= 0.47.0)
36
36
  multi_json
37
- excon (0.71.1)
37
+ excon (0.72.0)
38
38
  gem-release (2.1.1)
39
39
  i18n (1.8.2)
40
40
  concurrent-ruby (~> 1.0)
@@ -44,8 +44,8 @@ GEM
44
44
  minitest (5.14.0)
45
45
  multi_json (1.14.1)
46
46
  rake (13.0.1)
47
- rake_factory (0.17.0)
48
- activesupport (~> 5.2)
47
+ rake_factory (0.23.0)
48
+ activesupport (>= 4)
49
49
  rake (~> 13.0)
50
50
  rspec (3.9.0)
51
51
  rspec-core (~> 3.9.0)
@@ -0,0 +1,182 @@
1
+ module RakeDocker
2
+ module Container
3
+ REPORTER_MESSAGES = [
4
+ :checking_if_container_exists,
5
+ :container_exists,
6
+ :container_does_not_exist,
7
+ :checking_if_image_available,
8
+ :image_available,
9
+ :image_not_available,
10
+ :pulling_image,
11
+ :image_pulled,
12
+ :creating_container,
13
+ :container_created,
14
+ :checking_if_container_running,
15
+ :container_running,
16
+ :container_not_running,
17
+ :starting_container,
18
+ :container_started,
19
+ :waiting_for_container_to_be_ready,
20
+ :container_ready,
21
+ :stopping_container,
22
+ :container_stopped,
23
+ :deleting_container,
24
+ :container_deleted,
25
+ :done
26
+ ]
27
+
28
+ class NullReporter
29
+ REPORTER_MESSAGES.each { |message| define_method(message) {} }
30
+ end
31
+
32
+ module Utilities
33
+ def find_container(name)
34
+ begin
35
+ Docker::Container.get(name)
36
+ rescue Docker::Error::NotFoundError
37
+ nil
38
+ end
39
+ end
40
+ end
41
+
42
+ class Provisioner
43
+ include Utilities
44
+
45
+ attr_reader :name, :image, :ports, :environment, :ready, :reporter
46
+
47
+ def initialize(name, image, opts = {})
48
+ @name = name
49
+ @image = image
50
+ @environment = opts[:environment] || {}
51
+ @ports = opts[:ports] || []
52
+ @ready = opts[:ready?]
53
+ @reporter = opts[:reporter] || NullReporter.new
54
+ end
55
+
56
+ def execute
57
+ reporter.checking_if_container_exists(name)
58
+ container = find_container(name)
59
+ if container
60
+ reporter.container_exists(container)
61
+ ensure_container_running(container)
62
+ else
63
+ reporter.container_does_not_exist(name)
64
+ ensure_image_available(image)
65
+ create_and_start_container(name, image, ports, environment)
66
+ end
67
+ reporter.done
68
+ end
69
+
70
+ private
71
+
72
+ def ensure_image_available(image)
73
+ reporter.checking_if_image_available(image)
74
+ matching_images = Docker::Image.all(filter: image)
75
+ if matching_images.empty?
76
+ reporter.image_not_available(image)
77
+ pull_image(image)
78
+ else
79
+ reporter.image_available(image)
80
+ end
81
+ end
82
+
83
+ def pull_image(image)
84
+ reporter.pulling_image(image)
85
+ Docker::Image.create(fromImage: image)
86
+ reporter.image_pulled(image)
87
+ end
88
+
89
+ def ensure_container_running(container)
90
+ reporter.checking_if_container_running(container)
91
+ container = Docker::Container.get(name)
92
+ if !container_running?(container)
93
+ reporter.container_not_running(container)
94
+ start_container(container)
95
+ else
96
+ reporter.container_running(container)
97
+ end
98
+ end
99
+
100
+ def create_and_start_container(name, image, ports, environment)
101
+ start_container(create_container(image, name, ports, environment))
102
+ end
103
+
104
+ def create_container(image, name, ports, environment)
105
+ exposed_ports, port_bindings = process_ports(ports)
106
+ reporter.creating_container(name, image)
107
+ container = Docker::Container.create(
108
+ name: name,
109
+ Image: image,
110
+ ExposedPorts: exposed_ports,
111
+ HostConfig: {
112
+ PortBindings: port_bindings
113
+ },
114
+ Env: environment)
115
+ reporter.container_created(container)
116
+ container
117
+ end
118
+
119
+ def start_container(container)
120
+ reporter.starting_container(container)
121
+ container.start
122
+ reporter.container_started(container)
123
+ if ready && ready.respond_to?(:call)
124
+ reporter.waiting_for_container_to_be_ready(container)
125
+ ready.call(container)
126
+ reporter.container_ready(container)
127
+ end
128
+ container
129
+ end
130
+
131
+ def container_running?(container)
132
+ container_status(container) == 'running'
133
+ end
134
+
135
+ def container_status(container)
136
+ container.json['State']['Status']
137
+ end
138
+
139
+ def process_ports(ports)
140
+ port_config = ports.reduce({
141
+ exposed_ports: {},
142
+ port_bindings: {}
143
+ }) do |accumulator, port|
144
+ host_port, container_port = port.split(':')
145
+ accumulator[:exposed_ports]["#{container_port}/tcp"] = {}
146
+ accumulator[:port_bindings]["#{container_port}/tcp"] =
147
+ [{:'HostPort' => host_port}]
148
+ accumulator
149
+ end
150
+ [port_config[:exposed_ports], port_config[:port_bindings]]
151
+ end
152
+ end
153
+
154
+ class Destroyer
155
+ include Utilities
156
+
157
+ attr_reader :name, :reporter
158
+
159
+ def initialize(name, opts = {})
160
+ @name = name
161
+ @reporter = opts[:reporter] || NullReporter.new
162
+ end
163
+
164
+ def execute
165
+ reporter.checking_if_container_exists(name)
166
+ container = find_container(name)
167
+ if container
168
+ reporter.container_exists(container)
169
+ reporter.stopping_container(container)
170
+ container.stop
171
+ reporter.container_stopped(container)
172
+ reporter.deleting_container(container)
173
+ container.delete
174
+ reporter.container_deleted(container)
175
+ else
176
+ reporter.container_does_not_exist(name)
177
+ end
178
+ reporter.done
179
+ end
180
+ end
181
+ end
182
+ end
@@ -0,0 +1 @@
1
+ require 'rake_factory/kernel_extensions'
@@ -0,0 +1,31 @@
1
+ require 'rake_factory'
2
+ require 'docker'
3
+
4
+ require_relative '../tasks'
5
+
6
+ module RakeDocker
7
+ module TaskSets
8
+ class Container < RakeFactory::TaskSet
9
+ prepend RakeFactory::Namespaceable
10
+
11
+ parameter :container_name, required: true
12
+ parameter :image, required: true
13
+ parameter :ports
14
+ parameter :environment
15
+
16
+ parameter :ready_check
17
+
18
+ parameter :reporter
19
+
20
+ parameter :provision_task_name, default: :provision
21
+ parameter :destroy_task_name, default: :destroy
22
+
23
+ task Tasks::Provision, name: RakeFactory::DynamicValue.new { |ts|
24
+ ts.provision_task_name
25
+ }
26
+ task Tasks::Destroy, name: RakeFactory::DynamicValue.new { |ts|
27
+ ts.destroy_task_name
28
+ }
29
+ end
30
+ end
31
+ end
@@ -5,7 +5,7 @@ require_relative '../tasks'
5
5
 
6
6
  module RakeDocker
7
7
  module TaskSets
8
- class All < RakeFactory::TaskSet
8
+ class Image < RakeFactory::TaskSet
9
9
  prepend RakeFactory::Namespaceable
10
10
 
11
11
  parameter :image_name, required: true
@@ -32,12 +32,24 @@ module RakeDocker
32
32
  parameter :push_task_name, default: :push
33
33
  parameter :publish_task_name, default: :publish
34
34
 
35
- task Tasks::Clean, name: ->(ts) { ts.clean_task_name }
36
- task Tasks::Prepare, name: ->(ts) { ts.prepare_task_name }
37
- task Tasks::Build, name: ->(ts) { ts.build_task_name }
38
- task Tasks::Tag, name: ->(ts) { ts.tag_task_name }
39
- task Tasks::Push, name: ->(ts) { ts.push_task_name }
40
- task Tasks::Publish, name: ->(ts) { ts.publish_task_name }
35
+ task Tasks::Clean, name: RakeFactory::DynamicValue.new { |ts|
36
+ ts.clean_task_name
37
+ }
38
+ task Tasks::Prepare, name: RakeFactory::DynamicValue.new { |ts|
39
+ ts.prepare_task_name
40
+ }
41
+ task Tasks::Build, name: RakeFactory::DynamicValue.new { |ts|
42
+ ts.build_task_name
43
+ }
44
+ task Tasks::Tag, name: RakeFactory::DynamicValue.new { |ts|
45
+ ts.tag_task_name
46
+ }
47
+ task Tasks::Push, name: RakeFactory::DynamicValue.new { |ts|
48
+ ts.push_task_name
49
+ }
50
+ task Tasks::Publish, name: RakeFactory::DynamicValue.new { |ts|
51
+ ts.publish_task_name
52
+ }
41
53
  end
42
54
  end
43
55
  end
@@ -1 +1,2 @@
1
- require_relative 'task_sets/all'
1
+ require_relative 'task_sets/image'
2
+ require_relative 'task_sets/container'
@@ -7,8 +7,10 @@ module RakeDocker
7
7
  module Tasks
8
8
  class Build < RakeFactory::Task
9
9
  default_name :build
10
- default_description ->(t) { "Build #{t.image_name} image" }
11
- default_prerequisites ->(t) {
10
+ default_description RakeFactory::DynamicValue.new { |t|
11
+ "Build #{t.image_name} image"
12
+ }
13
+ default_prerequisites RakeFactory::DynamicValue.new { |t|
12
14
  t.prepare_task_name ?
13
15
  [Rake.application.current_scope
14
16
  .path_with_task_name(t.prepare_task_name)] :
@@ -4,7 +4,7 @@ module RakeDocker
4
4
  module Tasks
5
5
  class Clean < RakeFactory::Task
6
6
  default_name :clean
7
- default_description ->(t) {
7
+ default_description RakeFactory::DynamicValue.new { |t|
8
8
  "Clean #{t.image_name} image directory"
9
9
  }
10
10
 
@@ -0,0 +1,26 @@
1
+ require 'rake_factory'
2
+
3
+ require_relative '../container'
4
+
5
+ module RakeDocker
6
+ module Tasks
7
+ class Destroy < RakeFactory::Task
8
+ default_name :destroy
9
+ default_description RakeFactory::DynamicValue.new { |t|
10
+ "Destroy #{t.container_name ? "#{t.container_name} " : ""}container."
11
+ }
12
+
13
+ parameter :container_name, :required => true
14
+
15
+ parameter :reporter, default: Container::NullReporter.new
16
+
17
+ action do |t|
18
+ puts "Destroying #{t.container_name} container"
19
+ destroyer = Container::Destroyer.new(
20
+ t.container_name,
21
+ reporter: t.reporter)
22
+ destroyer.execute
23
+ end
24
+ end
25
+ end
26
+ end
@@ -4,7 +4,7 @@ module RakeDocker
4
4
  module Tasks
5
5
  class Prepare < RakeFactory::Task
6
6
  default_name :prepare
7
- default_description ->(t) {
7
+ default_description RakeFactory::DynamicValue.new { |t|
8
8
  "Prepare for build of #{t.image_name} image"
9
9
  }
10
10
 
@@ -0,0 +1,35 @@
1
+ require 'rake_factory'
2
+
3
+ require_relative '../container'
4
+
5
+ module RakeDocker
6
+ module Tasks
7
+ class Provision < RakeFactory::Task
8
+ default_name :provision
9
+ default_description RakeFactory::DynamicValue.new { |t|
10
+ "Provision #{t.container_name ? "#{t.container_name} " : ""}container."
11
+ }
12
+
13
+ parameter :container_name, :required => true
14
+ parameter :image, :required => true
15
+ parameter :ports
16
+ parameter :environment
17
+
18
+ parameter :ready_check
19
+
20
+ parameter :reporter, default: Container::NullReporter.new
21
+
22
+ action do |t|
23
+ puts "Provisioning #{t.container_name} container"
24
+ provisioner = Container::Provisioner.new(
25
+ t.container_name,
26
+ t.image,
27
+ ports: t.ports,
28
+ environment: t.environment,
29
+ ready?: t.ready_check,
30
+ reporter: t.reporter)
31
+ provisioner.execute
32
+ end
33
+ end
34
+ end
35
+ end
@@ -4,7 +4,9 @@ module RakeDocker
4
4
  module Tasks
5
5
  class Publish < RakeFactory::Task
6
6
  default_name :publish
7
- default_description ->(t) { "Publish #{t.image_name} image" }
7
+ default_description RakeFactory::DynamicValue.new { |t|
8
+ "Publish #{t.image_name} image"
9
+ }
8
10
 
9
11
  parameter :image_name
10
12
 
@@ -5,7 +5,7 @@ module RakeDocker
5
5
  module Tasks
6
6
  class Push < RakeFactory::Task
7
7
  default_name :push
8
- default_description ->(t) {
8
+ default_description RakeFactory::DynamicValue.new { |t|
9
9
  "Push #{t.image_name} image to repository"
10
10
  }
11
11
 
@@ -5,7 +5,7 @@ module RakeDocker
5
5
  module Tasks
6
6
  class Tag < RakeFactory::Task
7
7
  default_name :tag
8
- default_description ->(t) {
8
+ default_description RakeFactory::DynamicValue.new { |t|
9
9
  "Tag #{t.image_name} image for repository"
10
10
  }
11
11
 
@@ -4,3 +4,5 @@ require_relative 'tasks/build'
4
4
  require_relative 'tasks/tag'
5
5
  require_relative 'tasks/push'
6
6
  require_relative 'tasks/publish'
7
+ require_relative 'tasks/provision'
8
+ require_relative 'tasks/destroy'
@@ -1,3 +1,3 @@
1
1
  module RakeDocker
2
- VERSION = '1.3.0'
2
+ VERSION = '1.4.0.pre.1'
3
3
  end
data/lib/rake_docker.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'rake_docker/version'
2
2
  require 'rake_docker/output'
3
+ require 'rake_docker/container'
3
4
  require 'rake_docker/exceptions'
4
5
  require 'rake_docker/tasks'
5
6
  require 'rake_docker/task_sets'
@@ -7,6 +8,6 @@ require 'rake_docker/authentication'
7
8
 
8
9
  module RakeDocker
9
10
  def self.define_image_tasks(opts = {}, &block)
10
- RakeDocker::TaskSets::All.define(opts, &block)
11
+ RakeDocker::TaskSets::Image.define(opts, &block)
11
12
  end
12
13
  end
data/rake_docker.gemspec CHANGED
@@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
24
24
  spec.required_ruby_version = '>= 2.6'
25
25
 
26
26
  spec.add_dependency 'docker-api', '~> 1.34'
27
- spec.add_dependency 'rake_factory', '~> 0.17'
27
+ spec.add_dependency 'rake_factory', '~> 0.23'
28
28
  spec.add_dependency 'aws-sdk-ecr', '~> 1.2'
29
29
  spec.add_dependency 'colored2', '~> 3.1'
30
30
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rake_docker
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0.pre.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toby Clemson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-01-24 00:00:00.000000000 Z
11
+ date: 2020-02-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: docker-api
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0.17'
33
+ version: '0.23'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0.17'
40
+ version: '0.23'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: aws-sdk-ecr
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -202,14 +202,19 @@ files:
202
202
  - lib/rake_docker.rb
203
203
  - lib/rake_docker/authentication.rb
204
204
  - lib/rake_docker/authentication/ecr.rb
205
+ - lib/rake_docker/container.rb
205
206
  - lib/rake_docker/exceptions.rb
207
+ - lib/rake_docker/kernel_extensions.rb
206
208
  - lib/rake_docker/output.rb
207
209
  - lib/rake_docker/task_sets.rb
208
- - lib/rake_docker/task_sets/all.rb
210
+ - lib/rake_docker/task_sets/container.rb
211
+ - lib/rake_docker/task_sets/image.rb
209
212
  - lib/rake_docker/tasks.rb
210
213
  - lib/rake_docker/tasks/build.rb
211
214
  - lib/rake_docker/tasks/clean.rb
215
+ - lib/rake_docker/tasks/destroy.rb
212
216
  - lib/rake_docker/tasks/prepare.rb
217
+ - lib/rake_docker/tasks/provision.rb
213
218
  - lib/rake_docker/tasks/publish.rb
214
219
  - lib/rake_docker/tasks/push.rb
215
220
  - lib/rake_docker/tasks/tag.rb
@@ -237,9 +242,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
237
242
  version: '2.6'
238
243
  required_rubygems_version: !ruby/object:Gem::Requirement
239
244
  requirements:
240
- - - ">="
245
+ - - ">"
241
246
  - !ruby/object:Gem::Version
242
- version: '0'
247
+ version: 1.3.1
243
248
  requirements: []
244
249
  rubygems_version: 3.0.1
245
250
  signing_key: