Class: Stack

Inherits:
CloudstackCli::Base show all
Defined in:
lib/cloudstack-cli/commands/stack.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(stackfile) ⇒ Object



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

def create(stackfile)
  stack = parse_file(stackfile)
  project_id = find_project_by_name(stack["project"])

  say "Create stack #{stack["name"]}...", :green
  jobs = []
  stack["servers"].each do |instance|
    string_to_array(instance["name"]).each do |name|
      server = client.list_virtual_machines(name: name, project_id: project_id).first
      if server
        say "VM #{name} (#{server["state"]}) already exists.", :yellow
        jobs << {
          id: 0,
          name: "Create VM #{name}",
          status: 1
        }
      else
        options = {
          name: name,
          displayname: instance["decription"],
          zone: instance["zone"] || stack["zone"],
          template: instance["template"],
          iso: instance["iso"] ,
          offering: instance["offering"],
          networks: load_string_or_array(instance["networks"]),
          project: stack["project"],
          disk_offering: instance["disk_offering"],
          size: instance["disk_size"],
          group: instance["group"] || stack["group"],
          keypair: instance["keypair"] || stack["keypair"],
          sync: true
        }
        jobs << {
          id: client.deploy_virtual_machine(
            vm_options_to_params(options),
            {sync: true}
          )['jobid'],
          name: "Create VM #{name}"
        }
      end
    end
  end
  watch_jobs(jobs)

  say "Check for port forwarding rules...", :green
  jobs = []
  stack["servers"].each do |instance|
    string_to_array(instance["name"]).each do |name|
      if port_rules = string_to_array(instance["port_rules"])
        server = client.list_virtual_machines(name: name, project_id: project_id).first
        create_port_rules(server, port_rules, false).each_with_index do |job_id, index|
          jobs << {
            id: job_id,
            name: "Create port forwarding rules (#{port_rules[index]}) for VM #{name}"
          }
        end
      end
    end
  end
  watch_jobs(jobs)
  say "Finished.", :green
end

#destroy(stackfile) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/cloudstack-cli/commands/stack.rb', line 78

def destroy(stackfile)
  stack = parse_file(stackfile)
  project_id = find_project_by_name(stack["project"])
  servers = []
  stack["servers"].each do |server|
    string_to_array(server["name"]).each {|name| servers << name}
  end

  if options[:force] || yes?("Destroy the following VM #{servers.join(', ')}? [y/N]:", :yellow)
    jobs = []
    servers.each do |name|
      if server = client.list_virtual_machines(name: name, project_id: project_id).first
        jobs << {
          id: client.destroy_virtual_machine(
            { id: server['id'], expunge: options[:expunge] },
            { sync: true }
          )['jobid'],
          name: "Destroy VM #{name}"
        }
      end
    end
    watch_jobs(jobs)
    say "Finished.", :green
  end
end