]> git.openstreetmap.org Git - chef.git/blob - cookbooks/apache/resources/site.rb
Enable unified mode for custom resources
[chef.git] / cookbooks / apache / resources / site.rb
1 #
2 # Cookbook:: apache
3 # Resource:: apache_site
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 [:create, :enable]
23
24 property :site, :kind_of => String, :name_property => true
25 property :directory, :kind_of => String
26 property :cookbook, :kind_of => String
27 property :template, :kind_of => String, :required => [:create]
28 property :variables, :kind_of => Hash, :default => {}
29 property :reload_apache, :kind_of => [TrueClass, FalseClass], :default => true
30
31 action :create do
32   declare_resource :template, available_name do
33     cookbook new_resource.cookbook
34     source new_resource.template
35     owner "root"
36     group "root"
37     mode "644"
38     variables new_resource.variables.merge(:name => new_resource.site, :directory => site_directory)
39   end
40 end
41
42 action :enable do
43   link enabled_name do
44     to available_name
45     owner "root"
46     group "root"
47   end
48 end
49
50 action :disable do
51   link enabled_name do
52     action :delete
53   end
54 end
55
56 action :delete do
57   file available_name do
58     action :delete
59   end
60 end
61
62 action_class do
63   def site_directory
64     new_resource.directory || "/var/www/#{new_resource.site}"
65   end
66
67   def available_name
68     "/etc/apache2/sites-available/#{new_resource.site}.conf"
69   end
70
71   def enabled_name
72     case new_resource.site
73     when "default"
74       "/etc/apache2/sites-enabled/000-default.conf"
75     else
76       "/etc/apache2/sites-enabled/#{new_resource.site}.conf"
77     end
78   end
79 end
80
81 def after_created
82   notifies :reload, "service[apache2]" if reload_apache
83 end