Module: WGU::ChefComms

Defined in:
lib/pps_commons/chef_comms.rb

Class Method Summary collapse

Class Method Details

.client(config) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/pps_commons/chef_comms.rb', line 28

def self.client(config)
  client = ChefAPI::Connection.new(
      client:   ENV['CM_CHEF_API_CLIENT'],
      endpoint: config[:endpoint],
      key:      config[:key]
    )

    block_given? ? yield(client) : client
end

.client_configs(*given) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/pps_commons/chef_comms.rb', line 44

def self.client_configs(*given)
  endpoints = ENV.keys.select { |k| k =~ /^CM_CHEF_API_ENDPOINT/ }

  endpoints.map do |endpoint|
    chef_env = endpoint.gsub(/CM_CHEF_API_ENDPOINT_(\w+)$/, '\1')
    key_file = ENV["CM_CHEF_API_KEY_#{chef_env}"]
    { endpoint: ENV[endpoint], key: ::File.expand_path(key_file) }
  end
end

.clientsObject



38
39
40
41
42
# File 'lib/pps_commons/chef_comms.rb', line 38

def self.clients
  self.client_configs.map do |config|
    block_given? ? yield(self.client(config)) : self.client(config)
  end
end

.global_config(client, group_name, message) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pps_commons/chef_comms.rb', line 5

def self.global_config(client, group_name, message)
  # search through global config indexes for a match
  config =
    client.data_bags.find { |d_b| d_b.name =~ /#{group_name}/ }

  if config.nil?
    config = client.roles.fetch(group_name)

    unless config.nil?
      stop = Regexp.union([/for\s/, /with\s/, /using\s/])
      location =
        message.gsub(/.*\sdata\sstore\sunder\s(\w+)\s#{stop}.*/, '\1')
      usable_role =
        config.default_attributes[group_name][location].key?('java_opts')
    end

    usable_role ? [config, 'role'] : []
  else
    [config, 'data bag']
  end
end