Module: VagrantPlugins::ChefZero::EnvHelpers

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.active_machines(env) ⇒ Object



10
11
12
# File 'lib/vagrant-chef-zero/env_helpers.rb', line 10

def self.active_machines(env)
  @active ||= env[:machine].env.active_machines
end

.mutexObject



5
6
7
8
# File 'lib/vagrant-chef-zero/env_helpers.rb', line 5

def self.mutex
  # handle actions that may run in parallel with certain providers
  @mutex ||= Mutex.new
end

Instance Method Details

#berkshelf_enabled?(env) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/vagrant-chef-zero/env_helpers.rb', line 119

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

#chef_client?(env) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#chef_zero_enabled?(env) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#get_chef_server_url(env) ⇒ Object



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

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



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

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



89
90
91
92
93
94
95
# File 'lib/vagrant-chef-zero/env_helpers.rb', line 89

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



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

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



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/vagrant-chef-zero/env_helpers.rb', line 97

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



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

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



46
47
48
49
50
# File 'lib/vagrant-chef-zero/env_helpers.rb', line 46

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

#rm_knife_config(env) ⇒ Object



24
25
26
27
# File 'lib/vagrant-chef-zero/env_helpers.rb', line 24

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



29
30
31
32
33
34
35
36
37
38
# File 'lib/vagrant-chef-zero/env_helpers.rb', line 29

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



123
124
125
126
127
# File 'lib/vagrant-chef-zero/env_helpers.rb', line 123

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



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

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



14
15
16
17
18
19
20
21
22
# File 'lib/vagrant-chef-zero/env_helpers.rb', line 14

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