dapp 0.6.1 → 0.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (127) hide show
  1. checksums.yaml +4 -4
  2. data/bin/dapp +3 -3
  3. data/config/en/common.yml +19 -4
  4. data/config/en/net_status.yml +13 -4
  5. data/lib/dapp.rb +48 -16
  6. data/lib/dapp/application.rb +75 -40
  7. data/lib/dapp/application/path.rb +3 -2
  8. data/lib/dapp/application/stages.rb +59 -0
  9. data/lib/dapp/artifact.rb +22 -0
  10. data/lib/dapp/build/stage/after_install_artifact.rb +13 -0
  11. data/lib/dapp/build/stage/after_setup_artifact.rb +17 -0
  12. data/lib/dapp/build/stage/artifact_base.rb +80 -0
  13. data/lib/dapp/build/stage/artifact_default.rb +62 -0
  14. data/lib/dapp/build/stage/base.rb +47 -12
  15. data/lib/dapp/build/stage/before_install.rb +6 -5
  16. data/lib/dapp/build/stage/before_install_artifact.rb +13 -0
  17. data/lib/dapp/build/stage/before_setup.rb +7 -8
  18. data/lib/dapp/build/stage/before_setup_artifact.rb +13 -0
  19. data/lib/dapp/build/stage/build_artifact.rb +32 -0
  20. data/lib/dapp/build/stage/docker_instructions.rb +4 -22
  21. data/lib/dapp/build/stage/from.rb +7 -8
  22. data/lib/dapp/build/stage/ga_archive.rb +1 -1
  23. data/lib/dapp/build/stage/ga_archive_dependencies.rb +1 -1
  24. data/lib/dapp/build/stage/ga_artifact_patch.rb +21 -0
  25. data/lib/dapp/build/stage/ga_base.rb +6 -29
  26. data/lib/dapp/build/stage/ga_dependencies_base.rb +4 -13
  27. data/lib/dapp/build/stage/ga_latest_patch.rb +2 -2
  28. data/lib/dapp/build/stage/import_artifact.rb +69 -0
  29. data/lib/dapp/build/stage/install/ga_post_install_patch.rb +1 -1
  30. data/lib/dapp/build/stage/install/ga_post_install_patch_dependencies.rb +1 -1
  31. data/lib/dapp/build/stage/install/ga_pre_install_patch_dependencies.rb +1 -7
  32. data/lib/dapp/build/stage/install/install.rb +12 -7
  33. data/lib/dapp/build/stage/mod/logging.rb +10 -7
  34. data/lib/dapp/build/stage/setup/chef_cookbooks.rb +3 -4
  35. data/lib/dapp/build/stage/setup/ga_post_setup_patch.rb +1 -1
  36. data/lib/dapp/build/stage/setup/ga_pre_setup_patch.rb +2 -2
  37. data/lib/dapp/build/stage/setup/ga_pre_setup_patch_dependencies.rb +2 -8
  38. data/lib/dapp/build/stage/setup/setup.rb +12 -7
  39. data/lib/dapp/builder/base.rb +13 -1
  40. data/lib/dapp/builder/chef.rb +125 -79
  41. data/lib/dapp/builder/chef/cookbook_metadata.rb +1 -1
  42. data/lib/dapp/builder/shell.rb +2 -3
  43. data/lib/dapp/cli.rb +3 -3
  44. data/lib/dapp/cli/base.rb +0 -2
  45. data/lib/dapp/cli/bp.rb +0 -4
  46. data/lib/dapp/cli/build.rb +3 -7
  47. data/lib/dapp/cli/cleanup.rb +0 -2
  48. data/lib/dapp/cli/list.rb +0 -2
  49. data/lib/dapp/cli/mrproper.rb +22 -0
  50. data/lib/dapp/cli/push.rb +4 -3
  51. data/lib/dapp/cli/run.rb +0 -2
  52. data/lib/dapp/cli/spush.rb +0 -2
  53. data/lib/dapp/cli/stage_image.rb +24 -0
  54. data/lib/dapp/cli/stages.rb +7 -5
  55. data/lib/dapp/cli/stages/cleanup_local.rb +28 -0
  56. data/lib/dapp/cli/stages/cleanup_repo.rb +24 -0
  57. data/lib/dapp/cli/stages/{flush.rb → flush_local.rb} +4 -6
  58. data/lib/dapp/cli/stages/{cleanup.rb → flush_repo.rb} +4 -6
  59. data/lib/dapp/cli/stages/pull.rb +28 -0
  60. data/lib/dapp/cli/stages/push.rb +24 -0
  61. data/lib/dapp/config/application.rb +185 -44
  62. data/lib/dapp/config/artifact.rb +10 -50
  63. data/lib/dapp/config/directive/artifact.rb +77 -0
  64. data/lib/dapp/config/directive/build_dir.rb +9 -0
  65. data/lib/dapp/config/directive/chef.rb +95 -0
  66. data/lib/dapp/config/directive/docker/artifact.rb +26 -0
  67. data/lib/dapp/config/directive/docker/base.rb +91 -0
  68. data/lib/dapp/config/directive/git_artifact.rb +59 -0
  69. data/lib/dapp/config/directive/shell/artifact.rb +38 -0
  70. data/lib/dapp/config/directive/shell/base.rb +85 -0
  71. data/lib/dapp/config/directive/tmp_dir.rb +36 -0
  72. data/lib/dapp/config/main.rb +1 -7
  73. data/lib/dapp/core_ext/hash.rb +21 -0
  74. data/lib/dapp/docker_registry/base.rb +60 -5
  75. data/lib/dapp/docker_registry/mod/request.rb +2 -14
  76. data/lib/dapp/git_artifact.rb +32 -23
  77. data/lib/dapp/git_repo/base.rb +1 -1
  78. data/lib/dapp/image/argument.rb +3 -3
  79. data/lib/dapp/image/docker.rb +13 -16
  80. data/lib/dapp/image/scratch.rb +29 -0
  81. data/lib/dapp/image/stage.rb +33 -23
  82. data/lib/dapp/lock/base.rb +18 -11
  83. data/lib/dapp/lock/file.rb +18 -16
  84. data/lib/dapp/prctl.rb +1 -0
  85. data/lib/dapp/project.rb +22 -9
  86. data/lib/dapp/project/command/bp.rb +2 -5
  87. data/lib/dapp/project/command/build.rb +1 -2
  88. data/lib/dapp/project/command/cleanup.rb +9 -7
  89. data/lib/dapp/project/command/common.rb +37 -12
  90. data/lib/dapp/project/command/mrproper.rb +57 -0
  91. data/lib/dapp/project/command/push.rb +8 -2
  92. data/lib/dapp/project/command/run.rb +1 -1
  93. data/lib/dapp/project/command/stage_image.rb +15 -0
  94. data/lib/dapp/project/command/stages/cleanup_local.rb +100 -0
  95. data/lib/dapp/project/command/stages/cleanup_repo.rb +65 -0
  96. data/lib/dapp/project/command/stages/common.rb +48 -0
  97. data/lib/dapp/project/command/stages/flush_local.rb +24 -0
  98. data/lib/dapp/project/command/stages/flush_repo.rb +22 -0
  99. data/lib/dapp/project/command/stages/pull.rb +26 -0
  100. data/lib/dapp/project/command/stages/push.rb +22 -0
  101. data/lib/dapp/project/dappfile.rb +3 -1
  102. data/lib/dapp/project/deps/base.rb +52 -0
  103. data/lib/dapp/project/deps/gitartifact.rb +36 -0
  104. data/lib/dapp/project/lock.rb +20 -13
  105. data/lib/dapp/project/logging/base.rb +20 -3
  106. data/lib/dapp/project/logging/i18n.rb +25 -0
  107. data/lib/dapp/project/logging/paint.rb +47 -0
  108. data/lib/dapp/project/logging/process.rb +17 -10
  109. data/lib/dapp/project/shellout/base.rb +74 -0
  110. data/lib/dapp/project/shellout/streaming.rb +49 -0
  111. data/lib/dapp/project/shellout/system.rb +72 -0
  112. data/lib/dapp/project/ssh_agent.rb +8 -9
  113. data/lib/dapp/version.rb +1 -1
  114. metadata +48 -17
  115. data/lib/dapp/build/stage/artifact.rb +0 -40
  116. data/lib/dapp/build/stage/mod/artifact.rb +0 -79
  117. data/lib/dapp/config/chef.rb +0 -51
  118. data/lib/dapp/config/docker.rb +0 -82
  119. data/lib/dapp/config/git_artifact.rb +0 -51
  120. data/lib/dapp/config/shell.rb +0 -64
  121. data/lib/dapp/helper/i18n.rb +0 -20
  122. data/lib/dapp/helper/paint.rb +0 -27
  123. data/lib/dapp/helper/shellout.rb +0 -63
  124. data/lib/dapp/helper/streaming.rb +0 -47
  125. data/lib/dapp/project/command/stages_cleanup.rb +0 -72
  126. data/lib/dapp/project/command/stages_flush.rb +0 -20
  127. data/lib/dapp/project/paint.rb +0 -16
