Class: Vagrant::Provisioners::ChefClient

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

Overview

This class implements provisioning via chef-client, allowing provisioning with a chef server.

Defined Under Namespace

Classes: Config

Instance Attribute Summary

Attributes inherited from Base

#config, #env

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Chef

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

Methods included from Util::Counter

#get_and_update_counter, #mutex

Methods inherited from Base

#cleanup, #initialize

Constructor Details

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

Class Method Details

.config_classObject



38
39
40
# File 'lib/vagrant/provisioners/chef_client.rb', line 38

def self.config_class
  Config
end

Instance Method Details

#create_client_key_folderObject



59
60
61
62
63
64
# File 'lib/vagrant/provisioners/chef_client.rb', line 59

def create_client_key_folder
  env[:ui].info I18n.t("vagrant.provisioners.chef.client_key_folder")
  path = Pathname.new(config.client_key_path)

  env[:vm].channel.sudo("mkdir -p #{path.dirname}")
end

#encrypted_data_bag_secret_key_pathObject



123
124
125
# File 'lib/vagrant/provisioners/chef_client.rb', line 123

def encrypted_data_bag_secret_key_path
  File.expand_path(config.encrypted_data_bag_secret_key_path, env[:root_path])
end

#guest_validation_key_pathObject



127
128
129
# File 'lib/vagrant/provisioners/chef_client.rb', line 127

def guest_validation_key_path
  File.join(config.provisioning_path, "validation.pem")
end

#prepareObject

Raises:



42
43
44
45
46
# File 'lib/vagrant/provisioners/chef_client.rb', line 42

def prepare
  raise ChefError, :server_validation_key_required if config.validation_key_path.nil?
  raise ChefError, :server_validation_key_doesnt_exist if !File.file?(validation_key_path)
  raise ChefError, :server_url_required if config.chef_server_url.nil?
end

#provision!Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/vagrant/provisioners/chef_client.rb', line 48

def provision!
  verify_binary(chef_binary_path("chef-client"))
  chown_provisioning_folder
  create_client_key_folder
  upload_validation_key
  upload_encrypted_data_bag_secret if config.encrypted_data_bag_secret_key_path
  setup_json
  setup_server_config
  run_chef_client
end

#run_chef_clientObject

Raises:



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/vagrant/provisioners/chef_client.rb', line 91

def run_chef_client
  command_env = config.binary_env ? "#{config.binary_env} " : ""
  command = "#{command_env}#{chef_binary_path("chef-client")} -c #{config.provisioning_path}/client.rb -j #{config.provisioning_path}/dna.json"

  config.attempts.times do |attempt|
    if attempt == 0
      env[:ui].info I18n.t("vagrant.provisioners.chef.running_client")
    else
      env[:ui].info I18n.t("vagrant.provisioners.chef.running_client_again")
    end

    exit_status = env[:vm].channel.sudo(command) 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_server_configObject



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/vagrant/provisioners/chef_client.rb', line 77

def setup_server_config
  setup_config("provisioners/chef_client/client", "client.rb", {
    :node_name => config.node_name,
    :chef_server_url => config.chef_server_url,
    :validation_client_name => config.validation_client_name,
    :validation_key => guest_validation_key_path,
    :client_key => config.client_key_path,
    :file_cache_path => config.file_cache_path,
    :file_backup_path => config.file_backup_path,
    :environment => config.environment,
    :encrypted_data_bag_secret => config.encrypted_data_bag_secret
  })
end

#upload_encrypted_data_bag_secretObject



71
72
73
74
75
# File 'lib/vagrant/provisioners/chef_client.rb', line 71

def upload_encrypted_data_bag_secret
  env[:ui].info I18n.t("vagrant.provisioners.chef.upload_encrypted_data_bag_secret_key")
  env[:vm].channel.upload(encrypted_data_bag_secret_key_path,
                          config.encrypted_data_bag_secret)
end

#upload_validation_keyObject



66
67
68
69
# File 'lib/vagrant/provisioners/chef_client.rb', line 66

def upload_validation_key
  env[:ui].info I18n.t("vagrant.provisioners.chef.upload_validation_key")
  env[:vm].channel.upload(validation_key_path, guest_validation_key_path)
end

#validation_key_pathObject



119
120
121
# File 'lib/vagrant/provisioners/chef_client.rb', line 119

def validation_key_path
  File.expand_path(config.validation_key_path, env[:root_path])
end