Class: Vagrant::Provisioners::ChefSolo
- Extended by:
- Util::Counter
- Includes:
- Util::Counter
- Defined in:
- lib/vagrant/provisioners/chef_solo.rb
Overview
This class implements provisioning via chef-solo.
Defined Under Namespace
Classes: Config
Instance Attribute Summary collapse
-
#cookbook_folders ⇒ Object
readonly
Returns the value of attribute cookbook_folders.
-
#data_bags_folders ⇒ Object
readonly
Returns the value of attribute data_bags_folders.
-
#role_folders ⇒ Object
readonly
Returns the value of attribute role_folders.
Attributes inherited from Base
Class Method Summary collapse
Instance Method Summary collapse
-
#expanded_folders(paths, appended_folder = nil) ⇒ Object
Converts paths to a list of properly expanded paths with types.
-
#initialize(env, config) ⇒ ChefSolo
constructor
A new instance of ChefSolo.
- #prepare ⇒ Object
- #provision! ⇒ Object
- #run_chef_solo ⇒ Object
- #setup_solo_config ⇒ Object
-
#share_folders(prefix, folders) ⇒ Object
Shares the given folders with the given prefix.
- #verify_shared_folders(folders) ⇒ Object
Methods included from Util::Counter
Methods inherited from Chef
#chef_binary_path, #chown_provisioning_folder, #setup_config, #setup_json, #verify_binary
Methods inherited from Base
Constructor Details
#initialize(env, config) ⇒ ChefSolo
Returns a new instance of ChefSolo.
58 59 60 61 |
# File 'lib/vagrant/provisioners/chef_solo.rb', line 58 def initialize(env, config) super @logger = Log4r::Logger.new("vagrant::provisioners::chef_solo") end |
Instance Attribute Details
#cookbook_folders ⇒ Object (readonly)
Returns the value of attribute cookbook_folders.
50 51 52 |
# File 'lib/vagrant/provisioners/chef_solo.rb', line 50 def cookbook_folders @cookbook_folders end |
#data_bags_folders ⇒ Object (readonly)
Returns the value of attribute data_bags_folders.
52 53 54 |
# File 'lib/vagrant/provisioners/chef_solo.rb', line 52 def data_bags_folders @data_bags_folders end |
#role_folders ⇒ Object (readonly)
Returns the value of attribute role_folders.
51 52 53 |
# File 'lib/vagrant/provisioners/chef_solo.rb', line 51 def role_folders @role_folders end |
Class Method Details
.config_class ⇒ Object
54 55 56 |
# File 'lib/vagrant/provisioners/chef_solo.rb', line 54 def self.config_class Config end |
Instance Method Details
#expanded_folders(paths, appended_folder = nil) ⇒ Object
Converts paths to a list of properly expanded paths with types.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/vagrant/provisioners/chef_solo.rb', line 95 def (paths, appended_folder=nil) return [] if paths.nil? # Convert the path to an array if it is a string or just a single # path element which contains the folder location (:host or :vm) paths = [paths] if paths.is_a?(String) || paths.first.is_a?(Symbol) results = [] paths.each do |path| path = [:host, path] if !path.is_a?(Array) type, path = path # Create the local/remote path based on whether this is a host # or VM path. local_path = nil remote_path = nil if type == :host # Get the expanded path that the host path points to local_path = File.(path, env[:root_path]) # Super hacky but if we're expanded the default cookbook paths, # and one of the host paths doesn't exist, then just ignore it, # because that is fine. if paths.equal?(config._default_cookbook_path) && !File.directory?(local_path) @logger.info("'cookbooks' folder doesn't exist on defaults. Ignoring.") next end # Path exists on the host, setup the remote path remote_path = "#{config.provisioning_path}/chef-solo-#{get_and_update_counter(:cookbooks_path)}" else # Path already exists on the virtual machine. Expand it # relative to where we're provisioning. remote_path = File.(path, config.provisioning_path) # Remove drive letter if running on a windows host. This is a bit # of a hack but is the most portable way I can think of at the moment # to achieve this. Otherwise, Vagrant attempts to share at some crazy # path like /home/vagrant/c:/foo/bar remote_path = remote_path.gsub(/^[a-zA-Z]:/, "") end # If we have specified a folder name to append then append it remote_path += "/#{appended_folder}" if appended_folder # Append the result results << [type, local_path, remote_path] end results end |
#prepare ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/vagrant/provisioners/chef_solo.rb', line 63 def prepare @cookbook_folders = (config.cookbooks_path, "cookbooks") @role_folders = (config.roles_path, "roles") @data_bags_folders = (config.data_bags_path, "data_bags") share_folders("csc", @cookbook_folders) share_folders("csr", @role_folders) share_folders("csdb", @data_bags_folders) end |
#provision! ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/vagrant/provisioners/chef_solo.rb', line 73 def provision! # Verify that the proper shared folders exist. check = [] [@cookbook_folders, @role_folders, @data_bags_folders].each do |folders| folders.each do |type, local_path, remote_path| # We only care about checking folders that have a local path, meaning # they were shared from the local machine, rather than assumed to # exist on the VM. check << remote_path if local_path end end verify_shared_folders(check) verify_binary(chef_binary_path("chef-solo")) chown_provisioning_folder setup_json setup_solo_config run_chef_solo end |
#run_chef_solo ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/vagrant/provisioners/chef_solo.rb', line 173 def run_chef_solo command_env = config.binary_env ? "#{config.binary_env} " : "" command = "#{command_env}#{chef_binary_path("chef-solo")} -c #{config.provisioning_path}/solo.rb -j #{config.provisioning_path}/dna.json" config.attempts.times do |attempt| if attempt == 0 env[:ui].info I18n.t("vagrant.provisioners.chef.running_solo") else env[:ui].info I18n.t("vagrant.provisioners.chef.running_solo_again") end exit_status = env[:vm].channel.sudo(command, :error_check => false) do |type, data| # Output the data with the proper color based on the stream. color = type == :stdout ? :green : :red # Note: Be sure to chomp the data to avoid the newlines that the # Chef outputs. env[:ui].info(data.chomp, :color => color, :prefix => false) end # There is no need to run Chef again if it converges return if exit_status == 0 end # If we reached this point then Chef never converged! Error. raise ChefError, :no_convergence end |
#setup_solo_config ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/vagrant/provisioners/chef_solo.rb', line 158 def setup_solo_config cookbooks_path = guest_paths(@cookbook_folders) roles_path = guest_paths(@role_folders).first data_bags_path = guest_paths(@data_bags_folders).first setup_config("provisioners/chef_solo/solo", "solo.rb", { :node_name => config.node_name, :provisioning_path => config.provisioning_path, :cookbooks_path => cookbooks_path, :recipe_url => config.recipe_url, :roles_path => roles_path, :data_bags_path => data_bags_path, }) end |
#share_folders(prefix, folders) ⇒ Object
Shares the given folders with the given prefix. The folders should
be of the structure resulting from the expanded_folders
function.
149 150 151 152 153 154 155 156 |
# File 'lib/vagrant/provisioners/chef_solo.rb', line 149 def share_folders(prefix, folders) folders.each do |type, local_path, remote_path| if type == :host env[:vm].config.vm.share_folder("v-#{prefix}-#{self.class.get_and_update_counter(:shared_folder)}", remote_path, local_path, :nfs => config.nfs) end end end |
#verify_shared_folders(folders) ⇒ Object
201 202 203 204 205 206 207 208 |
# File 'lib/vagrant/provisioners/chef_solo.rb', line 201 def verify_shared_folders(folders) folders.each do |folder| @logger.debug("Checking for shared folder: #{folder}") if !env[:vm].channel.test("test -d #{folder}") raise ChefError, :missing_shared_folders end end end |