~gandelman-a/ubuntu/precise/facter/merge922788

« back to all changes in this revision

Viewing changes to lib/facter/domain.rb

  • Committer: Package Import Robot
  • Author(s): Adam Gandelman
  • Date: 2011-10-18 10:32:42 UTC
  • mfrom: (1.3.3 upstream)
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: package-import@ubuntu.com-20111018103242-ag8i8vejfp8v7b1b
Tags: upstream-1.6.1
ImportĀ upstreamĀ versionĀ 1.6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Fact: domain
 
2
#
 
3
# Purpose:
 
4
#   Return the host's primary DNS domain name.
 
5
 
6
# Resolution:
 
7
#   On UNIX (excluding Darwin), first try and use the hostname fact,
 
8
#   which uses the hostname system command, and then parse the output
 
9
#   of that.
 
10
#   Failing that it tries the dnsdomainname system command.
 
11
#   Failing that it uses /etc/resolv.conf and takes the domain from that, or as
 
12
#   a final resort, the search from that.
 
13
#   Otherwise returns nil.
 
14
#
 
15
#   On Windows uses the win32ole gem and winmgmts to get the DNSDomain value
 
16
#   from the Win32 networking stack.
 
17
#
 
18
# Caveats:
 
19
#
 
20
 
1
21
Facter.add(:domain) do
2
22
    setcode do
3
23
        # Get the domain from various sources; the order of these
4
24
        # steps is important
5
25
 
6
 
        Facter.value(:hostname)
7
 
        next $domain if defined? $domain and ! $domain.nil?
8
 
 
9
 
        domain = Facter::Util::Resolution.exec('dnsdomainname')
10
 
        next domain if domain =~ /.+\..+/
11
 
 
12
 
        if FileTest.exists?("/etc/resolv.conf")
 
26
        if name = Facter::Util::Resolution.exec('hostname') and 
 
27
            name =~ /.*?\.(.+$)/
 
28
 
 
29
            $1
 
30
        elsif domain = Facter::Util::Resolution.exec('dnsdomainname') and 
 
31
            domain =~ /.+\..+/
 
32
 
 
33
            domain
 
34
        elsif FileTest.exists?("/etc/resolv.conf")
13
35
            domain = nil
14
36
            search = nil
15
37
            File.open("/etc/resolv.conf") { |file|
24
46
            next domain if domain
25
47
            next search if search
26
48
        end
27
 
        nil
28
49
    end
29
50
end
30
51
 
31
52
Facter.add(:domain) do
32
53
    confine :kernel => :windows
33
54
    setcode do
34
 
        require 'win32ole'
 
55
        require 'facter/util/wmi'
35
56
        domain = ""
36
 
        wmi = WIN32OLE.connect("winmgmts://")
37
 
        query = "select DNSDomain from Win32_NetworkAdapterConfiguration where IPEnabled = True"
38
 
        wmi.ExecQuery(query).each { |nic|
 
57
        Facter::Util::WMI.execquery("select DNSDomain from Win32_NetworkAdapterConfiguration where IPEnabled = True").each { |nic|
39
58
            domain = nic.DNSDomain
40
59
            break
41
60
        }