vagabond 0.2.0 → 0.2.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 (73) hide show
  1. data/CHANGELOG.md +18 -0
  2. data/README.md +125 -4
  3. data/bin/vagabond +43 -16
  4. data/lib/vagabond/actions/cluster.rb +66 -0
  5. data/lib/vagabond/actions/create.rb +12 -7
  6. data/lib/vagabond/actions/destroy.rb +69 -15
  7. data/lib/vagabond/actions/init.rb +75 -0
  8. data/lib/vagabond/actions/provision.rb +4 -2
  9. data/lib/vagabond/actions/status.rb +33 -22
  10. data/lib/vagabond/actions/up.rb +8 -1
  11. data/lib/vagabond/bootstraps/server-zero.erb +20 -0
  12. data/lib/vagabond/bootstraps/server.erb +3 -2
  13. data/lib/vagabond/constants.rb +0 -15
  14. data/lib/vagabond/cookbooks/lxc/CHANGELOG.md +16 -0
  15. data/lib/vagabond/cookbooks/lxc/Gemfile +3 -2
  16. data/lib/vagabond/cookbooks/lxc/Gemfile.lock +30 -121
  17. data/lib/vagabond/cookbooks/lxc/README.md +43 -14
  18. data/lib/vagabond/cookbooks/lxc/attributes/default.rb +3 -3
  19. data/lib/vagabond/cookbooks/lxc/files/default/lxc-awesome-ephemeral +499 -0
  20. data/lib/vagabond/cookbooks/lxc/libraries/lxc.rb +223 -58
  21. data/lib/vagabond/cookbooks/lxc/libraries/lxc_file_config.rb +3 -0
  22. data/lib/vagabond/cookbooks/lxc/libraries/monkey.rb +51 -0
  23. data/lib/vagabond/cookbooks/lxc/metadata.rb +6 -5
  24. data/lib/vagabond/cookbooks/lxc/providers/config.rb +9 -16
  25. data/lib/vagabond/cookbooks/lxc/providers/container.rb +241 -229
  26. data/lib/vagabond/cookbooks/lxc/providers/default.rb +57 -0
  27. data/lib/vagabond/cookbooks/lxc/providers/ephemeral.rb +40 -0
  28. data/lib/vagabond/cookbooks/lxc/providers/fstab.rb +13 -54
  29. data/lib/vagabond/cookbooks/lxc/providers/interface.rb +13 -67
  30. data/lib/vagabond/cookbooks/lxc/providers/service.rb +14 -14
  31. data/lib/vagabond/cookbooks/lxc/recipes/default.rb +17 -4
  32. data/lib/vagabond/cookbooks/lxc/recipes/install_dependencies.rb +1 -1
  33. data/lib/vagabond/cookbooks/lxc/resources/config.rb +2 -2
  34. data/lib/vagabond/cookbooks/lxc/resources/container.rb +31 -6
  35. data/lib/vagabond/cookbooks/lxc/resources/default.rb +12 -0
  36. data/lib/vagabond/cookbooks/lxc/resources/ephemeral.rb +13 -0
  37. data/lib/vagabond/cookbooks/lxc/resources/fstab.rb +2 -1
  38. data/lib/vagabond/cookbooks/lxc/resources/interface.rb +6 -3
  39. data/lib/vagabond/cookbooks/lxc/resources/service.rb +1 -1
  40. data/lib/vagabond/cookbooks/lxc/templates/default/file_content.erb +2 -0
  41. data/lib/vagabond/cookbooks/lxc/templates/default/interface.erb +9 -3
  42. data/lib/vagabond/cookbooks/vagabond/README.md +10 -0
  43. data/lib/vagabond/cookbooks/vagabond/attributes/default.rb +1 -0
  44. data/lib/vagabond/cookbooks/vagabond/files/default/lxc-centos +13 -6
  45. data/lib/vagabond/cookbooks/vagabond/metadata.rb +1 -0
  46. data/lib/vagabond/cookbooks/vagabond/recipes/default.rb +46 -4
  47. data/lib/vagabond/cookbooks/vagabond/recipes/zero.rb +9 -0
  48. data/lib/vagabond/errors.rb +23 -0
  49. data/lib/vagabond/helpers.rb +41 -14
  50. data/lib/vagabond/internal_configuration.rb +120 -27
  51. data/lib/vagabond/kitchen.rb +143 -63
  52. data/lib/vagabond/knife.rb +8 -5
  53. data/lib/vagabond/layout.rb +16 -0
  54. data/lib/vagabond/monkey/kitchen_config.rb +23 -0
  55. data/lib/vagabond/server.rb +79 -63
  56. data/lib/vagabond/spec.rb +345 -0
  57. data/lib/vagabond/uploader.rb +30 -0
  58. data/lib/vagabond/uploader/berkshelf.rb +53 -0
  59. data/lib/vagabond/uploader/knife.rb +24 -0
  60. data/lib/vagabond/uploader/librarian.rb +31 -0
  61. data/lib/vagabond/vagabond.rb +30 -11
  62. data/lib/vagabond/vagabondfile.rb +40 -5
  63. data/lib/vagabond/version.rb +1 -1
  64. data/vagabond.gemspec +5 -2
  65. metadata +75 -15
  66. data/lib/vagabond/cookbooks/lxc/resources/#container.rb# +0 -28
  67. data/lib/vagabond/cookbooks/lxc/test/kitchen/Kitchenfile +0 -7
  68. data/lib/vagabond/cookbooks/lxc/test/kitchen/cookbooks/lxc_test/metadata.rb +0 -2
  69. data/lib/vagabond/cookbooks/lxc/test/kitchen/cookbooks/lxc_test/recipes/centos_lxc.rb +0 -0
  70. data/lib/vagabond/cookbooks/lxc/test/kitchen/cookbooks/lxc_test/recipes/chef-bootstrap.rb +0 -0
  71. data/lib/vagabond/cookbooks/lxc/test/kitchen/cookbooks/lxc_test/recipes/lxc_files.rb +0 -0
  72. data/lib/vagabond/cookbooks/lxc/test/kitchen/cookbooks/lxc_test/recipes/lxc_templates.rb +0 -0
  73. data/lib/vagabond/cookbooks/lxc/test/kitchen/cookbooks/lxc_test/recipes/ubuntu_lxc.rb +0 -0