@@ -0,0 +1,29 @@
1
+ module Dapp
2
+ # Image
3
+ module Image
4
+ # Stage
5
+ class Scratch < Stage
6
+ def initialize(**_kwargs)
7
+ super
8
+ @from_archives = []
9
+ end
10
+
11
+ def add_archive(*archives)
12
+ @from_archives.concat(archives.flatten)
13
+ end
14
+
15
+ def build!(**_kwargs)
16
+ build_from_command = if from_archives.empty?
17
+ "#{project.tar_path} c --files-from /dev/null"
18
+ else
19
+ "#{project.cat_path} #{from_archives.join(' ')}"
20
+ end
21
+ @built_id = project.system_shellout!("#{build_from_command} | docker import #{prepared_change} - ").stdout.strip
22
+ end
23
+
24
+ protected
25
+
26
+ attr_accessor :from_archives
27
+ end # Stage
28
+ end # Image
29
+ end # Dapp
@@ -6,59 +6,69 @@ module Dapp
6
6
  include Argument
7
7
 
8
8
  def initialize(name:, project:, built_id: nil, from: nil)
9
- @bash_commands = []
10
- @options = {}
11
- @change_options = {}
12
- @service_change_options = {}
13
9
  @container_name = "#{name[/[[^:].]*/]}.#{SecureRandom.hex(4)}"
