Class: Veewee::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/veewee/transaction.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_snapshot_vmachine(vmname, snapname) ⇒ Object



25
26
27
# File 'lib/veewee/transaction.rb', line 25

def self.create_snapshot_vmachine(vmname,snapname)
  Veewee::Shell.execute("VBoxManage snapshot '#{vmname}' take '#{snapname}'")
end

.list_snapshots(vmname) ⇒ Object



103
104
105
106
107
# File 'lib/veewee/transaction.rb', line 103

def self.list_snapshots(vmname)
  snapshotresult=Veewee::Shell.execute("VBoxManage showvminfo --machinereadable '#{vmname}' |grep ^SnapshotName| cut -d '=' -f 2").stdout
  snapshotlist=snapshotresult.gsub(/\"/,'').split(/\n/)
  return snapshotlist
end

.load_snapshot_vmachine(vmname, snapname) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/veewee/transaction.rb', line 29

def self.load_snapshot_vmachine(vmname,snapname)
  #if it running , shutdown first
  if (state_vmachine(vmname)=="running")
    stop_vmachine(vmname)
  end

  Veewee::Shell.execute("VBoxManage snapshot '#{vmname}' restore '#{snapname}'")
  #sometimes it takes some time to shutdown
  sleep 2
  Veewee::Shell.execute("VBoxManage startvm '#{vmname}'")
end

.remove_snapshot_vmachine(vmname, snapname) ⇒ Object



21
22
23
# File 'lib/veewee/transaction.rb', line 21

def self.remove_snapshot_vmachine(vmname,snapname)
  Veewee::Shell.execute("VBoxManage snapshot '#{vmname}' delete '#{snapname}'")
end

.rollback_snapshot(vmname, snapname) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/veewee/transaction.rb', line 59

def self.rollback_snapshot(vmname,snapname)
delete_flag=false

savestate_recover=false
if (state_vmachine(vmname)=="running")
  Veewee::Shell.execute("VBoxManage controlvm '#{vmname}' savestate")
  savestate_recover=true
 end

list_snapshots(vmname).each { |name|
  if name.match(/^#{snapname}-/) then
    delete_flag=true
  end
  if (delete_flag) then
    remove_snapshot_vmachine(vmname,name)
  end
}


 sleep 2

  Veewee::Shell.execute("VBoxManage startvm '#{vmname}'")

  if (savestate_recover)
    #Recovering from savestate nukes the network! This trick seem to work
    #Also within the vm /etc/init.d/networking restart , but that is OS specific
    #http://www.virtualbox.org/ticket/5666
    #http://www.virtualbox.org/ticket/5654
    #This is supposed to be fixed: http://www.virtualbox.org/changeset/25205 but alas
    Veewee::Shell.execute("VBoxManage controlvm '#{vmname}' nic1 nat")
    Veewee::Shell.execute("VBoxManage controlvm '#{vmname}' setlinkstate1 off")
    Veewee::Shell.execute("VBoxManage controlvm '#{vmname}' setlinkstate1 on")
    sleep 2

    #hmmm, virtualbox => when recovering from a restore , it looses the nat settings!!! So we need to do this again!!
    #thebox.ssh_enable_vmachine({:hostport => host_port , :guestport => 22} )
    #http://www.virtualbox.org/changeset/25402
    #[25402]: NAT: re-establish port-forwarding after savestate / restore state

  end

end

.snapshot_exists(vmname, snapname) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/veewee/transaction.rb', line 41

def self.snapshot_exists(vmname,snapname)
  list_snapshots(vmname).each { |name|
    if (name==snapname) then
      return true
    end
  }
  return false
end

.snapshot_version_exists(vmname, snapname) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/veewee/transaction.rb', line 50

def self.snapshot_version_exists(vmname,snapname)
      list_snapshots(vmname).each { |name|
        if name.match(/^#{snapname}-/) then
          return true
        end
      }
      return false
end

Instance Method Details

#transaction(name, params, &block) ⇒ Object



4
5
# File 'lib/veewee/transaction.rb', line 4

def transaction(name,params, &block)
end

#transaction2(name, options = { :checksum => "nochecksum"}, &block) ⇒ Object



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

def transaction2(name,options= { :checksum => "nochecksum"}, &block)
   if snapshot_exists(@vmname,name+"-"+options[:checksum])
      load_snapshot_vmachine(@vmname,name+"-"+options[:checksum])
    else
      if snapshot_version_exists(@vmname,name)
        rollback_snapshot(@vmname,name)
        #rollback to snapshot prior to this one
      end
      yield
      create_snapshot_vmachine(@vmname,name+"-"+options[:checksum])
    end
  #end
end