Class: VirtualMachine

Inherits:
CloudstackCli::Base show all
Defined in:
lib/cloudstack-cli/commands/virtual_machine.rb

Constant Summary

Constants included from CloudstackCli::Helper

CloudstackCli::Helper::ASYNC_STATES

Instance Attribute Summary

Attributes inherited from CloudstackCli::Base

#config

Instance Method Summary collapse

Methods inherited from CloudstackCli::Base

exit_on_failure?, start

Methods included from CloudstackCli::OptionResolver

#resolve_account, #resolve_cluster, #resolve_compute_offering, #resolve_disk_offering, #resolve_domain, #resolve_host, #resolve_ip_network_list, #resolve_iso, #resolve_iso_for_vm_deployment, #resolve_networks, #resolve_project, #resolve_snapshot, #resolve_template, #resolve_virtual_machine, #resolve_zone, #vm_options_to_params

Methods included from CloudstackCli::Helper

#ask_number, #bootstrap_server, #bootstrap_server_interactive, #create_port_rules, #create_server, #get_server_default_nic, #pf_rule_to_object, #print_job_status, #print_options, #run_background_jobs, #update_job_status, #update_jobs, #watch_jobs

Methods inherited from Thor

banner, basename2, old_subcommand, subcommand

Instance Method Details

#create(*names) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/cloudstack-cli/commands/virtual_machine.rb', line 101

def create(*names)
  if names.size == 0
    say "Please provide at least one virtual machine name.", :yellow
    exit 1
  end

  if options[:ip_network_list]
    options[:ip_network_list] = array_to_network_list(options[:ip_network_list])
  end

  vm_options_to_params
  say "Start deploying virtual machine#{"s" if names.size > 1}...", :green
  jobs = names.map do |name|
    if virtual_machine = find_vm_by_name(name)
      say "virtual machine #{name} (#{virtual_machine["state"]}) already exists.", :yellow
      job = {name: "Create virtual machine #{name}", status: 3}
    else
      job = {
        args: options.merge(name: name),
        name: "Create VM #{name}",
        status: -1
      }
    end
    job
  end

  if jobs.count{|job| job[:status] < 1 } > 0
    run_background_jobs(jobs, "deploy_virtual_machine")
  end

  successful_jobs = jobs.count {|job| job[:status] == 1 }
  if options[:port_rules].size > 0 && successful_jobs > 0
    say "Create port forwarding rules...", :green
    pjobs = []
    jobs.select{|job| job[:status] == 1}.each do |job|
      vm = job[:result]["virtualmachine"]
      create_port_rules(vm, options[:port_rules], false).each_with_index do |job_id, index|
        pjobs << {
          id: job_id,
          name: "Create port forwarding rule #{options[:port_rules][index]} for VM #{vm['name']}"
        }
      end
    end
    watch_jobs(pjobs)
  end
  say "Finished.", :green

  if successful_jobs > 0
    if options[:assumeyes] || yes?("Display password(s) for VM(s)? [y/N]:", :yellow)
      pw_table = [%w(VM Password)]
      jobs.select {|job| job[:status] == 1 && job[:result] }.each do |job|
        if result = job[:result]["virtualmachine"]
          pw_table << ["#{result["name"]}:", result["password"] || "n/a"]
        end
      end
      print_table(pw_table) if pw_table.size > 0
    end
  end
end

#create_interactiveObject



190
191
192
# File 'lib/cloudstack-cli/commands/virtual_machine.rb', line 190

def create_interactive
  bootstrap_server_interactive
end

#destroy(*names) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/cloudstack-cli/commands/virtual_machine.rb', line 165

def destroy(*names)
  if names.size == 0
    say "Please provide at least one virtual machine name.", :yellow
    exit 1
  end
  resolve_project
  names.each do |name|
    unless virtual_machine = find_vm_by_name(name)
      say "Virtual machine #{name} not found.", :red
    else
      action = options[:expunge] ? "Expunge" : "Destroy"
      ask = "#{action} #{virtual_machine['name']} (#{virtual_machine['state']})? [y/N]:"
      if options[:force] || yes?(ask, :yellow)
        say "destroying #{name} "
        client.destroy_virtual_machine(
          id: virtual_machine["id"],
          expunge: options[:expunge]
        )
        puts
      end
    end
  end
