forked from devopsgroup-io/vagrant-hostmanager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.rb
109 lines (93 loc) · 4.15 KB
/
config.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
module VagrantPlugins
module HostManager
class Config < Vagrant.plugin('2', :config)
attr_accessor :enabled
attr_accessor :manage_host
attr_accessor :manage_guest
attr_accessor :ignore_private_ip
attr_accessor :aliases
attr_accessor :include_offline
attr_accessor :ip_resolver
alias_method :enabled?, :enabled
alias_method :include_offline?, :include_offline
alias_method :manage_host?, :manage_host
alias_method :manage_guest?, :manage_guest
def initialize
@enabled = UNSET_VALUE
@manage_host = UNSET_VALUE
@manage_guest = UNSET_VALUE
@ignore_private_ip = UNSET_VALUE
@include_offline = UNSET_VALUE
@aliases = UNSET_VALUE
@ip_resolver = UNSET_VALUE
end
def merge(other)
super.tap do |result|
@enabled = false if @enabled == UNSET_VALUE
other.enabled = false if other.enabled == UNSET_VALUE
result.enabled = [@enabled, other.enabled].any?
@manage_host = false if @manage_host == UNSET_VALUE
other.manage_host = false if other.manage_host == UNSET_VALUE
result.manage_host = [@manage_host, other.manage_host].any?
@manage_guest = true if @manage_guest == UNSET_VALUE
other.manage_guest = true if other.manage_guest == UNSET_VALUE
result.manage_guest = [@manage_guest, other.manage_guest].any?
@ignore_private_ip = false if @ignore_private_ip == UNSET_VALUE
other.ignore_private_ip = false if other.ignore_private_ip == UNSET_VALUE
result.ignore_private_ip = [@ignore_private_ip, other.ignore_private_ip].any?
@include_offline = false if @include_offline == UNSET_VALUE
other.include_offline = false if other.include_offline == UNSET_VALUE
result.include_offline = [@include_offline, other.include_offline].any?
end
end
def finalize!
@enabled = false if @enabled == UNSET_VALUE
@manage_host = false if @manage_host == UNSET_VALUE
@manage_guest = true if @manage_guest == UNSET_VALUE
@ignore_private_ip = false if @ignore_private_ip == UNSET_VALUE
@include_offline = false if @include_offline == UNSET_VALUE
@aliases = [] if @aliases == UNSET_VALUE
@ip_resolver = nil if @ip_resolver == UNSET_VALUE
@aliases = [ @aliases ].flatten
end
def validate(machine)
errors = []
errors << validate_bool('hostmanager.enabled', @enabled)
errors << validate_bool('hostmanager.manage_host', @manage_host)
errors << validate_bool('hostmanager.manage_guest', @manage_guest)
errors << validate_bool('hostmanager.ignore_private_ip', @ignore_private_ip)
errors << validate_bool('hostmanager.include_offline', @include_offline)
errors.compact!
# check if aliases option is an Array
if !machine.config.hostmanager.aliases.kind_of?(Array) &&
!machine.config.hostmanager.aliases.kind_of?(String)
errors << I18n.t('vagrant_hostmanager.config.not_an_array_or_string', {
:config_key => 'hostmanager.aliases',
:is_class => aliases.class.to_s,
})
end
if !machine.config.hostmanager.ip_resolver.nil? &&
!machine.config.hostmanager.ip_resolver.kind_of?(Proc)
errors << I18n.t('vagrant_hostmanager.config.not_a_proc', {
:config_key => 'hostmanager.ip_resolver',
:is_class => ip_resolver.class.to_s,
})
end
errors.compact!
{ "HostManager configuration" => errors }
end
private
def validate_bool(key, value)
if ![TrueClass, FalseClass].include?(value.class) &&
value != UNSET_VALUE
I18n.t('vagrant_hostmanager.config.not_a_bool', {
:config_key => key,
:value => value.class.to_s
})
else
nil
end
end
end
end
end