Class: VM

Inherits:
Object
  • Object
show all
Defined in:
lib/floatyhelper/vm.rb

Class Method Summary collapse

Class Method Details

.alive(host, query = nil) ⇒ Object



24
25
26
27
# File 'lib/floatyhelper/vm.rb', line 24

def self.alive(host, query=nil)
  query ||= query(host)
  query['ok'] && query[host]['state'] == 'running'
end

.destroy(id) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/floatyhelper/vm.rb', line 7

def self.destroy(id)
  hosts = Hosts.get_hosts_from_id(id)
  Groups.delete_tag(id) if Groups.is_tag?(id)
  Groups.delete_all if id == 'all'
  hosts = hosts.select { |host| alive(host) }
  puts `floaty delete #{hosts.join(',')}` unless hosts.empty?
end

.get_current_lifetime(host) ⇒ Object



19
20
21
22
# File 'lib/floatyhelper/vm.rb', line 19

def self.get_current_lifetime(host)
  status = query(host)
  status['ok'] ? status[host]['lifetime'] : nil
end

.get_vm(platform = 'centos-7-x86_64') ⇒ Object



104
105
106
107
108
# File 'lib/floatyhelper/vm.rb', line 104

def self.get_vm(platform='centos-7-x86_64')
  response = `floaty get #{platform} 2>&1`
  raise "Error obtaining a VM: #{response}" if response.include?('error')
  return response.split(' ')[1].split('.')[0]
end

.getsnapshot(id, snaptag) ⇒ Object



83
84
85
86
# File 'lib/floatyhelper/vm.rb', line 83

def self.getsnapshot(id, snaptag)
  data = Conf.load_data
  data['snapshots'][id][snaptag]
end

.increaselife(id, amount) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/floatyhelper/vm.rb', line 29

def self.increaselife(id, amount)
  amount = 100 if amount.nil?
  hosts = Hosts.get_hosts_from_id(id)
  hosts.each do |host|
    if lifetime = get_current_lifetime(host)
      lifetime += amount
      print "#{host} to #{lifetime} hours: "
      puts `floaty modify #{host} --lifetime #{lifetime}`.split("\n")[0]
    else
      puts "Could not query host #{host}".red
    end
  end
end

.query(host) ⇒ Object



15
16
17
# File 'lib/floatyhelper/vm.rb', line 15

def self.query(host)
  eval(`floaty query #{host}`)
end

.revert(id, snaptag) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/floatyhelper/vm.rb', line 93

def self.revert(id, snaptag)
  snaptag = 'Blank Snaptag' unless snaptag
  shas = VM.getsnapshot(id, snaptag)
  shas.each do |host, sha|
    print "#{host}: #{sha} --- "
    puts `floaty revert #{host} #{sha}`
  end
  puts 'Waiting 10 seconds for revert to take effect'
  sleep(10)
end

.snapshot(id, snaptag, clr = false) ⇒ Object



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
# File 'lib/floatyhelper/vm.rb', line 43

def self.snapshot(id, snaptag, clr=false)
  snaptag = 'Blank Snaptag' unless snaptag
  hosts = Hosts.get_hosts_from_id(id)
  shas = {}
  hosts.each do |host|
    print "#{host}: "
    result = `floaty snapshot #{host}`
    message = result.split("\n")[0]
    answer = eval(result.sub(message,''))
    sha = answer[host]['snapshot']
    puts sha
    shas[host] = sha
  end

  data = Conf.load_data
  data['snapshots'] ||= {}
  data['snapshots'][id] ||= {}
  data['snapshots'][id][snaptag] = shas
  Conf.write_data(data)

  puts `tput clear` if clr
  puts 'Waiting for snapshots to appear in floaty query...'
  alldone = false
  while !alldone do
    puts `tput cup 1` if clr
    alldone = true
    print "\r" unless clr
    hosts.each do |host|
      answer = eval(`floaty query #{host}`)[host]
      done = answer.keys.include?('snapshots') && answer['snapshots'].include?(shas[host])
      status = done ? 'Done' : 'Wait'
      print "* #{host}: #{status} *" unless clr
      puts "* #{host}: #{status} *" if clr
      alldone &= done
    end
    sleep(1)
  end
  puts ''
end

.snaptag_exists?(id, snaptag) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
# File 'lib/floatyhelper/vm.rb', line 88

def self.snaptag_exists?(id, snaptag)
  data = Conf.load_data
  exists = data['snapshots'].keys.include?(id) ? data['snapshots'][id].keys.include?(snaptag) : false
end