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_compute_offering, #resolve_disk_offering, #resolve_domain, #resolve_host, #resolve_iso, #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, #get_server_fqdn, #get_server_public_ip, #get_ssh_port_forwarding_rule, #print_job_status, #print_options, #run_background_jobs, #update_job_status, #update_jobs, #watch_jobs

Instance Method Details

#create(*names) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/cloudstack-cli/commands/virtual_machine.rb', line 88

def create(*names)
  vm_options_to_params

  say "Start deploying virtual machine#{ "s" if names.size > 1 }...", :green
  jobs = names.map do |name|
    if virtual_machine = client.list_virtual_machines(name: name, project_id: options[:project_id]).first
      say "virtual machine #{name} (#{virtual_machine["state"]}) already exists.", :yellow
      job = {
        id: 0,
        name: "Create virtual machine #{name}",
        status: 1
      }
    else
      job = {
        id: client.deploy_virtual_machine(options, {sync: true})['jobid'],
        name: "Create virtual machine #{name}"
      }
    end
    job
  end
  watch_jobs(jobs)
  if options[:port_rules].size > 0
    say "Create port forwarding rules...", :green
    jobs = []
    names.each do |name|
      virtual_machine = client.list_virtual_machine(name: name, project_id: options[:project_id]).first
      create_port_rules(virtual_machine, options[:port_rules], false).each_with_index do |job_id, index|
        jobs << {
          id: job_id,
          name: "Create port forwarding ##{index + 1} rules for virtual machine #{virtual_machine['name']}"
        }
      end
    end
    watch_jobs(jobs)
  end
  say "Finished.", :green
end

#create_interactiveObject



150
151
152
# File 'lib/cloudstack-cli/commands/virtual_machine.rb', line 150

def create_interactive
  bootstrap_server_interactive
end

#destroy(*names) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/cloudstack-cli/commands/virtual_machine.rb', line 130

def destroy(*names)
  resolve_project
  names.each do |name|
    unless virtual_machine = client.list_virtual_machines(options.merge(name: name, listall: true)).first
      say "Virtual machine #{name} not found.", :red
    else
      ask = "Destroy #{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



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

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

#list_from_file(file) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cloudstack-cli/commands/virtual_machine.rb', line 44

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
    ) if options[:command]
  end
end

#reboot(name) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/cloudstack-cli/commands/virtual_machine.rb', line 188

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

#show(name) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cloudstack-cli/commands/virtual_machine.rb', line 59

def show(name)
  resolve_project
  options[:name] = name
  unless virtual_machine = client.list_virtual_machines({list_all: true}.merge options).first
    puts "No virtual machine found."
  else
    table = virtual_machine.map do |key, value|
      [ set_color("#{key}:", :yellow), "#{value}" ]
    end
    print_table table
  end
end

#start(name) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/cloudstack-cli/commands/virtual_machine.rb', line 172

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

#stop(name) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/cloudstack-cli/commands/virtual_machine.rb', line 157

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