Class: VM
- Inherits:
-
Object
- Object
- VM
- Defined in:
- lib/floatyhelper/vm.rb
Class Method Summary collapse
- .alive(host, query = nil) ⇒ Object
-
.destroy(id) ⇒ Object
VM Management ###.
- .find_pooled_platforms ⇒ Object
- .get_current_lifetime(host) ⇒ Object
-
.get_vm(platform: 'centos-7-x86_64', force_abs: false) ⇒ Object
Get a VM from floaty ###.
- .getsnapshot(id, snaptag) ⇒ Object
- .increaselife(id, amount = nil) ⇒ Object
- .pooled_platforms_default ⇒ Object
- .query(host) ⇒ Object
- .revert(id, snaptag) ⇒ Object
- .snaplist(tag) ⇒ Object
-
.snapshot(id, snaptag) ⇒ Object
Snapshots ###.
- .snaptag_exists?(id, snaptag) ⇒ Boolean
Class Method Details
.alive(host, query = nil) ⇒ Object
60 61 62 63 |
# File 'lib/floatyhelper/vm.rb', line 60 def self.alive(host, query = nil) query ||= query(host) query['ok'] && query[host]['state'] == 'running' end |
.destroy(id) ⇒ Object
VM Management ###
42 43 44 45 46 47 48 |
# File 'lib/floatyhelper/vm.rb', line 42 def self.destroy(id) hosts = Hosts.get_hosts_from_id(id) Groups.delete_tag(id) if Groups.tag?(id) Groups.delete_all if id == 'all' hosts = hosts.select { |host| alive(host) } puts Floaty.floaty_cmd("delete #{hosts.join(',')} --service vmpooler") unless hosts.empty? end |
.find_pooled_platforms ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/floatyhelper/vm.rb', line 26 def self.find_pooled_platforms begin # rubocop:disable Style/RedundantBegin result = Net::HTTP.get('vmpooler.delivery.puppetlabs.net','/status') result = JSON.parse(result) # Techinally, 'max > 0' tells you if it's a pooled platform, but if # the pool is empty, we'll want to fall back to ABS anyway. result['pools'].select { |_pool, info| info['ready'].positive? }.map { |pool, _info| pool.gsub('-pixa4','') } rescue StandardError # Not a great practice to swallow all errors, but this list is probably # pretty stable, so let's just pass along the default. puts 'Error looking up pooled platforms from vmpooler.delivery.puppetlabs.net. Using default pooled platform list instead.'.yellow pooled_platforms_default end end |
.get_current_lifetime(host) ⇒ Object
55 56 57 58 |
# File 'lib/floatyhelper/vm.rb', line 55 def self.get_current_lifetime(host) status = query(host) status['ok'] ? status[host]['lifetime'] : nil end |
.get_vm(platform: 'centos-7-x86_64', force_abs: false) ⇒ Object
Get a VM from floaty ###
159 160 161 162 163 164 165 166 167 168 |
# File 'lib/floatyhelper/vm.rb', line 159 def self.get_vm(platform: 'centos-7-x86_64', force_abs: false) if !find_pooled_platforms.include?(platform.gsub('-pixa4','')) || force_abs response = Floaty.floaty_cmd("get #{platform} --service abs --priority 1", use_pty: true) response.chomp.split('- ')[1].split[0].split('.')[0] else response = Floaty.floaty_cmd("get #{platform} --service vmpooler") raise "Error obtaining a VM: #{response}" if response.include?('error') response.split[1].split('.')[0] end end |
.getsnapshot(id, snaptag) ⇒ Object
131 132 133 134 |
# File 'lib/floatyhelper/vm.rb', line 131 def self.getsnapshot(id, snaptag) data = Config.load_data data['snapshots'][id][snaptag] end |
.increaselife(id, amount = nil) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/floatyhelper/vm.rb', line 65 def self.increaselife(id, amount = nil) amount ||= Config.get_config_setting('increaselife').to_i return if amount < 1 hosts = Hosts.get_hosts_from_id(id) hosts.each do |host| if (lifetime = get_current_lifetime(host)) lifetime += amount output = Floaty.floaty_cmd("modify #{host} --lifetime #{lifetime} --service vmpooler") if output =~ /Successfully modified/ puts "#{host} lifetime set to #{lifetime} hours".green else puts "Error setting VM lifetime: #{output}".red end else puts "Could not query host #{host}".red end end end |
.pooled_platforms_default ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/floatyhelper/vm.rb', line 12 def self.pooled_platforms_default [ 'centos-7-x86_64', 'centos-8-x86_64', 'oracle-7-x86_64', 'redhat-7-x86_64', 'redhat-8-x86_64', 'redhat-fips-7-x86_64', 'scientific-7-x86_64', 'sles-12-x86_64', 'ubuntu-1804-x86_64', ] end |
.query(host) ⇒ Object
50 51 52 53 |
# File 'lib/floatyhelper/vm.rb', line 50 def self.query(host) output = Floaty.floaty_cmd("query #{host} --service vmpooler") Floaty.parse_floaty_json_output(output) end |
.revert(id, snaptag) ⇒ Object
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/floatyhelper/vm.rb', line 147 def self.revert(id, snaptag) snaptag ||= 'Blank Snaptag' shas = VM.getsnapshot(id, snaptag) shas.each do |host, sha| print "#{host}: #{sha} --- " puts Floaty.floaty_cmd("revert #{host} #{sha} --service vmpooler") end puts 'Waiting 10 seconds for revert to take effect' sleep(10) end |
.snaplist(tag) ⇒ Object
141 142 143 144 145 |
# File 'lib/floatyhelper/vm.rb', line 141 def self.snaplist(tag) data = Config.load_data return [] if !data['snapshots'].keys.include?(tag) data['snapshots'][tag].keys end |
.snapshot(id, snaptag) ⇒ Object
Snapshots ###
85 86 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 121 122 123 124 125 126 127 128 129 |
# File 'lib/floatyhelper/vm.rb', line 85 def self.snapshot(id, snaptag) clr = Config.get_config_setting('vertical_snapshot_status') clr = clr.to_s.downcase == 'true' snaptag ||= 'Blank Snaptag' hosts = Hosts.get_hosts_from_id(id) shas = {} # There's probably a better way to do this... puts `tput clear` if clr hosts.each do |host| result = Floaty.floaty_cmd("snapshot #{host} --service vmpooler") answer = Floaty.parse_floaty_json_output(result) sha = answer[host]['snapshot'] puts "#{host}: #{sha}" shas[host] = sha end data = Config.load_data data['snapshots'] ||= {} data['snapshots'][id] ||= {} data['snapshots'][id][snaptag] = shas Config.write_data(data) puts puts 'Waiting for snapshots to appear in floaty query...' alldone = false until alldone puts `tput cup #{hosts.count + 2}` if clr alldone = true print "\r" unless clr hosts.each do |host| answer = query(host)[host] done = answer.keys.include?('snapshots') && answer['snapshots'].include?(shas[host]) status = done ? 'Done'.green : 'Wait'.yellow if clr puts "* %s #{status} *" % "#{host}:".ljust(16) else print "* %s #{status} *" % "#{host}:".ljust(16) end alldone &= done end sleep(1) end puts '' end |
.snaptag_exists?(id, snaptag) ⇒ Boolean
136 137 138 139 |
# File 'lib/floatyhelper/vm.rb', line 136 def self.snaptag_exists?(id, snaptag) data = Config.load_data data['snapshots'].keys.include?(id) ? data['snapshots'][id].keys.include?(snaptag) : false end |