@@ -1,16 +1,20 @@
1
1
  require 'thor'
2
2
  require 'chef'
3
+ require 'kitchen'
3
4
  require 'kitchen/busser'
5
+ require 'kitchen/loader/yaml'
6
+ require 'vagabond/monkey/kitchen_config'
4
7
 
5
- %w(helpers vagabondfile vagabond server helpers/cheffile_loader).each do |dep|
8
+ %w(constants errors helpers vagabondfile vagabond server helpers/cheffile_loader actions/status).each do |dep|
6
9
  require "vagabond/#{dep}"
7
10
  end
8
11
 
9
12
  module Vagabond
10
13
  class Kitchen < Thor
11
-
14
+
12
15
  include Thor::Actions
13
16
  include Helpers
17
+ include Actions::Status
14
18
 
15
19
  class << self
16
20
  def basename
@@ -19,13 +23,14 @@ module Vagabond
19
23
  end
20
24
 
21
25
  self.class_exec(&Vagabond::CLI_OPTIONS)
22
-
26
+
23
27
  attr_reader :kitchen
24
28
  attr_reader :platform_map
25
29
  attr_reader :vagabondfile
26
30
  attr_reader :ui
27
31
  attr_reader :name
28
32
  attr_reader :action
33
+ attr_reader :internal_config
29
34
 
30
35
  def initialize(*args)
31
36
  super
@@ -51,7 +56,7 @@ module Vagabond
51
56
  end
52
57
  end
53
58
 
