]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/apache/lib/puppet/provider/a2mod/gentoo.rb
add Openstack modules to 3rdparty
[dsa-puppet.git] / 3rdparty / modules / apache / lib / puppet / provider / a2mod / gentoo.rb
1 require 'puppet/util/filetype'
2 Puppet::Type.type(:a2mod).provide(:gentoo, :parent => Puppet::Provider) do
3   desc "Manage Apache 2 modules on Gentoo"
4
5   confine :operatingsystem => :gentoo
6   defaultfor :operatingsystem => :gentoo
7
8   attr_accessor :property_hash
9
10   def create
11     @property_hash[:ensure] = :present
12   end
13
14   def exists?
15     (!(@property_hash[:ensure].nil?) and @property_hash[:ensure] == :present)
16   end
17
18   def destroy
19     @property_hash[:ensure] = :absent
20   end
21
22   def flush
23     self.class.flush
24   end
25
26   class << self
27     attr_reader :conf_file
28   end
29
30   def self.clear
31     @mod_resources = []
32     @modules       = []
33     @other_args    = ""
34   end
35
36   def self.initvars
37     @conf_file     = "/etc/conf.d/apache2"
38     @filetype      = Puppet::Util::FileType.filetype(:flat).new(conf_file)
39     @mod_resources = []
40     @modules       = []
41     @other_args    = ""
42   end
43
44   self.initvars
45
46   # Retrieve an array of all existing modules
47   def self.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
52
53       # Extract all defines
54       while apache2_opts.sub!(/-D\s+(\w+)/, '')
55         @modules << $1.downcase
56       end
57
58       # Hang on to any remaining options.
59       if apache2_opts.match(/APACHE2_OPTS="(.+)"/)
60         @other_args = $1.strip
61       end
62
63       @modules.sort!.uniq!
64     end
65
66     @modules
67   end
68
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
74       end
75     end
76
77     # Store all resources using this provider for flushing
78     resources.each do |name, resource|
79       @mod_resources << resource
80     end
81   end
82
83   def self.instances
84     modules.map {|mod| new(:name => mod, :provider => :gentoo, :ensure => :present)}
85   end
86
87   def self.flush
88
89     mod_list       = modules
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]}
92
93     mod_list -= mods_to_remove
94     mod_list += mods_to_add
95     mod_list.sort!.uniq!
96
97     if modules != mod_list
98       opts = @other_args + " "
99       opts << mod_list.map {|mod| "-D #{mod.upcase}"}.join(" ")
100       opts.strip!
101       opts.gsub!(/\s+/, ' ')
102
103       apache2_opts = %Q{APACHE2_OPTS="#{opts}"}
104       Puppet.debug("Writing back \"#{apache2_opts}\" to #{conf_file}")
105
106       records = filetype.read.split(/\n/)
107
108       opts_index = records.find_index {|i| i.match(/^\s*APACHE2_OPTS/)}
109       records[opts_index] = apache2_opts
110
111       filetype.backup
112       filetype.write(records.join("\n"))
113       @modules = mod_list
114     end
115   end
116 end