Class: Vagrant::Provisioners::ChefSolo

Inherits:
Chef
  • Object
show all
Defined in:
lib/vagrant/provisioners/chef_solo.rb

Overview

This class implements provisioning via chef-solo.

Instance Attribute Summary

Attributes inherited from Base

#env

Instance Method Summary collapse

Methods inherited from Chef

#chown_provisioning_folder, #setup_json

Methods inherited from Base

#initialize

Methods included from Util

#error_and_exit, included, #logger, #wrap_output

Constructor Details

This class inherits a constructor from Vagrant::Provisioners::Base

Instance Method Details

#cookbook_path(i) ⇒ Object



56
57
58
# File 'lib/vagrant/provisioners/chef_solo.rb', line 56

def cookbook_path(i)
  full_provisioning_path "cookbooks-#{i}"
end

#cookbooks_pathObject



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/vagrant/provisioners/chef_solo.rb', line 68

def cookbooks_path
  result = []
  host_cookbook_paths.each_with_index do |host_path, i|
    result << cookbook_path(i)
  end

  # We're lucky that ruby's string and array syntax for strings is the
  # same as JSON, so we can just convert to JSON here and use that
  result = result.to_s if result.length == 1
  result.to_json
end

#host_cookbook_pathsObject



49
50
51
52
53
54
# File 'lib/vagrant/provisioners/chef_solo.rb', line 49

def host_cookbook_paths
  cookbooks = env.config.chef.cookbooks_path
  cookbooks = [cookbooks] unless cookbooks.is_a?(Array)
  cookbooks.collect! { |cookbook| full_env_path(cookbook)}
  return cookbooks
end

#host_role_pathObject



60
61
62
# File 'lib/vagrant/provisioners/chef_solo.rb', line 60

def host_role_path
  env.config.chef.role_path
end

#prepareObject



5
6
7
8
# File 'lib/vagrant/provisioners/chef_solo.rb', line 5

def prepare
  share_cookbook_folders
  share_role_folder
end

#provision!Object



10
11
12
13
14
15
# File 'lib/vagrant/provisioners/chef_solo.rb', line 10

def provision!
  chown_provisioning_folder
  setup_json
  setup_solo_config
  run_chef_solo
end

#role_pathObject



64
65
66
# File 'lib/vagrant/provisioners/chef_solo.rb', line 64

def role_path
  guest_role_path.to_json
end

#run_chef_soloObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/vagrant/provisioners/chef_solo.rb', line 38

def run_chef_solo
  logger.info "Running chef-solo..."
  env.ssh.execute do |ssh|
    ssh.exec!("cd #{env.config.chef.provisioning_path} && sudo chef-solo -c solo.rb -j dna.json") do |channel, data, stream|
      # TODO: Very verbose. It would be easier to save the data and only show it during
      # an error, or when verbosity level is set high
      logger.info("#{stream}: #{data}")
    end
  end
end

#setup_solo_configObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/vagrant/provisioners/chef_solo.rb', line 27

def setup_solo_config
  solo_file = <<-solo
file_cache_path "#{env.config.chef.provisioning_path}"
cookbook_path #{cookbooks_path}
solo
  solo_file << "role_path #{role_path}" if host_role_path

  logger.info "Uploading chef-solo configuration script..."
  env.ssh.upload!(StringIO.new(solo_file), File.join(env.config.chef.provisioning_path, "solo.rb"))
end

#share_cookbook_foldersObject



17
18
19
20
21
# File 'lib/vagrant/provisioners/chef_solo.rb', line 17

def share_cookbook_folders
  host_cookbook_paths.each_with_index do |cookbook, i|
    env.config.vm.share_folder("vagrant-chef-solo-#{i}", cookbook_path(i), cookbook)
  end
end

#share_role_folderObject



23
24
25
# File 'lib/vagrant/provisioners/chef_solo.rb', line 23

def share_role_folder
    env.config.vm.share_folder("vagrant-chef-solo-roles", guest_role_path, host_role_path) if host_role_path
end