This repository was archived by the owner on Apr 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault_pakefile.php
288 lines (243 loc) · 9.91 KB
/
default_pakefile.php
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
/*
* Default deploy tasks.
*
* These tasks can be used for general deployment, independent of
* the Symfony framework.
*
* @package Compose
* @author Ben Constable <ben@benconstable.co.uk>
* @version 1.0
*/
/*
* Setup Compose for deployment.
*
* Creates new directories on the remote server, and starts off with a "live" and "staging" environment.
* Creates a new Git remote, named after the "server_remote_name" property.
*/
pake_desc("Do initial setup. Make the remote Git repo, the site folder and the live and staging environments");
pake_task("setup_compose");
function run_setup_compose($obj, $args)
{
$ssh = get_prop("ssh");
$site = get_prop("site");
$server_remote = get_prop("server_remote_name");
$remote_git_root = get_prop("remote_git_root") . "/$site";
$remote_site_root = get_prop("remote_sites_root") . "/$site";
pake_echo_action("SETUP", "** Setting up Compose **");
// Create directories
pake_echo_comment(pake_sh("ssh $ssh mkdir $remote_git_root"));
pake_echo_comment(pake_sh("ssh $ssh mkdir $remote_site_root"));
// Setup repo
pake_echo_comment(pake_sh("ssh $ssh git --git-dir=\"$remote_git_root\" init --bare"));
// Create environments
run_setup_environment(false, array("staging"));
run_setup_environment(false, array("live"));
// Setup remote
pake_echo_comment(pake_sh("git remote add $server_remote $ssh:$remote_git_root"));
}
/*
* Creates a new Compose environment.
*
* All appropriate directories are created on the server and given the correct permissions.
*
* @param string (1st arg) Environment name
*/
pake_desc("Setup directory structure and permissions for a Compose site environment");
pake_task("setup_environment");
function run_setup_environment($obj, $args)
{
$env = get_environment($args);
$ssh = get_prop("ssh");
$site = get_prop("site");
$remote_git_root = get_prop("remote_git_root") . "/$site";
$remote_site_root = get_prop("remote_sites_root") . "/$site";
if ($env) {
pake_echo_action("CREATE ENV", "** Creating new environment: '$env' **");
// Create directory structure
pake_echo_comment(pake_sh("ssh $ssh mkdir $remote_site_root/$env"));
pake_echo_comment(pake_sh("ssh $ssh mkdir $remote_site_root/$env/.versions"));
pake_echo_comment(pake_sh("ssh $ssh mkdir $remote_site_root/$env/shared"));
pake_echo_comment(pake_sh("ssh $ssh touch $remote_site_root/$env/current"));
// Setup permissions
pake_echo_comment(pake_sh("ssh $ssh chgrp -R www-data $remote_site_root/$env/shared"));
pake_echo_comment(pake_sh("ssh $ssh chmod -R g=rx $remote_site_root/$env/shared"));
pake_echo_comment(pake_sh("ssh $ssh chmod -R u=rwx $remote_site_root/$env/shared"));
pake_echo_comment(pake_sh("ssh $ssh chmod -R u=rwx $remote_site_root/$env/.versions"));
pake_echo_comment(pake_sh("ssh $ssh chmod -R u=rwx $remote_site_root/$env/current"));
}
else {
pake_echo_error("No environment name given");
}
}
/*
* Tag the current commit.
*
* Wraps the 'git tag' command. For sensibility, only releases to the live
* evironment can be tagged using this task.
*
* @param string (1st arg) Environment name
* @param string (2nd arg) Tag name
*/
pake_desc("Tag the current version for release. Only the live environment can be tagged");
pake_task("tag");
function run_tag($obj, $args)
{
$env = get_environment($args);
if ($args[1]) {
if ($env === "live") {
$tag = $args[1];
pake_echo_action("TAG", "** Tagging release as '$tag' **");
pake_echo_comment(pake_sh("git tag -a \"$tag\""));
}
else {
pake_echo_comment("Found tag, but will only tag live environment");
}
}
}
/*
* Checkout the local branch defined in pake_properties.ini. This is called before
* every attempt to push to a remote, in order to make sure that incorrect branches
* aren't accidentally pushed.
*/
pake_desc("Ensure the correct local branch is checked out before pushing");
pake_task("checkout_local_branch");
function run_checkout_local_branch($obj, $args)
{
$branch = get_prop("local_branch");
pake_echo_action("CHECKOUT", "** Checking out local branch to push **");
pake_echo_comment(pake_sh("git checkout $branch"));
}
/*
* Push the local site version to the given server environment.
*
* @param string (1st arg) Environment name (optional, 'staging' by default)
*/
pake_desc("Push the local site version to the server on the given environment");
pake_task("push_server", "checkout_local_branch");
function run_push_server($obj, $args)
{
$env = get_environment($args);
$local_branch = get_prop("local_branch");
$server = get_prop("server_remote_name");
pake_echo_action("PUSH", "** Pushing to '$env' environment on the server **");
pake_echo_comment(pake_sh("git push $server $local_branch:$env"));
}
/*
* Remove the oldest version for the given environment from the server.
*
* @param string (1st arg) Environment name (optional, 'staging' by default)
*/
pake_desc("Remove the oldest version of a site from .versions/ directory");
pake_task("remove_old_version");
function run_remove_old_version($obj, $args)
{
$env = get_environment($args);
$ssh = get_prop("ssh");
$site = get_prop("site");
$remote_root = get_prop("remote_sites_root");
$versions = get_versions($args);
if ($versions !== null) {
if (count($versions) > 1) {
date_default_timezone_set("Europe/London");
$oldest = "";
$oldest_date = new DateTime();
foreach ($versions as $ver) {
if ($ver["date"] < $oldest_date) {
$oldest_date = $ver["date"];
$oldest = $ver["name"];
}
}
pake_echo_action("REMOVE", "** Removing site version dated " . $oldest_date->format("Y-m-d H:i:s") . " **");
pake_echo_comment(pake_sh("ssh $ssh rm -R $remote_root/$site/$env/.versions/$oldest"));
}
else {
pake_echo_error("Only one version found, will not remove");
}
}
}
/*
* Rollback to the previously pushed site version.
*
* @param string (1st arg) Environment name (optional, 'staging' by default)
*/
pake_desc("Rollback to the previous version, stored in .versions/");
pake_task("rollback");
function run_rollback($obj, $args)
{
$env = get_environment($args);
$ssh = get_prop("ssh");
$site = get_prop("site");
$remote_root = get_prop("remote_sites_root");
$versions = get_versions($args);
if ($versions !== null) {
if (count($versions) > 1) {
pake_echo_action("Rollback", "** Rolling back to previous site version **");
// remove the current symlink
pake_echo_comment(pake_sh("ssh $ssh rm $remote_root/$site/$env/current"));
date_default_timezone_set("Europe/London");
// find latest version
$latest = "";
$latest_date = "";
$latest_index = -1;
foreach ($versions as $ver) {
if ($ver["date"] >= $latest_date || $latest_date === "") {
$latest = $ver["name"];
$latest_date = $ver["date"];
}
$latest_index++;
}
// remove latest from $versions
unset($versions[$latest_index]);
// find previous version
$previous = "";
$previous_date = "";
foreach ($versions as $ver) {
if ($ver["date"] >= $previous_date || $previous_date === "") {
$previous = $ver["name"];
$previous_date = $ver["date"];
}
}
// remove latest
pake_echo_comment(pake_sh("ssh $ssh rm -R $remote_root/$site/$env/.versions/$latest"));
// create new current from previous
pake_echo_comment(pake_sh("ssh $ssh ln -s $remote_root/$site/$env/.versions/$previous $remote_root/$site/$env/current"));
}
else {
pake_echo_error("Cannot roll back, only one version found");
}
}
}
/*
* Push the local version of the site to the server, and check out the work tree to
* the given environment.
*
* @param string (1st arg) Environment name (optional, 'staging' by default)
*/
pake_desc("Jump on to the server and checkout new working tree to webroot");
pake_task("default_deploy", "tag", "push_server", "remove_old_version");
function run_default_deploy($obj, $args)
{
$env = get_environment($args);
$ssh = get_prop("ssh");
$site = get_prop("site");
$site_path = get_prop("remote_sites_root")."/$site/$env";
$git_path = get_prop("remote_git_root")."/$site";
//new directory for latest
date_default_timezone_set("Europe/London");
$version = "site~".date("Ymd\TH:i:s");
pake_echo_action("DEPLOY", "** Deploying site to the '$env' environment **");
//create new directory in .versions/
pake_echo_comment(pake_sh("ssh $ssh mkdir $site_path/.versions/$version"));
//checkout working tree to latest
pake_echo_comment(pake_sh("ssh $ssh git --git-dir=\"$git_path\" --work-tree=\"$site_path/.versions/$version\" checkout -f $env"));
//symlink to current
$result = pake_sh("ssh $ssh rm -R $site_path/current");
$result .= pake_sh("ssh $ssh ln -s $site_path/.versions/$version $site_path/current");
pake_echo_comment($result);
//sort out permissions (read and execute)
$result = pake_sh("ssh $ssh chgrp -R www-data $site_path/.versions/$version");
$result .= pake_sh("ssh $ssh chmod -R g=rx $site_path/.versions/$version");
pake_echo_comment($result);
}
?>