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

#action_env, #config

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, #env, #initialize, register, registered, #vm

Constructor Details

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

Instance Method Details

#create_client_key_folderObject



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

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

  vm.ssh.execute do |ssh|
    ssh.sudo!("mkdir -p #{path.dirname}")
  end
end

#encrypted_data_bag_secret_key_pathObject



111
112
113
# File 'lib/vagrant/provisioners/chef_client.rb', line 111

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



115
116
117
# File 'lib/vagrant/provisioners/chef_client.rb', line 115

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

#prepareObject

Raises:



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

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



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

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# 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"

  env.ui.info I18n.t("vagrant.provisioners.chef.running_client")
  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_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("chef_server_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



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

def upload_encrypted_data_bag_secret
  env.ui.info I18n.t("vagrant.provisioners.chef.upload_encrypted_data_bag_secret_key")
  vm.ssh.upload!(encrypted_data_bag_secret_key_path, config.encrypted_data_bag_secret)
end

#upload_validation_keyObject



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

def upload_validation_key
  env.ui.info I18n.t("vagrant.provisioners.chef.upload_validation_key")
  vm.ssh.upload!(validation_key_path, guest_validation_key_path)
end

#validation_key_pathObject



107
108
109
# File 'lib/vagrant/provisioners/chef_client.rb', line 107

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