Module: CloudstackCli::Helper

Included in:
Base
Defined in:
lib/cloudstack-cli/helper.rb

Constant Summary collapse

ASYNC_STATES =
{
  -1 => "waiting",
  0  => "running",
  1  => "completed",
  2  => "error",
  3  => "aborted"
}

Instance Method Summary collapse

Instance Method Details

#ask_number(question) ⇒ Object



9
10
11
12
# File 'lib/cloudstack-cli/helper.rb', line 9

def ask_number(question)
  number = ask(question).to_i - 1
  number < 0 ? 0 : number
end

#bootstrap_server(args = {}) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/cloudstack-cli/helper.rb', line 139

def bootstrap_server(args = {})
  if args[:project] && project = find_project(args[:project])
    project_id = project["id"]
    project_name = project['name']
  end

  if args[:name]
    args['displayname'] = args[:name]
    name = args[:name]
  elsif args[:displayname]
    name = args[:displayname]
  end

  unless server = client.list_virtual_machines(name: args[:name], project_id: project_id).first
    say "Create VM #{name}...", :yellow
    server = client.deploy_virtual_machine(args)
    puts
    say "VM #{name} has been created.", :green
  else
    say "VM #{name} already exists (#{server["state"]}).", :yellow
  end

  if args[:port_rules] && args[:port_rules].size > 0
    create_port_rules(server, args[:port_rules])
  end
  server
end

#bootstrap_server_interactiveObject



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/cloudstack-cli/helper.rb', line 241

def bootstrap_server_interactive
  zones = client.list_zones
  if zones.size > 1
    say "Select a availability zone:", :yellow
    print_options(zones)
    zone = ask_number("Zone Nr.: ")
  else
    zone = 0
  end

  projects = client.list_projects
  project_id = nil
  if projects.size > 0
    if yes?("Do you want to deploy your VM within a project? (y/N)") && projects.size > 0
      say "Select a project", :yellow
      print_options(projects)
      project = ask_number("Project Nr.: ")
      project_id = projects[project]['id'] rescue nil
    end
  end

  say "Please provide a name for the new VM", :yellow
  say "(spaces or special characters are NOT allowed)"
  server_name = ask("Server name: ")

  server_offerings = client.list_service_offerings
  say "Select a computing offering:", :yellow
  print_options(server_offerings)
  service_offering = ask_number("Offering Nr.: ")

  templates = client.list_templates(project_id: project_id, zone_id: zones[zone]["id"], template_filter: "executable")
  say "Select a template:", :yellow
  print_options(templates)
  template = ask_number("Template Nr.: ")

  networks = client.list_networks(project_id: project_id, zone_id: zones[zone]["id"])
  if networks.size > 1
    say "Select a network:", :yellow
    print_options(networks)
    network = ask_number("Network Nr.: ")
  else
    network = 0
  end

  say "You entered the following configuration:", :yellow
  table =  [["Zone", zones[zone]["name"]]]
  table << ["VM Name", server_name]
  table << ["Template", templates[template]["name"]]
  table << ["Offering", server_offerings[service_offering]["name"]]
  table << ["Network", networks[network]["name"]]
  table << ["Project", projects[project]["name"]] if project
  print_table table

  if yes? "Do you want to deploy this VM? (y/N)"
    bootstrap_server(
      name: server_name,
      zone_id: zones[zone]["id"],
      template_id: templates[template]["id"],
      serviceoffering_id: server_offerings[service_offering]["id"],
      network_ids: network ? networks[network]["id"] : nil,
      project_id: project_id
    )
  end
end

#create_port_rules(server, port_rules, async = true) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/cloudstack-cli/helper.rb', line 178

def create_port_rules(server, port_rules, async = true)
  frontendip_id = nil
  jobs = []
  client.verbose = async
  project_id = server['projectid'] || nil
  port_rules.each do |pf_rule|
    pf_rule = pf_rule_to_object(pf_rule)
    if pf_rule[:ipaddress]
      pub_ip = client.list_public_ip_addresses(
        network_id: get_server_default_nic(server)["networkid"],
        project_id: project_id,
        ipaddress: pf_rule[:ipaddress]
      )
      ip_addr = pub_ip.find { |addr| addr['ipaddress'] == pf_rule[:ipaddress]} if pub_ip
      if ip_addr
        frontendip = ip_addr['id']
      else
        say "Error: IP #{pf_rule[:ipaddress]} not found.", :red
        next
      end
    end

    # check if there is already an existing rule
    rules = client.list_port_forwarding_rules(
      networkid: get_server_default_nic(server)["networkid"],
      ipaddressid: frontendip_id,
      projectid: project_id
    )
    existing_pf_rules = rules.find do |rule|
      # remember matching address for additional rules
      frontendip_id = rule['ipaddressid'] if rule['virtualmachineid'] == server['id']

      rule['virtualmachineid'] == server['id'] &&
      rule['publicport'] == pf_rule[:publicport] &&
      rule['privateport'] == pf_rule[:privateport] &&
      rule['protocol'] == pf_rule[:protocol]
    end

    if existing_pf_rules
      say "Port forwarding rule on port #{pf_rule[:privateport]} for VM #{server["name"]} already exists.", :yellow
    else
      unless frontendip_id
        frontendip_id = client.associate_ip_address(
          network_id: get_server_default_nic(server)["networkid"],
          project_id: project_id
        )['ipaddress']['id']
      end
      args = pf_rule.merge({
        ipaddressid: frontendip_id,
        virtualmachineid: server["id"]
      })
      if async
        say "Create port forwarding rule #{pf_rule[:ipaddress]}:#{port} for VM #{server["name"]}.", :yellow
        client.create_port_forwarding_rule(args)
        return
      else
        jobs << client.create_port_forwarding_rule(args, {sync: true})['jobid']
      end
    end
  end
  jobs
