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.
11 12 13 14 |
# File 'lib/yle/aws/role/cli.rb', line 11 def initialize(argv) parse_args(argv) verify_args end |
Instance Attribute Details
#account_name ⇒ Object (readonly)
Returns the value of attribute account_name.
9 10 11 |
# File 'lib/yle/aws/role/cli.rb', line 9 def account_name @account_name end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
9 10 11 |
# File 'lib/yle/aws/role/cli.rb', line 9 def opts @opts end |
Instance Method Details
#assume_role ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/yle/aws/role/cli.rb', line 91 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
101 102 103 104 |
# File 'lib/yle/aws/role/cli.rb', line 101 def command @command = shell if @command.empty? @command end |
#default_shell ⇒ Object
118 119 120 |
# File 'lib/yle/aws/role/cli.rb', line 118 def default_shell ENV.fetch('SHELL', 'bash') end |
#execute ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/yle/aws/role/cli.rb', line 65 def execute if opts[:list] list_accounts elsif opts[:env] print_env_vars else run_command end end |
#list_accounts ⇒ Object
75 76 77 |
# File 'lib/yle/aws/role/cli.rb', line 75 def list_accounts puts Role.accounts end |
#parse_args(argv) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength
17 18 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 |
# File 'lib/yle/aws/role/cli.rb', line 17 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
79 80 81 |
# File 'lib/yle/aws/role/cli.rb', line 79 def print_env_vars assume_role(&:print_env_vars) end |
#run_command ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/yle/aws/role/cli.rb', line 83 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
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/yle/aws/role/cli.rb', line 106 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
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/yle/aws/role/cli.rb', line 51 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 |