54
- desc 'test COOKBOOK', 'Run test kitchen on COOKBOOK'
59
+ desc 'test [COOKBOOK]', 'Run test kitchen on COOKBOOK'
55
60
  method_option(:platform,
56
61
  :type => :string,
57
62
  :desc => 'Specify platform to test'
@@ -68,27 +73,33 @@ module Vagabond
68
73
  method_option(:parallel,
69
74
  :type => :boolean,
70
75
  :default => false,
71
- :desc => 'Build test nodes in parallel'
76
+ :desc => 'Build test nodes in parallel [not enabled yet]'
72
77
  )
73
78
  method_option(:suites,
74
79
  :type => :string,
75
80
  :desc => 'Specify suites to test [suite1,suite2,...]'
76
81
  )
77
- def test(cookbook)
82
+ def test(*args)
83
+ cookbook = args.first
78
84
  setup(cookbook, :test)
85
+
79
86
  ui.info "#{ui.color('Vagabond:', :bold)} - Kitchen testing for cookbook #{ui.color(name, :cyan)}"
80
87
  results = Mash.new
81
88
  platforms = [options[:platform] || platform_map.keys].flatten
82
- if(options[:cluster])
83
- ui.info ui.color(" -> Cluster Testing #{options[:cluster]}!", :yellow)
84
- if(kitchen[:clusters].nil? || kitchen[:clusters][options[:cluster]].nil?)
89
+ if(cluster_name = options[:cluster])
90
+ ui.info ui.color(" -> Cluster Testing #{cluster_name}!", :yellow)
91
+ if(kitchen.clusters.empty? || kitchen.clusters[cluster_name].nil?)
85
92
  ui.fatal "Requested cluster is not defined: #{options[:cluster]}"
86
- exit EXIT_CODES[:cluster_invalid]
93
+ raise VagabondError::ClusterInvalid.new(cluster_name)
87
94
  end
88
95
  serv = Server.new
96
+ if(@solo && serv.vagabondfile[:local_chef_server].empty?)
97
+ serv.vagabondfile[:local_chef_server].update(:enabled => true, :zero => true)
98
+ end
89
99
  serv.options = options
100
+ serv.send(:do_create)
90
101
  serv.auto_upload # upload everything : make optional?
91
- suites = kitchen[:clusters][options[:cluster]]
102
+ suites = kitchen.clusters[options[:cluster]]
92
103
  platforms.each do |platform|
93
104
  %w(local_server_provision test destroy).each do |action|
94
105
  suites.each do |suite_name|
@@ -103,6 +114,7 @@ module Vagabond
103
114
  end
104
115
  end
105
116
  end
117
+ serv.destroy
106
118
  else
107
119
  suites = options[:suites] ? options[:suites].split(',') : ['default']
108
120
  platforms.each do |platform|
@@ -124,17 +136,28 @@ module Vagabond
124
136
  ui.info " Suite: #{res[:suite_name]} -> #{res[:result] ? ui.color('SUCCESS!', :green) : ui.color('FAILED!', :red)}"
125
137
  end
126
138
  end
139
+ raise KitchenTestFailed.new(infos.map{|res| res[:suite_name] unless res[:result]}.compact)
127
140
  end
128
-
141
+
142
+ desc 'status [NAME]', 'Show test node status'
143
+ def status(name=nil)
144
+ setup(name, :status)
145
+ _status
146
+ end
147
+
129
148
  protected
130
149
 
150
+ def mappings_key
151
+ :test_mappings
152
+ end
153
+
131
154
  def local_server_provision_node(platform, suite_name)
132
155
  run_list = generate_runlist(platform, suite_name)
133
156
  v_inst = vagabond_instance(:up, platform, :suite_name => suite_name, :run_list => run_list)
134
157
  raise "ERROR! No local chef!" unless v_inst.options[:knife_opts]
135
158
  v_inst.send(:execute)
136
159
  end
137
-
160
+
138
161
  # TODO: Handle failed provision!
139
162
  def provision_node(platform, suite_name)
140
163
  run_list = generate_runlist(platform, suite_name)
@@ -161,18 +184,35 @@ module Vagabond
161
184
  v_inst.send(:execute)
162
185
  end
163
186
  end
164
-
187
+
165
188
  def setup(name, action)
189
+ @solo = name.to_s.strip.empty?
166
190
  @options = options.dup
167
- @vagabondfile = Vagabondfile.new(options[:vagabond_file])
191
+ @vagabondfile = Vagabondfile.new(options[:vagabond_file], :allow_missing)
168
192
  setup_ui
169
- @name = name
193
+ @internal_config = InternalConfiguration.new(@vagabondfile, ui, options)
194
+ @name = name || action == :status ? name : discover_name
195
+ load_kitchen_yml(@name) unless action == :status
170
196
  @action = action
171
- load_kitchen_yml
197
+ end
198
+
199
+ # TODO: Make this traverse up if in cookbook subdir
200
+ def discover_name
201
+ name = nil
202
+ if(File.exists?('metadata.rb'))
203
+ m = Chef::Cookbook::Metadata.new
204
+ m.from_file('metadata.rb')
205
+ name = m.name
206
+ unless(name)
207
+ name = File.basename(File.dirname(File.expand_path(Dir.pwd)))
208
+ end
209
+ end
210
+ raise "Failed to detect name of cookbook. Are we in the top directory?" unless name
211
+ name
172
212
  end
173
213
 
174
214
  def configure_for(l_name, platform, suite_name, runlist, *args)
175
- dir = File.join(File.dirname(vagabondfile.path), ".vagabond/node_configs/#{l_name}")
215
+ dir = File.join(File.dirname(vagabondfile.store_path), ".vagabond/node_configs/#{l_name}")
176
216
  FileUtils.mkdir_p(dir)
177
217
  _args = [args.include?(:integration) ? :integration : nil].compact
178
218
  write_dna(l_name, suite_name, dir, platform, runlist, *_args) if args.include?(:dna)
@@ -186,14 +226,14 @@ module Vagabond
186
226
  file.write("cookbook_path '#{File.join(dir, 'cookbooks')}'\n")
187
227
  end
188
228
  end
189
-
229
+
190
230
  def write_dna(l_name, suite_name, dir, platform, runlist, *args)
191
231
  key = args.include?(:integration) ? :integration_suites : :suites
192
232
  dna = Mash.new
193
- dna.merge!(platform_map[platform][:attributes] || {})
194
- s_args = kitchen[:suites].detect{|s|s[:name] == suite_name}
195
- if(s_args)
196
- dna.merge!(s_args)
233
+ dna.merge!(platform_map[platform][:attributes] || Mash.new)
234
+ suite = kitchen.suites.detect{|s|s.name == suite_name}
235
+ if(suite)
236
+ dna.merge!(suite.attributes)
197
237
  end
198
238
  dna[:run_list] = runlist
199
239
  File.open(File.join(dir, 'dna.json'), 'w') do |file|
@@ -202,30 +242,70 @@ module Vagabond
202
242
  end
203
243
 
204
244
  def cookbook_path
205
- Chef::CookbookLoader.new(
206
- File.join(File.dirname(vagabondfile.path), 'cookbooks')
207
- ).load_cookbooks[name].root_dir
245
+ if(@solo)
246
+ vagabondfile.directory
247
+ else
248
+ require 'chef/knife'
249
+ begin
250
+ Chef::Knife.new.configure_chef
251
+ rescue
252
+ # ignore
253
+ end
254
+ Chef::CookbookLoader.new(
255
+ Chef::Config[:cookbook_path]
256
+ ).load_cookbooks[name].root_dir
257
+ end
208
258
  end
209
-
210
- def load_cookbooks(l_name, suite_name, dir, platform, runlist, *_args)
259
+
260
+ def custom_cheffile
261
+ ui.warn "Installing Cooks with Librarian"
211
262
  contents = ['site "http://community.opscode.com/api/v1"']
212
263
  contents << "cookbook '#{name}', :path => '#{cookbook_path}'"
213
264
  contents << "cookbook 'minitest-handler'"
214
- # TODO - Customs from kitchen. Customs from root. Customs from cookbook
215
- File.open(File.join(dir, 'Cheffile'), 'w') do |file|
216
- file.write(contents.join("\n"))
265
+ contents.join("\n")
266
+ end
267
+
268
+ def load_cookbooks(l_name, suite_name, dir, platform, runlist, *_args)
269
+ if(File.exists?(File.join(vagabondfile.directory, 'Berksfile')))
270
+ berks_vendor
271
+ else
272
+ librarian_vendor
273
+ end
274
+ end
275
+
276
+ def berks_vendor
277
+ ui.info 'Cookbooks being vendored via berks'
278
+ berk_uploader = Uploader::Berkshelf.new(
279
+ vagabondfile.generate_store_path, options.merge(
280
+ :ui => ui,
281
+ :berksfile => File.join(vagabondfile.directory, 'Berksfile'),
282
+ :chef_server_url => options[:knife_opts].to_s.split(' ').last
283
+ )
284
+ )
285
+ berk_uploader.upload
286
+ end
287
+
288
+ def librarian_vendor
289
+ ui.info 'Cookbooks being vendored with librarian'
290
+ unless(File.exists?(cheffile = File.join(vagabondfile.directory, 'Cheffile')))
291
+ File.open(cheffile = vagabond.generate_store_path, 'w') do |file|
292
+ file.write custom_cheffile
293
+ end
217
294
  end
218
- com = "librarian-chef update"
219
- debug(com)
220
- c = Mixlib::ShellOut.new(com, :live_stream => options[:debug], :cwd => dir)
221
- c.run_command
222
- c.error!
295
+ librarian_uploader = Uploader::Librarian.new(
296
+ vagabondfile.generate_store_path, options.merge(
297
+ :ui => ui,
298
+ :cheffile => cheffile
299
+ )
300
+ )
301
+ librarian_uploader.prepare
223
302
  end
224
303
 
225
304
  def bus_node(v_inst, suite_name)
226
- unless(::Kitchen::Busser::DEFAULT_TEST_ROOT == c_path = File.join(cookbook_path, 'test/integration'))
305
+ test_path = options[:cluster] ? 'test/cluster' : 'test/integration'
306
+ unless(::Kitchen::Busser::DEFAULT_TEST_ROOT == c_path = File.join(cookbook_path, test_path))
227
307
  ::Kitchen::Busser.send(:remove_const, :DEFAULT_TEST_ROOT)
228
- ::Kitchen::Busser.const_set(:DEFAULT_TEST_ROOT, File.join(cookbook_path, 'test/integration'))
308
+ ::Kitchen::Busser.const_set(:DEFAULT_TEST_ROOT, c_path)
229
309
  end
230
310
  busser = ::Kitchen::Busser.new(suite_name)
231
311
  ui.info "#{ui.color('Kitchen:', :bold)} Setting up..."
@@ -236,7 +316,7 @@ module Vagabond
236
316
  end
237
317
  busser
238
318
  end
239
-
319
+
240
320
  def vagabond_instance(action, platform, args={})
241
321
  options[:disable_name_validate] = true
242
322
  v = Vagabond.new
@@ -260,42 +340,42 @@ module Vagabond
260
340
  v
261
341
  end
262
342
 
263
- def load_kitchen_yml
264
- y_path = File.join(
265
- File.dirname(vagabondfile.path), 'cookbooks', name, '.kitchen.yml'
343
+ def load_kitchen_yml(name)
344
+ @kitchen = ::Kitchen::Config.new(
345
+ :kitchen_root => cookbook_path,
346
+ :test_base_path => File.join(cookbook_path, 'test/integration'),
347
+ :loader => ::Kitchen::Loader::YAML.new(
348
+ File.join(cookbook_path, '.kitchen.yml')
349
+ )
266
350
  )
267
- if(File.exists?(y_path))
268
- @kitchen = Mash.new(YAML.load(File.read(y_path)))
269
- else
270
- ui.fatal "Cookbook #{name} does not have a .kitchen.yml file defined!"
271
- ui.info ui.color(" -> Path: #{y_path}", :red)
272
- exit EXIT_CODES[:kitchen_missing_yml]
273
- end
274
351
  end
275
352
 
276
353
  def platform_map
277
- @platform_map ||= Mash.new(Hash[*(
278
- kitchen[:platforms].map do |plat|
354
+ @platform_map ||= Mash[
355
+ *(
356
+ kitchen.platforms.map do |plat|
279
357
  [
280
- plat[:name], Mash.new(
281
- :template => plat[:driver_config][:box].scan(
282
- %r{([^-]+-[^-]+)$}
283
- ).flatten.first.to_s.gsub('.', '').gsub('-', '_'),
284
- :run_list => plat[:run_list],
285
- :attributes => plat[:attributes]
358
+ plat.name, Mash.new(
359
+ :template => plat.name.gsub('.', '').gsub('-', '_'),
360
+ :run_list => plat.run_list,
361
+ :attributes => plat.attributes
286
362
  )
287
363
  ]
288
364
  end.flatten
289
- )])
365
+ )
366
+ ]
290
367
  end
291
368
 
292
369
  def generate_runlist(platform, suite)
370
+ unless(platform_map[platform])
371
+ raise "Invalid platform #{platform}. Valid: #{platform_map.keys.sort.join(', ')}"
372
+ end
293
373
  r = platform_map[platform][:run_list]
294
- kitchen_suite = kitchen[:suites].detect do |k_s|
295
- k_s[:name] == suite
374
+ kitchen_suite = kitchen.suites.detect do |k_s|
375
+ k_s.name == suite
296
376
  end
297
- if(kitchen_suite && kitchen_suite[:run_list])
298
- r |= kitchen_suite[:run_list]
377
+ if(kitchen_suite && kitchen_suite.run_list)
378
+ r |= kitchen_suite.run_list
299
379
  end
300
380
  r.uniq
301
381
  end
@@ -304,7 +384,7 @@ module Vagabond
304
384
  unless(platform_map[plat])
305
385
  ui.fatal "Requested platform does not exist: #{ui.color(plat, :red)}"
306
386
  ui.info " -> Available platforms: #{platform_map.keys.sort.join(', ')}"
307
- exit EXIT_CODES[:kitchen_invalid_platform]
387
+ raise VagabondError::KitchenInvalidPlatform.new(plat)
308
388
  end
309
389
  end
310
390
  end
@@ -14,7 +14,7 @@ module Vagabond
14
14
  def initialize(*args)
15
15
  super
16
16
  end
17
-
17
+
18
18
  desc 'knife COMMAND', 'Run knife commands against local Chef server'
19
19
  def knife(command, *args)
20
20
  @options = options.dup
@@ -27,15 +27,18 @@ module Vagabond
27
27
  if(@vagabondfile[:local_chef_server] && @vagabondfile[:local_chef_server][:enabled])
28
28
  srv = Lxc.new(@internal_config[:mappings][:server])
29
29
  if(srv.running?)
30
- options[:knife_opts] = " -s https://#{srv.container_ip(10, true)}"
30
+ proto = @vagabondfile[:local_chef_server][:zero] ? 'http' : 'https'
31
+ options[:knife_opts] = " --server-url #{proto}://#{srv.container_ip(10, true)}"
31
32
  else
32
33
  options[:knife_opts] = ' -s https://no-local-server'
33
34
  end
34
35
  end
35
36
  end
36
- exec("knife #{[command, args].flatten.compact.join(' ')} #{options[:knife_opts]}")
37
+ command_string = [command, args.map{|s| "'#{s}'"}].flatten.compact.join(' ')
38
+ if(ENV['VAGABOND_KNIFE_DEBUG'])
39
+ puts "knife #{command_string} #{options[:knife_opts]}"
40
+ end
41
+ exec("knife #{command_string} #{options[:knife_opts]}")
37
42
  end
38
-
39
-
40
43
  end
41
44
  end
@@ -0,0 +1,16 @@
1
+ module Vagabond
2
+ class Layout
3
+
4
+ def initialize(base_dir)
5
+ unless(File.exists?(path = File.join(base_dir, 'spec/Layout')))
6
+ raise 'Spec layout file does not exist'
7
+ end
8
+ @l = Mash.new(self.instance_eval(IO.read(path), path, 1))
9
+ end
10
+
11
+ def [](k)
12
+ @l[k]
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ require 'kitchen/config'
2
+
3
+ module Vagabond
4
+ module MonkeyPatch
5
+ module KitchenConfig
6
+ def clusters
7
+ unless(@clusters)
8
+ @clusters = Hash[
9
+ *(
10
+ Array(data[:clusters]).map{ |name, suite_names|
11
+ [name, suite_names]
12
+ }.flatten(1)
13
+ )
14
+ ]
15
+ @clusters = Mash.new(@clusters)
16
+ end
17
+ @clusters
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ Kitchen::Config.send(:include, Vagabond::MonkeyPatch::KitchenConfig)
@@ -18,7 +18,7 @@ module Vagabond
18
18
  super
19
19
  @name = 'server'
20
20
  @base_template = 'ubuntu_1204' # TODO: Make this dynamic
21
- setup(nil, name)
21
+ setup('status')
22
22
  end
23
23
 
24
24
  desc 'server stop', 'Stops the local Chef server'
@@ -49,27 +49,31 @@ module Vagabond
49
49
  desc 'upload_roles', 'Upload all roles'
50
50
  def upload_roles
51
51
  am_uploading('roles') do
52
- com = "knife role from file #{File.join(base_dir, 'roles/*')} #{options[:knife_opts]}"
53
- debug(com)
54
- cmd = Mixlib::ShellOut.new(com, :live_stream => options[:debug])
55
- cmd.run_command
56
- cmd.error!
52
+ if(File.directory?(File.join(base_dir, 'roles')))
53
+ com = "knife role from file #{File.join(base_dir, 'roles/*')} #{options[:knife_opts]}"
54
+ debug(com)
55
+ cmd = Mixlib::ShellOut.new(com, :live_stream => options[:debug])
56
+ cmd.run_command
57
+ cmd.error!
58
+ end
57
59
  end
58
60
  end
59
61
 
60
62
  desc 'upload_databags', 'Upload all data bags'
61
63
  def upload_databags
62
64
  am_uploading('data bags') do
63
- Dir.glob(File.join(base_dir, "data_bags/*")).each do |b|
64
- next if %w(. ..).include?(b)
65
- coms = [
66
- "knife data bag create #{File.basename(b)} #{options[:knife_opts]}",
67
- "knife data bag from file #{File.basename(b)} #{options[:knife_opts]} --all"
68
- ].each do |com|
69
- debug(com)
70
- cmd = Mixlib::ShellOut.new(com, :live_stream => options[:debug])
71
- cmd.run_command
72
- cmd.error!
65
+ if(File.directory?(File.join(base_dir, 'data_bags')))
66
+ Dir.glob(File.join(base_dir, "data_bags/*")).each do |b|
67
+ next if %w(. ..).include?(b)
68
+ coms = [
69
+ "knife data bag create #{File.basename(b)} #{options[:knife_opts]}",
70
+ "knife data bag from file #{File.basename(b)} #{options[:knife_opts]} --all"
71
+ ].each do |com|
72
+ debug(com)
73
+ cmd = Mixlib::ShellOut.new(com, :live_stream => options[:debug])
74
+ cmd.run_command
75
+ cmd.error!
76
+ end
73
77
  end
74
78
  end
75
79
  end
@@ -78,18 +82,22 @@ module Vagabond
78
82
  desc 'upload_environments', 'Upload all environments'
79
83
  def upload_environments
80
84
  am_uploading('environments') do
81
- com = "knife environment from file #{File.join(base_dir, 'environments/*')} #{options[:knife_opts]}"
82
- debug(com)
83
- cmd = Mixlib::ShellOut.new(com, :live_stream => options[:debug])
84
- cmd.run_command
85
- cmd.error!
85
+ if(File.directory?(File.join(base_dir, 'environments')))
86
+ com = "knife environment from file #{File.join(base_dir, 'environments/*')} #{options[:knife_opts]}"
87
+ debug(com)
88
+ cmd = Mixlib::ShellOut.new(com, :live_stream => options[:debug])
89
+ cmd.run_command
90
+ cmd.error!
91
+ end
86
92
  end
87
93
  end
88
94
 
89
95
  desc 'upload_cookbooks', 'Upload all cookbooks'
90
96
  def upload_cookbooks
91
97
  am_uploading('cookbooks') do
92
- if(vagabondfile[:local_chef_server][:berkshelf])
98
+ if(vagabondfile[:local_chef_server][:librarian])
99
+ librarian_upload
100
+ elsif(vagabondfile[:local_chef_server][:berkshelf])
93
101
  berks_upload
94
102
  else
95
103
  raw_upload
@@ -111,64 +119,72 @@ module Vagabond
111
119
  yield
112
120
  ui.info ui.color(" -> UPLOADED #{thing.upcase}", :green)
113
121
  end
114
-
115
- def write_berks_config
116
- path = File.join(vagabond_dir, 'berks.json')
117
- if(File.exists?(path))
118
- cur = Mash.new(JSON.load(File.read(path)))
119
- else
120
- cur = Mash.new
121
- end
122
- url = "https://#{lxc.container_ip(10, true)}"
123
- if(cur[:chef].nil? || cur[:chef][:chef_server_url] != url)
124
- cur[:chef] = Mash.new(:chef_server_url => url)
125
- cur[:ssl] = Mash.new(:verify => false)
126
- File.open(path, 'w') do |file|
127
- file.write(JSON.dump(cur))
128
- end
129
- end
130
- end
131
122
 
132
123
  def do_create
133
- com = "#{options[:sudo]}lxc-clone -n #{generated_name} -o #{@base_template}"
134
- debug(com)
135
- cmd = Mixlib::ShellOut.new(com, :live_stream => options[:debug])
136
- cmd.run_command
137
- cmd.error!
138
124
  @lxc = Lxc.new(generated_name)
125
+ unless(@lxc.exists?)
126
+ com = "#{options[:sudo]}lxc-clone -n #{generated_name} -o #{@base_template}"
127
+ debug(com)
128
+ cmd = Mixlib::ShellOut.new(com, :live_stream => options[:debug])
129
+ cmd.run_command
130
+ cmd.error!
131
+ else
132
+ ui.warn 'Found server instance not if configuration. Adding and moving on.'
133
+ end
139
134
  @internal_config[:mappings][name] = generated_name
140
135
  @internal_config.save
141
136
  ui.info ui.color(' -> Chef Server container created!', :cyan)
142
137
  lxc.start
143
- ui.info ui.color(' -> Bootstrapping erchef...', :cyan)
144
- tem_file = File.expand_path(File.join(File.dirname(__FILE__), 'bootstraps/server.erb'))
145
- com = "#{options[:sudo]}knife bootstrap #{lxc.container_ip(10, true)} --template-file #{tem_file} -i /opt/hw-lxc-config/id_rsa"
146
- debug(com)
147
- cmd = Mixlib::ShellOut.new(com, :live_stream => options[:debug], :timeout => 1200)
148
- cmd.run_command
149
- cmd.error!
150
138
  ui.info ui.color(' -> Chef Server CREATED!', :green)
151
- options[:knife_opts] = " --server-url https://#{lxc.container_ip(20, true)}"
139
+ do_provision
152
140
  auto_upload if vagabondfile[:local_chef_server][:auto_upload]
153
141
  end
154
142
 
155
- def berks_upload
156
- write_berks_config
157
- com = "berks upload -c #{File.join(vagabond_dir, 'berks.json')}"
143
+ def do_provision
144
+ if(vagabondfile[:local_chef_server][:zero])
145
+ ui.info ui.color(' -> Bootstrapping chef-zero...', :cyan)
146
+ tem_file = File.expand_path(File.join(File.dirname(__FILE__), 'bootstraps/server-zero.erb'))
147
+ options[:knife_opts] = " --server-url http://#{lxc.container_ip(20, true)}"
148
+ else
149
+ ui.info ui.color(' -> Bootstrapping erchef...', :cyan)
150
+ tem_file = File.expand_path(File.join(File.dirname(__FILE__), 'bootstraps/server.erb'))
151
+ options[:knife_opts] = " --server-url https://#{lxc.container_ip(20, true)}"
152
+ end
153
+ com = "#{options[:sudo]}knife bootstrap #{lxc.container_ip(10, true)} --template-file #{tem_file} -i /opt/hw-lxc-config/id_rsa"
154
+ cmd = Mixlib::ShellOut.new(com, :live_stream => options[:debug], :timeout => 1200)
158
155
  debug(com)
159
- cmd = Mixlib::ShellOut.new(com, :live_stream => options[:debug])
160
156
  cmd.run_command
161
157
  cmd.error!
162
- ui.info "Berks cookbook upload complete!"
158
+ end
159
+
160
+ def berks_upload
161
+ ui.info 'Cookbooks being uploaded via berks'
162
+ berk_uploader = Uploader::Berkshelf.new(
163
+ vagabondfile.directory, options.merge(
164
+ :ui => ui,
165
+ :berksfile => File.join(vagabondfile.directory, vagabondfile[:local_chef_server][:berkshelf][:path] || 'Berksfile'),
166
+ :chef_server_url => options[:knife_opts].to_s.split(' ').last,
167
+ :berks_opts => vagabondfile[:local_chef_server][:berkshelf][:options]
168
+ )
169
+ )
170
+ berk_uploader.upload
163
171
  end
164
172
 
173
+ def librarian_upload
174
+ ui.info 'Cookbooks being uploaded with librarian'
175
+ librarian_uploader = Uploader::Librarian.new(
176
+ vagabondfile.generate_store_path, options.merge(
177
+ :ui => ui,
178
+ :cheffile => File.join(vagabondfile.directory, 'Cheffile')
179
+ )
180
+ )
181
+ librarian_uploader.upload
182
+ end
183
+
165
184
  def raw_upload
166
- com = "knife cookbook upload#{options[:knife_opts]} --all"
167
- debug(com)
168
- cmd = Mixlib::ShellOut.new(com, :live_stream => options[:debug])
169
- cmd.run_command
170
- cmd.error!
171
- ui.info "Cookbook upload complete!"
185
+ ui.info 'Cookbooks being uploaded via knife'
186
+ knife_uploader = Uploader::Knife.new(vagabondfile.directory, options.merge(:ui => ui))
187
+ knife_uploader.upload
172
188
  end
173
189
 
174
190
  end