class Bcome::Node::MetaDataLoader

Constants

META_DATA_FILE_PATH_PREFIX

Attributes

decryption_key[RW]

Public Class Methods

new() click to toggle source
# File lib/objects/node/meta_data_loader.rb, line 9
def initialize
  @all_metadata_filenames = Dir["#{META_DATA_FILE_PATH_PREFIX}/*"]
end

Public Instance Methods

data() click to toggle source
# File lib/objects/node/meta_data_loader.rb, line 15
def data
  @data ||= do_load
end
data_for_namespace(namespace) click to toggle source
# File lib/objects/node/meta_data_loader.rb, line 19
def data_for_namespace(namespace)
  static_data = data[namespace.to_sym] || {}
  static_data.merge(terraform_data_for_namespace(namespace))
end
do_load() click to toggle source
# File lib/objects/node/meta_data_loader.rb, line 58
def do_load
  all_meta_data = {}
  @all_metadata_filenames.each do |filepath|
    next if filepath =~ /-unenc/ # we only read from the encrypted, packed files.

    begin
      filedata = load_file_data_for(filepath)
      all_meta_data.deep_merge!(filedata) if filedata.is_a?(Hash)
    rescue Psych::SyntaxError => e
      raise Bcome::Exception::InvalidMetaDataConfig, "Error: #{e.message}"
    end
  end
  all_meta_data
end
load_file_data_for(filepath) click to toggle source
# File lib/objects/node/meta_data_loader.rb, line 40
def load_file_data_for(filepath)
  if filepath =~ /.enc/ # encrypted file contents
    prompt_for_decryption_key unless decryption_key
    encrypted_contents = File.read(filepath)
    decrypted_contents = encrypted_contents.decrypt(decryption_key)

    begin
      YAML.safe_load(decrypted_contents, [Symbol], [], true)
    rescue Exception => e
      @decryption_key = nil
      raise ::Bcome::Exception::InvalidMetaDataConfig, "#{e.class} #{e.message} - " + decrypted_contents
    end

  else # unencrypted
    YAML.load_file(filepath)
  end
end
prompt_for_decryption_key() click to toggle source
# File lib/objects/node/meta_data_loader.rb, line 32
def prompt_for_decryption_key
  decryption_key_prompt = 'Enter your Metadata key: '.informational

  print "\n#{decryption_key_prompt}"
  @decryption_key = STDIN.noecho(&:gets).chomp
  print "\r#{decryption_key_prompt}" + "**********\n\n"
end
terraform_data_for_namespace(namespace) click to toggle source
# File lib/objects/node/meta_data_loader.rb, line 24
def terraform_data_for_namespace(namespace)
  # Radical departure II - all we care is the outputs. This will then work across any terraform backend, and any version
  tf_state = ::Bcome::Terraform::Output.new(namespace)
  terraform_data = {}
  terraform_data['terraform_outputs'] = tf_state.output
  terraform_data
end