-
Notifications
You must be signed in to change notification settings - Fork 24
/
plugin.rb
111 lines (100 loc) · 4.13 KB
/
plugin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
begin
require 'vagrant'
rescue LoadError
raise 'The vagrant-registration plugin must be run within Vagrant.'
end
# This is a sanity check to make sure no one is attempting to install
# this into an early Vagrant version.
if Vagrant::VERSION < '1.2.0'
fail I18n.t('registration.plugin.compatible_message')
end
module VagrantPlugins
module Registration
class Plugin < Vagrant.plugin('2')
class << self
# vagrant-vbguest plugin updates GuestAdditions for VirtualBox
# and therefore needs to be run after the box got registered.
# See https://github.com/projectatomic/adb-vagrant-registration/issues/69
#
# vagrant-vbguest hooks before VagrantPlugins::ProviderVirtualBox::Action::CheckGuestAdditions
# (see https://github.com/mitchellh/vagrant/blob/master/plugins/providers/virtualbox/action.rb#L81)
# For registration to occur in time, it has to happen before that. Using WaitForCommunicator
# to be sure - https://github.com/dotless-de/vagrant-vbguest/blob/master/lib/vagrant-vbguest.rb#L53
#
# For vagrant-libvirt WaitTillUp is used
def register(hook)
setup_logging
registered = false
if virtual_box?
hook.after(VagrantPlugins::ProviderVirtualBox::Action::WaitForCommunicator,
VagrantPlugins::Registration::Action.action_register)
registered = true
end
if libvirt?
hook.after(VagrantPlugins::ProviderLibvirt::Action::WaitTillUp,
VagrantPlugins::Registration::Action.action_register)
registered = true
end
# Best guess for the other providers
unless registered
hook.after(Vagrant::Action::Builtin::WaitForCommunicator,
VagrantPlugins::Registration::Action.action_register)
end
end
def unregister_on_halt(hook)
setup_logging
hook.prepend(VagrantPlugins::Registration::Action.action_unregister_on_halt)
end
def unregister_on_destroy(hook)
setup_logging
hook.prepend(VagrantPlugins::Registration::Action.action_unregister_on_destroy)
end
end
name I18n.t('registration.plugin.name')
description I18n.t('registration.plugin.description')
action_hook(:registration_register, :machine_action_up, &method(:register))
action_hook(:registration_register, :machine_action_provision, &method(:register))
action_hook(:registration_unregister_on_halt, :machine_action_halt, &method(:unregister_on_halt))
action_hook(:registration_unregister_on_destroy, :machine_action_destroy, &method(:unregister_on_destroy))
config(:registration) do
setup_logging
require_relative 'config'
Config
end
# This sets up our log level to be whatever VAGRANT_LOG is
# for loggers prepended with 'vagrant_registration'
def self.setup_logging
require 'log4r'
level = nil
begin
level = Log4r.const_get(ENV['VAGRANT_LOG'].upcase)
rescue NameError
# This means that the logging constant wasn't found,
# which is fine. We just keep `level` as `nil`. But
# we tell the user.
level = nil
end
# Some constants, such as "true" resolve to booleans, so the
# above error checking doesn't catch it. This will check to make
# sure that the log level is an integer, as Log4r requires.
level = nil unless level.is_a?(Integer)
# Set the logging level on all "vagrant" namespaced
# logs as long as we have a valid level.
if level
logger = Log4r::Logger.new('vagrant_registration')
logger.outputters = Log4r::Outputter.stderr
logger.level = level
logger = nil
end
end
# Determines if VirtualBox is provider
def self.virtual_box?
defined?(VagrantPlugins::ProviderVirtualBox::Provider)
end
# Determines if LibVirt is provider
def self.libvirt?
defined?(VagrantPlugins::ProviderLibvirt::Provider)
end
end
end
end