percheron 0.2.4 → 0.3.0

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
  SHA1:
3
- metadata.gz: 3b2f1f9c52b7ccd444bd59daed1a2c5deeee49d1
4
- data.tar.gz: efcdb60313993319297a27521355529e987fb664
3
+ metadata.gz: 9d54bd472d28eb6065b125a2b34afcaaa081339a
4
+ data.tar.gz: 61427858a0ac55ed4c9dbb01b4bff1c84a6be39f
5
5
  SHA512:
6
- metadata.gz: 593dc6d5b9cc520eeaaf570d9a45a21b5c47a2a649a0537cd41f484460b1474575135fccefaa6c30fd3fc7fba25ac32771b4aa02f9fab9d7e20cfb202cedebfc
7
- data.tar.gz: 30b83d1150ccde52bc2b5bbbb7c57a4db8566d5d55c325238b01e440886fe2d44c1c8480cab392de7fcb946f5a2aff32fde3d0fc2616646bc82d18f24c297152
6
+ metadata.gz: a776a8b5509c634cacac0669dd16cc7b6ac8a2ecccbba9caf577fe2901e07ce10e9e94b38e8f5457dc37f2a3d0354791c45884c6030b19cd719d4111d07113de
7
+ data.tar.gz: 922ef6301086e3c34d3e563a3f18a62b6dfdfbf7f67e8f2f0829139446924a37c2499c88b9fc51944ddcc37b60d06f4e6469285570d6c5e0043f50895fe559c6
@@ -2,9 +2,12 @@ require 'clamp'
2
2
 
3
3
  require 'percheron/cli/abstract_command'
4
4
  require 'percheron/cli/list_command'
5
+ require 'percheron/cli/restart_command'
5
6
  require 'percheron/cli/start_command'
6
7
  require 'percheron/cli/stop_command'
7
8
  require 'percheron/cli/console_command'
9
+ require 'percheron/cli/create_command'
10
+ require 'percheron/cli/recreate_command'
8
11
  require 'percheron/cli/main_command'
9
12
 
10
13
  module Percheron
@@ -0,0 +1,12 @@
1
+ module Percheron
2
+ module CLI
3
+ class CreateCommand < AbstractCommand
4
+
5
+ parameter 'STACK_NAME', 'stack name'
6
+
7
+ def execute
8
+ Percheron::Stack.new(config, stack_name).create!
9
+ end
10
+ end
11
+ end
12
+ end
@@ -5,6 +5,9 @@ module Percheron
5
5
  subcommand 'console', 'Start a pry console session', ConsoleCommand
6
6
  subcommand 'start', 'Start a stack', StartCommand
7
7
  subcommand 'stop', 'Stop a stack', StopCommand
8
+ subcommand 'restart', 'Restart a stack', RestartCommand
9
+ subcommand 'create', 'Create a stack', CreateCommand
10
+ subcommand 'recreate', 'Recreate a stack', RecreateCommand
8
11
  end
9
12
  end
10
13
  end
@@ -0,0 +1,12 @@
1
+ module Percheron
2
+ module CLI
3
+ class RecreateCommand < AbstractCommand
4
+
5
+ parameter 'STACK_NAME', 'stack name'
6
+
7
+ def execute
8
+ Percheron::Stack.new(config, stack_name).recreate!(bypass_auto_recreate: true)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module Percheron
2
+ module CLI
3
+ class RestartCommand < AbstractCommand
4
+
5
+ parameter 'STACK_NAME', 'stack name'
6
+
7
+ def execute
8
+ Percheron::Stack.new(config, stack_name).restart!
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,6 +1,7 @@
1
1
  require 'percheron/container/actions/stop'
2
2
  require 'percheron/container/actions/start'
3
3
  require 'percheron/container/actions/create'
4
+ require 'percheron/container/actions/recreate'
4
5
  require 'percheron/container/actions/build'
5
6
 
6
7
  module Percheron