end

#listObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cloudstack-cli/commands/virtual_machine.rb', line 22

def list
  add_filters_to_options("listVirtualMachines") if options[:filter]
  
  resolve_project
  resolve_zone
  resolve_host
  resolve_iso
  if options[:command]
    command = options[:command].downcase
    options.delete(:command)
  end
  virtual_machines = client.list_virtual_machines(options)
  virtual_machines = filter_objects(virtual_machines) if options[:filter]
  if virtual_machines.size < 1
    puts "No virtual machines found."
  else
    print_virtual_machines(virtual_machines)
    execute_virtual_machines_commands(command, virtual_machines, options) if command
  end
end

#list_from_file(file) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cloudstack-cli/commands/virtual_machine.rb', line 51

def list_from_file(file)
  virtual_machines = parse_file(file)["virtual_machines"]
  if virtual_machines.size < 1
    puts "No virtual machines found."
  else
    print_virtual_machines(virtual_machines)
    execute_virtual_machines_commands(
      options[:command].downcase,
      virtual_machines,
      options
    ) if options[:command]
  end
end

#reboot(name) ⇒ Object



225
226
227
228
229
230
231
232
233
234
# File 'lib/cloudstack-cli/commands/virtual_machine.rb', line 225

def reboot(name)
  resolve_project
  unless virtual_machine = find_vm_by_name(name)
    say "Virtual machine #{name} not found.", :red
    exit 1
  end
  exit unless options[:force] || yes?("Reboot virtual_machine #{virtual_machine["name"]}? [y/N]:", :magenta)
  client.reboot_virtual_machine(id: virtual_machine['id'])
  puts
end

#show(name) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/cloudstack-cli/commands/virtual_machine.rb', line 67

def show(name)
  resolve_project
  options[:virtual_machine] = name
  virtual_machine = resolve_virtual_machine(true)
  table = virtual_machine.map do |key, value|
    [ set_color("#{key}:", :yellow), "#{value}" ]
  end
  print_table table
end

#start(name) ⇒ Object



211
212
213
214
215
216
217
218
219
220
# File 'lib/cloudstack-cli/commands/virtual_machine.rb', line 211

def start(name)
  resolve_project
  unless virtual_machine = find_vm_by_name(name)
    say "Virtual machine #{name} not found.", :red
    exit 1
  end
  say("Starting virtual machine #{virtual_machine['name']}", :magenta)
  client.start_virtual_machine(id: virtual_machine['id'])
  puts
end

#stop(name) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
# File 'lib/cloudstack-cli/commands/virtual_machine.rb', line 197

def stop(name)
  resolve_project
  unless virtual_machine = find_vm_by_name(name)
    say "Virtual machine #{name} not found.", :red
    exit 1
  end
  exit unless options[:force] ||
    yes?("Stop virtual machine #{virtual_machine['name']}? [y/N]:", :magenta)
  client.stop_virtual_machine(id: virtual_machine['id'])
  puts
end

#update(name) ⇒ Object



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
# File 'lib/cloudstack-cli/commands/virtual_machine.rb', line 261

def update(name)
  resolve_project
  unless vm = find_vm_by_name(name)
    say "Virtual machine #{name} not found.", :red
    exit 1
  end
  unless vm["state"].downcase == "stopped"
    say "Virtual machine #{name} (#{vm["state"]}) must be in a stopped state.", :red
    exit 1
  end
  unless options[:force] || yes?("Update virtual_machine #{name}? [y/N]:", :magenta)
    exit
  end
  if options[:user_data]
    # base64 encode user_data
    options[:user_data] = [options[:user_data]].pack("m")
  end
  vm = client.update_virtual_machine(options.merge(id: vm['id']))
  say "Virtual machine \"#{name}\" has been updated:", :green

  table = vm.select do |k, _|
    options.find {|k2, _| k2.gsub('_', '') == k }
  end.map do |key, value|
    [ set_color("#{key}:", :yellow), set_color("#{value}", :red) ]
  end
  print_table table
end