rake_docker 2.18.0.pre.3 → 2.18.0.pre.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'json'
2
4
  require 'colored2'
3
5
 
@@ -5,45 +7,100 @@ module RakeDocker
5
7
  module Output
6
8
  def self.parse(chunk)
7
9
  chunk.each_line do |line|
8
- yield self.parse_line(line)
10
+ yield parse_line(line)
9
11
  end
10
12
  end
11
13
 
12
14
  def self.parse_line(line)
13
- begin
14
- json = JSON.parse(line.strip)
15
- rescue JSON::ParserError
16
- return line
17
- end
18
-
19
- # Skip progress and aux as they are covered by other status messages
20
- return '' if json['progress'] && json['status']
21
- return '' if json['aux']
15
+ json, error = try_parse_json(line)
16
+ return to_unparseable(line) if error
22
17
 
23
- # Return error flag as a second result
24
- return [json['error'], true] if json['error']
18
+ return to_skipped(json) if line_skippable?(json)
19
+ return to_error(json) if line_includes_error?(json)
20
+ return to_stream(json) if line_includes_stream?(json)
21
+ return to_status_with_id(json) if line_includes_status_and_id?(json)
22
+ return to_status(json) if line_includes_status?(json)
25
23
 
26
- return json['stream'] if json['stream']
27
-
28
- if json['status']
29
- if json['id']
30
- return json['id'] + ': ' + json['status'] + "\n"
31
- else
32
- return json['status'] + "\n"
33
- end
34
- end
24
+ to_fallthrough(line)
25
+ end
35
26
 
36
- return line
27
+ def self.try_parse_json(line)
28
+ [JSON.parse(line.strip), false]
29
+ rescue JSON::ParserError
30
+ [nil, true]
37
31
  end
38
32
 
39
33
  def self.print(chunk)
40
- self.parse(chunk) do |text, is_error|
41
- if is_error
42
- $stdout.print text.red + "\n"
34
+ parse(chunk) do |text, error|
35
+ if error
36
+ $stdout.print "#{text.red}\n"
43
37
  raise text
44
38
  end
45
39
  $stdout.print text unless text.empty?
46
40
  end
47
41
  end
42
+
43
+ def self.line_includes_id?(json)
44
+ json['id']
45
+ end
46
+
47
+ def self.line_includes_status?(json)
48
+ json['status']
49
+ end
50
+
51
+ def self.line_includes_progress?(json)
52
+ json['progress']
53
+ end
54
+
55
+ def self.line_includes_aux?(json)
56
+ json['aux']
57
+ end
58
+
59
+ def self.line_includes_error?(json)
60
+ json['error']
61
+ end
62
+
63
+ def self.line_includes_stream?(json)
64
+ json['stream']
65
+ end
66
+
67
+ def self.line_includes_status_and_id?(json)
68
+ line_includes_status?(json) &&
69
+ line_includes_id?(json)
70
+ end
71
+
72
+ def self.line_skippable?(json)
73
+ (line_includes_status?(json) &&
74
+ line_includes_progress?(json)) ||
75
+ line_includes_aux?(json)
76
+ end
77
+
78
+ def self.to_unparseable(line)
79
+ [line, false]
80
+ end
81
+
82
+ def self.to_skipped(_)
83
+ ['', false]
84
+ end
85
+
86
+ def self.to_status_with_id(json)
87
+ ["#{json['id']}: #{json['status']}\n", false]
88
+ end
89
+
90
+ def self.to_status(json)
91
+ ["#{json['status']}\n", false]
92
+ end
93
+
94
+ def self.to_stream(json)
95
+ [json['stream'], false]
96
+ end
97
+
98
+ def self.to_error(json)
99
+ [json['error'], true]
100
+ end
101
+
102
+ def self.to_fallthrough(line)
103
+ [line, false]
104
+ end
48
105
  end
49
106
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rake_factory'
2
4
  require 'docker'
3
5
 
@@ -17,7 +19,7 @@ module RakeDocker
17
19
  parameter :ready_check
18
20
 
19
21
  parameter :reporter,
20
- default: RakeDocker::Container::PrintingReporter.new
22
+ default: RakeDocker::Container::PrintingReporter.new
21
23
 
22
24
  parameter :provision_task_name, default: :provision
23
25
  parameter :destroy_task_name, default: :destroy
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rake_factory'
2
4
  require 'docker'
3
5
 
@@ -1,2 +1,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'task_sets/image'
2
4
  require_relative 'task_sets/container'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rake_factory'
