vagrant-openvz 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. data/.gitignore +3 -0
  2. data/Gemfile +8 -0
  3. data/Gemfile.lock +39 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +54 -0
  6. data/Rakefile +1 -0
  7. data/Vagrantfile +30 -0
  8. data/box/centos-6-x86_64/VagrantFile +10 -0
  9. data/box/centos-6-x86_64/centos-6-x86_64.box +0 -0
  10. data/box/centos-6-x86_64/make_box.sh +1 -0
  11. data/box/centos-6-x86_64/metadata.json +3 -0
  12. data/lib/vagrant-openvz.rb +9 -0
  13. data/lib/vagrant-openvz/action.rb +165 -0
  14. data/lib/vagrant-openvz/action/boot.rb +21 -0
  15. data/lib/vagrant-openvz/action/check_created.rb +19 -0
  16. data/lib/vagrant-openvz/action/check_running.rb +19 -0
  17. data/lib/vagrant-openvz/action/create.rb +36 -0
  18. data/lib/vagrant-openvz/action/created.rb +20 -0
  19. data/lib/vagrant-openvz/action/destroy.rb +21 -0
  20. data/lib/vagrant-openvz/action/destroy_confirm.rb +15 -0
  21. data/lib/vagrant-openvz/action/fetch_ip.rb +19 -0
  22. data/lib/vagrant-openvz/action/forced_halt.rb +21 -0
  23. data/lib/vagrant-openvz/action/is_running.rb +18 -0
  24. data/lib/vagrant-openvz/action/message.rb +22 -0
  25. data/lib/vagrant-openvz/action/share_folders.rb +56 -0
  26. data/lib/vagrant-openvz/command/version.rb +11 -0
  27. data/lib/vagrant-openvz/config.rb +56 -0
  28. data/lib/vagrant-openvz/driver.rb +90 -0
  29. data/lib/vagrant-openvz/driver/cli.rb +81 -0
  30. data/lib/vagrant-openvz/errors.rb +29 -0
  31. data/lib/vagrant-openvz/plugin.rb +32 -0
  32. data/lib/vagrant-openvz/provider.rb +108 -0
  33. data/lib/vagrant-openvz/sudo_wrapper.rb +80 -0
  34. data/lib/vagrant-openvz/version.rb +5 -0
  35. data/vagrant-openvz.gemspec +23 -0
  36. data/vg.sh +4 -0
  37. metadata +114 -0
