caterer 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/caterer.gemspec CHANGED
@@ -18,6 +18,7 @@ Gem::Specification.new do |gem|
18
18
  gem.add_dependency 'net-ssh'
19
19
  gem.add_dependency 'net-scp'
20
20
  gem.add_dependency 'tilt'
21
+ gem.add_dependency 'erubis'
21
22
  gem.add_dependency 'oj'
22
23
  gem.add_dependency 'multi_json', '>= 1.3'
23
24
 
@@ -4,7 +4,7 @@ module Caterer
4
4
  class Bootstrap < Base
5
5
 
6
6
  def call(env)
7
- provisioner(env).bootstrap(env[:script])
7
+ provisioner(env).bootstrap
8
8
  @app.call(env)
9
9
  end
10
10
 
@@ -0,0 +1,14 @@
1
+ module Caterer
2
+ module Action
3
+ module Provisioner
4
+ class Install < Base
5
+
6
+ def call(env)
7
+ provisioner(env).install
8
+ @app.call(env)
9
+ end
10
+
11
+ end
12
+ end
13
+ end
14
+ end
@@ -4,6 +4,7 @@ module Caterer
4
4
  autoload :Base, 'caterer/action/provisioner/base'
5
5
  autoload :Bootstrap, 'caterer/action/provisioner/bootstrap'
6
6
  autoload :Cleanup, 'caterer/action/provisioner/cleanup'
7
+ autoload :Install, 'caterer/action/provisioner/install'
7
8
  autoload :Prepare, 'caterer/action/provisioner/prepare'
8
9
  autoload :Provision, 'caterer/action/provisioner/provision'
9
10
  end
@@ -32,6 +32,7 @@ Caterer.actions.register(:provision) do
32
32
  Vli::Action::Builder.new do
33
33
  use Caterer.actions.get(:validate)
34
34
  use Caterer.actions.get(:prepare)
35
+ use Caterer::Action::Provisioner::Install
35
36
  use Caterer::Action::Provisioner::Provision
36
37
  use Caterer.actions.get(:cleanup)
37
38
  end
@@ -42,6 +43,7 @@ Caterer.actions.register(:up) do
42
43
  use Caterer.actions.get(:validate)
43
44
  use Caterer.actions.get(:prepare)
44
45
  use Caterer::Action::Provisioner::Bootstrap
46
+ use Caterer::Action::Provisioner::Install
45
47
  use Caterer::Action::Provisioner::Provision
46
48
  use Caterer.actions.get(:cleanup)
47
49
  end
@@ -6,10 +6,6 @@ module Caterer
6
6
  options = {}
7
7
  opts = OptionParser.new do |opts|
8
8
  opts.banner = "Usage: cater bootstrap HOST [options]"
9
- opts.separator ""
10
- opts.on("-s SCRIPT", "--script SCRIPT", 'optional bootstrap script') do |s|
11
- options[:script] = s
12
- end
13
9
  end
14
10
 
15
11
  # Parse the options
@@ -17,7 +13,7 @@ module Caterer
17
13
  return if not argv
18
14
 
19
15
  with_target_servers(argv, options) do |server|
20
- server.bootstrap({:script => options[:script]})
16
+ server.bootstrap
21
17
  end
22
18
 
23
19
  0
@@ -6,10 +6,6 @@ module Caterer
6
6
  options = {}
7
7
  opts = OptionParser.new do |opts|
8
8
  opts.banner = "Usage: cater provision HOST [options]"
9
- opts.separator ""
10
- opts.on("-s SCRIPT", "--script SCRIPT", 'optional bootstrap script') do |s|
11
- options[:script] = s
12
- end
13
9
  end
14
10
 
15
11
  # Parse the options
@@ -17,7 +13,7 @@ module Caterer
17
13
  return if not argv
18
14
 
19
15
  with_target_servers(argv, options) do |server|
20
- server.up({:script => options[:script]})
16
+ server.up
21
17
  end
22
18
 
23
19
  0
@@ -5,15 +5,16 @@ module Caterer
5
5
  class ChefSolo
6
6
 
7
7
  attr_reader :name, :run_list
8
- attr_accessor :json, :cookbooks_path, :roles_path, :data_bags_path, :bootstrap_script
8
+ attr_accessor :json, :cookbooks_path, :roles_path, :data_bags_path, :bootstrap_scripts
9
9
 
10
10
  def initialize(name)
11
- @name = name
12
- @run_list = []
13
- @json = {}
14
- @cookbooks_path = ['cookbooks']
15
- @roles_path = ['roles']
16
- @data_bags_path = ['data_bags']
11
+ @name = name
12
+ @run_list = []
13
+ @json = {}
14
+ @cookbooks_path = ['cookbooks']
15
+ @roles_path = ['roles']
16
+ @data_bags_path = ['data_bags']
17
+ @bootstrap_scripts = []
17
18
  end
18
19
 
19
20
  def add_recipe(recipe)
@@ -25,7 +26,22 @@ module Caterer
25
26
  end