2
4
  require 'docker'
3
5
 
@@ -7,39 +9,44 @@ module RakeDocker
7
9
  module Tasks
8
10
  class Build < RakeFactory::Task
9
11
  default_name :build
10
- default_description RakeFactory::DynamicValue.new { |t|
12
+ default_description(RakeFactory::DynamicValue.new do |t|
11
13
  "Build #{t.image_name} image"
12
- }
13
- default_prerequisites RakeFactory::DynamicValue.new { |t|
14
- t.prepare_task_name ?
15
- [Rake.application.current_scope
16
- .path_with_task_name(t.prepare_task_name)] :
17
- []
18
- }
14
+ end)
15
+ default_prerequisites(RakeFactory::DynamicValue.new do |t|
16
+ if t.prepare_task_name
17
+ [Rake.application.current_scope
18
+ .path_with_task_name(t.prepare_task_name)]
19
+ else
20
+ []
21
+ end
22
+ end)
19
23
 
20
- parameter :image_name, :required => true
21
- parameter :repository_name, :required => true
24
+ parameter :image_name, required: true
25
+ parameter :repository_name, required: true
22
26
 
23
27
  parameter :credentials
24
28
 
25
29
  parameter :build_args
26
30
  parameter :platform
27
31
 
28
- parameter :work_directory, :required => true
32
+ parameter :work_directory, required: true
29
33
 
30
34
  parameter :prepare_task_name, default: :prepare
31
35
 
32
36
  action do |t|
33
37
  Docker.authenticate!(t.credentials) if t.credentials
34
38
 
35
- options = {t: t.repository_name}
36
- options = options.merge(
37
- buildargs: JSON.generate(t.build_args)) if t.build_args
39
+ options = { t: t.repository_name }
40
+ if t.build_args
41
+ options = options.merge(
42
+ buildargs: JSON.generate(t.build_args)
43
+ )
44
+ end
38
45
  options = options.merge(platform: t.platform) if t.platform
39
46
 
40
47
  Docker::Image.build_from_dir(
41
- File.join(t.work_directory, t.image_name),
42
- options
48
+ File.join(t.work_directory, t.image_name),
49
+ options
43
50
  ) do |chunk|
44
51
  Output.print chunk
45
52
  end
@@ -1,15 +1,17 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rake_factory'
2
4
 
3
5
  module RakeDocker
4
6
  module Tasks
5
7
  class Clean < RakeFactory::Task
6
8
  default_name :clean
7
- default_description RakeFactory::DynamicValue.new { |t|
9
+ default_description(RakeFactory::DynamicValue.new do |t|
8
10
  "Clean #{t.image_name} image directory"
9
- }
11
+ end)
10
12
 
11
- parameter :image_name, :required => true
12
- parameter :work_directory, :required => true
13
+ parameter :image_name, required: true
14
+ parameter :work_directory, required: true
13
15
 
14
16
  action do |t|
15
17
  rm_rf File.join(t.work_directory, t.image_name)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rake_factory'
2
4
 
3
5
  require_relative '../container'
@@ -6,19 +8,20 @@ module RakeDocker
6
8
  module Tasks
7
9
  class Destroy < RakeFactory::Task
8
10
  default_name :destroy
9
- default_description RakeFactory::DynamicValue.new { |t|
10
- "Destroy #{t.container_name ? "#{t.container_name} " : ""}container."
11
- }
11
+ default_description(RakeFactory::DynamicValue.new do |t|
12
+ "Destroy #{t.container_name ? "#{t.container_name} " : ''}container."
13
+ end)
12
14
 
13
- parameter :container_name, :required => true
15
+ parameter :container_name, required: true
14
16
 
15
17
  parameter :reporter, default: Container::PrintingReporter.new
16
18
 
17
19
  action do |t|
18
20
  puts "Destroying #{t.container_name} container"
19
21
  destroyer = Container::Destroyer.new(
20
- t.container_name,
21
- reporter: t.reporter)
22
+ t.container_name,
23
+ reporter: t.reporter
24
+ )
22
25
  destroyer.execute
23
26
  end
24
27
  end
@@ -1,29 +1,29 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rake_factory'
2
4
 
3
5
  module RakeDocker
4
6
  module Tasks
5
7
  class Prepare < RakeFactory::Task
6
8
  default_name :prepare
7
- default_description RakeFactory::DynamicValue.new { |t|
9
+ default_description(RakeFactory::DynamicValue.new do |t|
8
10
  "Prepare for build of #{t.image_name} image"
9
- }
11
+ end)
10
12
 
