Move trigger options into own plugin class

This commit is contained in:
Brian Cain 2018-03-09 16:46:29 -08:00
parent fdf1b58570
commit 6f00eb5679
No known key found for this signature in database
GPG Key ID: 43D51080D357A001
4 changed files with 207 additions and 114 deletions

View File

@ -1,152 +1,95 @@
require "vagrant"
require 'pry'
require File.expand_path("../vm_trigger", __FILE__)
module VagrantPlugins
module Kernel_V2
class TriggerConfig < Vagrant.plugin("2", :config)
DEFAULT_ON_ERROR = :halt
# Name for the given Trigger. Defaults to nil.
#
# @return [String]
attr_accessor :name
# Internal unique name for this provisioner
# Set to the given :name if exists, otherwise
# it's set as a UUID.
#
# Note: This is for internal use only.
#
# @return [String]
attr_reader :id
# A string to print at the WARN level
#
# @return [String]
attr_accessor :info
# A string to print at the WARN level
#
# @return [String]
attr_accessor :warn
# Determines what how a Trigger should behave if it runs into an error.
# Defaults to :halt, otherwise can only be set to :continue.
#
# @return [Symbol]
attr_accessor :on_error
# If set, will not run trigger for the configured Vagrant commands.
#
# @return [String, Array]
attr_accessor :ignore
# If set, will only run trigger for guests that match keys for this parameter.
#
# @return [String, Array]
attr_accessor :only_on
# A local inline or file script to execute for the trigger
#
# @return [Hash]
attr_accessor :run
# A remote inline or file script to execute for the trigger
#
# @return [Hash]
attr_accessor :run_remote
def initialize
@logger = Log4r::Logger.new("vagrant::config::trigger")
# Internal state
@id = SecureRandom.uuid
# Expected to store state like:
# {@id=>{"command" => [Triggers],"command2"=>[Triggers]}}
# finalize will take this data structure and construct action hooks
# Does this make sense
@_before_triggers = {} # A hash of all before triggers and their settings
@_after_triggers = {} # A hash of all after triggers and their settings
# Trigger config options
@name = UNSET_VALUE
@info = UNSET_VALUE
@warn = UNSET_VALUE
@on_error = UNSET_VALUE
@ignore = UNSET_VALUE
@only_on = UNSET_VALUE
@run = UNSET_VALUE
@run_remote = UNSET_VALUE
# Internal State
@_before_triggers = [] # An array of VagrantConfigTrigger objectrs
@_after_triggers = [] # An array of VagrantConfigTrigger objectrs
end
# Reads in and parses Vagrant command whitelist and settings for a defined
# trigger
#
# @param [Array, Symbol] command Vagrant command to create trigger on
# @param [Symbol] command Vagrant command to create trigger on
# @param [Block] block The defined before block
def before(*command, &block)
if block_given?
puts "the command: #{command}"
puts "the block: #{block}"
command.each do |cmd|
@_before_triggers[@id] = {cmd=>block}
end
puts @_before_triggers
elsif command.last.is_a?(Hash)
blck = command.pop
command.each do |cmd|
@_before_triggers[@id] = {cmd=>blck}
end
blk = block
if !block_given? && command.last.is_a?(Hash)
# We were given a hash rather than a block,
# so the last element should be the "config block"
# and the rest are commands for the trigger
blk = command.pop
else
# No config block given at all, validation step should throw error?
end
puts "The trigger: #{@_before_triggers}"
command.each do |cmd|
trigger = VagrantConfigTrigger.new(cmd)
trigger.add_config(blk)
@_before_triggers << trigger
end
end
# @param [Array, Symbol] command Vagrant command to create trigger on
# @param [Block] block The defined after block
# Reads in and parses Vagrant command whitelist and settings for a defined
# trigger
#
# @param [Symbol] command Vagrant command to create trigger on
# @param [Block] block The defined before block
def after(*command, &block)
if block_given?
store_after_trigger(command, block)
elsif command.last.is_a?(Hash)
blck = command.pop
store_after_trigger(command, blck)
blk = block
if !block_given? && command.last.is_a?(Hash)
# We were given a hash rather than a block,
# so the last element should be the "config block"
# and the rest are commands for the trigger
blk = command.pop
else
# No config block given at all, validation step should throw error?
end
command.each do |cmd|
trigger = VagrantConfigTrigger.new(cmd)
trigger.add_config(blk)
@_after_triggers << trigger
end
end
#-------------------------------------------------------------------
# Internal methods, don't call these.
#-------------------------------------------------------------------
def store_before_trigger(*command, block)
command.each do |cmd|
@_before_triggers[@id] = {cmd=>block}
end
end
def store_after_trigger(*command, block)
command.each do |cmd|
@_after_triggers[@id] = {cmd=>block}
end
end
def finalize!
# Ensure all config options are set to nil if untouched by user
@name = nil if @name == UNSET_VALUE
@info = nil if @info == UNSET_VALUE
@warn = nil if @warn == UNSET_VALUE
@on_error = DEFAULT_ON_ERROR if @on_error == UNSET_VALUE
@ignore = nil if @ignore == UNSET_VALUE
@only_on = nil if @only_on == UNSET_VALUE
@run = nil if @run == UNSET_VALUE
@run_remote = nil if @run_remote == UNSET_VALUE
# read through configured settings blocks and set their values
# and then set up action hooks here?
# for some reason not all triggers are showing up here
#puts @_before_triggers if !@_before_triggers.empty?
#puts @_after_triggers if !@_after_triggers.empty?
if !@_before_triggers.empty?
binding.pry
end
end
# Validate Trigger settings
def validate(machine)
if !@_before_triggers.empty?
binding.pry
end
errors = _detected_errors
@_before_triggers.each do |bt|
errors << bt.validate(machine)
end
@_after_triggers.each do |at|
errors << at.validate(machine)
end
{"triggers" => errors}
end

