caterer 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,8 +5,11 @@ module Caterer
5
5
  autoload :Bootstrap, 'caterer/action/provisioner/bootstrap'
6
6
  autoload :Cleanup, 'caterer/action/provisioner/cleanup'
7
7
  autoload :Install, 'caterer/action/provisioner/install'
8
+ autoload :Lock, 'caterer/action/provisioner/lock'
8
9
  autoload :Prepare, 'caterer/action/provisioner/prepare'
9
10
  autoload :Provision, 'caterer/action/provisioner/provision'
11
+ autoload :Unlock, 'caterer/action/provisioner/unlock'
12
+ autoload :Validate, 'caterer/action/provisioner/validate'
10
13
  end
11
14
  end
12
15
  end
@@ -0,0 +1,9 @@
1
+ module Caterer
2
+ module Action
3
+ module Provisioner
4
+ module Validate
5
+ autoload :Bootstrapped, 'caterer/action/provisioner/validate/bootstrapped'
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,22 @@
1
+ module Caterer
2
+ module Action
3
+ module Provisioner
4
+ module Validate
5
+ class Bootstrapped < Base
6
+
7
+ def call(env)
8
+
9
+ if not provisioner(env).bootstrapped?
10
+ env[:ui].error "Server not bootstrapped, cannot continue"
11
+ return
12
+ end
13
+
14
+ @app.call(env)
15
+
16
+ end
17
+
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -1,7 +1,10 @@
1
1
  module Caterer
2
2
  module Action
3
3
  module Server
4
+ autoload :Lock, 'caterer/action/server/lock'
5
+ autoload :Reboot, 'caterer/action/server/reboot'
4
6
  autoload :Validate, 'caterer/action/server/validate'
7
+ autoload :Unlock, 'caterer/action/server/unlock'
5
8
  end
6
9
  end
7
10
  end
@@ -0,0 +1,14 @@
1
+ module Caterer
2
+ module Action
3
+ module Server
4
+ class Lock < Base
5
+
6
+ def call(env)
7
+ env[:server].lock!
8
+ @app.call(env)
9
+ end
10
+
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,12 @@
1
+ module Caterer
2
+ module Action
3
+ module Server
4
+ class Reboot < Base
5
+ def call(env)
6
+ env[:server].reboot!
7
+ @app.call(env)
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,14 @@
1
+ module Caterer
2
+ module Action
3
+ module Server
4
+ class Unlock < Base
5
+
6
+ def call(env)
7
+ env[:server].unlock!
8
+ @app.call(env)
9
+ end
10
+
11
+ end
12
+ end
13
+ end
14
+ end
@@ -2,7 +2,8 @@ module Caterer
2
2
  module Action
3
3
  module Server
4
4
  module Validate
5
- autoload :SSH, 'caterer/action/server/validate/ssh'
5
+ autoload :SSH, 'caterer/action/server/validate/ssh'
6
+ autoload :Unlocked, 'caterer/action/server/validate/Unlocked'
6
7
  end
7
8
  end
8
9
  end
@@ -0,0 +1,21 @@
1
+ module Caterer
2
+ module Action
3
+ module Server
4
+ module Validate
5
+ class Unlocked < Base
6
+
7
+ def call(env)
8
+
9
+ if env[:server].locked?
10
+ env[:ui].error "Server is currently locked, cannot proceed"
11
+ return
12
+ end
13
+
14
+ @app.call(env)
15
+ end
16
+
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -4,47 +4,67 @@ Caterer.actions.register(:validate) do
4
4
  use Caterer::Action::Config::Validate::Image
5
5
  use Caterer::Action::Config::Validate::Provisioner
6
6
  use Caterer::Action::Server::Validate::SSH
7
+ use Caterer::Action::Server::Validate::Unlocked
7
8
  end
8
9
  end
9
10
 
10
- Caterer.actions.register(:prepare) do
11
+ Caterer.actions.register(:lock) do
11
12
  Vli::Action::Builder.new do
12
- use Caterer::Action::Provisioner::Prepare
13
+ use Caterer::Action::Config::Validate::Image
14
+ use Caterer::Action::Config::Validate::Provisioner
15
+ use Caterer::Action::Server::Validate::SSH
16
+ use Caterer::Action::Server::Lock
13
17
  end
