Class: GCE::Host::Config
- Inherits:
-
Object
- Object
- GCE::Host::Config
- Defined in:
- lib/gce/host/config.rb
Class Method Summary collapse
- .application_default_credentials_file ⇒ Object
- .array_value_delimiter ⇒ Object
- .auth_method ⇒ Object
- .config ⇒ Object
- .config_default ⇒ Object
- .config_default_file ⇒ Object
- .config_file ⇒ Object
- .configure(params) ⇒ Object
- .credentials ⇒ Object
- .credentials_file ⇒ Object
- .global_application_default_credentials_file ⇒ Object
- .log_level ⇒ Object
- .open_timeout_sec ⇒ Object
- .optional_array_keys ⇒ Object
-
.optional_array_options ⇒ Object
private.
- .optional_options ⇒ Object
- .optional_string_keys ⇒ Object
- .optional_string_options ⇒ Object
- .project ⇒ Object
- .project_default ⇒ Object
- .retries ⇒ Object
- .role_max_depth ⇒ Object
- .role_value_delimiter ⇒ Object
- .roles_key ⇒ Object
-
.status ⇒ Object
this makes configurable to change status to state to make compatible with AWS usually, users do not need to care of this.
- .timeout_sec ⇒ Object
Class Method Details
.application_default_credentials_file ⇒ Object
30 31 32 |
# File 'lib/gce/host/config.rb', line 30 def self.application_default_credentials_file @application_default_credentials_file ||= File.("~/.config/gcloud/application_default_credentials.json") end |
.array_value_delimiter ⇒ Object
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_method ⇒ Object
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 |
.config ⇒ Object
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_default ⇒ Object
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_file ⇒ Object
42 43 44 |
# File 'lib/gce/host/config.rb', line 42 def self.config_default_file File.('~/.config/gcloud/configurations/config_default') end |
.config_file ⇒ Object
16 17 18 |
# File 'lib/gce/host/config.rb', line 16 def self.config_file @config_file ||= File.(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 |
.credentials ⇒ Object
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_file ⇒ Object
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.(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_file ⇒ Object
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_level ⇒ Object
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_sec ⇒ Object
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_keys ⇒ Object
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_options ⇒ Object
private
109 110 111 112 113 |
# File 'lib/gce/host/config.rb', line 109 def self. @optional_array_options ||= Hash[optional_array_keys.map {|key| [StringUtil.singularize(StringUtil.underscore(key)), key] }] end |
.optional_options ⇒ Object
121 122 123 |
# File 'lib/gce/host/config.rb', line 121 def self. @optional_options ||= .merge() end |
.optional_string_keys ⇒ Object
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_options ⇒ Object
115 116 117 118 119 |
# File 'lib/gce/host/config.rb', line 115 def self. @optional_string_options ||= Hash[optional_string_keys.map {|key| [StringUtil.underscore(key), key] }] end |
.project ⇒ Object
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_default ⇒ Object
51 52 53 |
# File 'lib/gce/host/config.rb', line 51 def self.project_default (config_default['core'] || {})['project'] end |
.retries ⇒ Object
65 66 67 |
# File 'lib/gce/host/config.rb', line 65 def self.retries @retries ||= ENV['RETRIES'] || config.fetch('RETRIES', 5) end |
.role_max_depth ⇒ Object
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_delimiter ⇒ Object
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_key ⇒ Object
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 |
.status ⇒ Object
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_sec ⇒ Object
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 |