Module: VagrantPlugins::ChefZero::EnvHelpers

Included in:
Action::Reconfig, Action::Start, Action::Stop, Action::Upload
Defined in:
lib/vagrant-chef-zero/env_helpers.rb

Instance Method Summary collapse

Instance Method Details

#berkshelf_enabled?(env) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/vagrant-chef-zero/env_helpers.rb', line 108

def berkshelf_enabled?(env)
  env[:global_config].berkshelf.enabled == true && chef_client?(env)
end

#chef_client?(env) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/vagrant-chef-zero/env_helpers.rb', line 100

def chef_client?(env)
  provisioners(:chef_client, env).any?
end

#chef_zero_enabled?(env) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/vagrant-chef-zero/env_helpers.rb', line 104

def chef_zero_enabled?(env)
  env[:global_config].chef_zero.enabled && chef_client?(env)
end

#get_chef_server_url(env) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/vagrant-chef-zero/env_helpers.rb', line 49

def get_chef_server_url(env)
  current_chef_server_url = server_info(env)[:host]
  if ! current_chef_server_url || current_chef_server_url.empty?
    require 'socket'
    ip_address = Socket.ip_address_list.detect{|intf| intf.ipv4_private?}.ip_address
    current_chef_server_url = "http://#{ip_address}:4000"
  end
  current_chef_server_url
end

#get_fake_key_pathObject



67
68
69
70
71
72
73
74
75
76
# File 'lib/vagrant-chef-zero/env_helpers.rb', line 67

def get_fake_key_path
  require 'openssl'
  require 'tmpdir'
  fake_key = OpenSSL::PKey::RSA.new(2048)
  fake_directory = File.join(Dir.tmpdir, "fake_key")
  fake_key_path = File.join(fake_directory, "fake.pem")
  Dir.mkdir(fake_directory) unless File.exists?(fake_directory)
  File.open(fake_key_path,"w") {|f| f.puts fake_key } unless File.exists?(fake_key_path)
  fake_key_path
end

#get_host(env) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/vagrant-chef-zero/env_helpers.rb', line 78

def get_host(env)
  url = server_info(env)[:host]
  if url
    # Some terrible string manipulation to get the ip
    return url.split('//').last.split(':').first
  end
end

#get_key_path(env) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/vagrant-chef-zero/env_helpers.rb', line 59

def get_key_path(env)
  current_key_path = server_info(env)[:client_key]
  if ! current_key_path || ! ::File.exists?(current_key_path)
    current_key_path = get_fake_key_path
  end
  current_key_path
end

#get_port(env) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/vagrant-chef-zero/env_helpers.rb', line 86

def get_port(env)
  url = server_info(env)[:host]
  # Same with the port
  if url
    p = url.split(':').last
    if p && p != ""
      port = p
    end
  else
    port = "4000"
  end
  port
end

#get_validation_client_name(env) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/vagrant-chef-zero/env_helpers.rb', line 41

def get_validation_client_name(env)
  current_client_name = server_info(env)[:client_name]
  if ! current_client_name || current_client_name.empty?
    current_client_name = "dummy-validator"
  end
  current_client_name
end

#provisioners(name, env) ⇒ Object



37
38
39
# File 'lib/vagrant-chef-zero/env_helpers.rb', line 37

def provisioners(name, env)
  env[:machine].config.vm.provisioners.select { |prov| prov.name == name }
end

#rm_knife_config(env) ⇒ Object



15
16
17
18
# File 'lib/vagrant-chef-zero/env_helpers.rb', line 15

def rm_knife_config(env)
  File.unlink "#{env[:root_path]}/.zero-knife.rb" if File.exists? "#{env[:root_path]}/.zero-knife.rb"
  File.unlink get_key_path(env) if File.exists? get_key_path(env)
end

#server_info(env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/vagrant-chef-zero/env_helpers.rb', line 20

def server_info(env)
  dict = { host: nil, client_name: nil, client_key: nil }
  provisioners(:chef_client, env).each do |provisioner|
    host = provisioner.config.chef_server_url ||= nil
    client_name = provisioner.config.validation_client_name ||= nil
    client_key = provisioner.config.validation_key_path ||= nil
    dict = { host: host, client_name: client_name, client_key: client_key }
  end
  dict
end

#set_berkshelf_client_key(value) ⇒ Object



112
113
114
115
116
# File 'lib/vagrant-chef-zero/env_helpers.rb', line 112

def set_berkshelf_client_key(value)
  ObjectSpace.each_object(Berkshelf::Config).each do |o|
    o.chef.client_key = value
  end
end

#set_config(config_field, new_value, env) ⇒ Object



31
32
33
34
35
# File 'lib/vagrant-chef-zero/env_helpers.rb', line 31

def set_config(config_field, new_value, env)
  provisioners(:chef_client, env).each do |provisioner|
    provisioner.config.instance_variable_set(config_field, new_value)
  end
end

#write_knife_config(env) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/vagrant-chef-zero/env_helpers.rb', line 5

def write_knife_config(env)
  File.open("#{env[:root_path]}/.zero-knife.rb", 'w') do |f|
    f.puts <<-EOF
      chef_server_url '#{get_chef_server_url(env)}'
      node_name 'zero-host'
      client_key '#{get_key_path(env)}'
    EOF
  end
end