percheron 0.3.2 → 0.4.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e4d585a406d9b1b5a23ef33013450d00755ceaf3
4
- data.tar.gz: a63795fa29c8c6fd05de2f6bf9587c57bf0f98bb
3
+ metadata.gz: ddfc86ead3b58968ec07cccf692a841643a45060
4
+ data.tar.gz: a09225865990c275d6812e26ab9dd2496d1171f6
5
5
  SHA512:
6
- metadata.gz: 8b8b0e9fc1748c246c710ae0b52bf48f31665e0e9d4ae2445e4f9f5982e2461514dc194079f06c91da36c00671f05f4a02dd751a075b425bc4d4537bccf6f49e
7
- data.tar.gz: 0e09f330cd3a70896296e65d9610c3381a82c246a67e0c672187a92f00a8ddda7364abc70b7bc6efbc048fb6e4ab8a6b1f74be93858d48ba11067e3fde7b8b27
6
+ metadata.gz: a8a42293482b1d1ac38891aa44a2dbb1dd7d1d1000a1e2879ee78e07230e3af1d8f0747d8d8e9ab945f4534338362d561e6f2dfc53df2100311139c9934fb726
7
+ data.tar.gz: c153bc4ca7252f604143e96d55a6e5d8f0fe7684cf14e893657e00ab150fd227d4e78ccbe88169a85832325492a57d958d185f40b5198ae6b1389c6f724c2a04
data/README.md CHANGED
@@ -6,7 +6,7 @@
6
6
  [![Test Coverage](https://codeclimate.com/github/ashmckenzie/percheron/badges/coverage.svg)](https://codeclimate.com/github/ashmckenzie/percheron)
7
7
  [![Dependency Status](https://gemnasium.com/ashmckenzie/percheron.svg)](https://gemnasium.com/ashmckenzie/percheron)
8
8
 
9
- ![Perheron](https://raw.githubusercontent.com/ashmckenzie/percheron/cache-store/assets/percheron.png)
9
+ ![Percheron](https://raw.githubusercontent.com/ashmckenzie/percheron/master/assets/percheron.png)
10
10
 
11
11
  Organise your Docker containers with muscle and intelligence.
12
12
 
@@ -4,8 +4,10 @@ module Percheron
4
4
 
5
5
  parameter 'STACK_NAME', 'stack name'
6
6
 
7
+ option "--force", :flag, 'Force recreation', default: false
8
+
7
9
  def execute
8
- Percheron::Stack.new(config, stack_name).recreate!(bypass_auto_recreate: true)
10
+ Percheron::Stack.new(config, stack_name).recreate!(force_recreate: force?, force_auto_recreate: true)
9
11
  end
10
12
  end
11
13
  end
@@ -15,10 +15,7 @@ module Percheron
15
15
  end
16
16
 
17
17
  def stacks
18
- contents.stacks.inject({}) do |all, stack_config|
19
- all[stack_config.name] = stack_config unless all[stack_config.name]
20
- all
21
- end
18
+ contents.stacks.to_hash_by_key(:name)
22
19
  end
23
20
 
24
21
  def file_base_path
@@ -1,3 +1,5 @@
1
+ require 'open3'
2
+
1
3
  module Percheron
2
4
  module Container
3
5
  module Actions
@@ -9,10 +11,12 @@ module Percheron
9
11
  end
10
12
 
11
13
  def execute!
12
- base_dir = container.dockerfile.dirname.to_s
13
- $logger.debug "Building '#{container.image}'"
14
- Docker::Image.build_from_dir(base_dir, build_opts) do |out|
15
- $logger.debug '%s' % [ out.strip ]
14
+ in_working_directory(base_dir) do
15
+ execute_pre_build_scripts!
16
+ $logger.debug "Building '#{container.image}'"
17
+ Docker::Image.build_from_dir(base_dir, build_opts) do |out|
18
+ $logger.debug '%s' % [ out.strip ]
19
+ end
16
20
  end
17
21
  end
18
22
 
@@ -29,6 +33,31 @@ module Percheron
29
33
  }
30
34
  end
31
35
 
36
+ def base_dir
37
+ container.dockerfile.dirname.to_s
38
+ end
39
+
40
+ def in_working_directory(new_dir)
41
+ old_dir = Dir.pwd
42
+ Dir.chdir(new_dir)
43
+ yield
44
+ Dir.chdir(old_dir)
45
+ end
46
+
47
+ def execute_pre_build_scripts!
48
+ container.pre_build_scripts.each do |script|
49
+ in_working_directory(base_dir) do
50
+ command = '/bin/bash -x %s 2>&1' % Pathname.new(File.expand_path(script))
51
+ $logger.debug "Executing '#{command}' for '#{container.name}' container"
52
+ Open3.popen2e(command) do |stdin, stdout_err, wait_thr|
53
+ while line = stdout_err.gets
54
+ $logger.debug line.strip
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+
32
61
  end
33
62
  end
34
63
  end
@@ -8,7 +8,7 @@ module Percheron
8
8
  def_delegators :container_config, :name
9
9
 
10
10
  def_config_item_with_default :container_config, false, :auto_recreate
11
- def_config_item_with_default :container_config, [], :env, :ports, :volumes, :dependant_container_names
11
+ def_config_item_with_default :container_config, [], :env, :ports, :volumes, :dependant_container_names, :pre_build_scripts
12
12
 
13
13
  alias_method :auto_recreate?, :auto_recreate
14
14
 
@@ -66,6 +66,7 @@ module Percheron
66
66
  end
67
67
 
68
68
  def start!
69
+ start_dependant_containers!
69
70
  create!
70
71
  recreate!
71
72
  Container::Actions::Start.new(self).execute!
@@ -86,9 +87,9 @@ module Percheron
86
87
  end
87
88
  end
88
89
 
89
- def recreate!(bypass_auto_recreate: false)
90
+ def recreate!(force_recreate: false, force_auto_recreate: false)
90
91
  if exists?
91
- if recreate?(bypass_auto_recreate: bypass_auto_recreate)
92
+ if recreate?(force_recreate: force_recreate, force_auto_recreate: force_auto_recreate)
92
93
  $logger.warn "Container '#{name}' exists and will be recreated"
93
94
  Container::Actions::Recreate.new(self).execute!
94
95
  set_dockerfile_md5!
@@ -108,8 +109,8 @@ module Percheron
108
109
  !dockerfile_md5s_match?
109
110
  end
110
111
 
111
- def recreate?(bypass_auto_recreate: false)
112
- recreatable? && versions_mismatch? && (bypass_auto_recreate || auto_recreate?)
112
+ def recreate?(force_recreate: false, force_auto_recreate: false)
113
+ (force_recreate || (recreatable? && versions_mismatch?)) && (force_auto_recreate || auto_recreate?)
113
114
  end
114
115
 
115
116
  def running?
@@ -169,6 +170,18 @@ module Percheron
169
170
  @container_config ||= stack.container_configs[container_name] || Hashie::Mash.new({})
170
171
  end
171
172
 
173
+ def dependant_containers
174
+ dependant_container_names.map { |container_name| stack.containers[container_name] }
175
+ end
176
+
177
+ def start_dependant_containers!
178
+ dependant_containers.each do |container|
179
+ next if container.running?
180
+ $logger.debug "Container '#{container.name}' being started as it's a dependancy"
181
+ container.start!
182
+ end
183
+ end
184
+
172
185
  end
173
186
  end
174
187
  end
@@ -1,7 +1,8 @@
1
1
  module Percheron
2
2
  module CoreExtensions
3
3
  module Array
4
- module Returning
4
+ module Extras
5
+
5
6
  def return
6
7
  result = nil
7
8
  each do |x|
@@ -13,9 +14,17 @@ module Percheron
13
14
  end
14
15
  result
15
16
  end
17
+
18
+ def to_hash_by_key(key_attr)
19
+ inject({}) do |all, e|
20
+ all[e.send(key_attr)] = e unless all[e.send(key_attr)]
21
+ all
22
+ end
23
+ end
24
+
16
25
  end
17
26
  end
18
27
  end
19
28
  end
20
29
 
21
- Array.include(Percheron::CoreExtensions::Array::Returning)
30
+ Array.include(Percheron::CoreExtensions::Array::Extras)
@@ -30,10 +30,7 @@ module Percheron
30
30
  end
31
31
 
32
32
  def container_configs
33
- stack_config.containers.inject({}) do |all, container|
34
- all[container.name] = container unless all[container.name]
35
- all
36
- end
33
+ stack_config.containers.to_hash_by_key(:name)
37
34
  end
38
35
 
39
36
  def containers
@@ -61,8 +58,8 @@ module Percheron
61
58
  exec_on_containers { |container| container.create! }
62
59
  end
63
60
 
64
- def recreate!(bypass_auto_recreate: false)
65
- exec_on_containers { |container| container.recreate!(bypass_auto_recreate: bypass_auto_recreate) }
61
+ def recreate!(force_recreate: false, force_auto_recreate: false)
62
+ exec_on_containers { |container| container.recreate!(force_recreate: force_recreate, force_auto_recreate: force_auto_recreate) }
66
63
  end
67
64
 
68
65
  def valid?
@@ -1,3 +1,3 @@
1
1
  module Percheron
2
- VERSION = "0.3.2"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: percheron
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ash McKenzie