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

« back to all changes in this revision

Viewing changes to lib/puppet/rails/resource.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:
2
2
require 'puppet/rails/param_name'
3
3
require 'puppet/rails/param_value'
4
4
require 'puppet/rails/puppet_tag'
 
5
require 'puppet/rails/benchmark'
5
6
require 'puppet/util/rails/collection_merger'
6
7
 
7
8
class Puppet::Rails::Resource < ActiveRecord::Base
8
9
    include Puppet::Util::CollectionMerger
9
10
    include Puppet::Util::ReferenceSerializer
 
11
    include Puppet::Rails::Benchmark
10
12
 
11
13
    has_many :param_values, :dependent => :destroy, :class_name => "Puppet::Rails::ParamValue"
12
14
    has_many :param_names, :through => :param_values, :class_name => "Puppet::Rails::ParamName"
17
19
    belongs_to :source_file
18
20
    belongs_to :host
19
21
 
 
22
    @tags = {}
 
23
    def self.tags
 
24
        @tags
 
25
    end
 
26
 
 
27
    # Determine the basic details on the resource.
 
28
    def self.rails_resource_initial_args(resource)
 
29
        result = [:type, :title, :line].inject({}) do |hash, param|
 
30
            # 'type' isn't a valid column name, so we have to use another name.
 
31
            to = (param == :type) ? :restype : param
 
32
            if value = resource.send(param)
 
33
                hash[to] = value
 
34
            end
 
35
            hash
 
36
        end
 
37
 
 
38
        # We always want a value here, regardless of what the resource has,
 
39
        # so we break it out separately.
 
40
        result[:exported] = resource.exported || false
 
41
 
 
42
        result
 
43
    end
 
44
 
20
45
    def add_resource_tag(tag)
21
 
        pt = Puppet::Rails::PuppetTag.find_or_create_by_name(tag, :include => :puppet_tag)
22
 
        resource_tags.create(:puppet_tag => pt)
 
46
        pt = Puppet::Rails::PuppetTag.accumulate_by_name(tag)
 
47
        resource_tags.build(:puppet_tag => pt)
23
48
    end
24
49
 
25
50
    def file
56
81
        @tags_hash = hash
57
82
    end
58
83
 
59
 
    # returns a hash of param_names.name => [param_values]
60
 
    def get_params_hash(values = nil)
61
 
        values ||= @params_hash || Puppet::Rails::ParamValue.find_all_params_from_resource(self)
62
 
        if values.size == 0
63
 
            return {}
64
 
        end
65
 
        values.inject({}) do |hash, value|
66
 
            hash[value['name']] ||= []
67
 
            hash[value['name']] << value
68
 
            hash
69
 
        end
70
 
    end
71
 
 
72
 
    def get_tag_hash(tags = nil)
73
 
        tags ||= @tags_hash || Puppet::Rails::ResourceTag.find_all_tags_from_resource(self)
74
 
        return tags.inject({}) do |hash, tag|
75
 
            # We have to store the tag object, not just the tag name.
76
 
            hash[tag['name']] = tag
77
 
            hash
78
 
        end
79
 
    end
80
 
 
81
84
    def [](param)
82
85
        return super || parameter(param)
83
86
    end
84
87
 
 
88
    # Make sure this resource is equivalent to the provided Parser resource.
 
89
    def merge_parser_resource(resource)
 
90
        accumulate_benchmark("Individual resource merger", :attributes) { merge_attributes(resource) }
 
91
        accumulate_benchmark("Individual resource merger", :parameters) { merge_parameters(resource) }
 
92
        accumulate_benchmark("Individual resource merger", :tags) { merge_tags(resource) }
 
93
        save()
 
94
    end
 
95
 
 
96
    def merge_attributes(resource)
 
97
        args = self.class.rails_resource_initial_args(resource)
 
98
        args.each do |param, value|
 
99
            unless resource[param] == value
 
100
                self[param] = value
 
101
            end
 
102
        end
 
