~nvalcarcel/ubuntu/lucid/puppet/fix-546677

« back to all changes in this revision

Viewing changes to lib/puppet/provider/nameservice/netinfo.rb

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2009-12-23 00:48:10 UTC
  • mfrom: (1.1.10 upstream) (3.1.7 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091223004810-3i4oryds922g5n59
Tags: 0.25.1-3ubuntu1
* Merge from debian testing.  Remaining changes:
  - debian/rules:
    + Don't start puppet when first installing puppet.
  - debian/puppet.conf, lib/puppet/defaults.rb:
    + Move templates to /etc/puppet
  - lib/puppet/defaults.rb:
    + Fix /var/lib/puppet/state ownership.
  - man/man8/puppet.conf.8: 
    + Fix broken URL in manpage.
  - debian/control:
    + Update maintainer accordint to spec.
    + Puppetmaster Recommends -> Suggests
    + Created puppet-testsuite as a seperate. Allow the users to run puppet's 
      testsuite.
  - tests/Rakefile: Fix rakefile so that the testsuite can acutally be ran.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Manage NetInfo POSIX objects.
2
 
#
3
 
# This provider has been deprecated. You should be using the directoryservice
4
 
# nameservice provider instead.
5
 
 
6
 
require 'puppet'
7
 
require 'puppet/provider/nameservice'
8
 
 
9
 
class Puppet::Provider::NameService
10
 
class NetInfo < Puppet::Provider::NameService
11
 
    class << self
12
 
        attr_writer :netinfodir
13
 
    end
14
 
 
15
 
    # We have to initialize manually because we're not using
16
 
    # classgen() here.
17
 
    initvars()
18
 
 
19
 
    commands :lookupd => "/usr/sbin/lookupd"
20
 
 
21
 
    # Attempt to flush the database, but this doesn't seem to work at all.
22
 
    def self.flush
23
 
        begin
24
 
            lookupd "-flushcache"
25
 
        rescue Puppet::ExecutionFailure
26
 
            # Don't throw an error; it's just a failed cache flush
27
 
            Puppet.err "Could not flush lookupd cache: %s" % output
28
 
        end
29
 
    end
30
 
 
31
 
    # Similar to posixmethod, what key do we use to get data?  Defaults
32
 
    # to being the object name.
33
 
    def self.netinfodir
34
 
        if defined? @netinfodir
35
 
            return @netinfodir
36
 
        else
37
 
            return @resource_type.name.to_s + "s"
38
 
        end
39
 
    end
40
 
 
41
 
    def self.finish
42
 
        case self.name
43
 
        when :uid:
44
 
            noautogen
45
 
        when :gid:
46
 
            noautogen
47
 
        end
48
 
    end
49
 
    
50
 
    def self.instances
51
 
        warnonce "The NetInfo provider is deprecated; use directoryservice instead"
52
 
        report(@resource_type.validproperties).collect do |hash|
53
 
            self.new(hash)
54
 
        end
55
 
    end
56
 
    
57
 
    # Convert a NetInfo line into a hash of data.
58
 
    def self.line2hash(line, params)
59
 
        values = line.split(/\t/)
60
 
 
61
 
        hash = {}
62
 
        params.zip(values).each do |param, value|
63
 
            next if value == '#NoValue#'
64
 
            hash[param] = if value =~ /^[-0-9]+$/
65
 
                Integer(value)
66
 
            else
67
 
                value
68
 
            end
69
 
        end
70
 
        hash
71
 
    end
72
 
    
73
 
    # What field the value is stored under.
74
 
    def self.netinfokey(name)
75
 
        name = symbolize(name)
76
 
        self.option(name, :key) || name
77
 
    end
78
 
    
79
 
    # Retrieve the data, yo.
80
 
    # FIXME This should retrieve as much information as possible,
81
 
    # rather than retrieving it one at a time.
82
 
    def self.report(*params)
83
 
        dir = self.netinfodir()
84
 
        cmd = [command(:nireport), "/", "/%s" % dir]
85
 
        
86
 
        params.flatten!
87
 
 
88
 
        # We require the name in order to know if we match.  There's no
89
 
        # way to just report on our individual object, we have to get the
90
 
        # whole list.
91
 
        params.unshift :name unless params.include? :name
92
 
 
93
 
        params.each do |param|
94
 
            if key = netinfokey(param)
95
 
                cmd << key.to_s
96
 
            else
97
 
                raise Puppet::DevError,
98
 
                    "Could not find netinfokey for property %s" %
99
 
                    self.class.name
100
 
            end
101
 
        end
102
 
 
103
 
        begin
104
 
            output = execute(cmd)
105
 
        rescue Puppet::ExecutionFailure => detail
106
 
            Puppet.err "Failed to call nireport: %s" % detail
107
 
            return nil
108
 
        end
109
 
 
110
 
        return output.split("\n").collect { |line|
111
 
            line2hash(line, params)
112
 
        }
113
 
    end
114
 
    
115
 
    # How to add an object.
116
 
    def addcmd
117
 
        creatorcmd("-create")
118
 
    end
119
 
 
120
 
    def creatorcmd(arg)
121
 
        cmd = [command(:niutil)]
122
 
        cmd << arg
123
 
 
124
 
        cmd << "/" << "/%s/%s" % [self.class.netinfodir(), @resource[:name]]
125
 
        return cmd
126
 
    end
127
 
 
128
 
    def deletecmd
129
 
        creatorcmd("-destroy")
130
 
    end
131
 
    
132
 
    def destroy
133
 
        delete()
134
 
    end
135
 
 
136
 
    def ensure=(arg)
137
 
        warnonce "The NetInfo provider is deprecated; use directoryservice instead"
138
 
        super
139
 
 
140
 
        # Because our stupid type can't create the whole thing at once,
141
 
        # we have to do this hackishness.  Yay.
142
 
        if arg == :present
143
 
            @resource.class.validproperties.each do |name|
144
 
                next if name == :ensure
145
 
 
146
 
                # LAK: We use property.sync here rather than directly calling
147
 
                # the settor method because the properties might do some kind
148
 
                # of conversion.  In particular, the user gid property might
149
 
                # have a string and need to convert it to a number
150
 
                if @resource.should(name)
151
 
                    @resource.property(name).sync
152
 
                elsif value = autogen(name)
153
 
                    self.send(name.to_s + "=", value)
154
 
                else
155
 
                    next
156
 
                end
157
 
            end
158
 
        end
159
 
    end
160
 
 
161
 
    # Retrieve a specific value by name.
162
 
    def get(param)
163
 
        hash = getinfo(false)
164
 
        if hash
165
 
            return hash[param]
166
 
        else
167
 
            return :absent
168
 
        end
169
 
    end
170
 
 
171
 
    # Retrieve everything about this object at once, instead of separately.
172
 
    def getinfo(refresh = false)
173
 
        if refresh or (! defined? @infohash or ! @infohash)
174
 
            properties = [:name] + self.class.resource_type.validproperties
175
 
            properties.delete(:ensure) if properties.include? :ensure
176
 
            @infohash = single_report(*properties)
177
 
        end
178
 
 
179
 
        return @infohash
180
 
    end
181
 
 
182
 
    def modifycmd(param, value)
183
 
        cmd = [command(:niutil)]
184
 
        # if value.is_a?(Array)
185
 
        #     warning "Netinfo providers cannot currently handle multiple values"
186
 
        # end
187
 
 
188
 
        cmd << "-createprop" << "/" << "/%s/%s" % [self.class.netinfodir, @resource[:name]]
189
 
 
190
 
        value = [value] unless value.is_a?(Array)
191
 
        if key = netinfokey(param)
192
 
            cmd << key
193
 
            cmd += value
194
 
        else
195
 
            raise Puppet::DevError,
196
 
                "Could not find netinfokey for property %s" %
197
 
                self.class.name
198
 
        end
199
 
        cmd
200
 
    end
201
 
 
202
 
    # Determine the flag to pass to our command.
203
 
    def netinfokey(name)
204
 
        self.class.netinfokey(name)
205
 
    end
206
 
    
207
 
    # Get a report for a single resource, not the whole table
208
 
    def single_report(*properties)
209
 
        warnonce "The NetInfo provider is deprecated; use directoryservice instead"
210
 
        self.class.report(*properties).find do |hash| hash[:name] == self.name end
211
 
    end
212
 
 
213
 
    def setuserlist(group, list)
214
 
        cmd = [command(:niutil), "-createprop", "/", "/groups/%s" % group, "users", list.join(",")]
215
 
        begin
216
 
            output = execute(cmd)
217
 
        rescue Puppet::ExecutionFailure => detail
218
 
            raise Puppet::Error, "Failed to set user list on %s: %s" %
219
 
                [group, detail]
220
 
        end
221
 
    end
222
 
end
223
 
end
224