sahara 0.0.15 → 0.0.16

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -5,3 +5,5 @@ tmp/*
5
5
  .vagrant
6
6
  Vagrantfile
7
7
  Gemfile.lock
8
+ .bundle/
9
+ vendor/
@@ -2,7 +2,6 @@
2
2
  # vim: set fileencoding=utf-8
3
3
 
4
4
  require "vagrant"
5
- require File.expand_path("../sahara/session", __FILE__)
6
5
 
7
6
  module Sahara
8
7
  class Plugin < Vagrant.plugin("2")
@@ -12,8 +11,14 @@ module Sahara
12
11
  DESC
13
12
 
14
13
  command("sandbox") do
14
+ setup_i18n
15
15
  require File.expand_path("../sahara/command/root", __FILE__)
16
16
  Command::Root
17
17
  end
18
+
19
+ def self.setup_i18n
20
+ I18n.load_path << File.expand_path('../../locales/en.yml', __FILE__)
21
+ I18n.reload!
22
+ end
18
23
  end
19
24
  end
@@ -16,10 +16,19 @@ module Sahara
16
16
  argv = parse_options(opts)
17
17
  return if !argv
18
18
 
19
- ses = Sahara::Session::Command.new(@app, @env)
20
-
21
19
  with_target_vms(argv, :reverse => true) do |machine|
22
- ses.commit(machine)
20
+
21
+ ses = Sahara::Session::Factory.create(machine)
22
+ if !ses.is_vm_created? then
23
+ puts "[#{machine.name}] VM is not created"
24
+ next
25
+ end
26
+ if ses.is_snapshot_mode_on? then
27
+ ses.commit
28
+ else
29
+ puts "[#{machine.name}] Not sandbox mode now"
30
+ end
31
+
23
32
  end
24
33
  end
25
34
  end
@@ -16,10 +16,19 @@ module Sahara
16
16
  argv = parse_options(opts)
17
17
  return if !argv
18
18
 
19
- ses = Sahara::Session::Command.new(@app, @env)
20
-
21
19
  with_target_vms(argv, :reverse => true) do |machine|
22
- ses.off(machine)
20
+
21
+ ses = Sahara::Session::Factory.create(machine)
22
+ if !ses.is_vm_created? then
23
+ puts "[#{machine.name}] VM is not created"
24
+ next
25
+ end
26
+ if ses.is_snapshot_mode_on? then
27
+ ses.off
28
+ else
29
+ puts "[#{machine.name}] Not sandbox mode now"
30
+ end
31
+
23
32
  end
24
33
  end
25
34
  end
@@ -1,5 +1,3 @@
1
- require "optparse"
2
-
3
1
  module Sahara
4
2
  module Command
5
3
  class On < Vagrant.plugin("2", :command)
@@ -18,10 +16,19 @@ module Sahara
18
16
  argv = parse_options(opts)
19
17
  return if !argv
20
18
 
21
- ses = Sahara::Session::Command.new(@app, @env)
22
-
23
19
  with_target_vms(argv, :reverse => true) do |machine|
24
- ses.on(machine)
20
+
21
+ ses = Sahara::Session::Factory.create(machine)
22
+ if !ses.is_vm_created? then
23
+ puts "[#{machine.name}] VM is not created"
24
+ next
25
+ end
26
+ if !ses.is_snapshot_mode_on? then
27
+ ses.on
28
+ else
29
+ puts "[#{machine.name}] Already sandbox mode"
30
+ end
31
+
25
32
  end
26
33
  end
27
34
  end
@@ -16,10 +16,19 @@ module Sahara
16
16
  argv = parse_options(opts)
17
17
  return if !argv
18
18
 
19
- ses = Sahara::Session::Command.new(@app, @env)
20
-
21
19
  with_target_vms(argv, :reverse => true) do |machine|
22
- ses.rollback(machine)
20
+
21
+ ses = Sahara::Session::Factory.create(machine)
22
+ if !ses.is_vm_created? then
23
+ puts "[#{machine.name}] VM is not created"
24
+ next
25
+ end
26
+ if !ses.is_snapshot_mode_on? then
27
+ puts "[#{machine.name}] Not sandbox mode now"
28
+ next
29
+ end
30
+ ses.rollback
31
+
23
32
  end
24
33
  end
25
34
  end
@@ -1,4 +1,5 @@
1
1
  require 'optparse'
2
+ require "sahara/session/factory"
2
3
 
3
4
  module Sahara
4
5
  module Command
@@ -1,5 +1,3 @@
1
- require 'optparse'
2
-
3
1
  module Sahara
4
2
  module Command
5
3
  class Status < Vagrant.plugin("2", :command)
@@ -19,10 +17,19 @@ module Sahara
19
17
  argv = parse_options(opts)
20
18
  return if !argv
21
19
 
22
- ses = Sahara::Session::Command.new(@app, @env)
23
-
24
20
  with_target_vms(argv, :reverse => true) do |machine|
25
- ses.status(machine)
21
+
22
+ ses = Sahara::Session::Factory.create(machine)
23
+ if !ses.is_vm_created? then
24
+ puts "[#{machine.name}] VM is not created"
25
+ next
26
+ end
27
+ if ses.is_snapshot_mode_on? then
28
+ puts "[#{machine.name}] Sandbox mode is on"
29
+ else
30
+ puts "[#{machine.name}] Sandbox mode is off"
31
+ end
32
+
26
33
  end
27
34
  end
28
35
  end
@@ -0,0 +1,33 @@
1
+ module Sahara
2
+ module Errors
3
+
4
+ class Error < Vagrant::Errors::VagrantError
5
+ error_namespace("sahara.errors")
6
+ end
7
+
8
+ class ProviderNotSupported < Sahara::Errors::Error
9
+ error_key("provider_not_supported")
10
+ end
11
+
12
+ class LibvirtConnectionError < Sahara::Errors::Error
13
+ error_key("libvirt_connection_error")
14
+ end
15
+
16
+ class SnapshotMissing < Sahara::Errors::Error
17
+ error_key("snapshot_missing")
18
+ end
19
+
20
+ class SnapshotDeletionError < Sahara::Errors::Error
21
+ error_key("snapshot_deletion_error")
22
+ end
23
+
24
+ class SnapshotCreationError < Sahara::Errors::Error
25
+ error_key("snapshot_creation_error")
26
+ end
27
+
28
+ class SnapshotReversionError < Sahara::Errors::Error
29
+ error_key("snapshot_reversion_error")
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,20 @@
1
+ require "sahara/errors"
2
+
3
+ module Sahara
4
+ module Session
5
+ class Factory
6
+ def self.create(machine)
7
+ case machine.provider_name
8
+ when :virtualbox
9
+ require_relative "virtualbox"
10
+ Virtualbox.new(machine)
11
+ when :libvirt
12
+ require_relative "libvirt"
13
+ Libvirt.new(machine)
14
+ else
15
+ raise Sahara::Errors::ProviderNotSupported
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,124 @@
1
+ require "fog"
2
+
3
+ module Sahara
4
+ module Session
5
+ class Libvirt
6
+
7
+ def initialize(machine)
8
+ @machine=machine
9
+ @sandboxname="sahara-sandbox"
10
+ @connection=connect_to_libvirt
11
+ @domain = @connection.client.lookup_domain_by_uuid(@machine.id)
12
+ end
13
+
14
+ # based on VagrantPlugins::ProviderLibvirt::Action::ConnectLibvirt
15
+ def connect_to_libvirt
16
+ # Get config options for libvirt provider.
17
+ config = @machine.provider_config
18
+
19
+ # Setup connection uri.
20
+ uri = config.driver
21
+ if config.connect_via_ssh
22
+ uri << '+ssh://'
23
+ if config.username
24
+ uri << config.username + '@'
25
+ end
26
+
27
+ if config.host
28
+ uri << config.host
29
+ else
30
+ uri << 'localhost'
31
+ end
32
+ else
33
+ uri << '://'
34
+ uri << config.host if config.host
35
+ end
36
+
37
+ uri << '/system?no_verify=1'
38
+ # set ssh key for access to libvirt host
39
+ home_dir = `echo ${HOME}`.chomp
40
+ uri << "&keyfile=#{home_dir}/.ssh/id_rsa"
41
+
42
+ conn_attr = {}
43
+ conn_attr[:provider] = 'libvirt'
44
+ conn_attr[:libvirt_uri] = uri
45
+ conn_attr[:libvirt_username] = config.username if config.username
46
+ conn_attr[:libvirt_password] = config.password if config.password
47
+
48
+ begin
49
+ Fog::Compute.new(conn_attr)
50
+ rescue Fog::Errors::Error => e
51
+ raise Sahara::Errors::LibvirtConnectionError,
52
+ :error_message => e.message
53
+ end
54
+ end
55
+
56
+ def get_snapshot_if_exists
57
+ # if we can get snapshot description without exception it exists
58
+ begin
59
+ snapshot = @domain.lookup_snapshot_by_name(@sandboxname)
60
+ snapshot_desc = snapshot.xml_desc
61
+ rescue
62
+ raise Sahara::Errors::SnapshotMissing
63
+ end
64
+ return snapshot
65
+ end
66
+
67
+ def is_snapshot_mode_on?
68
+ begin
69
+ snapshot = get_snapshot_if_exists
70
+ rescue Sahara::Errors::SnapshotMissing
71
+ return false
72
+ end
73
+ return true
74
+ end
75
+
76
+ def off
77
+ snapshot = get_snapshot_if_exists
78
+ begin
79
+ snapshot.delete
80
+ rescue Fog::Errors::Error => e
81
+ raise Sahara::Errors::SnapshotDeletionError,
82
+ :error_message => e.message
83
+ end
84
+ end
85
+
86
+ def on
87
+ snapshot_desc = <<-EOF
88
+ <domainsnapshot>
89
+ <name>sahara-sandbox</name>
90
+ <description>Snapshot for vagrant sandbox</description>
91
+ </domainsnapshot>
92
+ EOF
93
+ begin
94
+ @domain.snapshot_create_xml(snapshot_desc)
95
+ rescue Fog::Errors::Error => e
96
+ raise Sahara::Errors::SnapshotCreationError,
97
+ :error_message => e.message
98
+ end
99
+ end
100
+
101
+ def commit
102
+ off
103
+ on
104
+ end
105
+
106
+ def rollback
107
+ snapshot = get_snapshot_if_exists
108
+ begin
109
+ # 4 is VIR_DOMAIN_SNAPSHOT_REVERT_FORCE
110
+ # needed due to https://bugzilla.redhat.com/show_bug.cgi?id=1006886
111
+ @domain.revert_to_snapshot(snapshot, 4)
112
+ rescue Fog::Errors::Error => e
113
+ raise Sahara::Errors::SnapshotReversionError,
114
+ :error_message => e.message
115
+ end
116
+ end
117
+
118
+ def is_vm_created?
119
+ return !@machine.id.nil?
120
+ end
121
+
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,83 @@
1
+ module Sahara
2
+ module Session
3
+ class Virtualbox
4
+
5
+ def initialize(machine)
6
+ @machine=machine
7
+ @instance_id = @machine.id
8
+ @vboxcmd=determine_vboxcmd
9
+ @sandboxname="sahara-sandbox"
10
+ end
11
+
12
+ def determine_vboxcmd
13
+ if windows?
14
+ # On Windows, we use the VBOX_INSTALL_PATH environmental
15
+ if ENV.has_key?("VBOX_INSTALL_PATH")
16
+ # The path usually ends with a \ but we make sure here
17
+ path = File.join(ENV["VBOX_INSTALL_PATH"], "VBoxManage.exe")
18
+ return "\"#{path}\""
19
+ end
20
+ else
21
+ # for other platforms assume it is on the PATH
22
+ return "VBoxManage"
23
+ end
24
+ end
25
+
26
+ def windows?
27
+ %W[mingw mswin].each do |text|
28
+ return true if RbConfig::CONFIG["host_os"].downcase.include?(text)
29
+ end
30
+ false
31
+ end
32
+
33
+ def list_snapshots
34
+ snapshotlist=Array.new
35
+ output = `#{@vboxcmd} showvminfo --machinereadable "#{@instance_id}"`
36
+ snapshotresult=output.scan(/SnapshotName.*=(.*)/).flatten
37
+ snapshotresult.each do |result|
38
+ clean=result.gsub(/\"/,'').chomp
39
+ snapshotlist << clean
40
+ end
41
+ snapshotlist
42
+ end
43
+
44
+ def is_snapshot_mode_on?
45
+ snapshots=self.list_snapshots
46
+ return snapshots.include?(@sandboxname)
47
+ end
48
+
49
+ def off
50
+ `#{@vboxcmd} snapshot "#{@instance_id}" delete "#{@sandboxname}" `
51
+ end
52
+
53
+ def on
54
+ `#{@vboxcmd} snapshot "#{@instance_id}" take "#{@sandboxname}" --pause`
55
+ end
56
+
57
+ def commit
58
+ `#{@vboxcmd} snapshot "#{@instance_id}" delete "#{@sandboxname}"`
59
+ `#{@vboxcmd} snapshot "#{@instance_id}" take "#{@sandboxname}" --pause`
60
+ end
61
+
62
+ def rollback
63
+ `#{@vboxcmd} controlvm "#{@instance_id}" poweroff `
64
+ sleep 2
65
+ `#{@vboxcmd} snapshot "#{@instance_id}" restore "#{@sandboxname}"`
66
+
67
+ gui_boot = @machine.provider_config.gui
68
+ if gui_boot
69
+ boot_mode = "gui"
70
+ else
71
+ boot_mode = "headless"
72
+ end
73
+ # restore boot mode
74
+ `#{@vboxcmd} startvm --type #{boot_mode} "#{@instance_id}" `
75
+ end
76
+
77
+ def is_vm_created?
78
+ return !@machine.id.nil?
79
+ end
80
+
81
+ end
82
+ end
83
+ end
@@ -1,3 +1,3 @@
1
1
  module Sahara
2
- VERSION = "0.0.15"
2
+ VERSION = "0.0.16"
3
3
  end
@@ -0,0 +1,15 @@
1
+ en:
2
+ sahara:
3
+ errors:
4
+ provider_not_supported: |-
5
+ Sahara does not support this provider.
6
+ libvirt_connection_error: |-
7
+ Error while connecting to libvirt: %{error_message}
8
+ snapshot_missing: |-
9
+ Snapshot used for sandbox had unexpectedly gone missing.
10
+ snapshot_deletion_error: |-
11
+ Error while deleting snapshot: %{error_message}
12
+ snapshot_creation_error: |-
13
+ Error while creating snapshot: %{error_message}
14
+ snapshot_reversion_error: |-
15
+ Error while reverting snapshot: %{error_message}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sahara
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.15
4
+ version: 0.0.16
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-18 00:00:00.000000000 Z
12
+ date: 2013-10-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: popen4
@@ -77,8 +77,12 @@ files:
77
77
  - lib/sahara/command/rollback.rb
78
78
  - lib/sahara/command/root.rb
79
79
  - lib/sahara/command/status.rb
80
- - lib/sahara/session.rb
80
+ - lib/sahara/errors.rb
81
+ - lib/sahara/session/factory.rb
82
+ - lib/sahara/session/libvirt.rb
83
+ - lib/sahara/session/virtualbox.rb
81
84
  - lib/sahara/version.rb
85
+ - locales/en.yml
82
86
  - sahara.gemspec
83
87
  homepage: http://github.com/jedi4ever/sahara/
84
88
  licenses: []
@@ -94,7 +98,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
94
98
  version: '0'
95
99
  segments:
96
100
  - 0
97
- hash: 2159711704490986345
101
+ hash: -2734223008788270957
98
102
  required_rubygems_version: !ruby/object:Gem::Requirement
99
103
  none: false
100
104
  requirements:
@@ -1,135 +0,0 @@
1
- require 'pp'
2
-
3
- module Sahara
4
- module Session
5
- class Command
6
-
7
- def initialize(app, env)
8
- @app = app
9
- @env = env
10
- @vboxcmd=determine_vboxcmd
11
- @sandboxname="sahara-sandbox"
12
- end
13
-
14
- def determine_vboxcmd
15
- if windows?
16
- # On Windows, we use the VBOX_INSTALL_PATH environmental
17
- if ENV.has_key?("VBOX_INSTALL_PATH")
18
- # The path usually ends with a \ but we make sure here
19
- path = File.join(ENV["VBOX_INSTALL_PATH"], "VBoxManage.exe")
20
- return "\"#{path}\""
21
- end
22
- else
23
- # for other platforms assume it is on the PATH
24
- return "VBoxManage"
25
- end
26
- end
27
-
28
- def windows?
29
- %W[mingw mswin].each do |text|
30
- return true if RbConfig::CONFIG["host_os"].downcase.include?(text)
31
- end
32
- false
33
- end
34
-
35
- def list_snapshots(machine)
36
- snapshotlist=Array.new
37
- instance_id = machine.id
38
- output = `#{@vboxcmd} showvminfo --machinereadable "#{instance_id}"`
39
- snapshotresult=output.scan(/SnapshotName.*=(.*)/).flatten
40
- snapshotresult.each do |result|
41
- clean=result.gsub(/\"/,'').chomp
42
- snapshotlist << clean
43
- end
44
- snapshotlist
45
- end
46
-
47
- def status(machine)
48
- if !self.is_vm_created?(machine) then
49
- puts "[#{machine.name}] VM is not created"
50
- return
51
- end
52
- if self.is_snapshot_mode_on?(machine) then
53
- puts "[#{machine.name}] Sandbox mode is on"
54
- else
55
- puts "[#{machine.name}] Sandbox mode is off"
56
- end
57
- end
58
-
59
- def is_snapshot_mode_on?(machine)
60
- snapshots=self.list_snapshots(machine)
61
- return snapshots.include?(@sandboxname)
62
- end
63
-
64
- def off(machine)
65
- if !self.is_vm_created?(machine) then
66
- puts "[#{machine.name}] VM is not created"
67
- return
68
- end
69
- if self.is_snapshot_mode_on?(machine) then
70
- instance_id = machine.id
71
- `#{@vboxcmd} snapshot "#{instance_id}" delete "#{@sandboxname}" `
72
- else
73
- puts "[#{machine.name}] Not sandbox mode now"
74
- end
75
- end
76
-
77
- def on(machine)
78
- if !self.is_vm_created?(machine) then
79
- puts "[#{machine.name}] VM is not created"
80
- return
81
- end
82
- if !self.is_snapshot_mode_on?(machine) then
83
- instance_id = machine.id
84
- `#{@vboxcmd} snapshot "#{instance_id}" take "#{@sandboxname}" --pause`
85
- else
86
- puts "[#{machine.name}] Already sandbox mode"
87
- end
88
- end
89
-
90
- def commit(machine)
91
- if !self.is_vm_created?(machine) then
92
- puts "[#{machine.name}] VM is not created"
93
- return
94
- end
95
- if self.is_snapshot_mode_on?(machine) then
96
- instance_id = machine.id
97
- `#{@vboxcmd} snapshot "#{instance_id}" delete "#{@sandboxname}"`
98
- `#{@vboxcmd} snapshot "#{instance_id}" take "#{@sandboxname}" --pause`
99
- else
100
- puts "[#{machine.name}] Not sandbox mode now"
101
- end
102
- end
103
-
104
- def rollback(machine)
105
- if !self.is_vm_created?(machine) then
106
- puts "[#{machine.name}] VM is not created"
107
- return
108
- end
109
- if !self.is_snapshot_mode_on?(machine) then
110
- puts "[#{machine.name}] Not sandbox mode now"
111
- return
112
- end
113
-
114
- instance_id = machine.id
115
- `#{@vboxcmd} controlvm "#{instance_id}" poweroff `
116
- sleep 2
117
- `#{@vboxcmd} snapshot "#{instance_id}" restore "#{@sandboxname}"`
118
-
119
- gui_boot = machine.provider_config.gui
120
- if gui_boot
121
- boot_mode = "gui"
122
- else
123
- boot_mode = "headless"
124
- end
125
- # restore boot mode
126
- `#{@vboxcmd} startvm --type #{boot_mode} "#{instance_id}" `
127
- end
128
-
129
- def is_vm_created?(machine)
130
- return !machine.id.nil?
131
- end
132
-
133
- end
134
- end
135
- end