11
- parameter :image_name, :required => true
13
+ parameter :image_name, required: true
12
14
 
13
- parameter :work_directory, :required => true
15
+ parameter :work_directory, required: true
14
16
 
15
- parameter :copy_spec, :default => []
16
- parameter :create_spec, :default => []
17
+ parameter :copy_spec, default: []
18
+ parameter :create_spec, default: []
17
19
 
18
20
  action do |t|
19
21
  image_directory = File.join(t.work_directory, t.image_name)
20
22
  FileUtils.mkdir_p image_directory
21
23
 
22
24
  t.copy_spec.each do |entry|
23
- from = entry.is_a?(Hash) ? entry[:from] : entry
24
- to = entry.is_a?(Hash) ?
25
- File.join(image_directory, entry[:to]) :
26
- image_directory
25
+ from = resolve_from(entry)
26
+ to = resolve_to(entry, image_directory)
27
27
 
28
28
  if File.directory?(from)
29
29
  FileUtils.mkdir_p(to)
@@ -45,6 +45,18 @@ module RakeDocker
45
45
  end
46
46
  end
47
47
  end
48
+
49
+ def resolve_from(entry)
50
+ entry.is_a?(Hash) ? entry[:from] : entry
51
+ end
52
+
53
+ def resolve_to(entry, image_directory)
54
+ if entry.is_a?(Hash)
55
+ File.join(image_directory, entry[:to])
56
+ else
57
+ image_directory
58
+ end
59
+ end
48
60
  end
49
61
  end
50
62
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rake_factory'
2
4
 
3
5
  require_relative '../container'
@@ -6,12 +8,12 @@ module RakeDocker
6
8
  module Tasks
7
9
  class Provision < RakeFactory::Task
8
10
  default_name :provision
9
- default_description RakeFactory::DynamicValue.new { |t|
10
- "Provision #{t.container_name ? "#{t.container_name} " : ""}container."
11
- }
11
+ default_description(RakeFactory::DynamicValue.new do |t|
12
+ "Provision #{t.container_name ? "#{t.container_name} " : ''}container."
13
+ end)
12
14
 
13
- parameter :container_name, :required => true
14
- parameter :image, :required => true
15
+ parameter :container_name, required: true
16
+ parameter :image, required: true
15
17
  parameter :ports
16
18
  parameter :environment
17
19
 
@@ -22,12 +24,13 @@ module RakeDocker
22
24
  action do |t|
23
25
  puts "Provisioning #{t.container_name} container"
24
26
  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)
27
+ t.container_name,
28
+ t.image,
29
+ ports: t.ports,
30
+ environment: t.environment,
31
+ ready?: t.ready_check,
32
+ reporter: t.reporter
33
+ )
31
34
  provisioner.execute
32
35
  end
33
36
  end
@@ -1,12 +1,14 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rake_factory'
2
4
 
3
5
  module RakeDocker
4
6
  module Tasks
5
7
  class Publish < RakeFactory::Task
6
8
  default_name :publish
7
- default_description RakeFactory::DynamicValue.new { |t|
9
+ default_description(RakeFactory::DynamicValue.new do |t|
8
10
  "Publish #{t.image_name} image"
9
- }
11
+ end)
10
12
 
11
13
  parameter :image_name
12
14
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rake_factory'
2
4
  require 'docker'
3
5
 
@@ -5,15 +7,15 @@ module RakeDocker
5
7
  module Tasks
6
8
  class Push < RakeFactory::Task
7
9
  default_name :push
8
- default_description RakeFactory::DynamicValue.new { |t|
10
+ default_description(RakeFactory::DynamicValue.new do |t|
9
11
  "Push #{t.image_name} image to repository"
10
- }
12
+ end)
11
13
 
12
- parameter :image_name, :required => true
13
- parameter :repository_url, :required => true
14
+ parameter :image_name, required: true
15
+ parameter :repository_url, required: true
14
16
 
15
17
  parameter :credentials
16
- parameter :tags, :required => true
18
+ parameter :tags, required: true
17
19
 
18
20
  action do |t|
19
21
  Docker.authenticate!(t.credentials) if t.credentials
@@ -21,7 +23,7 @@ module RakeDocker
21
23
  images = Docker::Image.all(filter: t.repository_url)
22
24
  if images.empty?
23
25
  raise ImageNotFound,
24
- "No image found for repository: '#{t.repository_url}'"
26
+ "No image found for repository: '#{t.repository_url}'"
25
27
  end
