~ubuntu-branches/ubuntu/lucid/puppet/lucid

1.1.1 by Matthew Palmer
Import upstream version 0.20.1
1
require 'puppet/provider/parsedfile'
2
3
#services = nil
4
#case Facter.value(:operatingsystem)
1.3.2 by Nigel Kersten
Import upstream version 0.25.1
5
#when "Solaris"; services = "/etc/inet/services"
1.1.1 by Matthew Palmer
Import upstream version 0.20.1
6
#else
7
#    services = "/etc/services"
8
#end
9
#
10
#Puppet::Type.type(:port).provide(:parsed,
11
#    :parent => Puppet::Provider::ParsedFile,
12
#    :default_target => services,
13
#    :filetype => :flat
14
#) do
15
#    text_line :comment, :match => /^\s*#/
16
#    text_line :blank, :match => /^\s*$/
17
#
18
#    # We're cheating horribly here -- we don't support ddp, because it assigns
19
#    # the same number to already-used names, and the same name to different
20
#    # numbers.
21
#    text_line :ddp, :match => /^\S+\s+\d+\/ddp/
22
#
23
#    # Also, just ignore the lines on OS X that don't have service names.
24
#    text_line :funky_darwin, :match => /^\s+\d+\//
25
#
26
#    # We have to manually parse the line, since it's so darn complicated.
27
#    record_line :parsed, :fields => %w{name port protocols alias description},
28
#        :optional => %w{alias description} do |line|
29
#        if line =~ /\/ddp/
30
#            raise "missed ddp in %s" % line
31
#        end
32
#        # The record might contain multiple port lines separated by \n.
33
#        hashes = line.split("\n").collect { |l| parse_port(l) }
34
#
35
#        # It's easy if there's just one hash.
36
#        if hashes.length == 1
37
#            return hashes.shift
38
#        end
39
#
40
#        # Else, merge the two records into one.
41
#        return port_merge(*hashes)
42
#    end
43
#
44
#    # Override how we split into lines, so that we always treat both protocol
45
#    # lines as a single line.  This drastically simplifies merging the two lines
46
#    # into one record.
47
#    def self.lines(text)
48
#        names = {}
49
#        lines = []
50
#
51
#        # We organize by number, because that's apparently how the ports work.
52
#        # You'll never be able to use Puppet to manage multiple entries
53
#        # with the same name but different numbers, though.
54
#        text.split("\n").each do |line|
55
#            if line =~ /^([-\w]+)\s+(\d+)\/[^d]/ # We want to skip ddp proto stuff
56
#                names[$1] ||= []
57
#                names[$1] << line
58
#                lines << [:special, $1]
59
#            else
60
#                lines << line
61
#            end
62
#        end
63
#
64
#        # Now, return each line in order, but join the ones with the same name
65
#        lines.collect do |line|
66
#            if line.is_a?(Array)
67
#                name = line[1]
68
#                if names[name]
69
#                    t = names[name].join("\n")
70
#                    names.delete(name)
71
#                    t
72
#                end
73
#            else
74
#                line
75
#            end
76
#        end.reject { |l| l.nil? }
77
#    end
78
#
79
#    # Parse a single port line, returning a hash.
80
#    def self.parse_port(line)
81
#        hash = {}
82
#        if line.sub!(/^(\S+)\s+(\d+)\/(\w+)\s*/, '')
83
#            hash[:name] = $1
84
#            hash[:number] = $2
85
#            hash[:protocols] = [$3]
86
#
87
#            unless line == ""
88
#                line.sub!(/^([^#]+)\s*/) do |value|
89
#                    aliases = $1
90
#
91
#                    # Remove any trailing whitespace
92
#                    aliases.strip!
93
#                    unless aliases =~ /^\s*$/
94
#                        hash[:alias] = aliases.split(/\s+/)
95
#                    end
96
#
97
#                    ""
98
#                end
99
#
100
#                line.sub!(/^\s*#\s*(.+)$/) do |value|
101
#                    desc = $1
102
#                    unless desc =~ /^\s*$/
103
#                        hash[:description] = desc.sub(/\s*$/, '')
104
#                    end
105
#
106
#                    ""
107
#                end
108
#            end
109
#        else
110
#            if line =~ /^\s+\d+/ and
111
#                Facter["operatingsystem"].value == "Darwin"
112
#                    #Puppet.notice "Skipping wonky OS X port entry %s" %
113
#                    #    line.inspect
114
#                    next
115
#            end
116
#            Puppet.notice "Ignoring unparseable line '%s' in %s" % [line, self.target]
117
#        end
118
#
119
#        if hash.empty?
120
#            return nil
121
#        else
122
#            return hash
123
#        end
124
#    end
125
#
126
#    # Merge two records into one.
127
#    def self.port_merge(one, two)
128
#        keys = [one.keys, two.keys].flatten.uniq
129
#
130
#        # We'll be returning the 'one' hash. so make any necessary modifications
131
#        # to it.
132
#        keys.each do |key|
133
#            # The easy case
134
#            if one[key] == two[key]
135
#                next
136
#            elsif one[key] and ! two[key]
137
#                next
138
#            elsif ! one[key] and two[key]
139
#                one[key] = two[key]
140
#            elsif one[key].is_a?(Array) and two[key].is_a?(Array)
141
#                one[key] = [one[key], two[key]].flatten.uniq
142
#            else
143
#                # Keep the info from the first hash, so don't do anything
144
#                #Puppet.notice "Cannot merge %s in %s with %s" %
145
#                #    [key, one.inspect, two.inspect]
146
#            end
147
#        end
148
#
149
#        return one
150
#    end
151
#
152
#    # Convert the current object into one or more services entry.
153
#    def self.to_line(hash)
154
#        unless hash[:record_type] == :parsed
155
#            return super
156
#        end
157
#
158
#        # Strangely, most sites seem to use tabs as separators.
159
#        hash[:protocols].collect { |proto|
160
#            str = "%s\t\t%s/%s" % [hash[:name], hash[:number], proto]
161
#
162
#            if value = hash[:alias] and value != :absent
163
#                str += "\t\t%s" % value.join(" ")
164
#            end
165
#
166
#            if value = hash[:description] and value != :absent
167
#                str += "\t# %s" % value
168
#            end
169
#            str
170
#        }.join("\n")
171
#    end
172
#end
173