Class: GCE::Host::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/gce/host/config.rb

Class Method Summary collapse

Class Method Details

.application_default_credentials_fileObject



30
31
32
# File 'lib/gce/host/config.rb', line 30

def self.application_default_credentials_file
  @application_default_credentials_file ||= File.expand_path("~/.config/gcloud/application_default_credentials.json")
end

.array_value_delimiterObject



93
94
95
# File 'lib/gce/host/config.rb', line 93

def self.array_value_delimiter
  @array_value_delimiter ||= ENV['ARRAY_VALUE_DELIMITER'] || config.fetch('ARRAY_VALUE_DELIMITER', ',')
end

.auth_methodObject



20
21
22
# File 'lib/gce/host/config.rb', line 20

def self.auth_method
  @auth_method ||= ENV['AUTH_METHOD'] || config.fetch('AUTH_METHOD', nil) || credentials['type'] || 'compute_engine'
end

.configObject



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/gce/host/config.rb', line 125

def self.config
  return @config if @config
  @config = {}
  if File.exist?(config_file)
    File.readlines(config_file).each do |line|
      next if line.start_with?('#')
      key, val = line.chomp.split('=', 2)
      @config[key] = val
    end
  end
  @config
end

.config_defaultObject



46
47
48
49
# File 'lib/gce/host/config.rb', line 46

def self.config_default
  # {'core'=>{'account'=>'xxx','project'=>'xxx'},'compute'=>{'zone'=>'xxx}}
  @config_default ||= File.readable?(config_default_file) ? IniFile.load(config_default_file).to_h : {}
end

.config_default_fileObject



42
43
44
# File 'lib/gce/host/config.rb', line 42

def self.config_default_file
  File.expand_path('~/.config/gcloud/configurations/config_default')
end

.config_fileObject



16
17
18
# File 'lib/gce/host/config.rb', line 16

def self.config_file
  @config_file ||= File.expand_path(ENV.fetch('GCE_HOST_CONFIG_FILE', File.exist?('/etc/sysconfig/gce-host') ? '/etc/sysconfig/gce-host' : '/etc/default/gce-host'))
end

.configure(params) ⇒ Object



10
11
12
13
14
# File 'lib/gce/host/config.rb', line 10

def self.configure(params)
  params.each do |key, val|
    send("#{key}=", val)
  end
end

.credentialsObject



38
39
40
# File 'lib/gce/host/config.rb', line 38

def self.credentials
  File.readable?(credentials_file) ? JSON.parse(File.read(credentials_file)) : {}
end

.credentials_fileObject



24
25
26
27
28
# File 'lib/gce/host/config.rb', line 24

def self.credentials_file
  # ref. https://developers.google.com/identity/protocols/application-default-credentials
  @credentials_file ||= File.expand_path(ENV['GOOGLE_APPLICATION_CREDENTIALS'] || config.fetch('GOOGLE_APPLICATION_CREDENTIALS', nil) ||
    (File.exist?(global_application_default_credentials_file) ? global_application_default_credentials_file : application_default_credentials_file))
end

.global_application_default_credentials_fileObject



34
35
36
# File 'lib/gce/host/config.rb', line 34

def self.global_application_default_credentials_file
  @global_application_default_credentials_file ||= '/etc/google/auth/application_default_credentials.json'
end

.log_levelObject



61
62
63
# File 'lib/gce/host/config.rb', line 61

def self.log_level
  @log_level ||= ENV['LOG_LEVEL'] || config.fetch('LOG_LEVEL', 'info')
end

.open_timeout_secObject



73
74
75
# File 'lib/gce/host/config.rb', line 73

def self.open_timeout_sec
  @open_timeout_sec ||= ENV['OPEN_TIMEOUT_SEC'] || config.fetch('OPEN_TIMEOUT_SEC', 300)
end

.optional_array_keysObject



81
82
83
# File 'lib/gce/host/config.rb', line 81

def self.optional_array_keys
  @optional_array_keys ||= (ENV['OPTIONAL_ARRAY_KEYS'] || config.fetch('OPTIONAL_ARRAY_KEYS', '')).split(',')
end

.optional_array_optionsObject

private



109
110
111
112
113
# File 'lib/gce/host/config.rb', line 109

def self.optional_array_options
  @optional_array_options ||= Hash[optional_array_keys.map {|key|
    [StringUtil.singularize(StringUtil.underscore(key)), key]
  }]
end

.optional_optionsObject



121
122
123
# File 'lib/gce/host/config.rb', line 121

def self.optional_options
  @optional_options ||= optional_array_options.merge(optional_string_options)
end

.optional_string_keysObject



85
86
87
# File 'lib/gce/host/config.rb', line 85

def self.optional_string_keys
  @optional_string_keys ||= (ENV['OPTIONAL_STRING_KEYS'] || config.fetch('OPTIONAL_STRING_KEYS', '')).split(',')
end

.optional_string_optionsObject



115
116
117
118
119
# File 'lib/gce/host/config.rb', line 115

def self.optional_string_options
  @optional_string_options ||= Hash[optional_string_keys.map {|key|
    [StringUtil.underscore(key), key]
  }]
end

.projectObject



55
56
57
58
59
# File 'lib/gce/host/config.rb', line 55

def self.project
  @project ||= ENV['GOOGLE_PROJECT'] || config.fetch('GOOGLE_PROJECT', nil) || credentials['project_id']
  @project ||= credentials['client_email'].chomp('.iam.gserviceaccount.com').split('@').last if credentials['client_email']
  @project ||= project_default || raise('project is not given, configure GOOGLE_PROJECT or run $ gcloud config set project PROJECT_NAME')
end

.project_defaultObject



51
52
53
# File 'lib/gce/host/config.rb', line 51

def self.project_default
  (config_default['core'] || {})['project']
end

.retriesObject



65
66
67
# File 'lib/gce/host/config.rb', line 65

def self.retries
  @retries ||= ENV['RETRIES'] || config.fetch('RETRIES', 5)
end

.role_max_depthObject



103
104
105
# File 'lib/gce/host/config.rb', line 103

def self.role_max_depth
  @role_max_depth ||= Integer(ENV['ROLE_MAX_DEPTH'] || config.fetch('ROLE_MAX_DEPTH', 3))
end

.role_value_delimiterObject



89
90
91
# File 'lib/gce/host/config.rb', line 89

def self.role_value_delimiter
  @role_value_delimiter ||= ENV['ROLE_VALUE_DELIMITER'] || config.fetch('ROLE_VALUE_DELIMITER', ':')
end

.roles_keyObject



77
78
79
# File 'lib/gce/host/config.rb', line 77

def self.roles_key
  @roles_key ||= ENV['ROLES_KEY'] || config.fetch('ROLES_KEY', 'roles')
end

.statusObject

this makes configurable to change status to state to make compatible with AWS usually, users do not need to care of this



99
100
101
# File 'lib/gce/host/config.rb', line 99

def self.status
  @status ||= ENV['STATUS'] || config.fetch('STATUS', 'status')
end

.timeout_secObject



69
70
71
# File 'lib/gce/host/config.rb', line 69

def self.timeout_sec
  @timeout_sec ||= ENV['TIMEOUT_SEC'] || config.fetch('TIMEOUT_SEC', 300)
end