Permalink
Browse files

final release of first gem version

  • Loading branch information...
1 parent 6ed1a57 commit fc36312067fe43ea20c87a99706fa6b12d31fdba @jedi4ever committed Apr 28, 2011
Showing with 317 additions and 34 deletions.
  1. +1 −0 .gitignore
  2. +1 −1 .rvmrc
  3. +2 −30 Gemfile.lock
  4. +7 −3 README.md
  5. +5 −0 Rakefile
  6. +2 −0 lib/sahara.rb
  7. +33 −0 lib/sahara/command.rb
  8. +178 −0 lib/sahara/session.rb
  9. +58 −0 lib/sahara/shell.rb
  10. +3 −0 lib/sahara/version.rb
  11. +27 −0 sahara.gemspec
View
@@ -1,5 +1,6 @@
.DS_Store
gems/*
+pkgs/*
tmp/*
.vagrant
Vagrantfile
View
2 .rvmrc
@@ -1,5 +1,5 @@
rvm use ruby-1.8.7
rvm_gemset_create_on_use_flag=1
-rvm gemset use drifter
+rvm gemset use sahara
alias vagrant="bundle exec vagrant"
alias irb="bundle exec irb"
View
@@ -1,13 +1,8 @@
PATH
remote: .
specs:
- drifter (0.0.1)
- cucumber (= 0.8.5)
- highline (~> 1.6.1)
- net-ssh (~> 2.1.0)
+ sahara (0.0.1)
popen4 (~> 0.1.2)
- progressbar
- rspec (~> 2.5.0)
thor (~> 0.14.6)
vagrant (~> 0.7.0)
@@ -17,24 +12,12 @@ GEM
Platform (0.4.0)
abstract (1.0.0)
archive-tar-minitar (0.5.2)
- builder (2.1.2)
- cucumber (0.8.5)
- builder (~> 2.1.2)
- diff-lcs (~> 1.1.2)
- gherkin (~> 2.1.4)
- json_pure (~> 1.4.3)
- term-ansicolor (~> 1.0.4)
- diff-lcs (1.1.2)
erubis (2.6.6)
abstract (>= 1.0.0)
ffi (0.6.3)
rake (>= 0.8.7)
- gherkin (2.1.5)
- trollop (~> 1.16.2)
- highline (1.6.1)
i18n (0.5.0)
json (1.5.1)
- json_pure (1.4.6)
mario (0.0.6)
net-scp (1.0.4)
net-ssh (>= 1.99.1)
@@ -43,19 +26,8 @@ GEM
popen4 (0.1.2)
Platform (>= 0.4.0)
open4 (>= 0.4.0)
- progressbar (0.9.0)
rake (0.8.7)
- rspec (2.5.0)
- rspec-core (~> 2.5.0)
- rspec-expectations (~> 2.5.0)
- rspec-mocks (~> 2.5.0)
- rspec-core (2.5.1)
- rspec-expectations (2.5.0)
- diff-lcs (~> 1.1.2)
- rspec-mocks (2.5.0)
- term-ansicolor (1.0.5)
thor (0.14.6)
- trollop (1.16.2)
vagrant (0.7.3)
archive-tar-minitar (= 0.5.2)
erubis (~> 2.6.6)
@@ -73,4 +45,4 @@ PLATFORMS
ruby
DEPENDENCIES
- drifter!
+ sahara!
View
@@ -1,6 +1,6 @@
# Description
-Sahara allows working in sandbox mode within vagrant.
+Sahara allows vagrant to operate in sandbox mode
Typical usage:
@@ -10,6 +10,10 @@ Typical usage:
- If not satisfied you can rollback: <pre>vagrant sandbox rollback</pre>
- To leave sandbox mod: <pre>vagrant sandbox off</pre>
-Many kudos go to the creators of vagrant
+Many kudos go to the creators of [vagrant](http://vagrantup.com)
-Note: this is currently work in progress
+# Installation
+
+This is now available as gem:
+
+<pre>gem install sahara</pre>
View
@@ -0,0 +1,5 @@
+require 'rubygems'
+require 'bundler'
+require 'bundler/setup'
+Bundler::GemHelper.install_tasks
+
View
@@ -0,0 +1,2 @@
+require 'vagrant'
+require 'sahara/command'
View
@@ -0,0 +1,33 @@
+require 'sahara/session'
+
+module Sahara
+ class Command < Vagrant::Command::GroupBase
+ register "sandbox","Manages a sandbox"
+
+ desc "status [NAME]", "Shows the status of the sandbox"
+ def status(boxname=nil)
+ Sahara::Session.status(boxname)
+ end
+
+ desc "on [NAME]", "Enters sandbox state"
+ def on(boxname=nil)
+ Sahara::Session.on(boxname)
+ end
+
+ desc "commit [NAME]", "Commits changes - moves sandbox initial state to currentstate"
+ def commit(boxname=nil)
+ Sahara::Session.commit(boxname)
+ end
+
+ desc "rollback [NAME]", "Rollback changes since sandbox state was entered "
+ def rollback(boxname=nil)
+ Sahara::Session.rollback(boxname)
+ end
+
+ desc "off [NAME] ", "Leaves sandbox state"
+ def off(boxname=nil)
+ Sahara::Session.off(boxname)
+ end
+
+ end
+end
View
@@ -0,0 +1,178 @@
+require 'pp'
+
+require 'drift/shell'
+
+module Drift
+ module Session
+
+ def self.determine_vboxcmd
+ return "VBoxManage"
+ end
+
+ def self.initialize
+ @vagrant_env=Vagrant::Environment.new
+ @vboxcmd=determine_vboxcmd
+ @sandboxname="drift-sandbox"
+ end
+
+ 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
+
+ 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_name="#{@vagrant_env.vms[boxname.to_sym].vm.name}"
+
+ #Creating a snapshot
+ puts "[#{boxname}] - Enabling sandbox"
+
+ execute("#{@vboxcmd} snapshot '#{instance_name}' take '#{@sandboxname}'")
+ end
+
+ end
+ end
+
+ 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_name="#{@vagrant_env.vms[boxname.to_sym].vm.name}"
+
+ #Discard snapshot so current state is the latest state
+ puts "[#{boxname}] - unwinding sandbox"
+ execute("#{@vboxcmd} snapshot '#{instance_name}' delete '#{@sandboxname}'")
+
+ #Now retake the snapshot
+ puts "[#{boxname}] - fastforwarding sandbox"
+
+ execute("#{@vboxcmd} snapshot '#{instance_name}' take '#{@sandboxname}'")
+
+ end
+
+ end
+
+ end
+
+ 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_name="#{@vagrant_env.vms[boxname.to_sym].vm.name}"
+
+ puts "[#{boxname}] - powering off machine"
+
+ #Poweroff machine
+ execute("#{@vboxcmd} controlvm '#{instance_name}' poweroff")
+
+ puts "[#{boxname}] - roll back machine"
+
+ #Rollback until snapshot
+ execute("#{@vboxcmd} snapshot '#{instance_name}' restore '#{@sandboxname}'")
+
+ puts "[#{boxname}] - starting the machine again"
+
+ #Startvm again
+ execute("#{@vboxcmd} startvm --type headless '#{instance_name}' ")
+
+ end
+
+ end
+
+
+ end
+
+ def self.off(selection)
+ self.initialize
+
+ on_selected_vms(selection) do |boxname|
+
+
+ instance_name="#{@vagrant_env.vms[boxname.to_sym].vm.name}"
+
+ 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_name}' delete '#{@sandboxname}' ")
+
+ end
+
+ end
+ end
+
+ def self.execute(command)
+ #puts "#{command}"
+ output=Sahara::Shell.execute("#{command}")
+ return output
+ end
+
+ def self.is_vm_created?(boxname)
+ return !@vagrant_env.vms[boxname.to_sym].vm.nil?
+ end
+
+ def self.list_snapshots(boxname)
+
+ instance_name="#{@vagrant_env.vms[boxname.to_sym].vm.name}"
+ snapshotlist=Array.new
+ snapshotresult=execute("#{@vboxcmd} showvminfo --machinereadable '#{instance_name}' |grep ^SnapshotName| cut -d '=' -f 2")
+ snapshotresult.each do |result|
+ clean=result.gsub(/\"/,'').chomp
+ snapshotlist << clean
+ end
+ return snapshotlist
+ end
+
+ def self.is_snapshot_mode_on?(boxname)
+ snapshots=list_snapshots(boxname)
+ return snapshots.include?(@sandboxname)
+ end
+
+ 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}"
+ yield name
+ end
+ else
+ yield selection
+ end
+ end
+ end
+end
+
+
+#command="#{@vboxcmd} unregistervm '#{boxname}' --delete"
+#puts command
+#puts "Deleting vm #{boxname}"
+
+#Exec and system stop the execution here
+#Veewee::Shell.execute("#{command}")
+
View
@@ -0,0 +1,58 @@
+#require 'open4'
+
+module Sahara
+ class Shell
+
+ def self.execute(command,options = {})
+ STDOUT.sync = true
+ output=nil
+ result=IO.popen("#{command}") {|f| output=f.readlines}
+ #{ |f| print "#{f}, #{f.class}" }
+# output=result.read
+ return output
+ end
+
+ #pty allows you to gradually see the output of a local command
+ #http://www.shanison.com/?p=415
+ def self.execute2(command, options = {} )
+ require "pty"
+ begin
+ PTY.spawn( command ) do |r, w, pid|
+ begin
+ #r.each { }
+ r.each { |line| print line;}
+
+ rescue Errno::EIO
+ end
+ end
+ rescue PTY::ChildExited => e
+ puts "The child process exited!"
+ end
+ end
+
+ #occassinally fails with 'no child processes
+ def self.execute3(command, options = {} )
+ defaults= { :port => "22", :exitcode => "0", :user => "root"}
+ options=defaults.merge(options)
+
+ status = POpen4::popen4(command) do |stdout,stderr,stdin|
+ stdout.each do |line|
+ puts line
+ end
+ end
+
+ @status=status.to_i
+
+ if (@status.to_s != options[:exitcode] )
+ if (options[:exitcode]=="*")
+ #its a test so we don't need to worry
+ else
+ raise "Exitcode was not what we expected"
+ end
+
+ end
+
+ end
+
+ end #Class
+end #Module
View
@@ -0,0 +1,3 @@
+module Sahara
+ VERSION = "0.0.1"
+end
Oops, something went wrong.

0 comments on commit fc36312

Please sign in to comment.