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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/danarchy_sys/cli/instance_manager.rb', line 5
def self.manager(os_compute)
comp_inst = os_compute.instances
puts 'Instance Manager: enter \'help\' to view available commands or \'main\' for the main menu.'
= .('instance')
instance = false
loop do
while instance == false
instance = chooser(os_compute)
return .('main') if instance == 'main'
end
print "#{instance.name} ~: "
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'
.('instance')
elsif cmd == 'main'
return .('main')
elsif cmd == 'chooser'
instance = chooser(os_compute)
elsif cmd == 'create'
PromptsCreateInstance.create_instance(os_compute, 'nil')
instance = chooser(os_compute)
elsif cmd == 'delete'
print "Are you sure you wish to delete instance: #{instance.name}? (this is permanent!) (Y/N): "
delete = comp_inst.delete_instance(instance.name) if gets.chomp =~ /^y(es)?$/i
if delete == true
puts "#{instance.name} has been deleted! Returning to the instance chooser."
instance = chooser(os_compute)
else
puts "#{instance.name} was not deleted!"
end
elsif cmd == 'status'
printf("%#{instance.name.size}s %0s %0s\n", instance.name, ' => ', instance.state)
elsif %w(pause unpause suspend resume start stop).include?(cmd.to_s)
status = instance.state
if cmd =~ /e$/
print "#{cmd.gsub(/e$/, 'ing')} #{instance.name} ."
else
print "#{cmd}ing #{instance.name} ."
end
response = comp_inst.send(cmd.to_s, instance.name.to_s)
if response == false
puts "\nInvalid action for #{instance.name}'s current status!"
next
end
until status != instance.state
instance = os_compute.instances.get_instance(instance.name)
sleep(3)
print ' .'
end
printf("\n%#{instance.name.size}s %0s %0s\n", instance.name, ' => ', instance.state)
elsif cmd == 'rebuild'
print "Should we rebuild #{instance.name}? (Y/N): "
if gets.chomp =~ /^y(es)?$/i
image = PromptsCreateInstance.image(os_compute.images)
instance.rebuild(image.id, instance.name)
print "Rebuilding #{instance.name} with #{image.name}"
instance = os_compute.instances.get_instance(instance.name)
until instance.state == 'ACTIVE'
instance = os_compute.instances.get_instance(instance.name)
sleep(3)
print ' .'
end
addrs = os_compute.instances.get_public_addresses(instance.name)
addrs.each { |ip| `ssh-keygen -R #{ip} &>/dev/null` }
puts "\nRebuild successful!"
else
puts "Not rebuilding #{instance.name} at this time."
end
elsif cmd == 'connect'
if instance.state == 'ACTIVE'
connect = os_compute.ssh(instance.name)
puts connect if connect != true
else
puts "Unable to connect: #{instance.name} is not running!"
end
else
.('instance')
puts "\nCommand \'#{cmd}\' not available. Enter a command from above."
end
return .('main') if instance == 'main'
end
end
|