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
|
# File 'lib/danarchy_sys/cli/instance_manager.rb', line 5
def self.manager(os_compute, settings)
@os_compute = os_compute
@settings = settings
@prompts_create_instance = PromptsCreateInstance.new(@os_compute, @settings)
puts 'Instance Manager: enter \'help\' to view available commands or \'main\' for the main menu.'
= Menus.('instance')
instance = false
loop do
trap('SIGINT') { print "\nEnter an instance to manage or enter a name for a new instance: " }
while instance == false
instance = chooser
return Menus.('main') if instance == 'main'
end
print "#{instance.name} ~: " if instance
cmd = gets
cmd = cmd ? cmd.chomp : abort('Exiting!')
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.('instance')
elsif cmd == 'main'
return Menus.('main')
elsif cmd == 'chooser'
instance = chooser
elsif cmd == 'create'
instance = @prompts_create_instance.create_instance(nil)
elsif cmd == 'delete'
print "Are you sure you wish to delete instance: #{instance.name}? (this is permanent!) (Y/N): "
delete = @os_compute.instances.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
else
puts "#{instance.name} was not deleted!"
end
elsif cmd == 'status'
instance = @os_compute.instances.get_instance(instance.name)
if instance.state != 'ACTIVE'
printf("%#{instance.name.size}s %0s %0s\n", instance.name, ' => ', instance.state)
elsif @os_compute.ssh(instance, 'uptime')[:stderr]
printf("%#{instance.name.size}s %0s %0s\n", instance.name, ' => ', 'WAITING')
else
printf("%#{instance.name.size}s %0s %0s\n", instance.name, ' => ', instance.state)
end
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 = @os_compute.instances.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'
image = @prompts_create_instance.image
print "Should we rebuild #{instance.name} with image: #{image.name}? (Y/N): "
if gets.chomp =~ /^y(es)?$/i
puts "Rebuilding #{instance.name} with #{image.name}"
instance = @os_compute.instances.rebuild_instance(instance, image)
puts "\nRebuild in progress!"
else
puts "Not rebuilding #{instance.name} at this time."
end
elsif cmd == 'connect'
if instance.state == 'ACTIVE'
@os_compute.ssh(instance.name)
else
puts "Unable to connect: #{instance.name} is not running!"
end
else
Menus.('instance')
puts "\nCommand \'#{cmd}\' not available. Enter a command from above."
end
return Menus.('main') if instance == 'main'
end
end
|