Class: Vagrant::Provisioners::ChefSolo

Inherits:
Chef
  • Object
show all
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

Attributes inherited from Base

#action_env, #config

Instance Method Summary collapse

Methods included from Util::Counter

get_and_update_counter, mutex

Methods inherited from Chef

#chef_binary_path, #chown_provisioning_folder, #initialize, #setup_config, #setup_json, #verify_binary

Methods inherited from Base

#cleanup, #env, #initialize, register, registered, #vm

Constructor Details

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

Instance Attribute Details

#cookbook_foldersObject (readonly)

Returns the value of attribute cookbook_folders.



34
35
36
# File 'lib/vagrant/provisioners/chef_solo.rb', line 34

def cookbook_folders
  @cookbook_folders
end

#data_bags_foldersObject (readonly)

Returns the value of attribute data_bags_folders.



36
37
38
# File 'lib/vagrant/provisioners/chef_solo.rb', line 36

def data_bags_folders
  @data_bags_folders
end

#role_foldersObject (readonly)

Returns the value of attribute role_folders.



35
36
37
# File 'lib/vagrant/provisioners/chef_solo.rb', line 35

def role_folders
  @role_folders
end

Instance Method Details

#expanded_folders(paths) ⇒ Object

Converts paths to a list of properly expanded paths with types.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/vagrant/provisioners/chef_solo.rb', line 57

def expanded_folders(paths)
  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)

  paths.map 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
    local_path = File.expand_path(path, env.root_path) if type == :host
    remote_path = nil
    if type == :host
      # 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.expand_path(path, config.provisioning_path)
    end

    # Return the result
    [type, local_path, remote_path]
  end
end

#guest_paths(folders) ⇒ Object

Extracts only the remote paths from a list of folders



130
131
132
# File 'lib/vagrant/provisioners/chef_solo.rb', line 130

def guest_paths(folders)
  folders.map { |parts| parts[2] }
end

#prepareObject



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

def prepare
  @cookbook_folders = expanded_folders(config.cookbooks_path)
  @role_folders = expanded_folders(config.roles_path)
  @data_bags_folders = expanded_folders(config.data_bags_path)

  share_folders("csc", @cookbook_folders)
  share_folders("csr", @role_folders)
  share_folders("csdb", @data_bags_folders)
end

#provision!Object



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

def provision!
  verify_binary(chef_binary_path("chef-solo"))
  chown_provisioning_folder
  setup_json
  setup_solo_config
  run_chef_solo
end

#run_chef_soloObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/vagrant/provisioners/chef_solo.rb', line 113

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"

  env.ui.info I18n.t("vagrant.provisioners.chef.running_solo")
  vm.ssh.execute do |ssh|
    ssh.sudo!(command) do |channel, type, data|
      if type == :exit_status
        ssh.check_exit_status(data, command)
      else
        env.ui.info("#{data}: #{type}")
      end
    end
  end
end

#setup_solo_configObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/vagrant/provisioners/chef_solo.rb', line 98

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("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.



89
90
91
92
93
94
95
96
# File 'lib/vagrant/provisioners/chef_solo.rb', line 89

def share_folders(prefix, folders)
  folders.each do |type, local_path, remote_path|
    if type == :host
      env.config.vm.share_folder("v-#{prefix}-#{self.class.get_and_update_counter(:shared_folder)}",
                                 remote_path, local_path, :nfs => config.nfs)
    end
  end
end