2018-03-08 22:20:21 +00:00
|
|
|
require "vagrant"
|
2018-03-10 00:46:29 +00:00
|
|
|
require 'pry'
|
|
|
|
|
|
|
|
require File.expand_path("../vm_trigger", __FILE__)
|
2018-03-08 22:20:21 +00:00
|
|
|
|
|
|
|
module VagrantPlugins
|
|
|
|
module Kernel_V2
|
|
|
|
class TriggerConfig < Vagrant.plugin("2", :config)
|
2018-03-08 23:34:39 +00:00
|
|
|
|
2018-03-08 22:20:21 +00:00
|
|
|
def initialize
|
|
|
|
@logger = Log4r::Logger.new("vagrant::config::trigger")
|
|
|
|
|
2018-03-10 00:46:29 +00:00
|
|
|
# Internal State
|
|
|
|
@_before_triggers = [] # An array of VagrantConfigTrigger objectrs
|
|
|
|
@_after_triggers = [] # An array of VagrantConfigTrigger objectrs
|
2018-03-08 22:20:21 +00:00
|
|
|
end
|
|
|
|
|
2018-03-10 00:45:48 +00:00
|
|
|
# Reads in and parses Vagrant command whitelist and settings for a defined
|
|
|
|
# trigger
|
|
|
|
#
|
2018-03-10 00:46:29 +00:00
|
|
|
# @param [Symbol] command Vagrant command to create trigger on
|
2018-03-10 00:45:48 +00:00
|
|
|
# @param [Block] block The defined before block
|
2018-03-09 00:00:51 +00:00
|
|
|
def before(*command, &block)
|
2018-03-10 00:46:29 +00:00
|
|
|
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
|
2018-03-10 00:45:48 +00:00
|
|
|
else
|
|
|
|
# No config block given at all, validation step should throw error?
|
|
|
|
end
|
2018-03-10 00:46:29 +00:00
|
|
|
|
|
|
|
command.each do |cmd|
|
2018-03-13 18:07:41 +00:00
|
|
|
trigger = create_trigger(cmd, blk)
|
2018-03-10 00:46:29 +00:00
|
|
|
@_before_triggers << trigger
|
|
|
|
end
|
2018-03-08 22:20:21 +00:00
|
|
|
end
|
|
|
|
|
2018-03-10 00:46:29 +00:00
|
|
|
# Reads in and parses Vagrant command whitelist and settings for a defined
|
|
|
|
# trigger
|
|
|
|
#
|
|
|
|
# @param [Symbol] command Vagrant command to create trigger on
|
2018-03-13 18:07:41 +00:00
|
|
|
# @param [Block] block The defined after block
|
2018-03-09 00:00:51 +00:00
|
|
|
def after(*command, &block)
|
2018-03-10 00:46:29 +00:00
|
|
|
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
|
2018-03-10 00:45:48 +00:00
|
|
|
else
|
|
|
|
# No config block given at all, validation step should throw error?
|
|
|
|
end
|
2018-03-08 22:20:21 +00:00
|
|
|
|
2018-03-10 00:45:48 +00:00
|
|
|
command.each do |cmd|
|
2018-03-13 18:07:41 +00:00
|
|
|
trigger = create_trigger(cmd, blk)
|
2018-03-10 00:46:29 +00:00
|
|
|
@_after_triggers << trigger
|
2018-03-10 00:45:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-10 00:46:29 +00:00
|
|
|
#-------------------------------------------------------------------
|
|
|
|
# Internal methods, don't call these.
|
|
|
|
#-------------------------------------------------------------------
|
2018-03-10 00:45:48 +00:00
|
|
|
|
2018-03-13 18:07:41 +00:00
|
|
|
# @param [Symbol] command Vagrant command to create trigger on
|
|
|
|
# @param [Block] block The defined config block
|
|
|
|
def create_trigger(command, block)
|
|
|
|
trigger = VagrantConfigTrigger.new(cmd)
|
|
|
|
if block.is_a?(Hash)
|
|
|
|
trigger.set_options(block)
|
|
|
|
else
|
|
|
|
block.call(trigger, VagrantConfigTrigger)
|
|
|
|
end
|
|
|
|
trigger.finalize!
|
|
|
|
return trigger
|
|
|
|
end
|
|
|
|
|
2018-03-08 22:20:21 +00:00
|
|
|
def finalize!
|
2018-03-10 00:46:29 +00:00
|
|
|
# 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
|
2018-03-08 22:20:21 +00:00
|
|
|
end
|
|
|
|
|
2018-03-08 23:31:58 +00:00
|
|
|
# Validate Trigger settings
|
2018-03-08 22:20:21 +00:00
|
|
|
def validate(machine)
|
2018-03-10 00:46:29 +00:00
|
|
|
if !@_before_triggers.empty?
|
|
|
|
binding.pry
|
|
|
|
end
|
|
|
|
|
2018-03-09 21:06:26 +00:00
|
|
|
errors = _detected_errors
|
2018-03-10 00:46:29 +00:00
|
|
|
@_before_triggers.each do |bt|
|
|
|
|
errors << bt.validate(machine)
|
|
|
|
end
|
|
|
|
|
|
|
|
@_after_triggers.each do |at|
|
|
|
|
errors << at.validate(machine)
|
|
|
|
end
|
2018-03-09 21:06:26 +00:00
|
|
|
|
|
|
|
{"triggers" => errors}
|
2018-03-08 22:20:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# The String representation of this Trigger.
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
def to_s
|
|
|
|
"Trigger"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|