Class: Bcome::Node::MetaDataLoader

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/objects/node/meta_data_loader.rb

Constant Summary collapse

META_DATA_FILE_PATH_PREFIX =
'bcome/metadata'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMetaDataLoader

Returns a new instance of MetaDataLoader.



9
10
11
# File 'lib/objects/node/meta_data_loader.rb', line 9

def initialize
  @all_metadata_filenames = Dir["#{META_DATA_FILE_PATH_PREFIX}/*"]
end

Instance Attribute Details

#decryption_keyObject

Returns the value of attribute decryption_key.



13
14
15
# File 'lib/objects/node/meta_data_loader.rb', line 13

def decryption_key
  @decryption_key
end

Instance Method Details

#dataObject



15
16
17
# File 'lib/objects/node/meta_data_loader.rb', line 15

def data
  @data ||= do_load
end

#data_for_namespace(namespace) ⇒ Object



19
20
21
22
# 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_loadObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/objects/node/meta_data_loader.rb', line 58

def do_load
   = {}
  @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)
      .deep_merge!(filedata) if filedata.is_a?(Hash)
    rescue Psych::SyntaxError => e
      raise Bcome::Exception::InvalidMetaDataConfig, "Error: #{e.message}"
    end
  end
  
end

#load_file_data_for(filepath) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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_keyObject



32
33
34
35
36
37
38
# 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) ⇒ Object



24
25
26
27
28
29
30
# 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