module WGU::ChefComms

Public Class Methods

global_config(client, group_name, message) click to toggle source
# 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

Private Class Methods

cache_client() click to toggle source
# 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) { |client| ... } click to toggle source
# 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) click to toggle source
# 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}"]
    ).body.read

    {
      endpoint: ENV[endpoint],
      key: key
    }
  end
end
clients() { |client| ... } click to toggle source
# 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