14
18
  end
15
19
 
16
- Caterer.actions.register(:cleanup) do
20
+ Caterer.actions.register(:unlock) do
17
21
  Vli::Action::Builder.new do
18
- use Caterer::Action::Provisioner::Cleanup
22
+ use Caterer::Action::Config::Validate::Image
23
+ use Caterer::Action::Config::Validate::Provisioner
24
+ use Caterer::Action::Server::Validate::SSH
25
+ use Caterer::Action::Server::Unlock
19
26
  end
20
27
  end
21
28
 
22
29
  Caterer.actions.register(:bootstrap) do
23
30
  Vli::Action::Builder.new do
24
31
  use Caterer.actions.get(:validate)
25
- use Caterer.actions.get(:prepare)
32
+ use Caterer::Action::Provisioner::Prepare
33
+ use Caterer::Action::Server::Lock
26
34
  use Caterer::Action::Provisioner::Bootstrap
27
- use Caterer.actions.get(:cleanup)
35
+ use Caterer::Action::Provisioner::Install
36
+ use Caterer::Action::Provisioner::Cleanup
37
+ use Caterer::Action::Server::Unlock
28
38
  end
29
39
  end
30
40
 
31
41
  Caterer.actions.register(:provision) do
32
42
  Vli::Action::Builder.new do
33
43
  use Caterer.actions.get(:validate)
34
- use Caterer.actions.get(:prepare)
35
- use Caterer::Action::Provisioner::Install
44
+ use Caterer::Action::Provisioner::Validate::Bootstrapped
45
+ use Caterer::Action::Provisioner::Prepare
46
+ use Caterer::Action::Server::Lock
36
47
  use Caterer::Action::Provisioner::Provision
37
- use Caterer.actions.get(:cleanup)
48
+ use Caterer::Action::Provisioner::Cleanup
49
+ use Caterer::Action::Server::Unlock
38
50
  end
39
51
  end
40
52
 
41
53
  Caterer.actions.register(:up) do
42
54
  Vli::Action::Builder.new do
43
55
  use Caterer.actions.get(:validate)
44
- use Caterer.actions.get(:prepare)
56
+ use Caterer::Action::Provisioner::Prepare
57
+ use Caterer::Action::Server::Lock
45
58
  use Caterer::Action::Provisioner::Bootstrap
46
- use Caterer::Action::Provisioner::Install
47
59
  use Caterer::Action::Provisioner::Provision
48
- use Caterer.actions.get(:cleanup)
60
+ use Caterer::Action::Provisioner::Cleanup
61
+ use Caterer::Action::Server::Unlock
62
+ end
63
+ end
64
+
65
+ Caterer.actions.register(:reboot) do
66
+ Vli::Action::Builder.new do
67
+ use Caterer::Action::Server::Validate::SSH
68
+ use Caterer::Action::Server::Reboot
49
69
  end
50
70
  end
@@ -2,9 +2,11 @@ module Caterer
2
2
  module Command
3
3
  autoload :Base, 'caterer/command/base'
4
4
  autoload :Bootstrap, 'caterer/command/bootstrap'
5
+ autoload :Lock, 'caterer/command/lock'
5
6
  autoload :Provision, 'caterer/command/provision'
6
7
  autoload :Reboot, 'caterer/command/reboot'
7
8
  autoload :Test, 'caterer/command/test'
9
+ autoload :Unlock, 'caterer/command/unlock'
8
10
  autoload :Up, 'caterer/command/up'
9
11
  end
10
12
  end
@@ -22,7 +22,10 @@ module Caterer
22
22
  opts.on('-P PORT', '--port PORT', 'assumes 22') do |p|
23
23
  options[:port] = p
24
24
  end
25
- opts.on('-d JSON', '--data JSON', 'json data that the provisioner may use' )do |d|
25
+ opts.on('-e ENGINE', '--engine ENGINE', 'provision engine' ) do |e|
26
+ options[:engine] = e
27
+ end
28
+ opts.on('-d JSON', '--data JSON', 'json data that the provisioner may use' ) do |d|
26
29
  options[:data] = d
27
30
  end