View File

@ -0,0 +1,112 @@
require 'log4r'
require 'pry'
module VagrantPlugins
module Kernel_V2
# Represents a single configured provisioner for a VM.
class VagrantConfigTrigger < Vagrant.plugin("2", :config)
DEFAULT_ON_ERROR = :halt
# Internal unique name for this trigger
#
# Note: This is for internal use only.
#
# @return [String]
attr_reader :id
# Name for the given Trigger. Defaults to nil.
#
# @return [String]
attr_accessor :name
# Command to fire the trigger on
#
# @return [Symbol]
attr_reader :command
# A string to print at the WARN level
#
# @return [String]
attr_accessor :info
# A string to print at the WARN level
#
# @return [String]
attr_accessor :warn
# Determines what how a Trigger should behave if it runs into an error.
# Defaults to :halt, otherwise can only be set to :continue.
#
# @return [Symbol]
attr_accessor :on_error
# If set, will not run trigger for the configured Vagrant commands.
#
# @return [String, Array]
attr_accessor :ignore
# If set, will only run trigger for guests that match keys for this parameter.
#
# @return [String, Array]
attr_accessor :only_on
# A local inline or file script to execute for the trigger
#
# @return [Hash]
attr_accessor :run
# A remote inline or file script to execute for the trigger
#
# @return [Hash]
attr_accessor :run_remote
def initialize(command)
@logger = Log4r::Logger.new("vagrant::config::vm::trigger::config")
#@logger.debug("Trigger defined: #{name}")
@name = UNSET_VALUE
@info = UNSET_VALUE
@warn = UNSET_VALUE
@on_error = UNSET_VALUE
@ignore = UNSET_VALUE
@only_on = UNSET_VALUE
@run = UNSET_VALUE
@run_remote = UNSET_VALUE
# Internal options
@id = SecureRandom.uuid
@command = command.to_sym
end
# Expecting a ruby block of a trigger config
def add_config(config_block)
if config_block.is_a?(Hash)
# We have a ruby hash
self.set_options(config_block)
else
# We have a ruby block
end
end
def finalize!
# Ensure all config options are set to nil if untouched by user
@name = nil if @name == UNSET_VALUE
@info = nil if @info == UNSET_VALUE
@warn = nil if @warn == UNSET_VALUE
@on_error = DEFAULT_ON_ERROR if @on_error == UNSET_VALUE
@ignore = nil if @ignore == UNSET_VALUE
@only_on = nil if @only_on == UNSET_VALUE
@run = nil if @run == UNSET_VALUE
@run_remote = nil if @run_remote == UNSET_VALUE
end
def validate(machine)
binding.pry
errors = _detected_errors
# Validate that each config option has the right values and is the right type
{"triggers" => errors}
end
end
end
end

View File

@ -29,8 +29,6 @@ describe VagrantPlugins::Kernel_V2::TriggerConfig do
allow(machine).to receive(:env).and_return(env)
allow(machine).to receive(:provider_config).and_return(nil)
allow(machine).to receive(:provider_options).and_return({})
subject.name = "foo"
end
it "is valid with test defaults" do

View File

@ -0,0 +1,40 @@
require File.expand_path("../../../../base", __FILE__)
require Vagrant.source_root.join("plugins/kernel_v2/config/vm_trigger")
describe VagrantPlugins::Kernel_V2::VagrantConfigTrigger do
include_context "unit"
subject { described_class.new }
let(:machine) { double("machine") }
def assert_invalid
errors = subject.validate(machine)
if !errors.values.any? { |v| !v.empty? }
raise "No errors: #{errors.inspect}"
end
end
def assert_valid
errors = subject.validate(machine)
if !errors.values.all? { |v| v.empty? }
raise "Errors: #{errors.inspect}"
end
end
before do
env = double("env")
allow(env).to receive(:root_path).and_return(nil)
allow(machine).to receive(:env).and_return(env)
allow(machine).to receive(:provider_config).and_return(nil)
allow(machine).to receive(:provider_options).and_return({})
subject.name = "foo"
end
it "is valid with test defaults" do
subject.finalize!
assert_valid
end
end