103
 
 
104
        # Handle file specially
 
105
        if (resource.file and  (!resource.file or self.file != resource.file))
 
106
            self.file = resource.file
 
107
        end
 
108
    end
 
109
 
 
110
    def merge_parameters(resource)
 
111
        catalog_params = {}
 
112
        resource.each do |param, value|
 
113
            catalog_params[param.to_s] = value
 
114
        end
 
115
 
 
116
        db_params = {}
 
117
 
 
118
        deletions = []
 
119
        @params_hash.each do |value|
 
120
            # First remove any parameters our catalog resource doesn't have at all.
 
121
            deletions << value['id'] and next unless catalog_params.include?(value['name'])
 
122
 
 
123
            # Now store them for later testing.
 
124
            db_params[value['name']] ||= []
 
125
            db_params[value['name']] << value
 
126
        end
 
127
 
 
128
        # Now get rid of any parameters whose value list is different.
 
129
        # This might be extra work in cases where an array has added or lost
 
130
        # a single value, but in the most common case (a single value has changed)
 
131
        # this makes sense.
 
132
        db_params.each do |name, value_hashes|
 
133
            values = value_hashes.collect { |v| v['value'] }
 
134
 
 
135
            unless value_compare(catalog_params[name], values)
 
136
                value_hashes.each { |v| deletions << v['id'] }
 
137
            end
 
138
        end
 
139
 
 
140
        # Perform our deletions.
 
141
        Puppet::Rails::ParamValue.delete(deletions) unless deletions.empty?
 
142
 
 
143
        # Lastly, add any new parameters.
 
144
        catalog_params.each do |name, value|
 
145
            next if db_params.include?(name)
 
146
            values = value.is_a?(Array) ? value : [value]
 
147
 
 
148
            values.each do |v|
 
149
                param_values.build(:value => serialize_value(v), :line => resource.line, :param_name => Puppet::Rails::ParamName.accumulate_by_name(name))
 
150
            end
 
151
        end
 
152
    end
 
153
 
 
154
    # Make sure the tag list is correct.
 
155
    def merge_tags(resource)
 
156
        in_db = []
 
157
        deletions = []
 
158
        resource_tags = resource.tags
 
159
        @tags_hash.each do |tag|
 
160
            deletions << tag['id'] and next unless resource_tags.include?(tag['name'])
 
161
            in_db << tag['name']
 
162
        end
 
163
        Puppet::Rails::ResourceTag.delete(deletions) unless deletions.empty?
 
164
 
 
165
        (resource_tags - in_db).each do |tag|
 
166
            add_resource_tag(tag)
 
167
        end
 
168
    end
 
169
 
 
170
    def value_compare(v,db_value)
 
171
        v = [v] unless v.is_a?(Array)
 
172
 
 
173
        v == db_value
 
174
    end
 
175
 
85
176
    def name
86
177
        ref()
87
178
    end
88
179
 
89
180
    def parameter(param)
90
181
        if pn = param_names.find_by_name(param)
91
 
            if pv = param_values.find(:first, :conditions => [ 'param_name_id = ?', pn]                                                            )
 
182
            if pv = param_values.find(:first, :conditions => [ 'param_name_id = ?', pn])
92
183
                return pv.value
93
184
            else
94
185
                return nil
112
203
        "%s[%s]" % [self[:restype].split("::").collect { |s| s.capitalize }.join("::"), self.title.to_s]
113
204
    end
114
205
 
 
206
    # Returns a hash of parameter names and values, no ActiveRecord instances.
 
207
    def to_hash
 
208
        Puppet::Rails::ParamValue.find_all_params_from_resource(self).inject({}) do |hash, value|
 
209
            hash[value['name']] ||= []
 
210
            hash[value['name']] << value.value
 
211
            hash
 
212
        end
 
213
    end
 
214
 
115
215
    # Convert our object to a resource.  Do not retain whether the object
116
216
    # is exported, though, since that would cause it to get stripped
117
217
    # from the configuration.