Module: Sahara::Session

Defined in:
lib/sahara/session.rb

Class Method Summary collapse

Class Method Details

.commit(selection) ⇒ Object



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
# File 'lib/sahara/session.rb', line 66

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_vboxcmdObject



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/sahara/session.rb', line 8

def self.determine_vboxcmd
  if windows?
    # On Windows, we use the VBOX_INSTALL_PATH environmental
    if ENV.has_key?("VBOX_INSTALL_PATH")
      # The path usually ends with a \ but we make sure here
      path = File.join(ENV["VBOX_INSTALL_PATH"], "VBoxManage.exe")
      return "\"#{path}\""
    end
  else
    # for other platforms assume it is on the PATH
    return "VBoxManage"
  end
end

.execute(command) ⇒ Object



167
168
169
170
171
# File 'lib/sahara/session.rb', line 167

def self.execute(command)
  #puts "#{command}"
  output=Sahara::Shell.execute("#{command}")
  return output
end

.initializeObject



29
30
31
32
33
# File 'lib/sahara/session.rb', line 29

def self.initialize
  @vagrant_env=Vagrant::Environment.new
  @vboxcmd=determine_vboxcmd
  @sandboxname="sahara-sandbox"
end

.is_snapshot_mode_on?(boxname) ⇒ Boolean

Returns:

  • (Boolean)


195
196
197
198
# File 'lib/sahara/session.rb', line 195

def self.is_snapshot_mode_on?(boxname)
  snapshots=list_snapshots(boxname)
  return snapshots.include?(@sandboxname)
end

.is_vm_created?(boxname) ⇒ Boolean

Returns:

  • (Boolean)


173
174
175
176
177
178
179
180
# File 'lib/sahara/session.rb', line 173

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



182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/sahara/session.rb', line 182

def self.list_snapshots(boxname)

  instance_uuid="#{@vagrant_env.vms[boxname.to_sym].uuid}"
  snapshotlist=Array.new
  output=execute("#{@vboxcmd} showvminfo --machinereadable \"#{instance_uuid}\"").join
  snapshotresult=output.scan(/SnapshotName.*=(.*)/).flatten
  snapshotresult.each do |result|
    clean=result.gsub(/\"/,'').chomp
    snapshotlist << clean
  end
  return snapshotlist
end

.off(selection) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/sahara/session.rb', line 144

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sahara/session.rb', line 48

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



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/sahara/session.rb', line 200

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 not created"
          end
        end
      else
        if is_vm_created?(name)
          yield name
        else
          puts "[#{name}] - machine not created"
        end
      end
    end
  else
    if is_vm_created?(selection)
      yield selection
    else
      puts "[#{selection}] - machine not created"
    end
  end
end

.rollback(selection) ⇒ Object



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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/sahara/session.rb', line 92

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



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sahara/session.rb', line 35

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

.windows?Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
# File 'lib/sahara/session.rb', line 22

def self.windows?
  %W[mingw mswin].each do |text|
    return true if RbConfig::CONFIG["host_os"].downcase.include?(text)
  end
  false
end