@@ -0,0 +1,112 @@
1
+ module Percheron
2
+ module Container
3
+ module Actions
4
+ class Recreate
5
+
6
+ def initialize(container)
7
+ @container = container
8
+ end
9
+
10
+ def execute!
11
+ unless temporary_container_exists?
12
+ create_image!
13
+ create_container!
14
+ rename!
15
+ else
16
+ $logger.warn "Not recreating '#{container.name}' container as '#{temporary_name}' because '#{temporary_name}' already exists"
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ attr_reader :container
23
+
24
+ def create_opts
25
+ {
26
+ 'name' => temporary_name,
27
+ 'Image' => container.image,
28
+ 'Hostname' => container.name,
29
+ 'Env' => container.env,
30
+ 'ExposedPorts' => container.exposed_ports,
31
+ 'VolumesFrom' => container.volumes
32
+ }
33
+ end
34
+
35
+ def temporary_name
36
+ '%s_wip' % container.name
37
+ end
38
+
39
+ # FIXME
40
+ def temporary_container_exists?
41
+ temporary_container.info.empty?
42
+ rescue Docker::Error::NotFoundError
43
+ false
44
+ end
45
+
46
+ def create_image!
47
+ unless image_exists?
48
+ $logger.debug "Creating '#{container.image}' image"
49
+ Container::Actions::Build.new(container).execute!
50
+ end
51
+ end
52
+
53
+ def create_container!
54
+ $logger.debug "Recreating '#{container.name}' container as '#{temporary_name}'"
55
+ Docker::Container.create(create_opts)
56
+ end
57
+
58
+ def rename!
59
+ save_current_running_state!
60
+ stop_current! if container.running?
61
+ rename_current_to_old!
62
+ rename_wip_to_current!
63
+ start_new! if container_was_running?
64
+ end
65
+
66
+ def rename_current_new_name
67
+ '%s_%s' % [ container.name, now_timestamp ]
68
+ end
69
+
70
+ def now_timestamp
71
+ Time.now.strftime('%Y%m%d%H%M%S')
72
+ end
73
+
74
+ def temporary_container
75
+ Docker::Container.get(temporary_name)
76
+ end
77
+
78
+ def save_current_running_state!
79
+ @container_was_running = container.running?
80
+ end
81
+
82
+ def stop_current!
83
+ Container::Actions::Stop.new(container).execute!
84
+ end
85
+
86
+ def rename_current_to_old!
87
+ $logger.debug "Renaming '#{container.name}' container to '#{rename_current_new_name}'"
88
+ container.docker_container.rename(rename_current_new_name)
89
+ end
90
+
91
+ def rename_wip_to_current!
92
+ # FIXME
93
+ $logger.debug "Renaming '#{temporary_name}' container to '#{container.name}'"
94
+ Docker::Container.get(temporary_name).rename(container.name)
95
+ end
96
+
97
+ def start_new!
98
+ container.start!
99
+ end
100
+
101
+ def image_exists?
102
+ Docker::Image.exist?(container.image)
103
+ end
104
+
105
+ def container_was_running?
106
+ @container_was_running
107
+ end
108
+
109
+ end
110
+ end
111
+ end
112
+ end
@@ -66,16 +66,48 @@ module Percheron
66
66
  end
67
67
 
68
68
  def start!
69
- Container::Actions::Create.new(self).execute! unless exists?
69
+ create!
70
+ recreate!
70
71
  Container::Actions::Start.new(self).execute!
71
72
  end
72
73
 