14
10
  @built_id = built_id
15
- super(name: name, project: project, from: from)
16
- end
17
11
 
18
- def labels
19
- self.class.image_config_option(image_id: built_id, option: 'labels')
12
+ @bash_commands = []
13
+ @options = {}
14
+ @change_options = {}
15
+ @service_change_options = {}
16
+
17
+ super(name: name, project: project, from: from)
20
18
  end
21
19
 
22
20
  def built_id
23
21
  @built_id ||= id
24
22
  end
25
23
 
26
- def build!(**kwargs)
27
- run!(**kwargs)
24
+ def build!
25
+ run!
28
26
  @built_id = commit!
29
27
  ensure
30
- shellout("docker rm #{container_name}")
28
+ project.shellout("docker rm #{container_name}")
31
29
  end
32
30
 
33
- def export!(name, log_verbose: false, log_time: false)
31
+ def export!(name)
34
32
  image = self.class.new(name: name, project: project, built_id: built_id)
35
- image.tag!(log_verbose: log_verbose, log_time: log_time)
36
- image.push!(log_verbose: log_verbose, log_time: log_time)
33
+ image.tag!
34
+ image.push!
37
35
  image.untag!
38
36
  end
39
37
 
40
- def tag!(log_verbose: false, log_time: false)
38
+ def import!(name)
39
+ image = self.class.new(name: name, project: project)
40
+ image.pull!
41
+ @built_id = image.built_id
42
+ tag!
43
+ image.untag!
44
+ end
45
+
46
+ def tag!
41
47
  project.log_warning(desc: { code: :another_image_already_tagged, context: 'warning' }) if !(existed_id = id).nil? && built_id != existed_id
