class BoxGrinder::BasePlugin

Attributes

deliverables[R]
plugin_info[R]

Public Class Methods

new() click to toggle source
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 37
def initialize
  @plugin_config = {}

  @deliverables = AStruct.new
  @supported_oses = AStruct.new
  @supported_platforms = []
  @target_deliverables = AStruct.new
  @dir = AStruct.new
end

Public Instance Methods

current_platform() click to toggle source
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 138
def current_platform
  platform = :raw

  if @previous_plugin_info[:type] == :platform
    platform = @previous_plugin_info[:name]
  end unless @previous_plugin_info.nil?

  platform.to_s
end
deliverables_exists?() click to toggle source
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 184
def deliverables_exists?
  raise "You can only check deliverables after the plugin is initialized, please initialize the plugin using init method." if @initialized.nil?

  return false if deliverables.empty?

  exists = true

  deliverables.each_value do |file|
    unless File.exists?(file)
      exists = false
      break
    end
  end

  exists
end
execute(args = nil) click to toggle source
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 156
def execute(args = nil)
  raise "You can only execute the plugin after the plugin is initialized, please initialize the plugin using init method." if @initialized.nil?
end
init(config, appliance_config, info, options = {}) click to toggle source
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 47
def init(config, appliance_config, info, options = {})
  @config = config
  @appliance_config = appliance_config
  @options = options
  @plugin_info = info

  # Optional options :)
  @type = options[:type] || @plugin_info[:name]
  @previous_plugin = options[:previous_plugin]
  @log = options[:log] || LogHelper.new
  @exec_helper = options[:exec_helper] || ExecHelper.new(:log => @log)
  @image_helper = options[:image_helper] || ImageHelper.new(@config, @appliance_config, :log => @log)

  @dir.base = "#{@appliance_config.path.build}/#{@plugin_info[:name]}-plugin"
  @dir.tmp = "#{@dir.base}/tmp"

  if @previous_plugin
    @previous_deliverables = @previous_plugin.deliverables
    @previous_plugin_info = @previous_plugin.plugin_info
  else
    @previous_deliverables = AStruct.new
  end

  # TODO get rid of that - we don't have plugin configuration files - everything is now in one place.
  read_plugin_config
  merge_plugin_config

  # Indicate whether deliverables of select plugin should be moved to final destination or not.
  # TODO Needs some thoughts - if we don't have deliverables that we care about - should they be in @deliverables?
  @move_deliverables = true

  # The plugin is initialized now. We can do some fancy stuff with it.
  @initialized = true

  self
end
is_supported_os?() click to toggle source
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 121
def is_supported_os?
  return true if @supported_oses.empty?
  return false unless !@supported_oses[@appliance_config.os.name].nil? and @supported_oses[@appliance_config.os.name].include?(@appliance_config.os.version)
  true
end
is_supported_platform?() click to toggle source
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 115
def is_supported_platform?
  return true if @supported_platforms.empty?
  return false if @previous_plugin_info[:type] == :platform and !@supported_platforms.include?(@previous_plugin_info[:name])
  true
end
merge_plugin_config() click to toggle source

This merges the plugin config with configuration provided in command line

# File lib/boxgrinder-build/plugins/base-plugin.rb, line 220
def merge_plugin_config
  config =
      case @plugin_info[:type]
        when :os
          @config.os_config
        when :platform
          @config.platform_config
        when :delivery
          @config.delivery_config
      end

  @plugin_config.merge!(config)
end
read_plugin_config() click to toggle source

This reads the plugin config from file

# File lib/boxgrinder-build/plugins/base-plugin.rb, line 214
def read_plugin_config
  return if @config[:plugins].nil? or @config[:plugins][@plugin_info[:name].to_s].nil?
  @plugin_config = @config[:plugins][@plugin_info[:name].to_s]
end
register_deliverable(deliverable) click to toggle source
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 93
def register_deliverable(deliverable)
  raise "You can only register deliverables after the plugin is initialized, please initialize the plugin using init method." if @initialized.nil?
  raise "Please specify deliverables as Hash, not #{deliverable.class}." unless deliverable.is_a?(Hash)

  deliverable.each do |name, path|
    @deliverables[name] = "#{@dir.tmp}/#{path}"
    @target_deliverables[name] = "#{@dir.base}/#{path}"
  end
end
register_supported_os(name, versions) click to toggle source
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 103
def register_supported_os(name, versions)
  raise "You can register supported operating system only after the plugin is initialized, please initialize the plugin using init method." if @initialized.nil?

  @supported_oses[name] = AStruct.new if @supported_oses[name].nil?
  @supported_oses[name] = versions
end
register_supported_platform(name) click to toggle source
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 110
def register_supported_platform(name)
  raise "You can register supported platform only after the plugin is initialized, please initialize the plugin using init method." if @initialized.nil?
  @supported_platforms << name
end
run(param = nil) click to toggle source
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 160
def run(param = nil)
  unless is_supported_os?
    raise PluginValidationError, "#{@plugin_info[:full_name]} plugin supports following operating systems: #{supported_oses}. Your appliance contains #{@appliance_config.os.name} #{@appliance_config.os.version} operating system which is not supported by this plugin, sorry."
  end

  unless is_supported_platform?
    raise PluginValidationError, "#{@plugin_info[:full_name]} plugin supports following platforms: #{@supported_platforms.join(', ')}. You selected #{@previous_plugin_info[:name]} platform which is not supported by this plugin, sorry."
  end

  FileUtils.rm_rf @dir.tmp
  FileUtils.mkdir_p @dir.tmp

  param.nil? ? execute : execute(param)

  # TODO execute post commands for platform plugins here?

  @deliverables.each do |name, path|
    @log.trace "Moving '#{path}' deliverable to target destination '#{@target_deliverables[name]}'..."
    FileUtils.mv(path, @target_deliverables[name])
  end if @move_deliverables

  FileUtils.rm_rf @dir.tmp
end
set_default_config_value(key, default_value=nil) { |key, default_value, plugin_config| ... } click to toggle source
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 205
def set_default_config_value(key, default_value=nil, &blk)
  if block_given? && !(@plugin_config[key].nil?)
    @plugin_config[key] = yield(key, default_value, @plugin_config[key])
  else
    @plugin_config[key] ||= default_value
  end
end
subtype(type) { || ... } click to toggle source

Validation helper method.

Execute the validation only for selected plugin type. TODO make this prettier somehow? Maybe moving validation to a class?

# File lib/boxgrinder-build/plugins/base-plugin.rb, line 88
def subtype(type)
  return unless @type == type
  yield if block_given?
end
supported_oses() click to toggle source
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 127
def supported_oses
  supported = ""

  @supported_oses.sort.each do |name, versions|
    supported << ", " unless supported.empty?
    supported << "#{name} (versions: #{versions.join(", ")})"
  end

  supported
end
validate_plugin_config(fields = [], doc = nil) click to toggle source
# File lib/boxgrinder-build/plugins/base-plugin.rb, line 148
def validate_plugin_config(fields = [], doc = nil)
  more_info = doc.nil? ? '' : "See #{doc} for more info"

  fields.each do |field|
    raise PluginValidationError, "Please specify a valid '#{field}' key in BoxGrinder configuration file: '#{@config.file}' or use CLI '--#{@plugin_info[:type]}-config #{field}:DATA' argument. #{more_info}" if @plugin_config[field].nil?
  end
end