lib/puppet/provider/parsedfile.rb in puppet-0.24.7 vs lib/puppet/provider/parsedfile.rb in puppet-0.24.8
- old
+ new
@@ -76,12 +76,28 @@
end
@modified.reject! { |t| flushed.include?(t) }
end
+ # Make sure our file is backed up, but only back it up once per transaction.
+ # We cheat and rely on the fact that @records is created on each prefetch.
+ def self.backup_target(target)
+ return nil unless target_object(target).respond_to?(:backup)
+
+ unless defined?(@backup_stats)
+ @backup_stats = {}
+ end
+ return nil if @backup_stats[target] == @records.object_id
+
+ target_object(target).backup
+ @backup_stats[target] = @records.object_id
+ end
+
# Flush all of the records relating to a specific target.
def self.flush_target(target)
+ backup_target(target)
+
records = target_records(target).reject { |r|
r[:ensure] == :absent
}
target_object(target).write(to_file(records))
end
@@ -106,14 +122,15 @@
super
end
# Return a list of all of the records we can find.
def self.instances
- prefetch()
- @records.find_all { |r| r[:record_type] == self.name }.collect { |r|
- new(r)
- }
+ targets.collect do |target|
+ prefetch_target(target)
+ end.flatten.reject { |r| skip_record?(r) }.collect do |record|
+ new(record)
+ end
end
# Override the default method with a lot more functionality.
def self.mk_resource_methods
[resource_type.validproperties, resource_type.parameters].flatten.each do |attr|
@@ -169,35 +186,43 @@
# set. We need to turn those three locations into a list of files,
# prefetch each one, and make sure they're associated with each appropriate
# resource instance.
def self.prefetch(resources = nil)
# Reset the record list.
- @records = []
- targets(resources).each do |target|
- @records += prefetch_target(target)
- end
+ @records = prefetch_all_targets(resources)
- if resources
- matchers = resources.dup
- @records.each do |record|
- # Skip things like comments and blank lines
- next if skip_record?(record)
+ match_providers_with_resources(resources)
+ end
- if name = record[:name] and resource = resources[name]
+ def self.match_providers_with_resources(resources)
+ return unless resources
+ matchers = resources.dup
+ @records.each do |record|
+ # Skip things like comments and blank lines
+ next if skip_record?(record)
+
+ if name = record[:name] and resource = resources[name]
+ resource.provider = new(record)
+ elsif respond_to?(:match)
+ if resource = match(record, matchers)
+ # Remove this resource from circulation so we don't unnecessarily try to match
+ matchers.delete(resource.title)
+ record[:name] = resource[:name]
resource.provider = new(record)
- elsif respond_to?(:match)
- if resource = match(record, matchers)
- # Remove this resource from circulation so we don't unnecessarily try to match
- matchers.delete(resource.title)
- record[:name] = resource[:name]
- resource.provider = new(record)
- end
end
end
end
end
+ def self.prefetch_all_targets(resources)
+ records = []
+ targets(resources).each do |target|
+ records += prefetch_target(target)
+ end
+ records
+ end
+
# Prefetch an individual target.
def self.prefetch_target(target)
target_records = retrieve(target).each do |r|
r[:on_disk] = true
r[:target] = target
@@ -215,10 +240,11 @@
target_records
end
# Is there an existing record with this name?
def self.record?(name)
+ return nil unless @records
@records.find { |r| r[:name] == name }
end
# Retrieve the text for the file. Returns nil in the unlikely
# event that it doesn't exist.
@@ -365,6 +391,5 @@
if @property_hash[:target] != :absent and @property_hash[:target]
self.class.modified(@property_hash[:target])
end
end
end
-