42
- shellout!("docker tag #{built_id} #{name}", log_verbose: log_verbose, log_time: log_time)
48
+ project.shellout!("docker tag #{built_id} #{name}")
43
49
  cache_reset
44
50
  end
45
51
 
52
+ def labels
53
+ raise Error::Build, code: :image_not_exist, data: { name: name } if built_id.nil?
54
+ self.class.image_config_option(image_id: built_id, option: 'labels')
55
+ end
56
+
46
57
  protected
47
58
 
48
59
  attr_reader :container_name
49
60
 
50
- def run!(log_verbose: false, log_time: false, introspect_error: false, introspect_before_error: false)
61
+ def run!
51
62
  raise Error::Build, code: :built_id_not_defined if from.built_id.nil?
52
- shellout!("docker run #{prepared_options} #{from.built_id} -ec '#{prepared_bash_command}'",
53
- log_verbose: log_verbose, log_time: log_time)
63
+ project.shellout!("docker run #{prepared_options} #{from.built_id} -ec '#{prepared_bash_command}'", log_verbose: true)
54
64
  rescue Error::Shellout => _e
55
- raise unless introspect_error || introspect_before_error
56
- built_id = introspect_error ? commit! : from.built_id
57
- raise Exception::IntrospectImage, data: { built_id: built_id, options: prepared_options, rmi: introspect_error }
65
+ raise unless project.introspect_error? || project.introspect_before_error?
66
+ built_id = project.introspect_error? ? commit! : from.built_id
67
+ raise Exception::IntrospectImage, data: { built_id: built_id, options: prepared_options, rmi: project.introspect_error? }
58
68
  end
59
69
 
60
70
  def commit!
61
- shellout!("docker commit #{prepared_change} #{container_name}").stdout.strip
71
+ project.shellout!("docker commit #{prepared_change} #{container_name}").stdout.strip
62
72
  end
63
73
  end # Stage
64
74
  end # Image
@@ -4,21 +4,20 @@ module Dapp
4
4
  # Base
5
5
  class Base
6
6
  attr_reader :name
7
- attr_reader :on_wait
8
- attr_reader :timeout
9
7
 
10
- def initialize(name, timeout: 60, on_wait: nil)
8
+ def initialize(name)
11
9
  @name = name
12
- @on_wait = on_wait
13
- @timeout = timeout
10
+ @active_locks = 0
14
11
  end
15
12
 
16
- def lock(shared: false)
17
- raise
13
+ def lock(timeout: 60, on_wait: nil, readonly: false)
14
+ _do_lock(timeout, on_wait, readonly) unless @active_locks > 0
15
+ @active_locks += 1
18
16
  end
19
17
 
20
18
  def unlock
21
- raise
19
+ @active_locks -= 1
20
+ _do_unlock if @active_locks.zero?
22
21
  end
23
22
 
24
23
  def synchronize(*args)
@@ -32,9 +31,17 @@ module Dapp
32
31
 
33
32
  protected
34
33
 
35
- def _waiting(&blk)
36
- if @on_wait
37
- @on_wait.call { ::Timeout.timeout(timeout, &blk) }
34
+ def _do_lock(_timeout, _on_wait, _readonly)
35
+ raise
36
+ end
37
+
38
+ def _do_unlock
39
+ raise
40
+ end
41
+
42
+ def _waiting(timeout, on_wait, &blk)
43
+ if on_wait
44
+ on_wait.call { ::Timeout.timeout(timeout, &blk) }
38
45
  else
39
46
  ::Timeout.timeout(timeout, &blk)
40
47
  end
@@ -3,20 +3,30 @@ module Dapp
3
3
  module Lock
4
4
  # File
5
5
  class File < Base
