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.
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Instance Method Summary collapse
- #duration ⇒ Object
- #execute ⇒ Object
-
#initialize(argv) ⇒ Cli
constructor
A new instance of Cli.
- #parse_args(argv) ⇒ Object
- #role_name ⇒ Object
- #run_command ⇒ Object
Constructor Details
#initialize(argv) ⇒ Cli
Returns a new instance of Cli.
11 12 13 |
# File 'lib/yle/aws/role/cli.rb', line 11 def initialize(argv) parse_args(argv) 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 |
#command ⇒ Object (readonly)
Returns the value of attribute command.
9 10 11 |
# File 'lib/yle/aws/role/cli.rb', line 9 def command @command 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
#duration ⇒ Object
98 99 100 |
# File 'lib/yle/aws/role/cli.rb', line 98 def duration opts[:duration] || Role.config['defaults']['duration'] end |
#execute ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/yle/aws/role/cli.rb', line 51 def execute if opts[:list] puts Role.accounts return end if !role_name STDERR.puts 'Role name must be passed with `--role` or set in the config' exit 64 end Role.assume_role(account_name, role_name, duration) do |role| STDERR.puts("Assumed role #{role.name}") if !opts[:quiet] if opts[:env] role.print_env_vars else run_command end end rescue Errors::AssumeRoleError => e STDERR.puts e exit 1 end |
#parse_args(argv) ⇒ Object
15 16 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 49 |
# File 'lib/yle/aws/role/cli.rb', line 15 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' 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 if !@account_name && !@opts[:list] STDERR.puts @opts exit 64 end rescue Slop::Error => e STDERR.puts e exit 64 end |
#role_name ⇒ Object
94 95 96 |
# File 'lib/yle/aws/role/cli.rb', line 94 def role_name opts[:role] || Role.config['defaults']['role'] end |
#run_command ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/yle/aws/role/cli.rb', line 76 def run_command cmd = command if cmd.empty? shell = ENV.fetch('SHELL', 'bash') cmd = [shell] if !opts[:quiet] puts "Executing shell '#{shell}' with the assumed role" puts "Use `exit` to quit" puts end end ret = system(*cmd) STDERR.puts "Failed to execute '#{cmd.first}'" if ret.nil? exit(1) if !ret end |