26
27
 
27
28
  def add_image(image)
28
- @run_list += Caterer.config.images[image].provisioner.run_list
29
+ image = Caterer.config.images[image]
30
+ raise "Unknown image :#{image}" if not image
31
+
32
+ provisioner = image.provisioner
33
+ raise "No provisioner for :#{image}" if not provisioner
34
+
35
+ if not provisioner.name == :chef_solo
36
+ raise "Incompatible provisioner. Must be :#{provisioner.name}"
37
+ end
38
+
39
+ @run_list += provisioner.run_list
40
+ @bootstrap_scripts += provisioner.bootstrap_scripts
41
+ end
42
+
43
+ def add_bootstrap(script)
44
+ @bootstrap_scripts << script
29
45
  end
30
46
 
31
47
  def errors
@@ -35,6 +51,18 @@ module Caterer
35
51
  errors[:run_list] = "is empty"
36
52
  end
37
53
 
54
+ if not @cookbooks_path.is_a? Array
55
+ errors[:cookbooks_path] = "must be an array"
56
+ end
57
+
58
+ if not @roles_path.is_a? Array
59
+ errors[:roles_path] = "must be an array"
60
+ end
61
+
62
+ if not @data_bags_path.is_a? Array
63
+ errors[:data_bags_path] = "must be an array"
64
+ end
65
+
38
66
  if errors.length > 0
39
67
  errors
40
68
  end
@@ -2,7 +2,7 @@ module Caterer
2
2
  module Provisioner
3
3
  class Base
4
4
 
5
- attr_reader :server
5
+ attr_reader :server, :config
6
6
 
7
7
  def initialize(server, config=nil)
8
8
  @server = server
@@ -11,6 +11,7 @@ module Caterer
11
11
 
12
12
  def bootstrap(script=nil); end
13
13
  def prepare; end
14
+ def install; end
14
15
  def provision; end
15
16
  def cleanup; end
16
17
 
@@ -1,4 +1,6 @@
1
1
  require 'tilt'
2
+ require 'erubis'
3
+ require 'erb'
2
4
  require 'oj'
3
5
  require 'multi_json'
4
6
 
@@ -6,34 +8,64 @@ module Caterer
6
8
  module Provisioner
7
9
  class ChefSolo < Base
8
10
 
9
- def bootstrap(script=nil)
11
+ def bootstrap
10
12
 
11
- script ||= config_bootstrap || default_bootstrap
13
+ if bootstrap_scripts.length == 0
14
+ server.ui.warn "No bootstrap scripts to execute"
15
+ end
16
+
17
+ # validate
18
+ with_bootstrap_scripts do |script, count|
19
+
20
+ if not File.exists? script
21
+ server.ui.error "#{script} does not exist!"
22
+ return
23
+ end
12
24
 
13
- if not File.exists? script
14
- server.ui.error "Script does not exist!"
15
- return
16
25
  end
17
26
 
18
27
  # upload
19
- server.ui.info "Uploading bootstrap script..."
20
- server.ssh.upload script, "#{bootstrap_path}"
28
+ with_bootstrap_scripts do |script, count|
21
29
 
22
- # set permissions
23
- server.ui.info "Applying permissions..."
24
- server.ssh.sudo "chown #{server.username} #{bootstrap_path}", :stream => true
25
- server.ssh.sudo "chmod 777 #{bootstrap_path}", :stream => true
30
+ server.ui.info "Uploading #{script}..."
31
+ server.ssh.upload script, "#{bootstrap_path}-#{count}"
32
+
33
+ server.ssh.sudo "chown #{server.username} #{bootstrap_path}-#{count}", :stream => true
34
+ server.ssh.sudo "chmod +x #{bootstrap_path}-#{count}", :stream => true
35
+
36
+ end
26
37
 
27
38
  # run
28
- server.ui.info "Running bootstrap script..."
29
- server.ssh.sudo "#{bootstrap_path}", :stream => true
39
+ with_bootstrap_scripts do |script, count|
40
+
41
+ server.ui.info "Running #{script}..."
42
+ server.ssh.sudo "#{bootstrap_path}-#{count}", :stream => true
43
+
44
+ end
45
+
30
46
  end
31
47
 
32
48
  def prepare
49
+ # create base dir
33
50
  server.ssh.sudo "mkdir -p #{base_path}", :stream => true
34
51
  server.ssh.sudo "chown -R #{server.username} #{base_path}", :stream => true
35
52
  end
36
53
 
54
+ def install
55
+ server.ui.info "Preparing installation..."
56
+
57
+ # upload
58
+ server.ssh.upload install_script, "#{install_path}"
59
+
60
+ # set permissions
61
+ server.ssh.sudo "chown #{server.username} #{install_path}", :stream => true
62
+ server.ssh.sudo "chmod +x #{install_path}", :stream => true
63
+
64
+ # run
65
+ server.ui.info "Installing chef-solo..."
66
+ server.ssh.sudo "#{install_path}", :stream => true
67
+ end
68
+
37
69
  def provision