6
+ class << self
7
+ attr_writer :counter
8
+
9
+ def counter
10
+ @counter ||= 0
11
+ end
12
+ end # << self
13
+
6
14
  attr_reader :lock_path
7
15
 
8
- def initialize(lock_path, name, **kwargs)
9
- super(name, **kwargs)
16
+ def initialize(lock_path, name)
17
+ super(name)
18
+
10
19
  @lock_path = Pathname.new(lock_path).tap(&:mkpath)
11
20
  end
12
21
 
13
- def lock(shared: false)
14
- return if @file
15
- @file = ::File.open(lock_path.join(name), ::File::RDWR | ::File::CREAT, 0644)
22
+ protected
23
+
24
+ def _do_lock(timeout, on_wait, readonly)
25
+ @file = ::File.open(lock_path.join(name), ::File::RDWR | ::File::CREAT, 0o644)
16
26
 
17
27
  begin
18
- mode = (shared ? ::File::LOCK_SH : ::File::LOCK_EX)
19
- _waiting { @file.flock(mode) } unless @file.flock(mode | ::File::LOCK_NB)
28
+ mode = (readonly ? ::File::LOCK_SH : ::File::LOCK_EX)
29
+ _waiting(timeout, on_wait) { @file.flock(mode) } unless @file.flock(mode | ::File::LOCK_NB)
20
30
  rescue ::Timeout::Error
21
31
  raise Dapp::Lock::Error::Timeout, code: :timeout,
22
32
  data: { name: name, timeout: timeout }
@@ -25,19 +35,11 @@ module Dapp
25
35
  self.class.counter += 1
26
36
  end
27
37
 
28
- def unlock
38
+ def _do_unlock
29
39
  @file.close
30
40
  @file = nil
31
41
  self.class.counter -= 1
32
42
  end
33
-
34
- class << self
35
- attr_writer :counter
36
-
37
- def counter
38
- @counter ||= 0
39
- end
40
- end # << self
41
43
  end # File
42
44
  end # Lock
43
45
  end # Dapp
@@ -1,3 +1,4 @@
1
+ # Prctl
1
2
  module Prctl
2
3
  PR_SET_PDEATHSIG = 1
3
4
 
@@ -3,7 +3,7 @@ module Dapp
3
3
  class Project
4
4
  include Lock
5
5
  include Dappfile
6
- include Paint
6
+
7
7
  include Command::Common
8
8
  include Command::Run
9
9
  include Command::Build
@@ -11,17 +11,30 @@ module Dapp
11
11
  include Command::Push
12
12
  include Command::Spush
13
13
  include Command::List
14
- include Command::StagesFlush
15
- include Command::StagesCleanup
14
+ include Command::Stages::CleanupLocal
15
+ include Command::Stages::CleanupRepo
16
+ include Command::Stages::FlushLocal
17
+ include Command::Stages::FlushRepo
18
+ include Command::Stages::Push
19
+ include Command::Stages::Pull
20
+ include Command::Stages::Common
16
21
  include Command::Cleanup
22
+ include Command::Mrproper
23
+ include Command::StageImage
17
24
  include Logging::Base
18
25
  include Logging::Process
26
+ include Logging::I18n
27
+ include Logging::Paint
19
28
 
20
29
  include SshAgent
21
- include Helper::I18n
22
- include Helper::Shellout
23
- include Helper::Paint
24
30
  include Helper::Sha256
31
+ include Helper::Trivia
32
+
33
+ include Deps::Gitartifact
34
+ include Deps::Base
35
+
36
+ include Shellout::Base
37
+ include Shellout::System
25
38
 
26
39
  attr_reader :cli_options
27
40
  attr_reader :apps_patterns
@@ -31,13 +44,13 @@ module Dapp
31
44
  @apps_patterns = apps_patterns || []
32
45
  @apps_patterns << '*' unless @apps_patterns.any?
33
46
 
34
- paint_initialize
35
- Helper::I18n.initialize
47
+ Logging::Paint.initialize(cli_options[:log_color])
48
+ Logging::I18n.initialize
36
49
  end
