4
5
6
7
8
9
10
11
12
13
14
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/danarchy_sys/cli/keypair_manager.rb', line 4
def self.manager(os_compute)
comp_kp = os_compute.compute_keypairs
puts 'Keypair Manager: enter \'help\' to view available commands or \'main\' for the main menu.'
= Menus.('keypair')
keypair = false
loop do
while keypair == false
keypair = chooser(os_compute)
return Menus.('main') if keypair == 'main'
end
print "#{keypair.name} ~: " if keypair
cmd = gets.chomp
next if cmd.empty?
abort('Exiting!') if cmd == 'exit'
if cmd =~ /^[0-9]*$/
[cmd.to_i].map { |k, v| cmd = k } if .keys.include? cmd.to_i
end
if cmd == 'help'
Menus.('keypair')
elsif cmd == 'main'
return Menus.('main')
elsif cmd == 'chooser'
keypair = chooser(os_compute)
elsif cmd == 'create'
print 'Enter a new keypair name: '
keypair_name = gets.chomp
keypair = comp_kp.create_keypair(keypair_name)
puts "Keypair: #{keypair_name} created!"
Menus.('keypair')
puts "Managing keypair: #{keypair_name}"
elsif cmd == 'delete'
print "Are you sure you wish to delete keypair: #{keypair.name}? (this is permanent!) (Y/N): "
delete = comp_kp.delete_keypair(keypair.name) if gets.chomp =~ /^y(es)?$/i
if delete == true
puts "#{keypair.name} has been deleted! Returning to the keypair chooser."
keypair = chooser(os_compute)
else
puts "#{keypair.name} was not deleted!"
end
elsif cmd == 'status'
printf("%#{keypair.name.size}s %0s %0s\n", keypair.name, ' => ', keypair.state)
elsif %w(pause unpause suspend resume start stop).include?(cmd.to_s)
status = keypair.state
if cmd =~ /e$/
print "#{cmd.gsub(/e$/, 'ing')} #{keypair.name} ."
else
print "#{cmd}ing #{keypair.name} ."
end
response = comp_kp.send(cmd.to_s, keypair.name.to_s)
if response == false
puts "\nInvalid action for #{keypair.name}'s current status!"
next
end
until status != keypair.state
keypair = os_compute.compute_keypairs.get_keypair(keypair.name)
sleep(3)
print ' .'
end
printf("\n%#{keypair.name.size}s %0s %0s\n", keypair.name, ' => ', keypair.state)
elsif cmd == 'connect'
if keypair.state == 'ACTIVE'
os_compute.compute_ssh(keypair.name.to_s)
else
puts "Unable to connect: #{keypair.name} is not running!"
end
else
Menus.('keypair')
puts "\nCommand \'#{cmd}\' not available. Enter a command from above."
end
return Menus.('main') if keypair == 'main'
end
end
|