Module: Sahara::Session
- Defined in:
- lib/sahara/session.rb
Class Method Summary collapse
- .commit(selection) ⇒ Object
- .determine_vboxcmd ⇒ Object
- .execute(command) ⇒ Object
- .initialize ⇒ Object
- .is_snapshot_mode_on?(boxname) ⇒ Boolean
- .is_vm_created?(boxname) ⇒ Boolean
- .list_snapshots(boxname) ⇒ Object
- .off(selection) ⇒ Object
- .on(selection) ⇒ Object
- .on_selected_vms(selection, &block) ⇒ Object
- .rollback(selection) ⇒ Object
- .status(selection) ⇒ Object
Class Method Details
.commit(selection) ⇒ Object
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 |
# File 'lib/sahara/session.rb', line 49 def self.commit(selection) self.initialize on_selected_vms(selection) do |boxname| if !is_snapshot_mode_on?(boxname) puts "[#{boxname}] - this requires that sandbox mode is on." else instance_uuid="#{@vagrant_env.vms[boxname.to_sym].uuid}" #Discard snapshot so current state is the latest state puts "[#{boxname}] - unwinding sandbox" execute("#{@vboxcmd} snapshot \"#{instance_uuid}\" delete \"#{@sandboxname}\"") #Now retake the snapshot puts "[#{boxname}] - fastforwarding sandbox" execute("#{@vboxcmd} snapshot \"#{instance_uuid}\" take \"#{@sandboxname}\" --pause") end end end |
.determine_vboxcmd ⇒ Object
8 9 10 |
# File 'lib/sahara/session.rb', line 8 def self.determine_vboxcmd return "VBoxManage" end |
.execute(command) ⇒ Object
150 151 152 153 154 |
# File 'lib/sahara/session.rb', line 150 def self.execute(command) #puts "#{command}" output=Sahara::Shell.execute("#{command}") return output end |
.initialize ⇒ Object
12 13 14 15 16 |
# File 'lib/sahara/session.rb', line 12 def self.initialize @vagrant_env=Vagrant::Environment.new @vboxcmd=determine_vboxcmd @sandboxname="sahara-sandbox" end |
.is_snapshot_mode_on?(boxname) ⇒ Boolean
177 178 179 180 |
# File 'lib/sahara/session.rb', line 177 def self.is_snapshot_mode_on?(boxname) snapshots=list_snapshots(boxname) return snapshots.include?(@sandboxname) end |
.is_vm_created?(boxname) ⇒ Boolean
156 157 158 159 160 161 162 163 |
# File 'lib/sahara/session.rb', line 156 def self.is_vm_created?(boxname) if @vagrant_env.vms[boxname.to_sym].nil? puts "[#{boxname}] - Box name doesn't exist in the Vagrantfile" return false else return !@vagrant_env.vms[boxname.to_sym].uuid.nil? end end |
.list_snapshots(boxname) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/sahara/session.rb', line 165 def self.list_snapshots(boxname) instance_uuid="#{@vagrant_env.vms[boxname.to_sym].uuid}" snapshotlist=Array.new snapshotresult=execute("#{@vboxcmd} showvminfo --machinereadable \"#{instance_uuid}\" |grep ^SnapshotName| cut -d '=' -f 2") snapshotresult.each do |result| clean=result.gsub(/\"/,'').chomp snapshotlist << clean end return snapshotlist end |
.off(selection) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/sahara/session.rb', line 127 def self.off(selection) self.initialize on_selected_vms(selection) do |boxname| instance_uuid="#{@vagrant_env.vms[boxname.to_sym].uuid}" if !is_snapshot_mode_on?(boxname) puts "[#{boxname}] - this requires that sandbox mode is on." else puts "[#{boxname}] - switching sandbox off" # We might wanna check for sandbox changes or not #Discard snapshot execute("#{@vboxcmd} snapshot \"#{instance_uuid}\" delete \"#{@sandboxname}\" ") end end end |
.on(selection) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/sahara/session.rb', line 31 def self.on(selection) self.initialize on_selected_vms(selection) do |boxname| if is_snapshot_mode_on?(boxname) puts "[#{boxname}] - snapshot mode is already on" else instance_uuid="#{@vagrant_env.vms[boxname.to_sym].uuid}" #Creating a snapshot puts "[#{boxname}] - Enabling sandbox" execute("#{@vboxcmd} snapshot \"#{instance_uuid}\" take \"#{@sandboxname}\" --pause") end end end |
.on_selected_vms(selection, &block) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/sahara/session.rb', line 182 def self.on_selected_vms(selection,&block) if selection.nil? #puts "no selection was done" @vagrant_env.vms.each do |name,vm| #puts "Processing #{name}" if @vagrant_env.multivm? if name.to_s!="default" if is_vm_created?(name) yield name else puts "[#{name}] - machine needs to be upped first" end end else if is_vm_created?(name) yield name else puts "[#{name}] - machine needs to be upped first" end end end else if is_vm_created?(selection) yield selection else puts "[#{selection}] - machine needs to be upped first" end end end |
.rollback(selection) ⇒ Object
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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/sahara/session.rb', line 75 def self.rollback(selection) self.initialize on_selected_vms(selection) do |boxname| if !is_snapshot_mode_on?(boxname) puts "[#{boxname}] - this requires that sandbox mode is on." else instance_uuid="#{@vagrant_env.vms[boxname.to_sym].uuid}" puts "[#{boxname}] - powering off machine" #Poweroff machine execute("#{@vboxcmd} controlvm \"#{instance_uuid}\" poweroff") #Poweroff takes a second or so to complete; Virtualbox will throw errors #if you try to restore a snapshot before it's ready. sleep 2 puts "[#{boxname}] - roll back machine" #Rollback until snapshot execute("#{@vboxcmd} snapshot \"#{instance_uuid}\" restore \"#{@sandboxname}\"") puts "[#{boxname}] - starting the machine again" #Startvm again # # Grab the boot_mode setting from the Vagrantfile config_boot_mode="#{@vagrant_env.vms[boxname.to_sym].config.vm.boot_mode.to_s}" case config_boot_mode when 'vrdp' boot_mode='headless' when 'headless' boot_mode='headless' when 'gui' boot_mode='gui' else puts "Vagrantfile config.vm.boot_mode=#{config_boot_mode} setting unknown - defaulting to headless" boot_mode='headless' end execute("#{@vboxcmd} startvm --type #{boot_mode} \"#{instance_uuid}\" ") end end end |
.status(selection) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/sahara/session.rb', line 18 def self.status(selection) self.initialize on_selected_vms(selection) do |boxname| if is_snapshot_mode_on?(boxname) puts "[#{boxname}] - snapshot mode is on" else puts "[#{boxname}] - snapshot mode is off" end end end |