26
28
 
27
29
  image = images.first
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rake_factory'
2
4
  require 'docker'
3
5
 
@@ -5,29 +7,29 @@ module RakeDocker
5
7
  module Tasks
6
8
  class Tag < RakeFactory::Task
7
9
  default_name :tag
8
- default_description RakeFactory::DynamicValue.new { |t|
10
+ default_description(RakeFactory::DynamicValue.new do |t|
9
11
  "Tag #{t.image_name} image for repository"
10
- }
12
+ end)
11
13
 
12
- parameter :image_name, :required => true
13
- parameter :repository_name, :required => true
14
- parameter :repository_url, :required => true
14
+ parameter :image_name, required: true
15
+ parameter :repository_name, required: true
16
+ parameter :repository_url, required: true
15
17
 
16
- parameter :tags, :required => true
18
+ parameter :tags, required: true
17
19
 
18
20
  action do |t|
19
21
  images = Docker::Image.all(filter: t.repository_name)
20
22
  if images.empty?
21
23
  raise ImageNotFound,
22
- "No image found with name: '#{t.image_name}'"
24
+ "No image found with name: '#{t.image_name}'"
23
25
  end
24
26
 
25
27
  image = images.first
26
28
 
27
29
  t.tags.each do |tag|
28
30
  image.tag(repo: t.repository_url,
29
- tag: tag,
30
- force: true)
31
+ tag: tag,
32
+ force: true)
31
33
  end
32
34
  end
33
35
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'tasks/clean'
2
4
  require_relative 'tasks/prepare'
3
5
  require_relative 'tasks/build'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RakeDocker
2
- VERSION = '2.18.0.pre.3'
4
+ VERSION = '2.18.0.pre.4'
3
5
  end
data/lib/rake_docker.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rake_docker/version'
2
4
  require 'rake_docker/output'
3
5
  require 'rake_docker/container'
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'rake_docker/version'
6
+
7
+ files = %w[
8
+ bin
9
+ lib
10
+ CODE_OF_CONDUCT.md
11
+ rake_docker.gemspec
12
+ Gemfile
13
+ LICENSE.txt
14
+ Rakefile
15
+ README.md
16
+ ]
17
+
18
+ Gem::Specification.new do |spec|
19
+ spec.name = 'rake_docker'
20
+ spec.version = RakeDocker::VERSION
21
+ spec.authors = ['InfraBlocks Maintainers']
22
+ spec.email = ['maintainers@infrablocks.io']
23
+
24
+ spec.summary = 'Rake tasks for managing images and containers.'
25
+ spec.description = 'Allows building, tagging and pushing images and '\
26
+ 'creating, starting, stopping and removing containers '\
27
+ 'from within rake tasks.'
28
+ spec.homepage = 'https://github.com/infrablocks/rake_docker'
29
+ spec.license = 'MIT'
30
+
31
+ spec.files = `git ls-files -z`.split("\x0").select do |f|
32
+ f.match(/^(#{files.map { |g| Regexp.escape(g) }.join('|')})/)
33
+ end
34
+ spec.bindir = 'exe'
35
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
36
+ spec.require_paths = ['lib']
37
+
38
+ spec.required_ruby_version = '>= 2.7'
39
+
40
+ spec.add_dependency 'aws-sdk-ecr', '~> 1.2'
41
+ spec.add_dependency 'colored2', '~> 3.1'
42
+ spec.add_dependency 'docker-api', '>= 1.34', '< 3.0'
43
+ spec.add_dependency 'rake_factory', '~> 0.23'
44
+
45
+ spec.add_development_dependency 'activesupport'
46
+ spec.add_development_dependency 'bundler'
47
+ spec.add_development_dependency 'gem-release'
48
+ spec.add_development_dependency 'memfs'
49
+ spec.add_development_dependency 'rake'
50
+ spec.add_development_dependency 'rake_circle_ci'
51
+ spec.add_development_dependency 'rake_github'
52
+ spec.add_development_dependency 'rake_gpg'
53
+ spec.add_development_dependency 'rake_ssh'
54
+ spec.add_development_dependency 'rspec'
55
+ spec.add_development_dependency 'rubocop'
56
+ spec.add_development_dependency 'rubocop-rake'
57
+ spec.add_development_dependency 'rubocop-rspec'
58
+ spec.add_development_dependency 'simplecov'
59
+
60
+ spec.metadata['rubygems_mfa_required'] = 'false'
61
+ end