@@ -0,0 +1,29 @@
1
+ require 'vagrant/errors'
2
+
3
+ module VagrantPlugins
4
+ module Openvz
5
+ module Errors
6
+
7
+ class ExecuteError < Vagrant::Errors::VagrantError
8
+ error_key(:openvz_execute_error)
9
+ end
10
+
11
+ class OpenvzNotInstalled < Vagrant::Errors::VagrantError
12
+ error_key(:openvz_not_installed)
13
+ end
14
+
15
+ # Box related errors
16
+ class TemplateFileMissing < Vagrant::Errors::VagrantError
17
+ error_key(:openvz_template_file_missing)
18
+ end
19
+
20
+ class RootFSTarballMissing < Vagrant::Errors::VagrantError
21
+ error_key(:openvz_invalid_box_version)
22
+ end
23
+
24
+ class IncompatibleBox < Vagrant::Errors::VagrantError
25
+ error_key(:openvz_incompatible_box)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,32 @@
1
+ require "vagrant"
2
+
3
+ module VagrantPlugins
4
+ module Openvz
5
+ class Plugin < Vagrant.plugin("2")
6
+
7
+ name "vagrant-openvz"
8
+
9
+ description <<-DESC
10
+ This plugin installs a provider that allows vagrant to manage
11
+ Openvz machine instances.
12
+ DESC
13
+
14
+ config(:openvz, :provider) do
15
+ require_relative 'config'
16
+ Config
17
+ end
18
+
19
+ command "vzctl-version" do
20
+ require_relative "command/version"
21
+ VzctlVersion
22
+ end
23
+
24
+ provider(:openvz) do
25
+ require_relative "provider"
26
+ Provider
27
+ end
28
+
29
+ end
30
+ end
31
+ end
32
+
@@ -0,0 +1,108 @@
1
+ begin
2
+ require "vagrant"
3
+ rescue LoadError
4
+ raise "This plugin must run within Vagrant."
5
+ end
6
+
7
+ require "log4r"
8
+
9
+ require "vagrant-openvz/action"
10
+ require "vagrant-openvz/driver"
11
+ require "vagrant-openvz/sudo_wrapper"
12
+
13
+ module VagrantPlugins
14
+ module Openvz
15
+ class Provider < Vagrant.plugin("2",:provider)
16
+ attr_reader :driver
17
+
18
+ def initialize(machine)
19
+ @logger = Log4r::Logger.new("vagrant::provider::openvz")
20
+ @machine = machine
21
+
22
+ ensure_openvz_installed!
23
+ machine_id_changed
24
+ end
25
+
26
+ def sudo_wrapper
27
+ @shell ||= begin
28
+ wrapper = @machine.provider_config.sudo_wrapper
29
+ wrapper = Pathname(wrapper).expand_path(@machine.env.root_path).to_s if wrapper
30
+ SudoWrapper.new(wrapper)
31
+ end
32
+ end
33
+
34
+ def action(name)
35
+ # Attempt to get the action method from the Action class if it
36
+ # exists, otherwise return nil to show that we don't support the
37
+ # given action.
38
+ action_method = "action_#{name}"
39
+ return Action.send(action_method) if Action.respond_to?(action_method)
40
+ nil
41
+ end
42
+
43
+ def ensure_openvz_installed!
44
+ unless system("which vzctl > /dev/null")
45
+ raise Errors::OpenvzNotInstalled
46
+ end
47
+ end
48
+
49
+ # If the machine ID changed, then we need to rebuild our underlying
50
+ # container.
51
+ def machine_id_changed
52
+ id = @machine.id
53
+ @logger.debug("In Provider::machine_id_changed, id=#{id}")
54
+
55
+ begin
56
+ @logger.debug("Instantiating the container for: #{id.inspect}")
57
+
58
+ @driver = Driver.new(id, self.sudo_wrapper)
59
+ @driver.validate!
60
+ rescue Driver::ContainerNotFound
61
+ # The container doesn't exist, so we probably have a stale
62
+ # ID. Just clear the id out of the machine and reload it.
63
+ @logger.debug("Container not found! Clearing saved machine ID and reloading.")
64
+ id = nil
65
+ retry
66
+ end
67
+ end
68
+
69
+ # Returns the SSH info for accessing the Container.
70
+ def ssh_info
71
+ # If the Container is not created then we cannot possibly SSH into it, so
72
+ # we return nil.
73
+ return nil if state == :not_created
74
+
75
+ # Run a custom action called "fetch_ip" which does what it says and puts
76
+ # the IP found into the `:machine_ip` key in the environment.
77
+ env = @machine.action("fetch_ip")
78
+
79
+ # If we were not able to identify the container's IP, we return nil
80
+ # here and we let Vagrant core deal with it ;)
81
+ return nil unless env[:machine_ip]
82
+
83
+ {
84
+ :host => env[:machine_ip],
85
+ :port => @machine.config.ssh.guest_port
86
+ }
87
+ end
88
+
89
+ def state
90
+ @logger.debug("In Provider::state, @driver.container_name=#{@driver.container_name}")
91
+ @logger.debug("In Provider::state, @machine.provider_config.vzctid=#{@machine.provider_config.vzctid}")
92
+
93
+ state_id = nil
94
+
95
+ state_id = :not_created if !@driver.container_name
96
+ state_id = @driver.state(@machine.provider_config.vzctid) if !state_id
97
+ state_id = :unknown if !state_id
98
+
99
+ short = state_id.to_s.gsub("_", " ")
100
+ long = state_id.to_s.gsub("_", " ")
101
+
102
+ @logger.debug("In Provider::state, state_id=#{state_id}")
103
+ Vagrant::MachineState.new(state_id, short, long)
104
+ end
105
+
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,80 @@
1
+ module VagrantPlugins
2
+ module Openvz
3
+ class SudoWrapper
4
+
5
+ # Include this so we can use `Subprocess` more easily.
6
+ include Vagrant::Util::Retryable
7
+
8
+ def initialize(wrapper_path = nil)
9
+ @wrapper_path = wrapper_path
10
+ # @logger = Log4r::Logger.new("vagrant::lxc::shell")
11
+ end
12
+
13
+ def run(*command)
14
+ command.unshift @wrapper_path if @wrapper_path
15
+ execute *(['sudo'] + command)
16
+ end
17
+
18
+ def su_c(command)
19
+ su_command = if @wrapper_path
20
+ "#{@wrapper_path} \"#{command}\""
21
+ else
22
+ "su root -c \"#{command}\""
23
+ end
24
+ @logger.debug "Running 'sudo #{su_command}'"
25
+ system "sudo #{su_command}"
26
+ end
27
+
28
+ private
29
+
30
+ # TODO: Review code below this line, it was pretty much a copy and
31
+ # paste from VirtualBox base driver and has no tests
32
+ def execute(*command, &block)
33
+ # Get the options hash if it exists
34
+ opts = {}
35
+ opts = command.pop if command.last.is_a?(Hash)
36
+
37
+ tries = 0
38
+ tries = 3 if opts[:retryable]
39
+
40
+ sleep = opts.fetch(:sleep, 1)
41
+
42
+ # Variable to store our execution result
43
+ r = nil
44
+
45
+ retryable(:on => Openvz::Errors::ExecuteError, :tries => tries, :sleep => sleep) do
46
+ # Execute the command
47
+ r = raw(*command, &block)
48
+
49
+ # If the command was a failure, then raise an exception that is
50
+ # nicely handled by Vagrant.
51
+ if r.exit_code != 0
52
+ if @interrupted
53
+ @logger.info("Exit code != 0, but interrupted. Ignoring.")
54
+ else
55
+ raise Openvz::Errors::ExecuteError, :command => command.inspect
56
+ end
57
+ end
58
+ end
59
+
60
+ # Return the output, making sure to replace any Windows-style
61
+ # newlines with Unix-style.
62
+ r.stdout.gsub("\r\n", "\n")
63
+ end
64
+
65
+ def raw(*command, &block)
66
+ int_callback = lambda do
67
+ @interrupted = true
68
+ @logger.info("Interrupted.")
69
+ end
70
+
71
+ # Append in the options for subprocess
72
+ command << { :notify => [:stdout, :stderr] }
73
+
74
+ Vagrant::Util::Busy.busy(int_callback) do
75
+ Vagrant::Util::Subprocess.execute(*command, &block)
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,5 @@
1
+ module VagrantPlugins
2
+ module Openvz
3
+ VERSION = "0.0.2"
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'vagrant-openvz/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "vagrant-openvz"
8
+ spec.version = VagrantPlugins::Openvz::VERSION
9
+ spec.authors = ["Aslan Brooke"]
10
+ spec.email = ["aslandbrooke@yahoo.com"]
11
+ spec.description = %q{Vagrant plugin for using OpenVZ as a provider.}
12
+ spec.summary = %q{Vagrant plugin for using OpenVZ as a provider.}
13
+ spec.homepage = "https://github.com/abrooke-forks/vagrant-openvz"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
data/vg.sh ADDED
@@ -0,0 +1,4 @@
1
+ #!/bin/bash
2
+ export VAGRANT_DEFAULT_PROVIDER=openvz
3
+ export VAGRANT_LOG=DEBUG
4
+ /usr/bin/time bundle exec vagrant $*
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vagrant-openvz
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Aslan Brooke
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-10-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Vagrant plugin for using OpenVZ as a provider.
47
+ email:
48
+ - aslandbrooke@yahoo.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - Gemfile.lock
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - Vagrantfile
60
+ - box/centos-6-x86_64/VagrantFile
61
+ - box/centos-6-x86_64/centos-6-x86_64.box
62
+ - box/centos-6-x86_64/make_box.sh
63
+ - box/centos-6-x86_64/metadata.json
64
+ - lib/vagrant-openvz.rb
65
+ - lib/vagrant-openvz/action.rb
66
+ - lib/vagrant-openvz/action/boot.rb
67
+ - lib/vagrant-openvz/action/check_created.rb
68
+ - lib/vagrant-openvz/action/check_running.rb
69
+ - lib/vagrant-openvz/action/create.rb
70
+ - lib/vagrant-openvz/action/created.rb
71
+ - lib/vagrant-openvz/action/destroy.rb
72
+ - lib/vagrant-openvz/action/destroy_confirm.rb
73
+ - lib/vagrant-openvz/action/fetch_ip.rb
74
+ - lib/vagrant-openvz/action/forced_halt.rb
75
+ - lib/vagrant-openvz/action/is_running.rb
76
+ - lib/vagrant-openvz/action/message.rb
77
+ - lib/vagrant-openvz/action/share_folders.rb
78
+ - lib/vagrant-openvz/command/version.rb
79
+ - lib/vagrant-openvz/config.rb
80
+ - lib/vagrant-openvz/driver.rb
81
+ - lib/vagrant-openvz/driver/cli.rb
82
+ - lib/vagrant-openvz/errors.rb
83
+ - lib/vagrant-openvz/plugin.rb
84
+ - lib/vagrant-openvz/provider.rb
85
+ - lib/vagrant-openvz/sudo_wrapper.rb
86
+ - lib/vagrant-openvz/version.rb
87
+ - vagrant-openvz.gemspec
88
+ - vg.sh
89
+ homepage: https://github.com/abrooke-forks/vagrant-openvz
90
+ licenses:
91
+ - MIT
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 1.8.24
111
+ signing_key:
112
+ specification_version: 3
113
+ summary: Vagrant plugin for using OpenVZ as a provider.
114
+ test_files: []