~ubuntu-branches/ubuntu/raring/facter/raring

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env ruby
#
# = Synopsis
#
# Collect and display facts about the system.
#
# = Usage
#
#   facter [-d|--debug] [-h|--help] [-p|--puppet] [-v|--version] [-y|--yaml] [fact] [fact] [...]
#
# = Description
#
# Collect and display facts about the current system.  The library behind
# Facter is easy to expand, making Facter an easy way to collect information
# about a system from within the shell or within Ruby.
#
# If no facts are specifically asked for, then all facts will be returned.
#
# = Options
#
# debug::
#   Enable debugging.
#
# help::
#   Print this help message
#
# puppet::
#   Load the Puppet libraries, thus allowing Facter to load Puppet-specific facts.
#
# version::
#   Print the version and exit.
#
# yaml::
#   Emit facts in YAML format.
#
# = Example
#
#   facter kernel
#
# = Author
#
# Luke Kanies
#
# = Copyright
#
# Copyright (c) 2006 Reductive Labs, LLC
# Licensed under the GNU Public License

require 'getoptlong'
require 'facter'

$haveusage = true

begin
    require 'rdoc/ri/ri_paths'
    require 'rdoc/usage'
rescue Exception
    $haveusage = false
end

def load_puppet
    require 'puppet'
    Puppet.parse_config

    # If you've set 'vardir' but not 'libdir' in your
    # puppet.conf, then the hook to add libdir to $:
    # won't get triggered.  This makes sure that it's setup
    # correctly.
    unless $LOAD_PATH.include?(Puppet[:libdir])
        $LOAD_PATH << Puppet[:libdir]
    end
end

$debug = 0

config = nil

result = GetoptLong.new(
    [ "--version", "-v", GetoptLong::NO_ARGUMENT       ],
    [ "--help",    "-h", GetoptLong::NO_ARGUMENT       ],
    [ "--debug",   "-d", GetoptLong::NO_ARGUMENT       ],
    [ "--yaml",    "-y", GetoptLong::NO_ARGUMENT       ],
    [ "--config",  "-c", GetoptLong::REQUIRED_ARGUMENT ],
    [ "--puppet",  "-p", GetoptLong::NO_ARGUMENT       ]
)

options = {
    :yaml => false
}

begin
    result.each { |opt,arg|
        case opt
        when "--version"
            puts "%s" % Facter.version
            exit
        when "--puppet"
            begin
                load_puppet()
            rescue LoadError => detail
                $stderr.puts "Could not load Puppet: %s" % detail
            end
        when "--yaml"
            options[:yaml] = true
        when "--debug"
            Facter.debugging(1)
        when "--help"
            if $haveusage
                RDoc::usage && exit
            else
                puts "No help available unless you have RDoc::usage installed"
                exit
            end
        else
            $stderr.puts "Invalid option '#{opt}'"
            exit(12)
        end
    }
rescue
    exit(12)
end

names = []

unless config.nil?
    File.open(config) { |file|
        names = file.readlines.collect { |line|
            line.chomp
        }
    }
end

ARGV.each { |item|
    names.push item
}

if names.empty?
    facts = Facter.to_hash
else
    facts = {}
    names.each { |name|
        begin
            facts[name] = Facter.value(name)
        rescue => error
            STDERR.puts "Could not retrieve %s: #{error}" % name
            exit 10
        end
    }
end

if options[:yaml]
    require 'yaml'
    puts YAML.dump(facts)
    exit(0)
end

facts.sort { |a, b| a[0].to_s <=> b[0].to_s }.each { |name,value|
    if facts.length == 1
        unless value.nil?
            puts value
        end
    else
        puts "%s => %s" % [name,value]
    end
}