37
50
 
38
51
  def name
39
52
  @name ||= begin
40
- shellout!("git -C #{path} config --get remote.origin.url").stdout.strip.split('/').last[/.*(?=.git)/]
53
+ system_shellout!("#{git_path} -C #{path} config --get remote.origin.url").stdout.strip.split('/').last[/.*(?=.git)/]
41
54
  rescue Error::Shellout
42
55
  File.basename(path)
43
56
  end
@@ -8,15 +8,12 @@ module Dapp
8
8
  def bp(repo)
9
9
  bp_step(:build)
10
10
  bp_step(:push, repo)
11
- bp_step(:stages_cleanup, repo)
11
+ bp_step(:stages_cleanup_local, repo)
12
12
  bp_step(:cleanup)
13
13
  end
14
14
 
15
15
  def bp_step(step, *args)
16
- log_step(step)
17
- with_log_indent do
18
- public_send(step, *args)
19
- end
16
+ log_step_with_indent(step) { public_send(step, *args) }
20
17
  end
21
18
  end
22
19
  end
@@ -9,8 +9,7 @@ module Dapp
9
9
  setup_ssh_agent
10
10
 
11
11
  build_configs.each do |config|
12
- log_step(config._name)
13
- with_log_indent do
12
+ log_step_with_indent(config._name) do
14
13
  Application.new(config: config, project: self).build!
15
14
  end
16
15
  end
@@ -8,13 +8,15 @@ module Dapp
8
8
  def cleanup
9
9
  build_configs.map(&:_basename).uniq.each do |basename|
10
10
  lock("#{basename}.images") do
11
- log(basename)
12
- containers_flush(basename)
13
- remove_images([
14
- 'docker images',
15
- %(--format '{{if ne "#{stage_cache(basename)}" .Repository }}{{.ID}}{{ end }}'),
16
- %(-f "label=dapp=#{stage_dapp_label(basename)}")
17
- ].join(' ')) # FIXME: negative filter is not currently supported by the Docker CLI
11
+ log_step_with_indent(basename) do
12
+ project_containers_flush(basename)
13
+ project_dangling_images_flush(basename)
14
+ remove_images_by_query([
15
+ 'docker images',
16
+ %(--format '{{if ne "#{stage_cache(basename)}" .Repository }}{{.ID}}{{ end }}'),
17
+ %(-f "label=dapp=#{stage_dapp_label(basename)}")
18
+ ].join(' ')) # FIXME: negative filter is not currently supported by the Docker CLI
19
+ end
18
20
  end
19
21
  end
20
22
  end
@@ -7,18 +7,38 @@ module Dapp
7
7
  module Common
8
8
  protected
9
9
 
