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_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(stackfile) ⇒ Object



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
66
67
68
69
70
71
72
# File 'lib/cloudstack-cli/commands/stack.rb', line 9

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|
      if !options[:limit] || options[:limit].include?(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.merge!({
            name: name,
            displayname: instance["decription"],
            zone: instance["zone"] || stack["zone"],
            project: stack["project"],
            template: instance["template"],
            iso: instance["iso"] ,
            offering: instance["offering"],
            networks: load_string_or_array(instance["networks"]),
            ip_network_list: instance["ip_network_list"],
            disk_offering: instance["disk_offering"],
            size: instance["disk_size"],
            group: instance["group"] || stack["group"],
            keypair: instance["keypair"] || stack["keypair"],
            ip_address: instance["ip_address"]
          })
          jobs << {
            id: client.deploy_virtual_machine(
              vm_options_to_params,
              {sync: true}
            )['jobid'],
            name: "Create VM #{name}"
          }
        end
      end
    end
  end
  watch_jobs(jobs)

  unless options[:skip_forwarding_rules]
    say "Check for port forwarding rules...", :green
    jobs = []
    stack["servers"].each do |instance|
      string_to_array(instance["name"]).each do |name|
        if (!options[:limit] || options[:limit].include?(name)) && 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|
            job_name = "Create port forwarding rules (#{port_rules[index]}) for VM #{name}"
            jobs << {id: job_id, name: job_name}
          end
        end
      end
    end
    watch_jobs(jobs)
  end
  say "Finished.", :green
end

#destroy(stackfile) ⇒ Object



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

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 do |name|
      if !options[:limit] || options[:limit].include?(name)
        servers << name
      end
    end
  end

  if servers.size == 0
    say "No servers in stack selected.", :yellow
    exit
  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