Class: Yle::AWS::Role::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/yle/aws/role/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_nameObject (readonly)

Returns the value of attribute account_name.



9
10
11
# File 'lib/yle/aws/role/cli.rb', line 9

def 
  @account_name
end

#optsObject (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(account_name, role_name, duration, &block) ⇒ Object



80
81
82
83
84
85
# File 'lib/yle/aws/role/cli.rb', line 80

def assume_role(, role_name, duration, &block)
  Role.assume_role(, role_name, duration, &block)
rescue Errors::AssumeRoleError => e
  STDERR.puts e
  exit 1
end

#commandObject



93
94
95
# File 'lib/yle/aws/role/cli.rb', line 93

def command
  @command ||= [shell]
end

#executeObject

rubocop:disable Metrics/AbcSize



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/yle/aws/role/cli.rb', line 61

def execute
  return list_accounts if opts[:list]

  assume_role(, opts[:role], opts[:duration]) do |role|
    STDERR.puts("Assumed role #{role.name}") if !opts[:quiet]

    if opts[:env]
      role.print_env_vars
    else
      run_command
    end
  end
end

#list_accountsObject

rubocop:enable Metrics/AbcSize



76
77
78
# File 'lib/yle/aws/role/cli.rb', line 76

def list_accounts
  puts Role.accounts
end

#parse_args(argv) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



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
50
51
52
53
54
55
56
57
# File 'lib/yle/aws/role/cli.rb', line 16

def parse_args(argv)
  @opts = Slop.parse(argv) do |o|
    o.banner = '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

  if !@opts[:list]
    if !@account_name
      STDERR.puts @opts
      exit 64
    elsif !(@opts[:role] || Role.default_role_name)
      STDERR.puts 'Role name must be passed with `--role` or set in the config'
      exit 64
    end
  end
rescue Slop::Error => e
  STDERR.puts e
  exit 64
end

#run_commandObject



87
88
89
90
91
# File 'lib/yle/aws/role/cli.rb', line 87

def run_command
  ret = system(*command)
  STDERR.puts "Failed to execute '#{command.first}'" if ret.nil?
  exit(1) if !ret
end

#shellObject



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/yle/aws/role/cli.rb', line 97

def shell
  shell = ENV.fetch('SHELL', 'bash')

  if !opts[:quiet]
    puts "Executing shell '#{shell}' with the assumed role"
    puts 'Use `exit` to quit'
    puts
  end

  [shell]
end