vagrant-hyperkit 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +22 -0
  3. data/.rspec +1 -0
  4. data/CHANGELOG.md +20 -0
  5. data/Gemfile +14 -0
  6. data/LICENSE +9 -0
  7. data/README.md +221 -0
  8. data/Rakefile +22 -0
  9. data/lib/vagrant-hyperkit.rb +22 -0
  10. data/lib/vagrant-hyperkit/action.rb +195 -0
  11. data/lib/vagrant-hyperkit/action/boot.rb +185 -0
  12. data/lib/vagrant-hyperkit/action/import.rb +69 -0
  13. data/lib/vagrant-hyperkit/action/is_created.rb +18 -0
  14. data/lib/vagrant-hyperkit/action/is_stopped.rb +18 -0
  15. data/lib/vagrant-hyperkit/action/message_already_created.rb +16 -0
  16. data/lib/vagrant-hyperkit/action/message_not_created.rb +16 -0
  17. data/lib/vagrant-hyperkit/action/message_will_not_destroy.rb +16 -0
  18. data/lib/vagrant-hyperkit/action/read_ssh_info.rb +27 -0
  19. data/lib/vagrant-hyperkit/action/read_state.rb +53 -0
  20. data/lib/vagrant-hyperkit/action/stop_instance.rb +49 -0
  21. data/lib/vagrant-hyperkit/action/terminate_instance.rb +31 -0
  22. data/lib/vagrant-hyperkit/action/timed_provision.rb +21 -0
  23. data/lib/vagrant-hyperkit/action/wait_for_state.rb +41 -0
  24. data/lib/vagrant-hyperkit/config.rb +83 -0
  25. data/lib/vagrant-hyperkit/errors.rb +19 -0
  26. data/lib/vagrant-hyperkit/plugin.rb +73 -0
  27. data/lib/vagrant-hyperkit/provider.rb +67 -0
  28. data/lib/vagrant-hyperkit/util/timer.rb +17 -0
  29. data/lib/vagrant-hyperkit/util/vagrant-hyperkit.rb +79 -0
  30. data/lib/vagrant-hyperkit/version.rb +5 -0
  31. data/locales/en.yml +83 -0
  32. data/spec/spec_helper.rb +1 -0
  33. data/spec/vagrant-hyperkit/config_spec.rb +33 -0
  34. data/templates/metadata.json.erb +3 -0
  35. data/templates/vagrant-aws_package_Vagrantfile.erb +5 -0
  36. data/vagrant-hyperkit.gemspec +61 -0
  37. data/vendor/xhyve-ruby/.gitignore +8 -0
  38. data/vendor/xhyve-ruby/.travis.yml +10 -0
  39. data/vendor/xhyve-ruby/Gemfile +3 -0
  40. data/vendor/xhyve-ruby/README.md +33 -0
  41. data/vendor/xhyve-ruby/Rakefile +42 -0
  42. data/vendor/xhyve-ruby/example/test.rb +17 -0
  43. data/vendor/xhyve-ruby/lib/rubygems_plugin.rb +7 -0
  44. data/vendor/xhyve-ruby/lib/xhyve.rb +4 -0
  45. data/vendor/xhyve-ruby/lib/xhyve/dhcp.rb +54 -0
  46. data/vendor/xhyve-ruby/lib/xhyve/guest.rb +92 -0
  47. data/vendor/xhyve-ruby/lib/xhyve/vendor/xhyve +0 -0
  48. data/vendor/xhyve-ruby/lib/xhyve/version.rb +4 -0
  49. data/vendor/xhyve-ruby/spec/fixtures/dhcpd_leases.txt +42 -0
  50. data/vendor/xhyve-ruby/spec/fixtures/guest/README.md +33 -0
  51. data/vendor/xhyve-ruby/spec/fixtures/guest/initrd +0 -0
  52. data/vendor/xhyve-ruby/spec/fixtures/guest/vmlinuz +0 -0
  53. data/vendor/xhyve-ruby/spec/lib/dhcp_spec.rb +35 -0
  54. data/vendor/xhyve-ruby/spec/lib/guest_spec.rb +51 -0
  55. data/vendor/xhyve-ruby/spec/spec_helper.rb +52 -0
  56. data/vendor/xhyve-ruby/xhyve-ruby.gemspec +23 -0
  57. metadata +192 -0
