Class: GCE::Host::Config

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

Class Method Summary collapse

Class Method Details

.array_value_delimiterObject



67
68
69
# File 'lib/gce/host/config.rb', line 67

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

.auth_methodObject



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

def self.auth_method
  @auth_method ||= ENV['AUTH_METHOD'] || config.fetch('AUTH_METHOD', 'application_default')
end

.configObject



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/gce/host/config.rb', line 95

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_fileObject



13
14
15
# File 'lib/gce/host/config.rb', line 13

def self.config_file
  @config_file ||= ENV.fetch('GCE_HOST_CONFIG_FILE', '/etc/sysconfig/gce-host')
end

.configure(params) ⇒ Object



7
8
9
10
11
# File 'lib/gce/host/config.rb', line 7

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

.credential_fileObject



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

def self.credential_file
  @credential_file ||= ENV['GOOGLE_CREDENTIAL_FILE'] || config.fetch('GOOGLE_CREDENTIAL_FILE', nil)
end

.log_levelObject



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

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

.open_timeout_secObject



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

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

.optional_array_keysObject



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

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

.optional_array_optionsObject

private



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

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

.optional_optionsObject



91
92
93
# File 'lib/gce/host/config.rb', line 91

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

.optional_string_keysObject



59
60
61
# File 'lib/gce/host/config.rb', line 59

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

.optional_string_optionsObject



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

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

.projectObject



25
26
27
28
29
30
31
32
33
# File 'lib/gce/host/config.rb', line 25

def self.project
  return @project if @project
  # ref. terraform https://www.terraform.io/docs/providers/google/
  @project ||= ENV['GOOGLE_PROJECT'] || config.fetch('GOOGLE_PROJECT', nil)
  if @project.nil? and credential_file and File.readable?(credential_file)
    @project ||= (JSON.parse(File.read(credential_file)) || {})['project_id']
  end
  @project
end

.retriesObject



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

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

.role_value_delimiterObject



63
64
65
# File 'lib/gce/host/config.rb', line 63

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

.roles_keyObject



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

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



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

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

.timeout_secObject



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

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