Class: Vagrant::Provisioners::ChefServer

Inherits:
Chef
  • Object
show all
Defined in:
lib/vagrant/provisioners/chef_server.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

#chown_provisioning_folder, #setup_config, #setup_json, #verify_binary

Methods inherited from Base

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

Constructor Details

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

Instance Method Details

#create_client_key_folderObject



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

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

#guest_validation_key_pathObject



92
93
94
# File 'lib/vagrant/provisioners/chef_server.rb', line 92

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

#prepareObject

Raises:



32
33
34
35
36
# File 'lib/vagrant/provisioners/chef_server.rb', line 32

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



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

def provision!
  verify_binary("chef-client")
  chown_provisioning_folder
  create_client_key_folder
  upload_validation_key
  setup_json
  setup_server_config
  run_chef_client
end

#run_chef_clientObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/vagrant/provisioners/chef_server.rb', line 72

def run_chef_client
  commands = ["cd #{config.provisioning_path}",
              "chef-client -c client.rb -j dna.json"]

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

#setup_server_configObject



62
63
64
65
66
67
68
69
70
# File 'lib/vagrant/provisioners/chef_server.rb', line 62

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
  })
end

#upload_validation_keyObject



57
58
59
60
# File 'lib/vagrant/provisioners/chef_server.rb', line 57

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



88
89
90
# File 'lib/vagrant/provisioners/chef_server.rb', line 88

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