Class: Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/vmfloaty/utils.rb

Class Method Summary collapse

Class Method Details

.format_hosts(hostname_hash) ⇒ Object

TODO: Takes the json response body from an HTTP GET request and “pretty prints” it



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/vmfloaty/utils.rb', line 7

def self.format_hosts(hostname_hash)
  host_hash = {}

  hostname_hash.delete("ok")
  hostname_hash.each do |type, hosts|
    if type == "domain"
      host_hash[type] = hosts
    else
      host_hash[type] = hosts["hostname"]
    end
  end

  host_hash.to_json
end

.generate_os_hash(os_args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/vmfloaty/utils.rb', line 22

def self.generate_os_hash(os_args)
  # expects args to look like:
  # ["centos", "debian=5", "windows=1"]

  # Build vm hash where
  #
  #  [operating_system_type1 -> total,
  #   operating_system_type2 -> total,
  #   ...]
  os_types = {}
  os_args.each do |arg|
    os_arr = arg.split("=")
    if os_arr.size == 1
      # assume they didn't specify an = sign if split returns 1 size
      os_types[os_arr[0]] = 1
    else
      os_types[os_arr[0]] = os_arr[1].to_i
    end
  end
  os_types
end

.prettyprint_hosts(hosts) ⇒ Object



44
45
46
47
48
49
# File 'lib/vmfloaty/utils.rb', line 44

def self.prettyprint_hosts(hosts)
  puts "Running VMs:"
  hosts.each do |vm|
    puts "- #{vm}"
  end
end