Date: Fri, 10 Jun 2016 12:36:00 GMT From: litong@FreeBSD.org To: svn-soc-all@FreeBSD.org Subject: socsvn commit: r305037 - in soc2016/litong/vagrant-bhyve/trunk: example_box lib lib/vagrant-bhyve Message-ID: <201606101236.u5ACa0Vb034905@socsvn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: litong Date: Fri Jun 10 12:36:00 2016 New Revision: 305037 URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=305037 Log: add initial example box Added: soc2016/litong/vagrant-bhyve/trunk/example_box/ soc2016/litong/vagrant-bhyve/trunk/example_box/README.md soc2016/litong/vagrant-bhyve/trunk/example_box/metadata.json soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/action.rb soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/config.rb soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/driver.rb soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/errors.rb soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/plugin.rb soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/provider.rb soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/util.rb Modified: soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve.rb soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/version.rb Added: soc2016/litong/vagrant-bhyve/trunk/example_box/README.md ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ soc2016/litong/vagrant-bhyve/trunk/example_box/README.md Fri Jun 10 12:36:00 2016 (r305037) @@ -0,0 +1,28 @@ +# Vagrant Bhyve Example Box + +Vagrant providers each require a custom provider-specific box format. +This folder shows the example contents of a box for the `bhyve` provider. +To turn this into a box create a vagrant image according documentation (don't + forget to install rsync command) and create box with following command: + +``` +$ tar cvzf custom_box.box ./metadata.json ./Vagrantfile ./box.img +``` + +This box works by using Vagrant's built-in Vagrantfile merging to setup +defaults for Bhyve. These defaults can easily be overwritten by higher-level +Vagrantfiles (such as project root Vagrantfiles). + +## Box Metadata + +Bhyve box should define at least two data fields in `metadata.json` file. + +* provider - Provider name is bhyve +* loader - Loader should be grub2-bhyve or bhyveload + +## Converting Boxes + +Instead of creating a box from scratch, you can use +[vagrant-mutate](https://github.com/sciurus/vagrant-mutate) +to take boxes created for other Vagrant providers and use them +with vagrant-bhyve Added: soc2016/litong/vagrant-bhyve/trunk/example_box/metadata.json ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ soc2016/litong/vagrant-bhyve/trunk/example_box/metadata.json Fri Jun 10 12:36:00 2016 (r305037) @@ -0,0 +1,4 @@ +{ + "provider" : "libvirt", + "loader" : "bhyveload" +} Modified: soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve.rb ============================================================================== --- soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve.rb Fri Jun 10 12:35:48 2016 (r305036) +++ soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve.rb Fri Jun 10 12:36:00 2016 (r305037) @@ -1,10 +1,13 @@ require "pathname" - require "vagrant-bhyve/version" module VagrantPlugin - module Bhyve + module ProviderBhyve lib_path = Pathname.new(File.expand_path("../vagrant-bhyve", __FILE__)) + autoload :Action, lib_path.join('action') + autoload :Driver, lib_path.join('driver') + autoload :Errors, lib_path.join('errors') + autoload :Util, lib_path.join('util') # This function returns the path to the source of this plugin Added: soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/action.rb ============================================================================== Added: soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/config.rb ============================================================================== Added: soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/driver.rb ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/driver.rb Fri Jun 10 12:36:00 2016 (r305037) @@ -0,0 +1,21 @@ +require "log4r" + +module VagrantPlugins + module ProviderBhyve + class Driver + + @@sudo = '' + + def initialize(machine) + @logger = Log4r::Logger.new("vagrant::bhyve::driver") + @machine = machine + end + + # if vagrant is excecuted by root (or with sudo) then the variable + # will be empty string, otherwise it will be 'sudo' to make sure we + # can run bhyve, bhyveload and pf with sudo privilege + def sudo + if Process.uid == 0 + @@sudo = '' + else + @@sudo = 'sudo' Added: soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/errors.rb ============================================================================== Added: soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/plugin.rb ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/plugin.rb Fri Jun 10 12:36:00 2016 (r305037) @@ -0,0 +1,73 @@ +begin + require "vagrant" +rescue LoadError + raise "The Vagrant Bhyve plugin must be run within Vagrant." +end + +############################################################# +# TBD some version check # +############################################################# + +module VagrantPlugins + module ProviderBhyve + class Plugin < Vagrant.plugin('2') + name "bhyve" + description <<-DESC + This plugin allows vagrant to manage VMs in bhyve, the hypervisor + provided by FreeBSD's kernel + DESC + + config(:bhyve, :provider) do + require_relative "config" + Config + end + + provider(:bhyve, parallel: true) do + require_relative "provider" + Provider + end + + # This initializes the internationalization strings. + def self.setup_i18n + I18n.load_path << File.expand_path('locales/en.yml', + ProviderLibvirt.source_root) + I18n.reload! + end + + # This sets up our log level to be whatever VAGRANT_LOG is. + def self.setup_logging + require 'log4r' + + level = nil + begin + level = Log4r.const_get(ENV['VAGRANT_LOG'].upcase) + rescue NameError + # This means that the logging constant wasn't found, + # which is fine. We just keep `level` as `nil`. But + # we tell the user. + level = nil + end + + # Some constants, such as "true" resolve to booleans, so the + # above error checking doesn't catch it. This will check to make + # sure that the log level is an integer, as Log4r requires. + level = nil if !level.is_a?(Integer) + + # Set the logging level on all "vagrant" namespaced + # logs as long as we have a valid level. + if level + logger = Log4r::Logger.new('vagrant_libvirt') + logger.outputters = Log4r::Outputter.stderr + logger.level = level + logger = nil + end + end + + # Setup logging and i18n before any autoloading loads other classes + # with logging configured as this prevents inheritance of the log level + # from the parent logger. + setup_logging + setup_i18n + end + end +end Added: soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/provider.rb ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/provider.rb Fri Jun 10 12:36:00 2016 (r305037) @@ -0,0 +1,68 @@ +require "vagrant" +require "log4r" + +module VagrantPlugins + module ProviderBhyve + autoload :Driver, 'vagrant-bhyve/driver' + + class Provider < Vagrant.plugin('2', :provider) + + def initialize(machine) + @logger = Log4r::Logger.new("vagrant::provider::bhyve") + @machine = machine + end + + def action(name) + # Attrmpt to get the action method from the Action class if it + # exists, otherwise return nil to show that we don't support the + # given action + action_method = "action_#{name}" + return Action.send(action_method) if Action.respond_to?(action_method) + nil + end + + def driver + return @driver if @driver + @driver = Driver.new(@machine) + end + + # This should return a hash of information that explains how to SSH + # into the machine. If the machine is not at a point where SSH is + # even possiable, then 'nil' should be returned + # + # The general structure of this returned hash should be the + # following: + # + # { + # host: "1.2.3.4", + # port: "22", + # username: "vagrant", + # private_key_path: "/path/to/my/key" + # } + def ssh_info + return nil if state.id != :running + + ip = driver.get_ipaddress(@machine) + + # We just return nil if were not able to identify the VM's IP and + # let Vagrant core deal with it like docker provider does + return nil if !ip + + ssh_info = { + host: ip, + port: @machine.config.ssh.guest_port + } +############################################################# +# TBD add more ssh info # +############################################################# + ssh_info + end + + def state + + end + + + end + end +end Added: soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/util.rb ============================================================================== Modified: soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/version.rb ============================================================================== --- soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/version.rb Fri Jun 10 12:35:48 2016 (r305036) +++ soc2016/litong/vagrant-bhyve/trunk/lib/vagrant-bhyve/version.rb Fri Jun 10 12:36:00 2016 (r305037) @@ -1,6 +1,7 @@ <<<<<<< f0976cd4b81347c0d687988ff6463d3113e547d2 <<<<<<< 8569ecf8bd116d9e214f6907f29993bd8e03d40d module VagrantPlugins +<<<<<<< 41be567461e651dace80def0b0d464b58d14e3da ======= module Vagrant >>>>>>> Initial commit @@ -8,6 +9,9 @@ module VagrantPlugins >>>>>>> remove bin module Bhyve +======= + module ProviderBhyve +>>>>>>> add initial example box VERSION = "0.1.0" end end
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201606101236.u5ACa0Vb034905>