@@ -0,0 +1,31 @@
1
+ require "log4r"
2
+ require "json"
3
+ require "fileutils"
4
+
5
+ module VagrantPlugins
6
+ module HYPERKIT
7
+ module Action
8
+ # This terminates the running instance.
9
+ class TerminateInstance
10
+ def initialize(app, env)
11
+ @app = app
12
+ @logger = Log4r::Logger.new("vagrant_hyperkit::action::terminate_instance")
13
+ end
14
+
15
+ def call(env)
16
+ stop_instance(env)
17
+ # Remove the tracking ID
18
+ env[:ui].info(I18n.t("vagrant_hyperkit.terminating"))
19
+ FileUtils.rm_rf(env[:machine].data_dir)
20
+
21
+ @app.call(env)
22
+ end
23
+
24
+ def stop_instance(env)
25
+ halt_env = env.dup
26
+ env[:action_runner].run(Action.action_halt, halt_env)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,21 @@
1
+ require "vagrant-hyperkit/util/timer"
2
+
3
+ module VagrantPlugins
4
+ module HYPERKIT
5
+ module Action
6
+ # This is the same as the builtin provision except it times the
7
+ # provisioner runs.
8
+ class TimedProvision < Vagrant::Action::Builtin::Provision
9
+ def run_provisioner(env, name, p)
10
+ timer = Util::Timer.time do
11
+ super
12
+ end
13
+
14
+ env[:metrics] ||= {}
15
+ env[:metrics]["provisioner_times"] ||= []
16
+ env[:metrics]["provisioner_times"] << [name, timer]
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,41 @@
1
+ require "log4r"
2
+ require "timeout"
3
+
4
+ module VagrantPlugins
5
+ module HYPERKIT
6
+ module Action
7
+ # This action will wait for a machine to reach a specific state or quit by timeout
8
+ class WaitForState
9
+ # env[:result] will be false in case of timeout.
10
+ # @param [Symbol] state Target machine state.
11
+ # @param [Number] timeout Timeout in seconds.
12
+ def initialize(app, env, state, timeout)
13
+ @app = app
14
+ @logger = Log4r::Logger.new("vagrant_hyperkit::action::wait_for_state")
15
+ @state = state
16
+ @timeout = timeout
17
+ end
18
+
19
+ def call(env)
20
+ env[:result] = true
21
+ if env[:machine].state.id == @state
22
+ @logger.info(I18n.t("vagrant_hyperkit.already_status", :status => @state))
23
+ else
24
+ @logger.info("Waiting for machine to reach state #{@state}")
25
+ begin
26
+ Timeout.timeout(@timeout) do
27
+ until env[:machine].state.id == @state
28
+ sleep 2
29
+ end
30
+ end
31
+ rescue Timeout::Error
32
+ env[:result] = false # couldn't reach state in time
33
+ end
34
+ end
35
+
36
+ @app.call(env)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,83 @@
1
+ require "vagrant"
2
+ require "iniparse"
3
+
4
+ module VagrantPlugins
5
+ module HYPERKIT
6
+ class Config < Vagrant.plugin("2", :config)
7
+
8
+
9
+ # The number of CPUs to give the VM
10
+ #
11
+ # @return [Fixnum]
12
+ attr_accessor :cpus
13
+
14
+ # The amount of memory to give the VM
15
+ #
16
+ # This can just be a simple integer for memory in MB
17
+ # or you can use the suffixed style, eg. 2G for two
18
+ # Gigabytes
19
+ #
20
+ # @return [String]
21
+ attr_accessor :memory
22
+
23
+
24
+ # The path to the xhyve binary if you don't want to
25
+ # use the one bundled with xhyve-ruby
26
+ #
27
+ # @return [String]
28
+ attr_accessor :xhyve_binary
29
+
30
+ # The mac address of the VM
31
+ #
32
+ # @return [String]
33
+ attr_accessor :mac
34
+
35
+ # The uuid of the VM
36
+ #
37
+ # @return [String]
38
+ attr_accessor :uuid
39
+
40
+ # The kernel command to initialize the machine
41
+ # The default command assumes that the instalation is done directly on the
42
+ # disk and not using a lvm. For example, by default, Centos intallation uses
43
+ # LVM. Moreover, it might be interesting to add new kernel options.
44
+ # @return [String]
45
+ attr_accessor :kernel_command
46
+
47
+ def initialize(region_specific=false)
48
+ @cpus = UNSET_VALUE
49
+ @memory = UNSET_VALUE
50
+ @xhyve_binary = UNSET_VALUE
51
+ @mac = UNSET_VALUE
52
+ @uuid = UNSET_VALUE
53
+ @kernel_command = UNSET_VALUE
54
+
55
+ # Internal state (prefix with __ so they aren't automatically
56
+ # merged)
57
+ @__compiled_region_configs = {}
58
+ @__finalized = false
59
+ @__region_config = {}
60
+ @__region_specific = region_specific
61
+ end
62
+
63
+ #-------------------------------------------------------------------
64
+ # Internal methods.
65
+ #-------------------------------------------------------------------
66
+
67
+ def finalize!
68
+
69
+ @cpus = 1 if @cpus == UNSET_VALUE
70
+ @memory = 1024 if @memory == UNSET_VALUE
71
+ @xhyve_binary = nil if @xhyve_binary == UNSET_VALUE
72
+ @kernel_command = %Q{"earlyprintk=serial console=ttyS0 root=/dev/vda1 ro"} if @kernel_command == UNSET_VALUE
73
+ # Mark that we finalized
74
+ @__finalized = true
75
+ end
76
+
77
+ def validate(machine)
78
+ errors = _detected_errors
79
+ { "XHYVE Provider" => errors }
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,19 @@
1
+ require "vagrant"
2
+
3
+ module VagrantPlugins
4
+ module HYPERKIT
5
+ module Errors
6
+ class VagrantXHYVEError < Vagrant::Errors::VagrantError
7
+ error_namespace("vagrant_hyperkit.errors")
8
+ end
9
+
10
+ class RsyncError < VagrantXYHVEError
11
+ error_key(:rsync_error)
12
+ end
13
+
14
+ class MkdirError < VagrantXHYVEError
15
+ error_key(:mkdir_error)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,73 @@
1
+ begin
2
+ require "vagrant"
3
+ rescue LoadError
4
+ raise "The Vagrant HYPERKIT plugin must be run within Vagrant."
5
+ end
6
+
7
+ # This is a sanity check to make sure no one is attempting to install
8
+ # this into an early Vagrant version.
9
+ if Vagrant::VERSION < "1.2.0"
10
+ raise "The Vagrant HYPERKIT plugin is only compatible with Vagrant 1.2+"
11
+ end
12
+
13
+ module VagrantPlugins
14
+ module HYPERKIT
15
+ class Plugin < Vagrant.plugin("2")
16
+ name "HYPERKIT"
17
+ description <<-DESC
18
+ This plugin installs a provider that allows Vagrant to manage
19
+ machines in XHYVE.
20
+ DESC
21
+
22
+ config(:hyperkit, :provider) do
23
+ require_relative "config"
24
+ Config
25
+ end
26
+
27
+ provider(:hyperkit) do
28
+ # Setup logging and i18n
29
+ setup_logging
30
+ setup_i18n
31
+
32
+ # Return the provider
33
+ require_relative "provider"
34
+ Provider
35
+ end
36
+
37
+ # This initializes the internationalization strings.
38
+ def self.setup_i18n
39
+ I18n.load_path << File.expand_path("locales/en.yml", HYPERKIT.source_root)
40
+ I18n.reload!
41
+ end
42
+
43
+ # This sets up our log level to be whatever VAGRANT_LOG is.
44
+ def self.setup_logging
45
+ require "log4r"
46
+
47
+ level = nil
48
+ begin
49
+ level = Log4r.const_get(ENV["VAGRANT_LOG"].upcase)
50
+ rescue NameError
51
+ # This means that the logging constant wasn't found,
52
+ # which is fine. We just keep `level` as `nil`. But
53
+ # we tell the user.
54
+ level = nil
55
+ end
56
+
57
+ # Some constants, such as "true" resolve to booleans, so the
58
+ # above error checking doesn't catch it. This will check to make
59
+ # sure that the log level is an integer, as Log4r requires.
60
+ level = nil if !level.is_a?(Integer)
61
+
62
+ # Set the logging level on all "vagrant" namespaced
63
+ # logs as long as we have a valid level.
64
+ if level
65
+ logger = Log4r::Logger.new("vagrant_hyperkit")
66
+ logger.outputters = Log4r::Outputter.stderr
67
+ logger.level = level
68
+ logger = nil
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,67 @@
1
+ require "log4r"
2
+ require "vagrant"
3
+
4
+ module VagrantPlugins
5
+ module HYPERKIT
6
+ class Provider < Vagrant.plugin("2", :provider)
7
+ def initialize(machine)
8
+ @machine = machine
9
+ end
10
+
11
+ def action(name)
12
+ # Attempt to get the action method from the Action class if it
13
+ # exists, otherwise return nil to show that we don't support the
14
+ # given action.
15
+ action_method = "action_#{name}"
16
+ return Action.send(action_method) if Action.respond_to?(action_method)
17
+ nil
18
+ end
19
+
20
+ # def ssh_info
21
+ # droplet = Provider.droplet(@machine)
22
+
23
+ # return nil if droplet['status'].to_sym != :active
24
+
25
+ # public_network = droplet['networks']['v4'].find { |network| network['type'] == 'public' }
26
+
27
+ # return {
28
+ # :host => public_network['ip_address'],
29
+ # :port => '22',
30
+ # :username => 'root',
31
+ # :private_key_path => nil
32
+ # }
33
+ # end
34
+
35
+ def ssh_info
36
+ # Run a custom action called "read_ssh_info" which does what it
37
+ # says and puts the resulting SSH info into the `:machine_ssh_info`
38
+ # key in the environment.
39
+ env = @machine.action("read_ssh_info")
40
+ env[:machine_ssh_info]
41
+ end
42
+
43
+ def state
44
+ # Run a custom action we define called "read_state" which does
45
+ # what it says. It puts the state in the `:machine_state_id`
46
+ # key in the environment.
47
+ env = @machine.action("read_state")
48
+
49
+ state_id = env[:machine_state_id]
50
+
51
+ # Get the short and long description
52
+ short = I18n.t("vagrant_hyperkit.states.short_#{state_id}")
53
+ long = I18n.t("vagrant_hyperkit.states.long_#{state_id}")
54
+
55
+ # Return the MachineState object
56
+ Vagrant::MachineState.new(state_id, short, long)
57
+ rescue
58
+ Vagrant::MachineState.new(:not_running, "", "")
59
+ end
60
+
61
+ def to_s
62
+ id = @machine.id.nil? ? "new" : @machine.id
63
+ "XHYVE (#{id})"
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,17 @@
1
+ module VagrantPlugins
2
+ module HYPERKIT
3
+ module Util
4
+ class Timer
5
+ # A basic utility method that times the execution of the given
6
+ # block and returns it.
7
+ def self.time
8
+ start_time = Time.now.to_f
9
+ yield
10
+ end_time = Time.now.to_f
11
+
12
+ end_time - start_time
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,79 @@
1
+ require 'xhyve'
2
+
3
+ module VagrantPlugins
4
+ module HYPERKIT
5
+ module Util
6
+ # TODO: send all this upstream
7
+ class XhyveGuest < Xhyve::Guest
8
+
9
+ def initialize(**opts)
10
+ super.tap do |s|
11
+ @pid = opts.fetch(:pid, nil)
12
+ @mac = opts[:mac] unless opts[:mac].nil?
13
+ end
14
+ end
15
+
16
+ def start
17
+ return @pid if running?
18
+ super
19
+ end
20
+
21
+ def options
22
+ {
23
+ :pid => @pid,
24
+ :kernel => @kernel,
25
+ :initrd => @initrd,
26
+ :cmdline => @cmdline,
27
+ :blockdevs => @blockdevs,
28
+ :memory => @memory,
29
+ :processors => @processors,
30
+ :uuid => @uuid,
31
+ :serial => @serial,
32
+ :acpi => @acpi,
33
+ :networking => @networking,
34
+ :foreground => @foreground,
35
+ :command => @command,
36
+ :mac => @mac,
37
+ :ip => ip,
38
+ :binary => @binary
39
+ }
40
+ end
41
+
42
+ def build_command
43
+ cmd = [
44
+ "sudo",
45
+ "#{@binary}",
46
+ "#{'-A' if @acpi}",
47
+ '-U', @uuid,
48
+ '-m', @memory,
49
+ '-c', @processors,
50
+ '-s', '0:0,hostbridge',
51
+ "#{"-s #{PCI_BASE - 2}:0,virtio-net,en0" if @networking }" ,
52
+ "#{build_block_device_parameter}",
53
+ '-s', '31,lpc',
54
+ '-l', "#{@serial},stdio",
55
+ '-f', "kexec,#{@kernel},#{@initrd},'#{@cmdline}'"
56
+ ].join(' ')
57
+
58
+ cmd
59
+ end
60
+
61
+ def log
62
+ @logger ||= Log4r::Logger.new("vagrant_hyperkit::vagrant_hyperkit")
63
+ end
64
+
65
+ def build_block_device_parameter
66
+ block_device_parameter = ""
67
+ @blockdevs.each_with_index.map do |p, i|
68
+ if p.include? "qcow"
69
+ block_device_parameter << "-s #{PCI_BASE + i},virtio-blk,file://#{p},format\=qcow "
70
+ else
71
+ block_device_parameter << "-s #{PCI_BASE + i},virtio-blk,#{p} "
72
+ end
73
+ end
74
+ block_device_parameter
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,5 @@
1
+ module VagrantPlugins
2
+ module HYPERKIT
3
+ VERSION = '0.4.3'
4
+ end
5
+ end