summaryrefslogtreecommitdiffstats
path: root/lib/facter/hostname.rb
blob: 0dcd01c69b953f14de3d7533b2a11f94603063c5 (plain)
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
# Fact: hostname
#
# Purpose: Return the system's short hostname.
#
# Resolution:
#   On all system bar Darwin, parses the output of the "hostname" system command
#   to everything before the first period.
#   On Darwin, uses the system configuration util to get the LocalHostName
#   variable.
#
# Caveats:
#

Facter.add(:hostname, :ldapname => "cn") do
    setcode do
        hostname = nil
        name = Facter::Util::Resolution.exec('hostname') or nil
        if name
            if name =~ /^([\w-]+)\.(.+)$/
                hostname = $1
                # the Domain class uses this
                $domain = $2
            else
                hostname = name
            end
            hostname
        else
            nil
        end
    end
end

Facter.add(:hostname) do
    confine :kernel => :darwin, :kernelrelease => "R7"
    setcode do
        %x{/usr/sbin/scutil --get LocalHostName}
    end
end