class VirtualMachine
Public Instance Methods
Source
# File lib/cloudstack-cli/commands/virtual_machine.rb 348 def array_to_network_list(arr) 349 arr.each.map do |item| 350 name = item.split(':')[0] 351 ip = item.split(':')[1] 352 {"name" => name, "ip" => ip} 353 end 354 end
Source
# File lib/cloudstack-cli/commands/virtual_machine.rb 101 def create(*names) 102 if names.size == 0 103 say "Please provide at least one virtual machine name.", :yellow 104 exit 1 105 end 106 107 if options[:ip_network_list] 108 options[:ip_network_list] = array_to_network_list(options[:ip_network_list]) 109 end 110 111 vm_options_to_params 112 say "Start deploying virtual machine#{"s" if names.size > 1}...", :green 113 jobs = names.map do |name| 114 if virtual_machine = find_vm_by_name(name) 115 say "virtual machine #{name} (#{virtual_machine["state"]}) already exists.", :yellow 116 job = {name: "Create virtual machine #{name}", status: 3} 117 else 118 job = { 119 args: options.merge(name: name), 120 name: "Create VM #{name}", 121 status: -1 122 } 123 end 124 job 125 end 126 127 if jobs.count{|job| job[:status] < 1 } > 0 128 run_background_jobs(jobs, "deploy_virtual_machine") 129 end 130 131 successful_jobs = jobs.count {|job| job[:status] == 1 } 132 if options[:port_rules].size > 0 && successful_jobs > 0 133 say "Create port forwarding rules...", :green 134 pjobs = [] 135 jobs.select{|job| job[:status] == 1}.each do |job| 136 vm = job[:result]["virtualmachine"] 137 create_port_rules(vm, options[:port_rules], false).each_with_index do |job_id, index| 138 pjobs << { 139 id: job_id, 140 name: "Create port forwarding rule #{options[:port_rules][index]} for VM #{vm['name']}" 141 } 142 end 143 end 144 watch_jobs(pjobs) 145 end 146 say "Finished.", :green 147 148 if successful_jobs > 0 149 if options[:assumeyes] || yes?("Display password(s) for VM(s)? [y/N]:", :yellow) 150 pw_table = [%w(VM Password)] 151 jobs.select {|job| job[:status] == 1 && job[:result] }.each do |job| 152 if result = job[:result]["virtualmachine"] 153 pw_table << ["#{result["name"]}:", result["password"] || "n/a"] 154 end 155 end 156 print_table(pw_table) if pw_table.size > 0 157 end 158 end 159 end
Source
# File lib/cloudstack-cli/commands/virtual_machine.rb 190 def create_interactive 191 bootstrap_server_interactive 192 end
Source
# File lib/cloudstack-cli/commands/virtual_machine.rb 165 def destroy(*names) 166 if names.size == 0 167 say "Please provide at least one virtual machine name.", :yellow 168 exit 1 169 end 170 resolve_project 171 names.each do |name| 172 unless virtual_machine = find_vm_by_name(name) 173 say "Virtual machine #{name} not found.", :red 174 else 175 action = options[:expunge] ? "Expunge" : "Destroy" 176 ask = "#{action} #{virtual_machine['name']} (#{virtual_machine['state']})? [y/N]:" 177 if options[:force] || yes?(ask, :yellow) 178 say "destroying #{name} " 179 client.destroy_virtual_machine( 180 id: virtual_machine["id"], 181 expunge: options[:expunge] 182 ) 183 puts 184 end 185 end 186 end 187 end
Source
# File lib/cloudstack-cli/commands/virtual_machine.rb 328 def execute_virtual_machines_commands(command, virtual_machines, options = {}) 329 unless %w(start stop reboot).include?(command) 330 say "\nCommand #{options[:command]} not supported.", :red 331 exit 1 332 end 333 exit unless options[:force] || 334 yes?("\n#{command.capitalize} the virtual machine(s) above? [y/N]:", :magenta) 335 336 jobs = virtual_machines.map do |vm| 337 { 338 job_id: nil, 339 args: { id: vm["id"] }, 340 name: "#{command.capitalize} virtual machine #{vm['name']}", 341 status: -1 342 } 343 end 344 345 run_background_jobs(jobs, "#{command}_virtual_machine") 346 end
Source
# File lib/cloudstack-cli/commands/virtual_machine.rb 291 def find_vm_by_name(name) 292 client.list_virtual_machines( 293 name: options[:virtual_machine], 294 listall: true, 295 project_id: options[:project_id] 296 ).find {|vm| vm["name"] == name } 297 end
Source
# File lib/cloudstack-cli/commands/virtual_machine.rb 22 def list 23 add_filters_to_options("listVirtualMachines") if options[:filter] 24 resolve_account 25 resolve_project 26 resolve_zone 27 resolve_host 28 resolve_iso 29 if options[:command] 30 command = options[:command].downcase 31 options.delete(:command) 32 end 33 virtual_machines = client.list_virtual_machines(options) 34 virtual_machines = filter_objects(virtual_machines) if options[:filter] 35 if virtual_machines.size < 1 36 puts "No virtual machines found." 37 else 38 print_virtual_machines(virtual_machines) 39 execute_virtual_machines_commands(command, virtual_machines, options) if command 40 end 41 end
Source
# File lib/cloudstack-cli/commands/virtual_machine.rb 51 def list_from_file(file) 52 virtual_machines = parse_file(file)["virtual_machines"] 53 if virtual_machines.size < 1 54 puts "No virtual machines found." 55 else 56 print_virtual_machines(virtual_machines) 57 execute_virtual_machines_commands( 58 options[:command].downcase, 59 virtual_machines, 60 options 61 ) if options[:command] 62 end 63 end
Source
# File lib/cloudstack-cli/commands/virtual_machine.rb 299 def print_virtual_machines(virtual_machines) 300 case options[:format].to_sym 301 when :yaml 302 puts({virtual_machines: virtual_machines}.to_yaml) 303 when :json 304 puts JSON.pretty_generate(virtual_machines: virtual_machines) 305 else 306 with_i_name = virtual_machines.first['instancename'] 307 with_h_name = virtual_machines.first['hostname'] 308 table = [["Name", "State", "Offering", "Zone", options[:project_id] ? "Project" : "Account", "IP's"]] 309 table.first.insert(1, "Instance-Name") if with_i_name 310 table.first.insert(-1, "Host-Name") if with_h_name 311 virtual_machines.each do |virtual_machine| 312 table << [ 313 virtual_machine['name'], 314 virtual_machine['state'], 315 virtual_machine['serviceofferingname'], 316 virtual_machine['zonename'], 317 options[:project_id] ? virtual_machine['project'] : virtual_machine['account'], 318 virtual_machine['nic'].map { |nic| nic['ipaddress']}.join(' ') 319 ] 320 table.last.insert(1, virtual_machine['instancename']) if with_i_name 321 table.last.insert(-1, virtual_machine['hostname']) if with_h_name 322 end 323 print_table table 324 say "Total number of virtual machines: #{virtual_machines.count}" 325 end 326 end
Source
# File lib/cloudstack-cli/commands/virtual_machine.rb 225 def reboot(name) 226 resolve_project 227 unless virtual_machine = find_vm_by_name(name) 228 say "Virtual machine #{name} not found.", :red 229 exit 1 230 end 231 exit unless options[:force] || yes?("Reboot virtual_machine #{virtual_machine["name"]}? [y/N]:", :magenta) 232 client.reboot_virtual_machine(id: virtual_machine['id']) 233 puts 234 end
Source
# File lib/cloudstack-cli/commands/virtual_machine.rb 67 def show(name) 68 resolve_project 69 options[:virtual_machine] = name 70 virtual_machine = resolve_virtual_machine(true) 71 table = virtual_machine.map do |key, value| 72 [ set_color("#{key}:", :yellow), "#{value}" ] 73 end 74 print_table table 75 end
Source
# File lib/cloudstack-cli/commands/virtual_machine.rb 211 def start(name) 212 resolve_project 213 unless virtual_machine = find_vm_by_name(name) 214 say "Virtual machine #{name} not found.", :red 215 exit 1 216 end 217 say("Starting virtual machine #{virtual_machine['name']}", :magenta) 218 client.start_virtual_machine(id: virtual_machine['id']) 219 puts 220 end
Source
# File lib/cloudstack-cli/commands/virtual_machine.rb 197 def stop(name) 198 resolve_project 199 unless virtual_machine = find_vm_by_name(name) 200 say "Virtual machine #{name} not found.", :red 201 exit 1 202 end 203 exit unless options[:force] || 204 yes?("Stop virtual machine #{virtual_machine['name']}? [y/N]:", :magenta) 205 client.stop_virtual_machine(id: virtual_machine['id']) 206 puts 207 end
Source
# File lib/cloudstack-cli/commands/virtual_machine.rb 261 def update(name) 262 resolve_project 263 unless vm = find_vm_by_name(name) 264 say "Virtual machine #{name} not found.", :red 265 exit 1 266 end 267 unless vm["state"].downcase == "stopped" 268 say "Virtual machine #{name} (#{vm["state"]}) must be in a stopped state.", :red 269 exit 1 270 end 271 unless options[:force] || yes?("Update virtual_machine #{name}? [y/N]:", :magenta) 272 exit 273 end 274 if options[:user_data] 275 # base64 encode user_data 276 options[:user_data] = [options[:user_data]].pack("m") 277 end 278 vm = client.update_virtual_machine(options.merge(id: vm['id'])) 279 say "Virtual machine \"#{name}\" has been updated:", :green 280 281 table = vm.select do |k, _| 282 options.find {|k2, _| k2.gsub('_', '') == k } 283 end.map do |key, value| 284 [ set_color("#{key}:", :yellow), set_color("#{value}", :red) ] 285 end 286 print_table table 287 end