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