28
31
  opts.on('-i IMAGE', '--image IMAGE', 'corresponds to a image in Caterfile') do |i|
@@ -0,0 +1,24 @@
1
+ module Caterer
2
+ module Command
3
+ class Lock < Base
4
+
5
+ def execute
6
+ options = {}
7
+ opts = OptionParser.new do |opts|
8
+ opts.banner = "Usage: cater lock HOST [options]"
9
+ end
10
+
11
+ # Parse the options
12
+ argv = parse_options(opts, options, true)
13
+ return if not argv
14
+
15
+ with_target_servers(argv, options) do |server|
16
+ server.lock
17
+ end
18
+
19
+ 0
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -5,15 +5,17 @@ module Caterer
5
5
  def execute
6
6
  options = {}
7
7
  opts = OptionParser.new do |opts|
8
- opts.banner = "Usage: cater provision HOST [options]"
8
+ opts.banner = "Usage: cater reboot HOST [options]"
9
9
  end
10
10
 
11
11
  # Parse the options
12
12
  argv = parse_options(opts, options, true)
13
13
  return if not argv
14
14
 
15
- @env.ui.info options
16
- @env.ui.info argv
15
+ with_target_servers(argv, options) do |server|
16
+ server.reboot
17
+ end
18
+
17
19
  0
18
20
  end
19
21
 
@@ -0,0 +1,24 @@
1
+ module Caterer
2
+ module Command
3
+ class Unlock < Base
4
+
5
+ def execute
6
+ options = {}
7
+ opts = OptionParser.new do |opts|
8
+ opts.banner = "Usage: cater unlock HOST [options]"
9
+ end
10
+
11
+ # Parse the options
12
+ argv = parse_options(opts, options, true)
13
+ return if not argv
14
+
15
+ with_target_servers(argv, options) do |server|
16
+ server.unlock
17
+ end
18
+
19
+ 0
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -1,6 +1,7 @@
1
1
  # commands
2
- Caterer.commands.register(:test) { Caterer::Command::Test }
3
2
  Caterer.commands.register(:bootstrap) { Caterer::Command::Bootstrap }
3
+ Caterer.commands.register(:lock) { Caterer::Command::Lock }
4
4
  Caterer.commands.register(:provision) { Caterer::Command::Provision }
5
+ Caterer.commands.register(:reboot) { Caterer::Command::Reboot }
6
+ Caterer.commands.register(:unlock) { Caterer::Command::Unlock }
5
7
  Caterer.commands.register(:up) { Caterer::Command::Up }
6
- Caterer.commands.register(:reboot) { Caterer::Command::Reboot }
@@ -4,13 +4,23 @@ module Caterer
4
4
  module Communication
5
5
  class Rsync
6
6
 
7
+ class << self
8
+
9
+ def available?
10
+ `command -v rsync &>/dev/null`
11
+ $?.exitstatus == 0 ? true : false
12
+ end
13
+
14
+ end
15
+
7
16
  def initialize(server)
8
17
  @server = server
9
- @logger = Log4r::Logger.new("caterer::communication::ssh")
18
+ @logger = Log4r::Logger.new("caterer::communication::rsync")
10
19
  end
11
20
 
12
21
  def sync(from, to)
13
- PTY.spawn(rsync_cmd(from, to)) do |stdout, stdin|
22
+ exit_status = nil
23
+ PTY.spawn(rsync_cmd(from, to)) do |stdout, stdin, pid|
14
24
  eof = false
15
25
  until eof do
16
26
  begin
@@ -18,29 +28,20 @@ module Caterer
18
28
  if out.match /password:/
19
29
  stdin.puts @server.password
20
30
  else
21
- print out if print_worthy out
31
+ @logger.debug("stdout: #{out}")
22
32
  end
23
33
  rescue EOFError
24
34
  eof = true
25
35
  end
26
36
  end
37
+ Process.wait(pid)
38
+ exit_status = $?.exitstatus
27
39
  end
40
+ exit_status
28
41
  end
29
42
 
30
43
  protected
31
44
 
32
- def print_worthy(data)
33
- [
34
- /^\r\n$/, # empty lines
35
- /^Warning: Permanently added/, # key added nonsense
36
- ].each do |cruft|
37
- if data.match cruft
38
- return false
39
- end
40
- end
41
- true
42
- end
43
-
44
45
  def rsync_cmd(from, to)