end

#create_server(args = {}) ⇒ Object



167
168
169
170
171
172
173
174
175
176
# File 'lib/cloudstack-cli/helper.rb', line 167

def create_server(args = {})
  if args[:project] && project = find_project(args[:project])
    project_id = project["id"]
    project_name = project['name']
  end
  unless server = client.list_virtual_machines(name: args[:name], project_id: project_id)
    server = client.deploy_virtual_machine(args)
  end
  server
end

#get_server_default_nic(server) ⇒ Object



306
307
308
309
310
# File 'lib/cloudstack-cli/helper.rb', line 306

def get_server_default_nic(server)
  server['nic'].each do |nic|
    return nic if nic['isdefault']
  end
end

#pf_rule_to_object(pf_rule) ⇒ Object



312
313
314
315
316
317
318
319
320
# File 'lib/cloudstack-cli/helper.rb', line 312

def pf_rule_to_object(pf_rule)
  pf_rule = pf_rule.split(":")
  {
    ipaddress: (pf_rule[0] == '' ? nil : pf_rule[0]),
    privateport: pf_rule[1],
    publicport: (pf_rule[2] || pf_rule[1]),
    protocol: (pf_rule[3] || 'tcp').downcase
  }
end


125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/cloudstack-cli/helper.rb', line 125

def print_job_status(jobs, spinner, opts = {t_start: Time.now})
  print ("\r" + "\e[A\e[K" * (jobs.size + 1)) unless opts[:no_clear]
  jobs.each_with_index do |job, i|
    print "#{job[:name]} : job #{ASYNC_STATES[job[:status]]} "
    puts job[:status] == 0 ? spinner.first : ""
  end
  t_elapsed = opts[:t_start] ? (Time.now - opts[:t_start]).round(1) : 0
  completed = jobs.count{|j| j[:status] == 1 }
  say "Completed: #{completed} of #{jobs.size} (#{t_elapsed}s)", :magenta
  sleep opts[:sleeptime] || 0.1
  spinner.push spinner.shift
  spinner
end


3
4
5
6
7
# File 'lib/cloudstack-cli/helper.rb', line 3

def print_options(options, attr = 'name')
  options.to_enum.with_index(1).each do |option, i|
     puts "#{i}: #{option[attr]}"
  end
end

#run_background_jobs(jobs, command) ⇒ Object



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
# File 'lib/cloudstack-cli/helper.rb', line 64

def run_background_jobs(jobs, command)
  view_thread = Thread.new do
    chars = %w(| / - \\)
    call = 0
    opts = {t_start: Time.now}

    while jobs.count{|job| job[:status] < 1 } > 0 do
      if call.modulo(40) == 0
        t = Thread.new { jobs = update_jobs(jobs, command) }
        while t.alive?
          chars = print_job_status(jobs, chars,
            call == 0 ? opts.merge(no_clear: true) : opts
          )
          call += 1
        end
        t.join
      else
        chars = print_job_status(jobs, chars,
          call == 0 ? opts.merge(no_clear: true) : opts
        )
        call += 1
      end
    end
    print_job_status(jobs, chars,
      call == 0 ? opts.merge(no_clear: true) : opts
    )
  end
  view_thread.join
end

#update_job_status(jobs) ⇒ Object



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

def update_job_status(jobs)
  jobs.each do |job|
    job[:status] = 0 unless job[:status]
    if job[:status] == 0
      result = client.query_async_job_result(job_id: job[:id])
      job[:status] = result["jobstatus"]
      # add result information for terminated jobs
      if job[:status].between?(1, 2)
        job[:result] = result["jobresult"]
      end
    end
  end
  jobs
end

#update_jobs(jobs, command) ⇒ Object



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
# File 'lib/cloudstack-cli/helper.rb', line 94

def update_jobs(jobs, command)
  # update running job status
  threads = jobs.select{|job| job[:status] == 0 }.map do |job|
    Thread.new do
      result = client.query_async_job_result(job_id: job[:job_id])
      job[:status] = result['jobstatus']
      if job[:status].between?(1, 2)
        job[:result] = result["jobresult"]
      end
    end
  end
  threads.each(&:join)

  # launch new jobs if required and possible
  launch_capacity = (options[:concurrency] ||= 10) - jobs.count{|job| job[:status] == 0 }
  threads = []
  jobs.select{|job| job[:status] == -1 }.each do |job|
    if launch_capacity > 0
      threads << Thread.new do
        job[:job_id] = client.send(
          command, job[:args], { sync: true }
        )['jobid']
        job[:status] = 0
      end
      launch_capacity -= 1
    end
  end
  threads.each(&:join)
  jobs
end

#watch_jobs(jobs) ⇒ Object



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
# File 'lib/cloudstack-cli/helper.rb', line 22

def watch_jobs(jobs)
  chars = %w(| / - \\)
  call = 0
  opts = {t_start: Time.now}
  jobs = update_job_status(jobs)
  while jobs.count{|job| job[:status].to_i < 1 } > 0 do
    if call.modulo(40) == 0
      t = Thread.new { jobs = update_job_status(jobs) }
      while t.alive?
        chars = print_job_status(jobs, chars,
          call == 0 ? opts.merge(no_clear: true) : opts
        )
        call += 1
      end
      t.join
    else
      chars = print_job_status(jobs, chars,
        call == 0 ? opts.merge(no_clear: true) : opts
      )
      call += 1
    end
  end
  print_job_status(jobs, chars,
    call == 0 ? opts.merge(no_clear: true) : opts
  )
end