Class: Chef::Provisioning::OpenNebulaDriver::Credentials

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/provisioning/opennebula_driver/credentials.rb

Overview

Implementation of Provider class.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Credentials

Returns a new instance of Credentials.



33
34
35
36
37
# File 'lib/chef/provisioning/opennebula_driver/credentials.rb', line 33

def initialize(options = {})
  @credentials = {}
  load_default(options)
  load_profiles
end

Class Method Details

.method_missing(name, *args, &block) ⇒ Object



99
100
101
# File 'lib/chef/provisioning/opennebula_driver/credentials.rb', line 99

def self.method_missing(name, *args, &block)
  singleton.send(name, *args, &block)
end

.singletonObject



103
104
105
# File 'lib/chef/provisioning/opennebula_driver/credentials.rb', line 103

def self.singleton
  @one_credentials ||= Credentials.new
end

Instance Method Details

#[](name) ⇒ Object



44
45
46
47
# File 'lib/chef/provisioning/opennebula_driver/credentials.rb', line 44

def [](name)
  fail "Profile '#{name}' does not exist" unless @credentials[name]
  @credentials[name]
end

#defaultObject



39
40
41
42
# File 'lib/chef/provisioning/opennebula_driver/credentials.rb', line 39

def default
  fail 'No credentials loaded!  Do you have a ~/.one/one_auth file?' if @credentials.empty?
  @credentials[ENV['ONE_DEFAULT_PROFILE'] || 'default'] || @credentials.first[1]
end

#load_default(options = {}) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/chef/provisioning/opennebula_driver/credentials.rb', line 49

def load_default(options = {})
  oneauth_file = ENV['ONE_AUTH'] || File.expand_path('~/.one/one_auth')
  begin
    creds = File.read(oneauth_file).strip
    @credentials['default'] = { :credentials => creds, :options => options }
  end if File.file?(oneauth_file)
end

#load_file(filename, options = {}) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/chef/provisioning/opennebula_driver/credentials.rb', line 90

def load_file(filename, options = {})
  creds = File.read(filename).strip if File.file?(filename)
  @credentials['default'] = {
    :credentials => creds,
    :options => options
  } unless creds.nil?
  @credentials
end

#load_plain(creds, options = {}) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/chef/provisioning/opennebula_driver/credentials.rb', line 82

def load_plain(creds, options = {})
  @credentials['default'] = {
    :credentials => creds,
    :options => options
  } unless creds.nil?
  @credentials
end

#load_profilesObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/chef/provisioning/opennebula_driver/credentials.rb', line 57

def load_profiles
  file = nil
  if ENV['ONE_CONFIG'] && !ENV['ONE_CONFIG'].empty? && File.file?(ENV['ONE_CONFIG'])
    file = ENV['ONE_CONFIG']
  elsif ENV['HOME'] && File.file?("#{ENV['HOME']}/.one/one_config")
    file = "#{ENV['HOME']}/.one/one_config"
  elsif File.file?("/var/lib/one/.one/one_config")
    file = "/var/lib/one/.one/one_config"
  else
    Chef::Log.info("No ONE_CONFIG file found, will use default profile")
  end
  json = {}
  begin
    content_hash = JSON.parse(File.read(file), :symbolize_names => true)
    content_hash.each { |k, v| json[k.to_s] = v }
  rescue StandardError => e_file
    Chef::Log.warn("Failed to read config file #{file}: #{e_file.message}")
  rescue JSON::ParserError => e_json
    Chef::Log.warn("Failed to parse config file #{file}: #{e_json.message}")
  rescue
    Chef::Log.warn("Failed to read or parse config file #{file}: #{$!}")
  end
  @credentials.merge!(json)
end