class Object

Constants

ARGV_DUP

Public Instance Methods

ensure_root() click to toggle source
# File bin/boxgrinder-build, line 164
def ensure_root
  unless Process.uid == 0
    puts("Currently running as non-root user, BoxGrinder will re-launch under `sudo -E` and change to your local user after the OS plugin.")
    # http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/151376
    ruby = File.join(RbConfig::CONFIG["bindir"],
                     RbConfig::CONFIG["RUBY_INSTALL_NAME"] + RbConfig::CONFIG["EXEEXT"])
    exec("sudo -E #{ruby} -I#{$:.join(':')} #{Pathname.new(__FILE__).realpath} #{ARGV_DUP.join(" ")} --change-to-user")
  end
end
process_options(options) click to toggle source
# File bin/boxgrinder-build, line 70
def process_options(options)
  OptionParser.new do |opts|
    program = File.basename($0)

    opts.banner = <<EOB
Usage: #{program} [appliance definition file] [options]

A tool for building VM images from simple definition files.
        
Homepage:
    http://boxgrinder.org/

Documentation:
    http://boxgrinder.org/tutorials/

Examples:
    $ #{program} jeos.appl                                                           # Build KVM image for jeos.appl
    $ #{program} jeos.appl -f                                                        # Build KVM image for jeos.appl with removing previous build for this image
    $ #{program} jeos.appl --os-config format:qcow2                                  # Build KVM image for jeos.appl with a qcow2 disk
    $ #{program} jeos.appl -p vmware --platform-config type:personal,thin_disk:true  # Build VMware image for VMware Server, Player, Fusion using thin (growing) disk
    $ #{program} jeos.appl -p ec2 -d ami                                             # Build and register AMI for jeos.appl
    $ #{program} jeos.appl -p vmware -d local                                        # Build VMware image for jeos.appl and deliver it to local directory
EOB


    opts.separator ""
    opts.separator "Options:"

    opts.on("-p", "--platform [TYPE]", "The name of platform you want to convert to.") { |v| options.platform = v.to_sym }
    opts.on("-d", "--delivery [METHOD]", "The delivery method for selected appliance.") { |v| options.delivery = v.to_sym }
    opts.on("-f", "--force", "Force image creation - removes all previous builds for selected appliance. Default: false.") { |v| options.force = v }

    opts.separator ""
    opts.separator "Plugin configuration options:"

    opts.on("-l", "--plugins [PLUGINS]", Array, "Comma separated list of additional plugins. Default: empty.") do |v|
      options[:additional_plugins] = v
    end

    opts.separator ""

    opts.on("--os-config [CONFIG]", Array, "Operating system plugin configuration in format: key1:value1,key2:value2.") do |v|
      validate_hash_option(options, :os_config, v) do |entry|
        puts "Operating system plugin configuration is not valid at '#{entry}'. Valid format is: 'key:value'."
      end
    end

    opts.on("--platform-config [CONFIG]", Array, "Platform plugin configuration in format: key1:value1,key2:value2.") do |v|
      validate_hash_option(options, :platform_config, v) do |entry|
        puts "Platform plugin configuration is not valid at '#{entry}'. Valid format is: 'key:value'."
      end
    end

    opts.on("--delivery-config [CONFIG]", Array, "Delivery plugin configuration in format: key1:value1,key2:value2.") do |v|
      validate_hash_option(options, :delivery_config, v) do |entry|
        puts "Delivery plugin configuration is not valid at '#{entry}'. Valid format is: 'key:value'."
      end
    end

    opts.separator ""
    opts.separator "Logging options:"

    opts.on("--debug", "Prints debug information while building. Default: false.") { options[:log_level] = :debug }
    opts.on("--trace", "Prints trace information while building. Default: false.") { options[:log_level] = :trace }
    opts.on("-b", "--backtrace", "Prints full backtrace if errors occur whilst building. Default: true if console log is set to debug or trace, otherwise false.") { |v| options[:backtrace] = v }

    opts.separator ""
    opts.separator 'Common options:'

    opts.on_tail('--help', 'Show this message.') do
      puts opts
      exit
    end

    opts.on_tail('--version', 'Print the version.') do
      puts "BoxGrinder Build #{File.read("#{File.dirname(__FILE__)}/../CHANGELOG").match(/^v(.*)/)[1]}"

      [:os, :platform, :delivery].each do |type|
        puts
        puts "Available #{type} plugins:"
        BoxGrinder::PluginManager.instance.plugins[type].each do |name, plugin_info|
          puts " - #{name} plugin for #{plugin_info[:full_name]}"
        end
      end

      exit
    end

    opts.on('--change-to-user', 'Change from root to local user after the OS plugin completes.') do
      options[:change_to_user] = true
    end
  end
end
validate_hash_option(options, name, value) { |entry| ... } click to toggle source
# File bin/boxgrinder-build, line 45
def validate_hash_option(options, name, value)
  value.each do |entry|
    if entry =~ /^(\S+?):(\S+)$/

      k = $1.strip
      v = $2.strip

      if v =~ /^([-+]?(0|[1-9][0-9_]*))$/
        v = v.to_i
      elsif v =~ /^(y|Y|yes|Yes|YES|true|True|TRUE|on|On|ON)$/
        v = true
      elsif v =~ /^(n|N|no|No|NO|false|False|FALSE|off|Off|OFF)$/
        v = false
      elsif v =~ /^([-+]?([0-9][0-9_]*)?\.[0-9.]*([eE][-+][0-9]+)?)$/
        v = v.to_f
      end

      options[name][k] = v
    else
      yield entry if block_given?
      abort
    end
  end
end