38
70
 
39
71
  # create cookbooks directory
@@ -42,8 +74,8 @@ module Caterer
42
74
 
43
75
  # sync cookbooks
44
76
  server.ui.info "Syncing cookbooks..."
45
- @config.cookbooks_path.each do |path|
46
- upload_directory path, cookbooks_path
77
+ config.cookbooks_path.each do |path|
78
+ upload_directory path, "#{cookbooks_path}/#{Digest::MD5.hexdigest(path)}"
47
79
  end
48
80
 
49
81
  # create roles directory
@@ -52,7 +84,7 @@ module Caterer
52
84
 
53
85
  # sync roles
54
86
  server.ui.info "Syncing roles..."
55
- @config.roles_path.each do |path|
87
+ config.roles_path.each do |path|
56
88
  upload_directory path, roles_path
57
89
  end
58
90
 
@@ -62,7 +94,7 @@ module Caterer
62
94
 
63
95
  # sync databags
64
96
  server.ui.info "Syncing data bags..."
65
- @config.data_bags_path.each do |path|
97
+ config.data_bags_path.each do |path|
66
98
  upload_directory path, data_bags_path
67
99
  end
68
100
 
@@ -83,7 +115,19 @@ module Caterer
83
115
  end
84
116
 
85
117
  def cleanup
86
- # server.ssh.sudo "rm -rf #{base_path}", :stream => true
118
+ # installer
119
+ server.ssh.sudo "rm -f #{install_path}", :stream => true
120
+
121
+ # bootstrap scripts
122
+ server.ssh.sudo "rm -f #{bootstrap_path}*", :stream => true
123
+
124
+ # solo.rb
125
+ server.ssh.sudo "rm -f #{solo_path}", :stream => true
126
+
127
+ # json
128
+ server.ssh.sudo "rm -f #{json_config_path}", :stream => true
129
+
130
+ # for now, leave cookbooks, roles, and data bags for faster provisioning
87
131
  end
88
132
 
89
133
  protected
@@ -102,10 +146,24 @@ module Caterer
102
146
  end
103
147
  end
104
148
 
149
+ def with_bootstrap_scripts
150
+ config.bootstrap_scripts.each_with_index do |script, index|
151
+ yield script, index if block_given?
152
+ end
153
+ end
154
+
155
+ def bootstrap_scripts
156
+ config.bootstrap_scripts
157
+ end
158
+
105
159
  def base_path
106
160
  "/tmp/cater-chef-solo"
107
161
  end
108
162
 
163
+ def install_path
164
+ "#{base_path}/install"
165
+ end
166
+
109
167
  def bootstrap_path
110
168
  "#{base_path}/bootstrap"
111
169
  end
@@ -123,10 +181,10 @@ module Caterer
123
181
  end
124
182
 
125
183
  def config_bootstrap
126
- @config.bootstrap_script
184
+ config.bootstrap_script
127
185
  end
128
186
 
129
- def default_bootstrap
187
+ def install_script
130
188
  File.expand_path("../../../templates/provisioner/chef_solo/bootstrap.sh", __FILE__)
131
189
  end
132
190
 
@@ -139,7 +197,7 @@ module Caterer
139
197
  end
140
198
 
141
199
  def json_config
142
- MultiJson.dump(@config.json.merge({:run_list => @config.run_list}))
200
+ MultiJson.dump(config.json.merge({:run_list => config.run_list}))
143
201
  end
144
202
 
145
203
  def json_config_path
@@ -1,3 +1,3 @@
1
1
  module Caterer
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,3 +1,5 @@
1
+ file_cache_path "<%= base_path %>"
1
2
  node_name "<%= server.host %>"
2
- cookbook_path "<%= cookbooks_path %>"
3
+ cookbook_path <%= config.cookbooks_path.map { |path| "#{cookbooks_path}/#{Digest::MD5.hexdigest(path)}" }.inspect %>
3
4
  data_bag_path "<%= data_bags_path %>"
5
+ role_path "<%= roles_path %>"
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.1.2
4
+ version: 0.2.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-19 00:00:00.000000000 Z
12
+ date: 2012-12-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: log4r
@@ -107,6 +107,22 @@ dependencies:
107
107
  - - ! '>='
108
108
  - !ruby/object:Gem::Version
109
109
  version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: erubis
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
110
126
  - !ruby/object:Gem::Dependency
111
127
  name: oj
112
128
  requirement: !ruby/object:Gem::Requirement
@@ -174,6 +190,7 @@ files:
174
190
  - lib/caterer/action/provisioner/base.rb
175
191
  - lib/caterer/action/provisioner/bootstrap.rb
176
192
  - lib/caterer/action/provisioner/cleanup.rb
193
+ - lib/caterer/action/provisioner/install.rb
177
194
  - lib/caterer/action/provisioner/prepare.rb
178
195
  - lib/caterer/action/provisioner/provision.rb
179
196
  - lib/caterer/action/server.rb