45
46
  "rsync -zr -e 'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p #{@server.port}' --delete #{from} #{@server.username}@#{@server.host}:#{to}"
46
47
  end
@@ -9,11 +9,12 @@ module Caterer
9
9
  @config = config
10
10
  end
11
11
 
12
- def bootstrap(script=nil); end
13
- def prepare; end
14
- def install; end
15
- def provision; end
16
- def cleanup; end
12
+ def bootstrap; end
13
+ def bootstrapped?; true; end
14
+ def cleanup; end
15
+ def install; end
16
+ def prepare; end
17
+ def provision; end
17
18
 
18
19
  end
19
20
  end
@@ -10,10 +10,6 @@ module Caterer
10
10
 
11
11
  def bootstrap
12
12
 
13
- if bootstrap_scripts.length == 0
14
- server.ui.warn "No bootstrap scripts to execute"
15
- end
16
-
17
13
  # validate
18
14
  with_bootstrap_scripts do |script, count|
19
15
 
@@ -45,6 +41,11 @@ module Caterer
45
41
 
46
42
  end
47
43
 
44
+ def bootstrapped?
45
+ res = server.ssh.sudo "command -v chef-solo &>/dev/null"
46
+ res == 0 ? true : false
47
+ end
48
+
48
49
  def prepare
49
50
  # create base dir
50
51
  server.ssh.sudo "mkdir -p #{base_path}", :stream => true
@@ -115,6 +116,8 @@ module Caterer
115
116
  end
116
117
 
117
118
  def cleanup
119
+ server.ui.info "Cleaning up..."
120
+
118
121
  # installer
119
122
  server.ssh.sudo "rm -f #{install_path}", :stream => true
120
123
 
@@ -136,11 +139,16 @@ module Caterer
136
139
  if File.exists? from
137
140
  if @server.can_rsync?
138
141
  from += "/" if not from.match /\/$/
139
- @server.rsync.sync(from, to)
142
+ res = @server.rsync.sync(from, to)
143
+ server.ui.error "rsync was unsuccessful" if res != 0
140
144
  else
145
+ server.ui.warn "Rsync unavailable, falling back to scp (slower)..."
141
146
  unique = Digest::MD5.hexdigest(from)
147
+ server.ssh.sudo "rm -rf #{to}/*", :stream => true
148
+ server.ssh.sudo "mkdir -p #{to}/#{unique}", :stream => true
149
+ server.ssh.sudo "chown -R #{server.username} #{to}/#{unique}", :stream => true
142
150
  server.ssh.upload from, "#{to}/#{unique}"
143
- server.ssh.sudo "mv #{to}/#{unique}/* #{to}/", :stream => true
151
+ server.ssh.sudo "mv #{to}/#{unique}/**/* #{to}/", :stream => true
144
152
  server.ssh.sudo "rm -rf #{to}/#{unique}", :stream => true
145
153
  end
146
154
  end
@@ -180,6 +188,14 @@ module Caterer
180
188
  "#{base_path}/data_bags"
181
189
  end
182
190
 
191
+ def solo_path
192
+ "#{base_path}/solo.rb"
193
+ end
194
+
195
+ def json_config_path
196
+ "#{base_path}/config.json"
197
+ end
198
+
183
199
  def config_bootstrap
184
200
  config.bootstrap_script
185
201
  end
@@ -188,10 +204,6 @@ module Caterer
188
204
  File.expand_path("../../../templates/provisioner/chef_solo/bootstrap.sh", __FILE__)
189
205
  end
190
206
 
191
- def solo_path
192
- "#{base_path}/solo.rb"
193
- end
194
-
195
207
  def solo_content
196
208
  Tilt.new(File.expand_path('../../../templates/provisioner/chef_solo/solo.erb', __FILE__)).render(self)
197
209
  end
@@ -204,10 +216,6 @@ module Caterer
204
216
  {:run_list => config.run_list}.merge(config.json).merge(server.data)
205
217
  end
206
218
 
207
- def json_config_path
208
- "#{base_path}/config.json"
209
- end
210
-
211
219
  def command_string
