Vagrant triggering the plugin before everything else

Viewed 68

We are creating a plugin for authentication for our internal usage. We want to use the plugin for authentication. Plugin checks boxes in environment and if boxes are not there it will authenticate to artifactory. Otherwise it will skip authentication. We need to trigger plugin before vagrant starts downloading boxes. However I tried all hooks one by one. I need to call this before vagrant downloads base images.

Plugin:

  • In config I receive box list to download so I can compare with the boxes already in environment
  • In plugin I call setup first with action hooks. (I tried all the hooks )
  • In setup action I call envset and call authenticate action.

However I can't get authenticate action running before vagrant starts downloading files and it fails since authentication token is not retrieved. I know ENV set gets called (I commented the line gets called in below) But the line where i call authenticate action does not get called.

plugin.rb

module VagrantPlugins
  module Authenticate
    class Plugin < Vagrant.plugin("2")
      # Require a particular version of Vagrant
      Vagrant.require_version(">= 1.5")

      name "authenticate"
      description <<-DESC
      Automatically pops up authentication prompt for artifactory access
      DESC


      [:environment_plugins_loaded, :environment_load, :environment_unload, :machine_action_boot, :machine_action_config_validate,  :machine_action_destroy, :machine_action_halt, :machine_action_package, :machine_action_provision, :machine_action_read_state, :machine_action_reload, :machine_action_resume, :machine_action_run_command, :machine_action_ssh, :machine_action_suspend, :machine_action_sync_folders, :machine_action_up].each do |action|
        action_hook(:authenticate_provision, action) do |hook|
          hook.after(Vagrant::Action::Builtin::ConfigValidate, Action::Base.setup)
        end
      end



      config(:authenticate) do
        require_relative "config"
        Config
      end
    end
  end
end

setup.rb

module VagrantPlugins
  module Authenticate
    module Action
      class Base
        # Cleanup any shared folders before destroying the VM.

        # loading the datafile from disk.
        def self.setup
          Vagrant::Action::Builder.new.tap do |b|
#This line of code gets called
            b.use Vagrant::Action::Builtin::EnvSet, authenticate: Env.new
#This line does not get called
            b.use Action::Authenticate

          end
        end

authenticate.rb

module VagrantPlugins
  module Authenticate
    module Action
      class Authenticate < Base
        def call(env)
#Never reaches this point
          puts env[:machine].config.authenticate.to_hash[:enabled]

          unless env[:machine].config.authenticate.to_hash[:enabled]
            @logger.info "Authenticate disabled, skipping"
            return @app.call(env)
          end ...

In vagrant file VagrantFile :

$vagrant_server_base_url = <Internal_address>
Vagrant.configure("2") do |config|

  config.vagrant.plugins = ["vagrant-none-communicator", "vagrant-proxyconf", "vagrant-host-shell", "vagrant-authenticate"]

  config.proxy.http     = <proxy_address>
  config.proxy.https    = <proxy_address>
  config.authenticate.enabled=true

  config.vm.define "client" do |client|
    client.vm.box = $client
    client.vm.box_url = $vagrant_repo_url + $client + ".box"
    client.vm.box_check_update = $box_check_update
    client.vm.hostname = "client"
0 Answers
Related