]> git.openstreetmap.org Git - chef.git/blob - cookbooks/apache/resources/module.rb
0ac0f8f01471cd161811c8a537155d6f774b7535
[chef.git] / cookbooks / apache / resources / module.rb
1 #
2 # Cookbook:: apache
3 # Resource:: apache_module
4 #
5 # Copyright:: 2013, OpenStreetMap Foundation
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # https://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19
20 default_action [:install, :enable]
21
22 property :module, :kind_of => String, :name_attribute => true
23 property :package, :kind_of => String
24 property :conf, :kind_of => String
25 property :variables, :kind_of => Hash, :default => {}
26 property :restart_apache, :kind_of => [TrueClass, FalseClass], :default => true
27
28 action :install do
29   declare_resource :package, package_name unless installed?
30
31   if new_resource.conf
32     template available_name("conf") do
33       source new_resource.conf
34       owner "root"
35       group "root"
36       mode 0o644
37       variables new_resource.variables
38     end
39   end
40 end
41
42 action :enable do
43   execute "a2enmod-#{new_resource.module}" do
44     command "a2enmod #{new_resource.module}"
45     user "root"
46     group "root"
47     not_if { ::File.exist?(enabled_name("load")) }
48   end
49
50   link enabled_name("load") do
51     to available_name("load")
52     owner "root"
53     group "root"
54   end
55
56   link enabled_name("conf") do
57     to available_name("conf")
58     owner "root"
59     group "root"
60     only_if { ::File.exist?(available_name("conf")) }
61   end
62 end
63
64 action :disable do
65   link enabled_name("load") do
66     action :delete
67   end
68
69   link enabled_name("conf") do
70     action :delete
71   end
72 end
73
74 action :delete do
75   package package_name do
76     action :remove
77   end
78 end
79
80 action_class do
81   def package_name
82     new_resource.package || "libapache2-mod-#{new_resource.module}"
83   end
84
85   def available_name(extension)
86     "/etc/apache2/mods-available/#{new_resource.module}.#{extension}"
87   end
88
89   def enabled_name(extension)
90     "/etc/apache2/mods-enabled/#{new_resource.module}.#{extension}"
91   end
92
93   def installed?
94     ::File.exist?(available_name("load"))
95   end
96 end
97
98 def after_created
99   notifies :restart, "service[apache2]" if restart_apache
100 end