Class: Yle::AWS::Role::Cli
- Inherits:
-
Object
- Object
- Yle::AWS::Role::Cli
- Defined in:
- lib/yle/aws/role/cli.rb
Instance Attribute Summary collapse
-
#account_name ⇒ Object
readonly
Returns the value of attribute account_name.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Instance Method Summary collapse
- #assume_role ⇒ Object
- #command ⇒ Object
- #default_shell ⇒ Object
- #execute ⇒ Object
-
#initialize(argv) ⇒ Cli
constructor
A new instance of Cli.
- #list_accounts ⇒ Object
-
#parse_args(argv) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength.
- #print_env_vars ⇒ Object
- #run_command ⇒ Object
- #shell ⇒ Object
-
#verify_args ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/MethodLength.
Constructor Details
#initialize(argv) ⇒ Cli
Returns a new instance of Cli.
13 14 15 16 |
# File 'lib/yle/aws/role/cli.rb', line 13 def initialize(argv) parse_args(argv) verify_args end |
Instance Attribute Details
#account_name ⇒ Object (readonly)
Returns the value of attribute account_name.
11 12 13 |
# File 'lib/yle/aws/role/cli.rb', line 11 def account_name @account_name end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
11 12 13 |
# File 'lib/yle/aws/role/cli.rb', line 11 def opts @opts end |
Instance Method Details
#assume_role ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/yle/aws/role/cli.rb', line 93 def assume_role Role.assume_role(account_name, opts[:role], opts[:duration]) do |role| STDERR.puts("Assumed role #{role.name}") if !opts[:quiet] yield role end rescue Errors::AssumeRoleError => e STDERR.puts e exit 1 end |
#command ⇒ Object
103 104 105 106 |
# File 'lib/yle/aws/role/cli.rb', line 103 def command @command = shell if @command.empty? @command end |
#default_shell ⇒ Object
120 121 122 |
# File 'lib/yle/aws/role/cli.rb', line 120 def default_shell ENV.fetch('SHELL', 'bash') end |
#execute ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/yle/aws/role/cli.rb', line 67 def execute if opts[:list] list_accounts elsif opts[:env] print_env_vars else run_command end end |
#list_accounts ⇒ Object
77 78 79 |
# File 'lib/yle/aws/role/cli.rb', line 77 def list_accounts puts Role.accounts end |
#parse_args(argv) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/yle/aws/role/cli.rb', line 19 def parse_args(argv) @opts = Slop.parse(argv) do |o| o. = 'Usage: asu <account> [options] -- [command ...]' o.separator ' or: asu --list' o.separator '' o.separator ' account The account ID or pattern of the role account' o.separator ' command Command to execute with the role. ' \ 'Defaults to launching new shell session.' o.separator '' o.integer '-d', '--duration', 'Duration for the role credentials. ' \ "Default: #{Role.default_duration}" o.bool '--env', 'Print out environment variables and exit' o.bool '-l', '--list', 'Print out all configured account aliases' o.bool '-q', '--quiet', 'Be quiet' o.string '-r', '--role', "Name of the role. Default: '#{Role.default_role_name}'" o.separator '' o.on '-h', '--help', 'Prints this help' do puts o exit end o.on '-v', '--version', 'Prints the version information' do puts VERSION exit end end @account_name = @opts.args.shift @command = @opts.args rescue Slop::Error => e STDERR.puts e exit 64 end |
#print_env_vars ⇒ Object
81 82 83 |
# File 'lib/yle/aws/role/cli.rb', line 81 def print_env_vars assume_role(&:print_env_vars) end |
#run_command ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/yle/aws/role/cli.rb', line 85 def run_command assume_role do ret = system(*command) STDERR.puts "Failed to execute '#{command.first}'" if ret.nil? exit(1) if !ret end end |
#shell ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/yle/aws/role/cli.rb', line 108 def shell shell = default_shell if !opts[:quiet] puts "Executing shell '#{shell}' with the assumed role" puts 'Use `exit` to quit' puts end [shell] end |
#verify_args ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/MethodLength
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/yle/aws/role/cli.rb', line 53 def verify_args return if opts[:list] if !account_name STDERR.puts opts exit 64 end if !(opts[:role] || Role.default_role_name) STDERR.puts 'Role name must be passed with `--role` or set in the config' exit 64 end end |