Class: VM

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

Class Method Summary collapse

Class Method Details

.destroy(id) ⇒ Object



7
8
9
10
11
12
# 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'
  puts `floaty delete #{hosts.join(',')}`
end

.get_current_lifetime(host) ⇒ Object



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

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

.getsnapshot(id, snaptag) ⇒ Object



74
75
76
77
# File 'lib/floatyhelper/vm.rb', line 74

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

.increaselife(id, amount) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/floatyhelper/vm.rb', line 23

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



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

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

.revert(id, snaptag) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/floatyhelper/vm.rb', line 84

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) ⇒ Object



37
38
39
40
41
42
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
# File 'lib/floatyhelper/vm.rb', line 37

def self.snapshot(id, snaptag)
  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

  puts 'Waiting for snapshots to appear in floaty query...'
  alldone = false
  while !alldone do
    alldone = true
    print "\r"
    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} *"
      alldone &= done
    end
    sleep(1)
  end
  puts ''

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

.snaptag_exists?(id, snaptag) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
# File 'lib/floatyhelper/vm.rb', line 79

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