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,20 @@
1
+ module VagrantPlugins
2
+ module Openvz
3
+ module Action
4
+ class Created
5
+ def initialize(app, env)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+
11
+ # Set the result to be true if the machine is created.
12
+ env[:result] = true
13
+ env[:result] = false if env[:machine].state.id == :not_created
14
+
15
+ @app.call(env)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ module VagrantPlugins
2
+ module Openvz
3
+ module Action
4
+ class Destroy
5
+ def initialize(app, env)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ config = env[:machine].provider_config
11
+
12
+ env[:ui].info "Destroying!"
13
+ env[:machine].provider.driver.destroy(config.vzctid)
14
+
15
+ env[:machine].id = nil
16
+ @app.call env
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ require "vagrant/action/builtin/confirm"
2
+
3
+ module VagrantPlugins
4
+ module Openvz
5
+ module Action
6
+ class DestroyConfirm < Vagrant::Action::Builtin::Confirm
7
+ def initialize(app, env)
8
+ force_key = :force_confirm_destroy
9
+ message = "Destory Confirmation? [y/n] "
10
+ super(app, env, message, force_key)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,19 @@
1
+ module VagrantPlugins
2
+ module Openvz
3
+ module Action
4
+ class FetchIp
5
+ def initialize(app, env)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ config = env[:machine].provider_config
11
+
12
+ env[:machine_ip] ||= env[:machine].provider.driver.fetch_ip(config.vzctid)
13
+
14
+ @app.call(env)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,21 @@
1
+ module VagrantPlugins
2
+ module Openvz
3
+ module Action
4
+ class ForcedHalt
5
+ def initialize(app, env)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ config = env[:machine].provider_config
11
+
12
+ if env[:machine].provider.state.id == :exist_mounted_running
13
+ env[:machine].provider.driver.forced_halt(config.vzctid)
14
+ end
15
+
16
+ @app.call(env)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,18 @@
1
+ module VagrantPlugins
2
+ module Openvz
3
+ module Action
4
+ class IsRunning
5
+ def initialize(app, env)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+
11
+ env[:result] = env[:machine].state.id == :exist_mounted_running
12
+
13
+ @app.call(env)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,22 @@
1
+ module VagrantPlugins
2
+ module Openvz
3
+ module Action
4
+ class Message
5
+ def initialize(app, env, msg_key, type = :info)
6
+ @app = app
7
+ @msg_key = msg_key
8
+ @type = type
9
+ end
10
+
11
+ def call(env)
12
+ machine = env[:machine]
13
+ message = "#{machine.name}: #{@msg_key}"
14
+
15
+ env[:ui].send @type, message
16
+
17
+ @app.call env
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,56 @@
1
+ module VagrantPlugins
2
+ module Openvz
3
+ module Action
4
+ class ShareFolders
5
+ def initialize(app, env)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ @env = env
11
+ prepare_folders
12
+ @app.call env
13
+ add_folders
14
+ end
15
+
16
+ def shared_folders
17
+ {}.tap do |result|
18
+ @env[:machine].config.vm.synced_folders.each do |id, data|
19
+ # This to prevent overwriting the actual shared folders data
20
+ result[id] = data.dup
21
+ end
22
+ end
23
+ end
24
+
25
+ # Prepares the shared folders by verifying they exist and creating them
26
+ # if they don't.
27
+ def prepare_folders
28
+ shared_folders.each do |id, options|
29
+ hostpath = Pathname.new(options[:hostpath]).expand_path(@env[:root_path])
30
+ if !hostpath.directory? && options[:create]
31
+ # Host path doesn't exist, so let's create it.
32
+ begin
33
+ hostpath.mkpath
34
+ rescue Errno::EACCES
35
+ raise Vagrant::Errors::SharedFolderCreateFailed,
36
+ :path => hostpath.to_s
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ def add_folders
43
+ folders = []
44
+ shared_folders.each do |id, data|
45
+ folders << {
46
+ :name => id,
47
+ :hostpath => File.expand_path(data[:hostpath], @env[:root_path]),
48
+ :guestpath => data[:guestpath]
49
+ }
50
+ end
51
+ @env[:machine].provider.driver.share_folders(folders,@env[:machine].provider_config.vzctid)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,11 @@
1
+
2
+ module VagrantPlugins
3
+ module Openvz
4
+ class VzctlVersion < Vagrant.plugin("2", "command")
5
+ def execute
6
+ puts `vzctl --version`
7
+ return 0
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,56 @@
1
+ require "vagrant"
2
+
3
+ module VagrantPlugins
4
+ module Openvz
5
+ class Config < Vagrant.plugin("2", :config)
6
+ attr_accessor :physpages
7
+ attr_accessor :hostname
8
+ attr_accessor :ipadd
9
+ attr_accessor :nameserver
10
+ attr_accessor :vzctid
11
+ attr_accessor :pubkey
12
+ attr_accessor :diskspace
13
+ attr_accessor :diskinodes
14
+ attr_accessor :quotatime
15
+
16
+ # A String that points to a file that acts as a wrapper for sudo commands.
17
+ # This allows us to have a single entry when whitelisting NOPASSWD commands
18
+ # on /etc/sudoers
19
+ attr_accessor :sudo_wrapper
20
+
21
+ def initialize
22
+ @customizations = []
23
+ @sudo_wrapper = UNSET_VALUE
24
+ end
25
+
26
+ def finalize!
27
+ @sudo_wrapper = nil if @sudo_wrapper == UNSET_VALUE
28
+ end
29
+
30
+ def validate(machine)
31
+ errors = []
32
+
33
+ errors << "config.physpages" if physpages.nil?
34
+ errors << "config.hostname" if hostname.nil?
35
+ errors << "config.ipadd" if ipadd.nil?
36
+ errors << "config.nameserver" if nameserver.nil?
37
+ errors << "config.vzctid" if vzctid.nil?
38
+ errors << "config.pubkey" if pubkey.nil?
39
+ errors << "config.diskspace" if diskspace.nil?
40
+ errors << "config.diskinodes" if diskinodes.nil?
41
+ errors << "config.quotatime" if quotatime.nil?
42
+
43
+ if @sudo_wrapper
44
+ hostpath = Pathname.new(@sudo_wrapper).expand_path(machine.env.root_path)
45
+ if ! hostpath.file?
46
+ errors << 'vagrant_openvz.sudo_wrapper_not_found'
47
+ elsif ! hostpath.executable?
48
+ errors << 'vagrant_openvz.sudo_wrapper_not_executable'
49
+ end
50
+ end
51
+
52
+ { "openvz provider" => errors }
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,90 @@
1
+ require "vagrant-openvz/errors"
2
+ require "vagrant-openvz/driver/cli"
3
+ require "etc"
4
+
5
+ require "log4r"
6
+
7
+
8
+ module VagrantPlugins
9
+ module Openvz
10
+ class Driver
11
+
12
+ # This is raised if the container can't be found when initializing it with
13
+ # a name.
14
+ class ContainerNotFound < StandardError; end
15
+
16
+ # Root folder where container configs are stored
17
+ CONTAINERS_PATH = '/vz/root/'
18
+
19
+ attr_reader :container_name, :customizations
20
+
21
+ def generate_guest_path(path,vzctlid)
22
+ Pathname.new("#{CONTAINERS_PATH}#{vzctlid}").join(path.gsub(/^\//, ''))
23
+ end
24
+
25
+ def initialize(container_name, sudo_wrapper, cli = nil)
26
+ @logger = Log4r::Logger.new("vagrant::provider::openvz")
27
+ @container_name = container_name
28
+ @logger.debug("In Driver::initialize, @container_name=#{@container_name}")
29
+ @sudo_wrapper = sudo_wrapper
30
+ @cli = cli || CLI.new(sudo_wrapper, container_name)
31
+ # @logger = Log4r::Logger.new("vagrant::provider::lxc::driver")
32
+ @customizations = []
33
+ end
34
+
35
+ def share_folders(folders,vzctlid)
36
+ folders.each do |folder|
37
+ guestpath = generate_guest_path(folder[:guestpath],vzctlid)
38
+ @logger.debug("In Driver::share_folders, guestpath=#{guestpath}")
39
+ @logger.debug("In Driver::create, guestpath.directory?=#{guestpath.directory?}")
40
+ unless guestpath.directory?
41
+ begin
42
+ @logger.debug("Guest path doesn't exist, creating: #{guestpath}")
43
+ @sudo_wrapper.run("mkdir", '-p', guestpath.to_s)
44
+ rescue Errno::EACCES
45
+ raise Vagrant::Errors::SharedFolderCreateFailed, :path => guestpath.to_s
46
+ end
47
+ end
48
+ @cli.share_folder(folder[:hostpath],guestpath)
49
+ end
50
+ end
51
+
52
+ def validate!
53
+ raise ContainerNotFound if @container_name && ! @cli.list.include?(@container_name)
54
+ end
55
+
56
+ def create(name,vzctlid,settings={})
57
+ @logger.debug("In Driver::create, parameter name=#{name}")
58
+ @cli.name = @container_name = name
59
+ @logger.debug("In Driver::create, @container_name=#{@container_name}")
60
+
61
+ @cli.create vzctlid, settings
62
+ end
63
+
64
+ def start(vzctlid,pubkey)
65
+ @logger.info('Starting container...')
66
+ @cli.start(vzctlid,pubkey)
67
+ end
68
+
69
+ def forced_halt(vzctlid)
70
+ @logger.info('Shutting down container...')
71
+ @cli.stop(vzctlid)
72
+ end
73
+
74
+ def destroy(vzctlid)
75
+ @cli.destroy(vzctlid)
76
+ end
77
+
78
+ def state(vzctlid)
79
+ if @container_name
80
+ @cli.status(vzctlid)
81
+ end
82
+ end
83
+
84
+ def fetch_ip(vzctlid)
85
+ @cli.fetch_ip(vzctlid)
86
+ end
87
+
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,81 @@
1
+
2
+ module VagrantPlugins
3
+ module Openvz
4
+ class Driver
5
+ class CLI
6
+ attr_accessor :name
7
+
8
+ def initialize(sudo_wrapper, name = nil)
9
+ @sudo_wrapper = sudo_wrapper
10
+ @name = name
11
+ end
12
+
13
+ def list
14
+ run(:vzlist,"-a").split(/\s+/).uniq
15
+ end
16
+
17
+ def fetch_ip(vzctlid)
18
+ run(:vzlist,"-a","-H","-t","-o","ip","#{vzctlid}")
19
+ end
20
+
21
+ def create(vzctlid,settings={})
22
+
23
+ # run :rsync, settings[:box_location],settings[:template_location]
24
+
25
+ run :vzctl, 'create', vzctlid, '--ostemplate', settings[:template_name]
26
+
27
+ settings.delete(:box_location)
28
+ settings.delete(:template_location)
29
+ settings.delete(:template_name)
30
+
31
+ settings.each do |key,value|
32
+ run :vzctl,'set',vzctlid,"--#{key}","#{value}","--save"
33
+ end
34
+
35
+ end
36
+
37
+ def start(vzctlid,pubkey)
38
+ run :vzctl, 'start', "#{vzctlid}"
39
+ add_vagrant_user(vzctlid,pubkey)
40
+ end
41
+
42
+ def destroy(vzctlid)
43
+ run :vzctl, 'destroy', "#{vzctlid}"
44
+ end
45
+
46
+ def stop(vzctlid)
47
+ run :vzctl, 'stop', "#{vzctlid}"
48
+ end
49
+
50
+ def status(vzctlid)
51
+ if @name && run(:vzctl, 'status', "#{vzctlid}") =~ /[a-z]+\s[0-9]+\s([a-z]+)\s([a-z]+)\s([a-z]+)/i
52
+ status = "#{$1}_#{$2}_#{$3}".downcase.to_sym
53
+ status
54
+ elsif @name
55
+ :unknown
56
+ end
57
+ end
58
+
59
+ def share_folder(source,destination)
60
+ run :mount, "-o", "bind", "#{source}", "#{destination}"
61
+ end
62
+
63
+ private
64
+
65
+ def add_vagrant_user(vzctlid,pubkey)
66
+ run :vzctl, "exec", "#{vzctlid}", "if [[ ! \`grep vagrant /etc/passwd\` ]]; then adduser vagrant; fi"
67
+ run :vzctl, "exec", "#{vzctlid}", "echo 'vagrant:vagrant' | chpasswd"
68
+ run :vzctl, "exec", "#{vzctlid}", "mkdir -p /home/vagrant/.ssh"
69
+ run :vzctl, "exec", "#{vzctlid}", "echo '#{pubkey}' > /home/vagrant/.ssh/authorized_keys"
70
+ run :vzctl, "exec", "#{vzctlid}", "chown -R vagrant:vagrant /home/vagrant/.ssh"
71
+ run :vzctl, "exec", "#{vzctlid}", "echo 'vagrant ALL=NOPASSWD: ALL' > /etc/sudoers.d/vagrant"
72
+ run :vzctl, "exec", "#{vzctlid}", "sed -i -e 's/Defaults requiretty/#Defaults requiretty/' /etc/sudoers"
73
+ end
74
+
75
+ def run(command, *args)
76
+ @sudo_wrapper.run("#{command}", *args)
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end