]> git.openstreetmap.org Git - chef.git/blob - cookbooks/apache/resources/module.rb
Merge remote-tracking branch 'tigerfell/pr257'
[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 unified_mode true
21
22 default_action [:install, :enable]
23
24 property :module, :kind_of => String, :name_property => true
25 property :package, :kind_of => String
26 property :conf, :kind_of => String
27 property :variables, :kind_of => Hash, :default => {}
28 property :restart_apache, :kind_of => [TrueClass, FalseClass], :default => true
29
30 action :install do
31   declare_resource :package, package_name unless installed?
32
33   if new_resource.conf
34     template available_name("conf") do
35       source new_resource.conf
36       owner "root"
37       group "root"
38       mode "644"
39       variables new_resource.variables
40     end
41   end
42 end
43
44 action :enable do
45   execute "a2enmod-#{new_resource.module}" do
46     command "a2enmod #{new_resource.module}"
47     user "root"
48     group "root"
49     not_if { ::File.exist?(enabled_name("load")) }
50   end
51
52   link enabled_name("load") do
53     to available_name("load")
54     owner "root"
55     group "root"
56   end
57
58   link enabled_name("conf") do
59     to available_name("conf")
60     owner "root"
61     group "root"
62     only_if { ::File.exist?(available_name("conf")) }
63   end
64 end
65
66 action :disable do
67   link enabled_name("load") do
68     action :delete
69   end
70
71   link enabled_name("conf") do
72     action :delete
73   end
74 end
75
76 action :delete do
77   package package_name do
78     action :remove
79   end
80 end
81
82 action_class do
83   def package_name
84     new_resource.package || "libapache2-mod-#{new_resource.module}"
85   end
86
87   def available_name(extension)
88     "/etc/apache2/mods-available/#{new_resource.module}.#{extension}"
89   end
90
91   def enabled_name(extension)
92     "/etc/apache2/mods-enabled/#{new_resource.module}.#{extension}"
93   end
94
95   def installed?
96     ::File.exist?(available_name("load"))
97   end
98 end
99
100 def after_created
101   notifies :restart, "service[apache2]" if restart_apache
102 end