10
- def containers_flush(basename)
11
- remove_containers(%(docker ps -a -f "label=dapp" -f "name=#{container_name(basename)}" -q), force: true)
10
+ def project_images(basename)
11
+ shellout!(%(docker images --format="{{.Repository}}:{{.Tag}}" #{stage_cache(basename)})).stdout.strip
12
12
  end
13
13
 
14
- def remove_images(images_query, force: false)
15
- force_option = force ? ' -f' : ''
16
- with_subquery(images_query) { |ids| run_command(%(docker rmi#{force_option} #{ids.join(' ')})) }
14
+ def project_containers_flush(basename)
15
+ remove_containers_by_query(%(docker ps -a -f "label=dapp" -f "name=#{container_name(basename)}" -q), force: true)
16
+ end
17
+
18
+ def project_dangling_images_flush(basename)
19
+ remove_images_by_query(%(docker images -f "dangling=true" -f "label=dapp=#{stage_dapp_label(basename)}" -q), force: true)
20
+ end
21
+
22
+ def remove_images_by_query(images_query, force: false)
23
+ with_subquery(images_query) { |ids| remove_images(ids, force: force) }
24
+ end
25
+
26
+ def remove_images(ids, force: false)
27
+ remove_base('docker rmi%{force_option} %{ids}', ids, force: force)
17
28
  end
18
29
 
19
- def remove_containers(containers_query, force: false)
30
+ def remove_containers_by_query(containers_query, force: false)
31
+ with_subquery(containers_query) { |ids| remove_containers(ids, force: force) }
32
+ end
33
+
34
+ def remove_containers(ids, force: false)
35
+ remove_base('docker rm%{force_option} %{ids}', ids, force: force)
36
+ end
37
+
38
+ def remove_base(query_format, ids, force: false)
39
+ return if ids.empty?
20
40
  force_option = force ? ' -f' : ''
21
- with_subquery(containers_query) { |ids| run_command(%(docker rm#{force_option} #{ids.join(' ')})) }
41
+ ids.each_slice(50) { |chunk| run_command(query_format % ({ force_option: force_option, ids: chunk.join(' ') })) }
22
42
  end
23
43
 
24
44
  def with_subquery(query)
@@ -27,11 +47,8 @@ module Dapp
27
47
  end
28
48
 
29
49
  def run_command(cmd)
30
- if @cli_options[:dry_run] # FIXME
31
- puts cmd
32
- else
33
- shellout!(cmd)
34
- end
50
+ log(cmd) if log_verbose? || dry_run?
51
+ shellout!(cmd) unless dry_run?
35
52
  end
36
53
 
37
54
  def stage_cache(basename)
@@ -45,6 +62,14 @@ module Dapp
45
62
  def container_name(basename)
46
63
  basename
47
64
  end
65
+
66
+ def proper_cache_version?
67
+ !!cli_options[:proper_cache_version]
68
+ end
69
+
70
+ def log_proper_cache(&blk)
71
+ log_step_with_indent(:'proper cache', &blk)
72
+ end
48
73
  end
49
74
  end
50
75
  end # Project
@@ -0,0 +1,57 @@
1
+ module Dapp
2
+ # Project
3
+ class Project
4
+ # Command
5
+ module Command
6
+ # Mrproper
7
+ module Mrproper
8
+ def mrproper
9
+ log_step_with_indent(:mrproper) do
10
+ if proper_all?
11
+ log_step_with_indent(:containers) { dapp_containers_flush }
12
+ log_step_with_indent(:images) { dapp_images_flush }
13
+ elsif proper_cache_version?
14
+ log_proper_cache do
15
+ proper_cache_images = proper_cache_all_images
16
+ remove_images(dapp_images.lines.select { |id| !proper_cache_images.lines.include?(id) }.map(&:strip))
17
+ end
18
+ else
19
+ raise Error::Project, code: :mrproper_required_option
20
+ end
21
+ end
22
+ end
23
+
24
+ protected
25
+
26
+ def proper_all?
27
+ !!cli_options[:proper_all]
28
+ end
29
+
30
+ def dapp_containers_flush
31
+ remove_containers_by_query('docker ps -a -f "label=dapp" -q', force: true)
32
+ end
33
+
34
+ def dapp_dangling_images_flush
35
+ remove_images_by_query('docker images -f "dangling=true" -f "label=dapp" -q', force: true)
36
+ end
37
+
38
+ def dapp_images_flush
39
+ dapp_dangling_images_flush
40
+ remove_images(dapp_images.lines.map(&:strip), force: true)
41
+ end
42
+
43
+ def dapp_images
44
+ @dapp_images ||= shellout!('docker images -f "dangling=false" --format="{{.Repository}}:{{.Tag}}" -f "label=dapp"').stdout.strip
45
+ end
46
+
47
+ def proper_cache_all_images
48
+ shellout!([
49
+ 'docker images',
50
+ '--format="{{.Repository}}:{{.Tag}}"',
51
+ %(-f "label=dapp-cache-version=#{Dapp::BUILD_CACHE_VERSION}" -f "dangling=false")
52
+ ].join(' ')).stdout.strip
53
+ end
54
+ end
55
+ end
56
+ end # Project
57
+ end # Dapp