(#12116) Windows domain/fqdn error when no domain
This fixes an annoying issue that causes facter to throw errors when domain is
not properly configured or doesn't exist. With this fix, it will properly
return an empty domain and empty fqdn without errorring.
When moving up the chain to the less specific domain fact, added a
windows_hostname command and a check so that it also does not fail with path
not found errors.
Paired-with: Josh Cooper<josh@puppetlabs.com>
Rob Reynolds
10 years ago
28 | 28 |
# Due to dangerous behavior of 'hostname -f' on old OS, we will explicitly opt-in
|
29 | 29 |
# 'hostname -f' --hkenney May 9, 2012
|
30 | 30 |
basic_hostname = 'hostname 2> /dev/null'
|
|
31 |
windows_hostname = 'hostname > NUL'
|
31 | 32 |
full_hostname = 'hostname -f 2> /dev/null'
|
32 | 33 |
can_do_hostname_f = Regexp.union /Linux/i, /FreeBSD/i, /Darwin/i
|
33 | 34 |
|
34 | 35 |
hostname_command = if Facter.value(:kernel) =~ can_do_hostname_f
|
35 | 36 |
full_hostname
|
|
37 |
elsif Facter.value(:kernel) == "windows"
|
|
38 |
windows_hostname
|
36 | 39 |
else
|
37 | 40 |
basic_hostname
|
38 | 41 |
end
|
|
41 | 44 |
and name =~ /.*?\.(.+$)/
|
42 | 45 |
|
43 | 46 |
return_value = $1
|
44 | |
elsif domain = Facter::Util::Resolution.exec('dnsdomainname 2> /dev/null') \
|
|
47 |
elsif Facter.value(:kernel) != "windows" and domain = Facter::Util::Resolution.exec('dnsdomainname 2> /dev/null') \
|
45 | 48 |
and domain =~ /.+/
|
46 | 49 |
|
47 | 50 |
return_value = domain
|
|
79 | 82 |
break
|
80 | 83 |
}
|
81 | 84 |
end
|
|
85 |
|
|
86 |
domain ||= ''
|
|
87 |
|
82 | 88 |
domain.gsub(/\.$/, '')
|
83 | 89 |
end
|
84 | 90 |
end
|
171 | 171 |
|
172 | 172 |
Facter.fact(:domain).value.should == 'foo.com'
|
173 | 173 |
end
|
|
174 |
|
|
175 |
context "without any network adapters with a specified DNSDomain" do
|
|
176 |
let(:hostname_command) { 'hostname > NUL' }
|
|
177 |
|
|
178 |
it "should return nil" do
|
|
179 |
nic = stubs 'nic'
|
|
180 |
nic.stubs(:DNSDomain).returns(nil)
|
|
181 |
Facter::Util::Resolution.stubs(:exec).with(hostname_command).returns('sometest')
|
|
182 |
FileTest.stubs(:exists?).with("/etc/resolv.conf").returns(false)
|
|
183 |
|
|
184 |
require 'facter/util/wmi'
|
|
185 |
Facter::Util::WMI.stubs(:execquery).with("select DNSDomain from Win32_NetworkAdapterConfiguration where IPEnabled = True").returns([nic])
|
|
186 |
|
|
187 |
Facter.fact(:domain).value.should be_nil
|
|
188 |
end
|
|
189 |
end
|
174 | 190 |
end
|
175 | 191 |
end
|
176 | 192 |
|