Module: WGU::ChefComms

Defined in:
lib/pps_commons/chef_comms.rb

Class Method Summary collapse

Class Method Details

.cache_clientObject



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

def self.cache_client
  @cache_client ||= Aws::S3::Client.new(
    region:             ENV['AWS_REGION'],
    access_key_id:      ENV['ACCESS_KEY_ID'],
    secret_access_key:  ENV['SECRET_ACCESS_KEY']
  )
end

.client(config) ⇒ Object



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

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/pps_commons/chef_comms.rb', line 52

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 = self.cache_client.get_object(
      bucket: ENV['PPS_PASS_CACHE'],
      key: ENV["CM_CHEF_API_KEY_#{chef_env}"]
    )

    {
      endpoint: ENV[endpoint],
      key: key
    }
  end
end

.clientsObject



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

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