/
nginx_cakephp.rb
210 lines (182 loc) · 8.19 KB
/
nginx_cakephp.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
include_recipe "php"
folders = ["log", "private", "releases", "shared"]
cake_shared_folders = [
"config", "tmp", "tmp/cache", "tmp/logs", "tmp/repos", "tmp/sessions", "tmp/tests",
"tmp/cache/data", "tmp/cache/models", "tmp/cache/persistent", "tmp/cache/views"
]
# Iterate over all the existing cakephp applications
node[:cakephp_applications].each do |hostname, sites|
# Iterate over each site
sites.each do |site_base, site_info|
# Iterate over each environment
node[:server].each do |environment, env_config|
# Create both staging and production environments
directory "#{env_config[:dir]}/#{hostname}/#{site_base}" do
owner "deploy"
group "deploy"
mode "0755"
recursive true
end
# Create all the hosting-related directories
folders.each do |folder|
directory "#{env_config[:dir]}/#{hostname}/#{site_base}/#{folder}" do
owner "deploy"
group "deploy"
mode "0755"
recursive true
end
end
# Link the cakephp core
link "cake_core_#{hostname}_#{site_base}_#{environment}" do
to "#{env_config[:dir]}/resources/#{site_info[:cake_version]}/cake"
target_file "#{env_config[:dir]}/#{hostname}/#{site_base}/cake"
owner "deploy"
group "deploy"
action :nothing
end
# Create all the shared app directories
cake_shared_folders.each do |folder|
directory "#{env_config[:dir]}/#{hostname}/#{site_base}/shared/#{folder}" do
owner "deploy"
group "deploy"
mode "0777"
recursive true
action :nothing
subscribes :create, resources(:link => "cake_core_#{hostname}_#{site_base}_#{environment}"), :immediately
end
end
# Make the shared temporary directory writable
execute "tmp_permissions_#{hostname}_#{site_base}_#{environment}" do
command "chmod -R 777 #{env_config[:dir]}/#{hostname}/#{site_base}/shared/tmp"
action :nothing
subscribes :run, resources(:directory => "#{env_config[:dir]}/#{hostname}/#{site_base}/shared/tmp"), :immediately
end
# Link in the shared directory to the public directory
link "tmp_link_#{hostname}_#{site_base}_#{environment}" do
to "#{env_config[:dir]}/#{hostname}/#{site_base}/shared/tmp"
target_file "#{env_config[:dir]}/#{hostname}/#{site_base}/public/tmp"
action :nothing
subscribes :create, resources(:execute => "tmp_permissions_#{hostname}_#{site_base}_#{environment}"), :immediately
end
# Clone the repository
git "#{env_config[:dir]}/#{hostname}/#{site_base}/public" do
repository site_info[:repository]
reference "HEAD"
user "deploy"
group "deploy"
action :checkout
notifies :create, resources(:link => "cake_core_#{hostname}_#{site_base}_#{environment}"), :immediately
end
# Initialize the submodules
execute "submodules_#{hostname}_#{site_base}_#{environment}" do
command "cd #{env_config[:dir]}/#{hostname}/#{site_base}/public && git submodule init && git submodule update"
user "deploy"
group "deploy"
action :nothing
subscribes :run, resources(:git => "#{env_config[:dir]}/#{hostname}/#{site_base}/public"), :immediately
end
# Link the bootstrap.php
link "app_bootstrap_#{hostname}_#{site_base}_#{environment}" do
to "#{env_config[:dir]}/#{hostname}/#{site_base}/shared/config/bootstrap.php"
target_file "#{env_config[:dir]}/#{hostname}/#{site_base}/public/config/bootstrap.php"
owner "deploy"
group "deploy"
action :nothing
end
# Link the core.php
link "app_core_#{hostname}_#{site_base}_#{environment}" do
to "#{env_config[:dir]}/#{hostname}/#{site_base}/shared/config/core.php"
target_file "#{env_config[:dir]}/#{hostname}/#{site_base}/public/config/core.php"
owner "deploy"
group "deploy"
action :nothing
end
# database.php.erb
link "app_database_#{hostname}_#{site_base}_#{environment}" do
to "#{env_config[:dir]}/#{hostname}/#{site_base}/shared/config/database.php"
target_file "#{env_config[:dir]}/#{hostname}/#{site_base}/public/config/database.php"
owner "deploy"
group "deploy"
action :nothing
end
# Create the app bootstrap.php
template "#{env_config[:dir]}/#{hostname}/#{site_base}/shared/config/bootstrap.php" do
source "cakephp/bootstrap.php.erb"
owner "deploy"
group "deploy"
mode 0644
action :nothing
notifies :create, resources(:link => "app_bootstrap_#{hostname}_#{site_base}_#{environment}"), :immediately
subscribes :create, resources(:link => "cake_core_#{hostname}_#{site_base}_#{environment}"), :immediately
end
# Create the app core.php
template "#{env_config[:dir]}/#{hostname}/#{site_base}/shared/config/core.php" do
source "cakephp/core.php.erb"
owner "deploy"
group "deploy"
mode 0644
variables(
:debug => site_info[:variables][environment][:debug],
:cache_disable => site_info[:variables][environment][:cache_disable],
:session_save => site_info[:variables][environment][:session_save],
:security_salt => site_info[:variables][environment][:security_salt],
:security_cipher_seed => site_info[:variables][environment][:security_cipher_seed]
)
action :nothing
notifies :create, resources(:link => "app_core_#{hostname}_#{site_base}_#{environment}"), :immediately
subscribes :create, resources(:link => "cake_core_#{hostname}_#{site_base}_#{environment}"), :immediately
end
# Create the app database.php
template "#{env_config[:dir]}/#{hostname}/#{site_base}/shared/config/database.php" do
source "cakephp/database.php.erb"
owner "deploy"
group "deploy"
mode 0644
variables(
:login => site_info[:variables][environment][:login],
:password => site_info[:variables][environment][:password],
:database => site_info[:variables][environment][:database]
)
action :nothing
notifies :create, resources(:link => "app_database_#{hostname}_#{site_base}_#{environment}"), :immediately
subscribes :create, resources(:link => "cake_core_#{hostname}_#{site_base}_#{environment}"), :immediately
end
# Nginx setup
# Note that each site can have multiple environments (which we are already looping through)
# as well as multiple "apps" pointing at the same environment
# for example, the api and the main app might be the same application, but simply be different
# in their bootstrap and routes folder, based on environment variables
if !site_info[:variables][environment].key?(:apps)
site_info[:variables][environment][:apps] = {
"default" => {"CAKEPHP_APP" => "default"}
}
end
site_info[:variables][environment][:apps].each do |app_env, env_vars|
template_name = "#{hostname}.#{site_base}-#{environment}"
template_name += "_#{app_env}" unless app_env == "default"
subdomain = site_info[:subdomain].empty? ? "" : site_info[:subdomain] + "."
subdomain = app_env == "default" ? subdomain : subdomain + app_env + "."
template "#{node[:nginx][:dir]}/sites-available/#{template_name}" do
source "nginx_php.erb"
owner "root"
group "root"
mode 0644
variables(
:folder_base=> "#{env_config[:dir]}/#{hostname}/#{site_base}",
:root => "#{env_config[:dir]}/#{hostname}/#{site_base}/public/webroot",
:nick => "#{site_base}.#{hostname}",
:resources => [ "php", "html", "htm" ],
:hostname => hostname,
:subdomain => subdomain,
:environment=> environment,
:app_env => app_env,
:env_vars => env_vars
)
end
nginx_site "#{template_name}" do
action :enable
end
end
end
end
end