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
14
# File 'lib/yle/aws/role/cli.rb', line 11

def initialize(argv)
  parse_args(argv)
  verify_args
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_roleObject



91
92
93
94
95
96
97
98
99
# File 'lib/yle/aws/role/cli.rb', line 91

def assume_role
  Role.assume_role(, 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

#commandObject



101
102
103
104
# File 'lib/yle/aws/role/cli.rb', line 101

def command
  @command = shell if @command.empty?
  @command
end

#default_shellObject



118
119
120
# File 'lib/yle/aws/role/cli.rb', line 118

def default_shell
  ENV.fetch('SHELL', 'bash')
end

#executeObject



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_accountsObject



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.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
rescue Slop::Error => e
  STDERR.puts e
  exit 64
end


79
80
81
# File 'lib/yle/aws/role/cli.rb', line 79

def print_env_vars
  assume_role(&:print_env_vars)
end

#run_commandObject



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

#shellObject



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_argsObject

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 !
    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