Class: Jupiter::Cli::Output
- Inherits:
-
Object
- Object
- Jupiter::Cli::Output
- Defined in:
- lib/jupiter/cli/output.rb
Defined Under Namespace
Classes: InvalidNameError, InvalidSelectionError
Instance Attribute Summary collapse
-
#cli ⇒ Object
Returns the value of attribute cli.
Instance Method Summary collapse
- #ask_how_to_clone ⇒ Object
- #ask_new_vm_name(vmserver) ⇒ Object
- #ask_vm_to_clone(vmserver) ⇒ Object
- #ask_which_task ⇒ Object
- #clearscreen ⇒ Object
- #draw_line(character, length, text, color1, color2) ⇒ Object
- #draw_menu ⇒ Object
- #draw_new_line ⇒ Object
-
#initialize(cli) ⇒ Output
constructor
A new instance of Output.
- #list_virtual_machines(vmserver) ⇒ Object
- #manage_host(vmserver, options) ⇒ Object
- #manage_vm(vmserver, options) ⇒ Object
- #select_vm_to_manage(vmserver) ⇒ Object
- #show_templates_available(templatehost) ⇒ Object
- #show_vm_ip(vmserver, vm) ⇒ Object
- #vm_current_state(vmserver, targetvm) ⇒ Object
Constructor Details
#initialize(cli) ⇒ Output
Returns a new instance of Output.
7 8 9 |
# File 'lib/jupiter/cli/output.rb', line 7 def initialize(cli) self.cli = cli end |
Instance Attribute Details
#cli ⇒ Object
Returns the value of attribute cli.
5 6 7 |
# File 'lib/jupiter/cli/output.rb', line 5 def cli @cli end |
Instance Method Details
#ask_how_to_clone ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/jupiter/cli/output.rb', line 40 def ask_how_to_clone valid_answers = ['1','2'] task = nil until valid_answers.include? task do draw_line('', 80, 'Would you like to clone one of these virtual machines or use a template?', 'white', 'cyan') draw_new_line draw_line('', 80, '1. Clone Virtual Machine', 'white', 'cyan') draw_line('', 80, '2. Use a Template', 'white', 'cyan') draw_new_line task = cli.ask("Please select a number:") if valid_answers.include?(task) == false puts "\n\n* INVALID ANSWER *\n\n" end end return task end |
#ask_new_vm_name(vmserver) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/jupiter/cli/output.rb', line 96 def ask_new_vm_name(vmserver) draw_line('#', 80, '', 'white', '') draw_line('#', 80, "Naming the new virtual machine", 'white', 'green') draw_line('#', 80, '', 'white', '') draw_new_line draw_line('', 80, 'When naming the new virtual machine please use only alpha-numeric', 'white', 'cyan') draw_line('', 80, 'characters, dashes and underscores.', 'white', 'cyan') draw_new_line new_vm_name = cli.ask("\nPlease enter the name of the new virtual machine without spaces:") new_vm = Jupiter::Input.new.clean_filename(new_vm_name) draw_line('#', 80, '', 'white', '') draw_line('#', 80, "The new virtual machine's name will be", 'white', 'cyan') draw_line('#', 80, '', 'white', '') draw_line(' ', 80, "#{new_vm}", 'white', 'green') draw_line('#', 80, '', 'white', '') return new_vm end |
#ask_vm_to_clone(vmserver) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/jupiter/cli/output.rb', line 57 def ask_vm_to_clone(vmserver) valid_answers = vmserver.server_vm_list list_virtual_machines(vmserver) draw_line('#', 80, "Note that these VMs might not all be registered on the server.", 'white', 'red') draw_line('#', 80, "You can only clone VMs that are registered.", 'white', 'red') draw_line('#', 80, '', 'white', '') targetvm = nil until valid_answers.include? targetvm do targetvm = cli.ask("\nPlease enter the valid name of the virtual machine you wish to clone:") if valid_answers.include?(targetvm) == false puts "\n\n* INVALID VIRTUAL MACHINE NAME *\n\n" end end return targetvm end |
#ask_which_task ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/jupiter/cli/output.rb', line 23 def ask_which_task valid_answers = ['1','2'] task = nil until valid_answers.include? task do draw_line('', 80, 'Would you like to create a new virtual machine or work with an existing one?', 'white', 'cyan') draw_new_line draw_line('', 80, '1. Clone / Create a Virtual Machine ', 'white', 'cyan') draw_line('', 80, '2. Work with an existing Virtual Machine', 'white', 'cyan') draw_new_line task = cli.ask("Please select a number:") if valid_answers.include?(task) == false puts "\n\n* INVALID ANSWER *\n\n" end end return task end |
#clearscreen ⇒ Object
11 12 13 |
# File 'lib/jupiter/cli/output.rb', line 11 def clearscreen puts "\e[H\e[2J" # ANSI escape sequence to clear screen end |
#draw_line(character, length, text, color1, color2) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/jupiter/cli/output.rb', line 155 def draw_line(character, length, text, color1, color2) line_minus_text = length - text.length half_line = line_minus_text / 2 line_segment1 = half_line - 1 line_segment2 = half_line - 1 line_to_messure = %Q[#{character * line_segment1} #{text} #{character * line_segment2}] line_segment2 += 1 if line_to_messure.length < length if color1.empty? && text.empty? output_line = character * length elsif color1.empty? && text.length > 0 output_line = %Q[#{character * line_segment1} #{text} #{character * line_segment2}] elsif text.empty? output_line = character * length output_line = Colorizer.color(color1, output_line) else line_segment1 = character * line_segment1 line_segment2 = character * line_segment2 line_segment1 = Colorizer.color(color1, line_segment1) line_segment2 = Colorizer.color(color1, line_segment2) text = Colorizer.color(color2, text) output_line = %Q[#{line_segment1} #{text} #{line_segment2}] end puts output_line end |
#draw_menu ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/jupiter/cli/output.rb', line 15 def draw_line('#', 80, '', 'white', '') draw_line('#', 80, 'WELCOME TO JUPITER', 'white', 'green') draw_line('#', 80, '', 'white', '') draw_line('#', 80, "We hope the god of sky and thunder will make your day most good and stuff.", 'white', 'green') draw_line('#', 80, '', 'white', '') end |
#draw_new_line ⇒ Object
151 152 153 |
# File 'lib/jupiter/cli/output.rb', line 151 def draw_new_line puts "\n" end |
#list_virtual_machines(vmserver) ⇒ Object
127 128 129 130 131 132 133 134 135 |
# File 'lib/jupiter/cli/output.rb', line 127 def list_virtual_machines(vmserver) draw_line('#', 80, '', 'white', '') draw_line('#', 80, "These are the currect virtual machines found on #{vmserver.name}", 'white', 'green') draw_line('#', 80, '', 'white', '') draw_new_line vmserver.server_vm_list.each { |x| puts "* #{x}" } draw_new_line draw_line('#', 80, '', 'white', '') end |
#manage_host(vmserver, options) ⇒ Object
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/jupiter/cli/output.rb', line 303 def manage_host(vmserver, ) list_virtual_machines(vmserver) clone_from = ask_how_to_clone if clone_from == '1' targetvm = ask_vm_to_clone(vmserver) new_vm_name = ask_new_vm_name(vmserver) vm_current_state(vmserver, targetvm) return if [:pretend] puts %Q[\nStarting cloning of #{targetvm} to a new virtual machine called #{new_vm_name}. This may take some time. Please wait...] time = Benchmark.realtime { vmserver.clone_vm(targetvm, new_vm_name) } puts %Q[\nFinished cloning.] puts %Q[\nElapses time: #{time}\n] puts %Q[\nNow booting the new VM...\n] sleep 10 show_vm_ip(vmserver, new_vm_name) newname = cli.ask("\n#{new_vm_name} is the name used by the ESX server to reference the VM but we need to also change the VM's actually hostname.\nPlease enter a valid hostname for the new virtual machine: (lowercase with no spaces or special characters)") ip = vmserver.guest_ip(new_vm_name) vm = Jupiter::VirtualMachine.new(ip, 2500) puts vm.rename_linux_guest(newname) draw_line('#', 80, '', 'white', '') elsif clone_from == '2' templatehost = Jupiter::Template.new(vmserver) templatevm = show_templates_available(templatehost) new_vm_name = ask_new_vm_name(vmserver) templatelist = templatehost.templates_list raise InvalidNameError.new("#{new_vm_name} is already taken as a name.") if templatelist.include?(new_vm_name) puts %Q[\nConnecting to template share on #{templatehost.host} to transfer template.] puts %Q[The transfer may take some time depending on the file size and bandwidth...] return if [:pretend] puts templatehost.transfer_template(templatevm, new_vm_name) sleep 10 # Give time for new VM to spinup before offering managment menu manage_vm(vmserver, ) else puts %Q[\n"#{clone_from}" is not a valid option. Exiting...\n] return end end |
#manage_vm(vmserver, options) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/jupiter/cli/output.rb', line 183 def manage_vm(vmserver, ) if [:guest].nil? vmname = select_vm_to_manage(vmserver) else vmname = [:guest] end vmstate = vm_current_state(vmserver, vmname) return if [:pretend] if vmstate != 'running' power_on = cli.ask("\nWould you like to power on #{vmname}? (yes/no)\n") if power_on == 'yes' vmserver.power_on_vm(vmname) elsif power_on == 'no' puts %Q[\nSince #{vmname} is not powered on some options will not be available.\n] else puts %Q[\nNot a valid response. Exiting...\n] return end end draw_new_line draw_line('#', 80, '', 'white', '') draw_line('#', 80, "Please select a task to perform on #{vmname}", 'white', 'green') draw_line('#', 80, '', 'white', '') draw_new_line puts %Q[ 1. Change hostname\n] puts %Q[ 2. Reboot (graceful restart)\n] puts %Q[ 3. Shutdown (graceful stop)\n] puts %Q[ 4. Cycle Power (non-graceful restart)\n] puts %Q[ 5. Kill Power (non-graceful)\n] puts %Q[ 6. Remove deploy user from sudoers\n] puts %Q[ 7. Display current IP address of #{vmname}\n] puts %Q[ 8. Increase the alocated HD space for #{vmname} (VM must be off)\n] puts %Q[ 9. Grow the LVM partition to fill the HD on #{vmname}\n] puts %Q[ 10. Generate Nagios config file for #{vmname}\n] choice = cli.ask("\n Enter Choice:").to_i case choice when 1 newname = cli.ask("\nPlease enter a valid hostname for the new virtual machine (lowercase with no spaces or special characters): ") new_hostname = Jupiter::Input.new.clean_filename(newname) draw_line('#', 80, '', 'white', '') draw_line('#', 80, "The new hostname will be", 'white', 'cyan') draw_line('#', 80, '', 'white', '') draw_line(' ', 80, "#{new_hostname}", 'white', 'green') draw_line('#', 80, '', 'white', '') ip = vmserver.guest_ip(vmname) vm = Jupiter::VirtualMachine.new(ip, 2500) puts vm.rename_linux_guest(new_hostname) manage_vm(vmserver, ) when 2 puts %Q[\nSending reboot command to #{vmname}...\n] vmserver.reboot_vm(vmname) when 3 puts %Q[\nSending shutdown command to #{vmname}...\n] vmserver.shutdown_vm(vmname) when 4 cycle = cli.ask("\nAre you sure? Only use this option if the system is locked up. Otherwise use the standard restart options for the guest OS. (yes / no): ") if cycle == 'yes' puts %Q[\nCycling the power for #{vmname}...\n] vmserver.power_cycle_vm(vmname) else puts %[\nExiting...\n] return end when 5 kill = cli.ask("\nAre you sure? (yes / no): ") if kill == 'yes' puts %Q[\nKilling power for #{vmname}...\n] vmserver.power_off_vm(vmname) else puts %[\nExiting...\n] return end when 6 ip = vmserver.guest_ip(vmname) vm = Jupiter::VirtualMachine.new(ip, 2500) puts vm.remove_from_sudo_group('deploy') manage_vm(vmserver, ) when 7 puts vmserver.guest_ip(vmname) manage_vm(vmserver, ) when 8 current_size = vmserver.vmdk_size(vmname) puts %Q[\nThe current HD size is #{current_size} GB.\n] size_in_gb = cli.ask(%Q[\nPlease enter the new size for the virtual HD in gigabytes: ]) vmserver.resize_vmdk(vmname, size_in_gb) new_size = vmserver.vmdk_size(vmname) puts %Q[\nThe new HD size is #{new_size} GB.\n] manage_vm(vmserver, ) when 9 ip = vmserver.guest_ip(vmname) vm = Jupiter::VirtualMachine.new(ip, 2500) puts vm.resize_lvm_volume_step_1 vmserver.reboot_vm(vmname) sleep 45 puts vm.resize_lvm_volume_step_2 manage_vm(vmserver, ) when 10 draw_new_line draw_line('#', 80, '', 'white', '') draw_line('#', 80, "Nagios File Generation", 'white', 'cyan') draw_line('#', 80, '', 'white', '') draw_line(' ', 80, "This will generate a Nagios object entry that can be pasted into", 'white', 'green') draw_line(' ', 80, "your nagios config or uploaded to your nagios server if a proper", 'white', 'green') draw_line(' ', 80, "nagios server entry is in your Jupiter yaml file.", 'white', 'green') draw_new_line draw_line(' ', 80, "This will also only be valid if your system has a static IP address and", 'white', 'green') draw_line(' ', 80, "the services will only be functional if the nagios client is installed.", 'white', 'green') draw_line('#', 80, '', 'white', '') nagios_choice = cli.ask("\n Enter 1 to display Nagios entry or 2 to upload to Nagios Server:").to_i nagios = Jupiter::Nagios.new nagios.action(nagios_choice, vmname, vmname, vmserver.guest_ip(vmname)) manage_vm(vmserver, ) else puts %[\nInvalid choice. Exiting...\n] return end end |
#select_vm_to_manage(vmserver) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/jupiter/cli/output.rb', line 73 def select_vm_to_manage(vmserver) vm_list = vmserver.server_vm_list draw_line('#', 80, '', 'white', '') draw_line('#', 80, "These are the currect virtual machines found on #{vmserver.name}", 'white', 'green') draw_line('#', 80, '', 'white', '') draw_new_line vm_list.each_index { |x| puts "#{x + 1}. #{vm_list[x]}" } draw_new_line draw_line('#', 80, '', 'white', '') targetvm = cli.ask("\nPlease select the number of the Virtual Machine you wish to manage:") target = targetvm.to_i - 1 if vm_list[target].nil? || target < 0 raise InvalidSelectionError.new("Not a valid selection.") else targetvm = vmserver.server_vm_list[target].to_s draw_new_line draw_line('#', 80, '', 'white', '') draw_line('#', 80, "#{targetvm} is selected for managment", 'white', 'green') draw_line('#', 80, '', 'white', '') return targetvm end end |
#show_templates_available(templatehost) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/jupiter/cli/output.rb', line 137 def show_templates_available(templatehost) templatevm = '' templatelist = templatehost.templates_list draw_line('#', 80, '', 'white', '') draw_line('#', 80, "These are the currect templates found on #{templatehost.host}", 'white', 'green') draw_line('#', 80, '', 'white', '') draw_new_line templatelist.each { |x| puts "* #{x.gsub('.tar.gz','')}" } while templatelist.include?(templatevm + '.tar.gz') == false do templatevm = cli.ask("\nPlease enter the valid name of the template you wish to use:") end return templatevm end |
#show_vm_ip(vmserver, vm) ⇒ Object
120 121 122 123 124 125 |
# File 'lib/jupiter/cli/output.rb', line 120 def show_vm_ip(vmserver, vm) draw_line('#', 80, '', 'white', '') draw_line('#', 80, "#{vm}'s current IP is: #{vmserver.guest_ip(vm)}", 'white', 'green') draw_line('#', 80, '', 'white', '') draw_new_line end |
#vm_current_state(vmserver, targetvm) ⇒ Object
114 115 116 117 118 |
# File 'lib/jupiter/cli/output.rb', line 114 def vm_current_state(vmserver, targetvm) puts %Q[\nConnecting to VMware host server #{vmserver.name}...] puts %Q[\n#{targetvm}'s current state is "#{vmserver.vm_state(targetvm)}"\nNote that this is only valid if VMware Tools is running on the virtual machine.\nIf the kernel has been upgraded recently, you many need to run vmware-config-tools.pl again.] return vmserver.vm_state(targetvm) end |