212
220
  "chef-solo -c #{solo_path} -j #{json_config_path}"
213
221
  end
@@ -15,6 +15,10 @@ module Caterer
15
15
  @key = opts[:key]
16
16
  @images = opts[:images] || []
17
17
  @data = opts[:data] || {}
18
+
19
+ @logger = Log4r::Logger.new("caterer::server")
20
+
21
+ @logger.info("Server: #{opts.inspect}")
18
22
  end
19
23
 
20
24
  def bootstrap(opts={})
@@ -51,6 +55,46 @@ module Caterer
51
55
  end
52
56
  end
53
57
 
58
+ def lock(opts={})
59
+ if @images.length == 0
60
+ ui.error "image list is empty, nothing to do"
61
+ end
62
+ @images.each do |i|
63
+ ui.info "*** Locking image: #{i} ***"
64
+ run_action(:lock, opts.merge({:image => i}))
65
+ end
66
+ end
67
+
68
+ def unlock(opts={})
69
+ if @images.length == 0
70
+ ui.error "image list is empty, nothing to do"
71
+ end
72
+ @images.each do |i|
73
+ ui.info "*** Unlocking image: #{i} ***"
74
+ run_action(:unlock, opts.merge({:image => i}))
75
+ end
76
+ end
77
+
78
+ def reboot!
79
+ ui.info "Rebooting..."
80
+ ssh.sudo "shutdown -r now", :stream => true
81
+ end
82
+
83
+ def lock!
84
+ ui.info "Locking..."
85
+ ssh.sudo "touch #{lock_path}"
86
+ end
87
+
88
+ def unlock!
89
+ ui.info "Unlocking..."
90
+ ssh.sudo "rm -f #{lock_path}"
91
+ end
92
+
93
+ def locked?
94
+ res = ssh.sudo "[ -f #{lock_path} ]"
95
+ res == 0 ? true : false
96
+ end
97
+
54
98
  def ssh
55
99
  @ssh ||= Communication::SSH.new(self)
56
100
  end
@@ -60,7 +104,7 @@ module Caterer
60
104
  end
61
105
 
62
106
  def can_rsync?
63
- true
107
+ !Vli::Util::Platform.windows? and Communication::Rsync.available?
64
108
  end
65
109
 
66
110
  def ssh_opts
@@ -116,5 +160,11 @@ module Caterer
116
160
  env.action_runner.run(name, options)
117
161
  end
118
162
 
163
+ protected
164
+
165
+ def lock_path
166
+ "/tmp/cater.lock"
167
+ end
168
+
119
169
  end
120
170
  end
@@ -1,3 +1,3 @@
1
1
  module Caterer
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: caterer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-22 00:00:00.000000000 Z
12
+ date: 2012-12-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: log4r
@@ -193,17 +193,25 @@ files:
193
193
  - lib/caterer/action/provisioner/install.rb
194
194
  - lib/caterer/action/provisioner/prepare.rb
195
195
  - lib/caterer/action/provisioner/provision.rb
196
+ - lib/caterer/action/provisioner/validate.rb
197
+ - lib/caterer/action/provisioner/validate/bootstrapped.rb
196
198
  - lib/caterer/action/server.rb
199
+ - lib/caterer/action/server/lock.rb
200
+ - lib/caterer/action/server/reboot.rb
201
+ - lib/caterer/action/server/unlock.rb
197
202
  - lib/caterer/action/server/validate.rb
198
203
  - lib/caterer/action/server/validate/ssh.rb
204
+ - lib/caterer/action/server/validate/unlocked.rb
199
205
  - lib/caterer/actions.rb
200
206
  - lib/caterer/cli.rb
201
207
  - lib/caterer/command.rb
202
208
  - lib/caterer/command/base.rb
203
209
  - lib/caterer/command/bootstrap.rb
210
+ - lib/caterer/command/lock.rb
204
211
  - lib/caterer/command/provision.rb
205
212
  - lib/caterer/command/reboot.rb
206
213
  - lib/caterer/command/test.rb
214
+ - lib/caterer/command/unlock.rb
207
215
  - lib/caterer/command/up.rb
208
216
  - lib/caterer/commands.rb
209
217
  - lib/caterer/communication.rb