Class: Vbox::Transaction

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_snapshot_vmachine(vmname, snapname) ⇒ Object



27
28
29
30
31
# File 'lib/vbox/transaction.rb', line 27

def self.create_snapshot_vmachine(vmname,snapname)
  @vboxcmd=Vbox::Session.determine_vboxcmd

  Vbox::Shell.execute("#{@vboxcmd} snapshot '#{vmname}' take '#{snapname}'")
end

.list_snapshots(vmname) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/vbox/transaction.rb', line 111

def self.list_snapshots(vmname)
  @vboxcmd=Vbox::Session.determine_vboxcmd

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

.load_snapshot_vmachine(vmname, snapname) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/vbox/transaction.rb', line 33

def self.load_snapshot_vmachine(vmname,snapname)
  @vboxcmd=Vbox::Session.determine_vboxcmd

  #if it running , shutdown first
  if (state_vmachine(vmname)=="running")
    stop_vmachine(vmname)
  end

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

.remove_snapshot_vmachine(vmname, snapname) ⇒ Object



21
22
23
24
25
# File 'lib/vbox/transaction.rb', line 21

def self.remove_snapshot_vmachine(vmname,snapname)
  @vboxcmd=Vbox::Session.determine_vboxcmd

  Vbox::Shell.execute("#{@vboxcmd} snapshot '#{vmname}' delete '#{snapname}'")
end

.rollback_snapshot(vmname, snapname) ⇒ Object



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
101
102
103
104
105
106
107
108
# File 'lib/vbox/transaction.rb', line 65

def self.rollback_snapshot(vmname,snapname)
  delete_flag=false
  @vboxcmd=Vbox::Session.determine_vboxcmd

  savestate_recover=false
  if (state_vmachine(vmname)=="running")
    Vbox::Shell.execute("#{@vboxcmd} 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)
      sleep 1
    end
  }


  sleep 2

  Vbox::Shell.execute("#{@vboxcmd} 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
    Vbox::Shell.execute("#{@vboxcmd} controlvm '#{vmname}' nic1 nat")
    Vbox::Shell.execute("#{@vboxcmd} controlvm '#{vmname}' setlinkstate1 off")
    Vbox::Shell.execute("#{@vboxcmd} 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



47
48
49
50
51
52
53
54
# File 'lib/vbox/transaction.rb', line 47

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



56
57
58
59
60
61
62
63
# File 'lib/vbox/transaction.rb', line 56

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/vbox/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/vbox/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