Module: GClouder::Project::ID
Class Method Summary
collapse
Methods included from Shell
included, #shell
Methods included from Logging
#add, #bad, #change, #debug, #error, #fatal, #good, included, #info, log, loggers, #remove, report, #resource_state, setup, #warn, #warning
check, #cli_args, cli_args, included, valid_resources
project, #project
Methods included from Helpers
#hash_to_args, included, #module_exists?, #to_arg, #to_deep_merge_hash, #valid_json?
Class Method Details
.bail ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/gclouder/resources/project_id.rb', line 27
def self.bail
puts "not authenticated against API and --activate-service-accounts option not passed"
puts ""
puts "please either:"
puts ""
puts " run: gcloud auth login && gcloud auth application-default login"
puts ""
puts " or: specify --activate-service-accounts flag and make sure the relevant keys exist in the keys dir"
puts ""
exit 1
end
|
.current ⇒ Object
19
20
21
22
23
24
25
|
# File 'lib/gclouder/resources/project_id.rb', line 19
def self.current
return if cli_args[:ignore_default_service_account]
id = shell("gcloud auth list --format json | jq -r '.[] | select(.status == \"ACTIVE\") | .account'")
return id if !id.empty?
return if cli_args[:activate_service_accounts]
bail
end
|
.default ⇒ Object
59
60
61
|
# File 'lib/gclouder/resources/project_id.rb', line 59
def self.default
shell("gcloud config set account #{project['project_id']}")
end
|
.dir ⇒ Object
63
64
65
|
# File 'lib/gclouder/resources/project_id.rb', line 63
def self.dir
cli_args[:keys_dir] || File.join(ENV["HOME"], "keys")
end
|
.id ⇒ Object
10
11
12
|
# File 'lib/gclouder/resources/project_id.rb', line 10
def self.id
@id
end
|
.key_file(project_id) ⇒ Object
67
68
69
|
# File 'lib/gclouder/resources/project_id.rb', line 67
def self.key_file(project_id)
File.join(dir, "gcloud-service-key-#{project_id}.json")
end
|
.load ⇒ Object
14
15
16
17
|
# File 'lib/gclouder/resources/project_id.rb', line 14
def self.load
@id ||= current
switch(project["project_id"]) if @id.nil?
end
|
.rescue ⇒ Object
46
47
48
49
50
51
52
53
|
# File 'lib/gclouder/resources/project_id.rb', line 46
def self.rescue
if @id.nil?
shell("gcloud config unset account")
return
end
switch(@id)
end
|
.reset ⇒ Object
55
56
57
|
# File 'lib/gclouder/resources/project_id.rb', line 55
def self.reset
switch(project["project_id"])
end
|
.switch(project_id) ⇒ Object
39
40
41
42
43
44
|
# File 'lib/gclouder/resources/project_id.rb', line 39
def self.switch(project_id)
return unless project_id
if cli_args[:activate_service_accounts]
shell("gcloud --quiet auth activate-service-account --key-file #{key_file(project_id)}")
end
end
|