Class: Utils
- Inherits:
-
Object
- Object
- Utils
- Defined in:
- lib/vmfloaty/utils.rb
Class Method Summary collapse
-
.format_hosts(hostname_hash) ⇒ Object
TODO: Takes the json response body from an HTTP GET request and “pretty prints” it.
- .generate_os_hash(os_args) ⇒ Object
- .get_all_token_vms(verbose, url, token) ⇒ Object
- .get_vm_info(hosts, verbose, url) ⇒ Object
- .prettyprint_hosts(hosts, verbose, url) ⇒ Object
- .prettyprint_status(status, message, pools, verbose) ⇒ Object
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 21 22 23 24 25 |
# File 'lib/vmfloaty/utils.rb', line 7 def self.format_hosts(hostname_hash) host_hash = {} hostname_hash.delete("ok") domain = hostname_hash["domain"] hostname_hash.each do |type, hosts| if type != "domain" if hosts["hostname"].kind_of?(Array) hosts["hostname"].map!{|host| host + "." + domain } else hosts["hostname"] = hosts["hostname"] + "." + domain end host_hash[type] = hosts["hostname"] end end host_hash.to_json end |
.generate_os_hash(os_args) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/vmfloaty/utils.rb', line 27 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 |
.get_all_token_vms(verbose, url, token) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/vmfloaty/utils.rb', line 83 def self.get_all_token_vms(verbose, url, token) # get vms with token status = Auth.token_status(verbose, url, token) vms = status[token]['vms'] if vms.nil? raise "You have no running vms" end running_vms = vms['running'] running_vms end |
.get_vm_info(hosts, verbose, url) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/vmfloaty/utils.rb', line 49 def self.get_vm_info(hosts, verbose, url) vms = {} hosts.each do |host| vm_info = Pooler.query(verbose, url, host) if vm_info['ok'] vms[host] = {} vms[host]['domain'] = vm_info[host]['domain'] vms[host]['template'] = vm_info[host]['template'] vms[host]['lifetime'] = vm_info[host]['lifetime'] vms[host]['running'] = vm_info[host]['running'] vms[host]['tags'] = vm_info[host]['tags'] end end vms end |
.prettyprint_hosts(hosts, verbose, url) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/vmfloaty/utils.rb', line 65 def self.prettyprint_hosts(hosts, verbose, url) puts "Running VMs:" vm_info = get_vm_info(hosts, verbose, url) vm_info.each do |vm,info| domain = info['domain'] template = info['template'] lifetime = info['lifetime'] running = info['running'] = info['tags'] || {} tag_pairs = .map {|key,value| "#{key}: #{value}" } duration = "#{running}/#{lifetime} hours" = [template, duration, *tag_pairs] puts "- #{vm}.#{domain} (#{metadata.join(", ")})" end end |
.prettyprint_status(status, message, pools, verbose) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/vmfloaty/utils.rb', line 96 def self.prettyprint_status(status, , pools, verbose) pools.select! {|name,pool| pool['ready'] < pool['max']} if ! verbose width = pools.keys.map(&:length).max pools.each do |name,pool| begin max = pool['max'] ready = pool['ready'] pending = pool['pending'] missing = max - ready - pending char = 'o' puts "#{name.ljust(width)} #{(char*ready).green}#{(char*pending).yellow}#{(char*missing).red}" rescue => e puts "#{name.ljust(width)} #{e.red}" end end puts puts .colorize(status['status']['ok'] ? :default : :red) end |