Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
317 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,5 +1,6 @@ | |||
.DS_Store | .DS_Store | ||
gems/* | gems/* | ||
pkgs/* | |||
tmp/* | tmp/* | ||
.vagrant | .vagrant | ||
Vagrantfile | Vagrantfile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,5 +1,5 @@ | |||
rvm use ruby-1.8.7 | rvm use ruby-1.8.7 | ||
rvm_gemset_create_on_use_flag=1 | rvm_gemset_create_on_use_flag=1 | ||
rvm gemset use drifter | rvm gemset use sahara | ||
alias vagrant="bundle exec vagrant" | alias vagrant="bundle exec vagrant" | ||
alias irb="bundle exec irb" | alias irb="bundle exec irb" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,5 @@ | |||
require 'rubygems' | |||
require 'bundler' | |||
require 'bundler/setup' | |||
Bundler::GemHelper.install_tasks | |||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,2 @@ | |||
require 'vagrant' | |||
require 'sahara/command' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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}") | |||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,3 @@ | |||
module Sahara | |||
VERSION = "0.0.1" | |||
end |
Oops, something went wrong.