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



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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/cloudstack-cli/commands/stack.rb', line 13

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)
        if server = client.list_virtual_machines(
          name: name, project_id: project_id, listall: true
        ).find {|vm| vm["name"] == name }
          say "VM #{name} (#{server["state"]}) already exists.", :yellow
          jobs << {
            id: 0,
            name: "Create VM #{name}",
            status: 3
          }
        else
          options.merge!({
            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"]
          })
          vm_options_to_params
          jobs << {
            job_id: nil,
            args: options.merge(name: name),
            name: "Create VM #{name}",
            status: -1
          }
        end
      end
    end
  end

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

  # count jobs with status 1 => Completed
  successful_jobs = jobs.count {|job| job[:status] == 1 }
  unless successful_jobs == 0 || options[:skip_forwarding_rules]
    say "Check for port forwarding rules...", :green
    pjobs = []
    jobs.select{|job| job[:status] == 1}.each do |job|
      vm = job[:result]["virtualmachine"]
      vm_def = find_vm_in_stack(vm["name"], stack)
      if port_rules = string_to_array(vm_def["port_rules"])
        create_port_rules(vm, port_rules, false).each_with_index do |job_id, index|
          job_name = "Create port forwarding rules (#{port_rules[index]}) for VM #{vm["name"]}"
          pjobs << {id: job_id, name: job_name}
        end
      end
    end
    watch_jobs(pjobs)
    pjobs.each do |job|
      if job[:result]
        result = job[:result]["portforwardingrule"]
        puts "Created port forwarding rule #{result['ipaddress']}:#{result['publicport']} => #{result['privateport']} for VM #{result['virtualmachinename']}"
      end
    end
  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

#destroy(stackfile) ⇒ Object



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

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 #{'and expunge ' if options[:expunge]}the following VM(s)? #{servers.join(', ')} [y/N]:", :yellow)
    jobs = []
    servers.each do |name|
      if server = client.list_virtual_machines(
        name: name, project_id: project_id, listall: true
        ).find {|vm| vm["name"] == name }
        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