1 require 'puppet/util/filetype'
2 Puppet::Type.type(:a2mod).provide(:gentoo, :parent => Puppet::Provider) do
3 desc "Manage Apache 2 modules on Gentoo"
5 confine :operatingsystem => :gentoo
6 defaultfor :operatingsystem => :gentoo
8 attr_accessor :property_hash
11 @property_hash[:ensure] = :present
15 (!(@property_hash[:ensure].nil?) and @property_hash[:ensure] == :present)
19 @property_hash[:ensure] = :absent
27 attr_reader :conf_file
37 @conf_file = "/etc/conf.d/apache2"
38 @filetype = Puppet::Util::FileType.filetype(:flat).new(conf_file)
46 # Retrieve an array of all existing modules
48 if @modules.length <= 0
49 # Locate the APACHE_OPTS variable
50 records = filetype.read.split(/\n/)
51 apache2_opts = records.grep(/^\s*APACHE2_OPTS=/).first
54 while apache2_opts.sub!(/-D\s+(\w+)/, '')
55 @modules << $1.downcase
58 # Hang on to any remaining options.
59 if apache2_opts.match(/APACHE2_OPTS="(.+)"/)
60 @other_args = $1.strip
69 def self.prefetch(resources={})
70 # Match resources with existing providers
71 instances.each do |provider|
72 if resource = resources[provider.name]
73 resource.provider = provider
77 # Store all resources using this provider for flushing
78 resources.each do |name, resource|
79 @mod_resources << resource
84 modules.map {|mod| new(:name => mod, :provider => :gentoo, :ensure => :present)}
90 mods_to_remove = @mod_resources.select {|mod| mod.should(:ensure) == :absent}.map {|mod| mod[:name]}
91 mods_to_add = @mod_resources.select {|mod| mod.should(:ensure) == :present}.map {|mod| mod[:name]}
93 mod_list -= mods_to_remove
94 mod_list += mods_to_add
97 if modules != mod_list
98 opts = @other_args + " "
99 opts << mod_list.map {|mod| "-D #{mod.upcase}"}.join(" ")
101 opts.gsub!(/\s+/, ' ')
103 apache2_opts = %Q{APACHE2_OPTS="#{opts}"}
104 Puppet.debug("Writing back \"#{apache2_opts}\" to #{conf_file}")
106 records = filetype.read.split(/\n/)
108 opts_index = records.find_index {|i| i.match(/^\s*APACHE2_OPTS/)}
109 records[opts_index] = apache2_opts
112 filetype.write(records.join("\n"))