74
+ def restart!
75
+ stop!
76
+ start!
77
+ end
78
+
79
+ def create!
80
+ unless exists?
81
+ $logger.debug "Container '#{name}' does not exist, creating"
82
+ Container::Actions::Create.new(self).execute!
83
+ else
84
+ $logger.warn "Not creating '#{name}' container as it already exists"
85
+ end
86
+ end
87
+
88
+ def recreate!(bypass_auto_recreate: false)
89
+ if exists?
90
+ if recreate?(bypass_auto_recreate: bypass_auto_recreate)
91
+ $logger.warn "Container '#{name}' exists and will be recreated"
92
+ Container::Actions::Recreate.new(self).execute!
93
+ else
94
+ if recreatable?
95
+ $logger.warn "Container '#{name}' MD5's do not match, consider recreating"
96
+ else
97
+ $logger.debug "Container '#{name}' does not need to be recreated"
98
+ end
99
+ end
100
+ else
101
+ $logger.warn "Not recreating '#{name}' container as it does not exist"
102
+ end
103
+ end
104
+
73
105
  def recreatable?
74
- exists? && !md5s_match?
106
+ !dockerfile_md5s_match?
75
107
  end
76
108
 
77
- def recreate?
78
- recreatable? && versions_mismatch? && auto_recreate?
109
+ def recreate?(bypass_auto_recreate: false)
110
+ recreatable? && versions_mismatch? && (bypass_auto_recreate || auto_recreate?)
79
111
  end
80
112
 
81
113
  def running?
@@ -94,7 +126,7 @@ module Percheron
94
126
 
95
127
  attr_reader :config, :stack, :container_name
96
128
 
97
- def md5s_match?
129
+ def dockerfile_md5s_match?
98
130
  stored_dockerfile_md5 == current_dockerfile_md5
99
131
  end
100
132
 
@@ -45,6 +45,18 @@ module Percheron
45
45
  exec_on_containers { |container| container.start! }
46
46
  end
47
47
 
48
+ def restart!
49
+ exec_on_containers { |container| container.restart! }
50
+ end
51
+
52
+ def create!
53
+ exec_on_containers { |container| container.create! }
54
+ end
55
+
56
+ def recreate!(bypass_auto_recreate: false)
57
+ exec_on_containers { |container| container.recreate!(bypass_auto_recreate: bypass_auto_recreate) }
58
+ end
59
+
48
60
  def valid?
49
61
  Validators::Stack.new(self).valid?
50
62
  end
@@ -1,3 +1,3 @@
1
1
  module Percheron
2
- VERSION = "0.2.4"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -29,4 +29,5 @@ Gem::Specification.new do |spec|
29
29
  spec.add_development_dependency 'rake', '~> 10.0'
30
30
  spec.add_development_dependency 'rspec', '~> 3.0'
31
31
  spec.add_development_dependency 'simplecov', '~> 0.9'
32
+ spec.add_development_dependency 'timecop', '~> 0.7'
32
33
  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.2.4
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ash McKenzie
@@ -150,6 +150,20 @@ dependencies:
150
150
  - - "~>"
151
151
  - !ruby/object:Gem::Version
152
152
  version: '0.9'
153
+ - !ruby/object:Gem::Dependency
154
+ name: timecop
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '0.7'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '0.7'
153
167
  description:
154
168
  email:
155
169
  - ash@the-rebellion.net
@@ -171,8 +185,11 @@ files:
171
185
  - lib/percheron/cli.rb
172
186
  - lib/percheron/cli/abstract_command.rb
173
187
  - lib/percheron/cli/console_command.rb
188
+ - lib/percheron/cli/create_command.rb
174
189
  - lib/percheron/cli/list_command.rb
175
190
  - lib/percheron/cli/main_command.rb
191
+ - lib/percheron/cli/recreate_command.rb
192
+ - lib/percheron/cli/restart_command.rb
176
193
  - lib/percheron/cli/start_command.rb
177
194
  - lib/percheron/cli/stop_command.rb
178
195
  - lib/percheron/config.rb
@@ -181,6 +198,7 @@ files:
181
198
  - lib/percheron/container/actions.rb
182
199
  - lib/percheron/container/actions/build.rb
183
200
  - lib/percheron/container/actions/create.rb
201
+ - lib/percheron/container/actions/recreate.rb
184
202
  - lib/percheron/container/actions/start.rb
185
203
  - lib/percheron/container/actions/stop.rb
186
204
  - lib/percheron/container/main.rb