forked from baumrock/RockMigrations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deployment.php
504 lines (460 loc) · 13.4 KB
/
Deployment.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
<?php
namespace RockMigrations;
use ProcessWire\Config;
use ProcessWire\Paths;
use ProcessWire\WireData;
// we make sure that the current working directory is the PW root
chdir(dirname(dirname(dirname(__DIR__))));
require_once "wire/core/ProcessWire.php";
class Deployment extends WireData
{
const keep = 2;
public $branch;
public $chown = true; // chown by default
public $delete = [];
public $dry = false;
private $isVerbose = false;
public $paths;
private $php = "php";
private $robots;
public $share = [];
public function __construct($argv = null, $whitelistedPath = null)
{
$this->paths = new WireData();
// get branch from script arguments
$this->branch = '';
if ($argv and count($argv) > 1) $this->branch = $argv[1];
// path to the current release
$this->paths->release = getcwd();
// path to the root that contains all releases and current + shared folder
$this->paths->root = dirname($this->paths->release);
if (strpos($this->paths->root, (string)$whitelistedPath) !== 0) {
// the current root path does not match the provided path argument
// this means we are not on the deployment server, so we make it dry
$this->echo("Not in whitelist path!");
$this->echo("Current path: {$this->paths->root}");
$this->echo("Whitelisted path: $whitelistedPath");
$this->echo("Running dry...");
$this->dry();
}
// path to shared folder
$this->paths->shared = $this->paths->root . "/shared";
// setup default share directories
$this->share = [
'/site/config-local.php',
'/site/assets/files',
'/site/assets/logs',
];
// setup default delete directories
$this->delete = [
'/.ddev',
'/.git',
'/.github',
'/site/assets/backups',
'/site/assets/cache',
'/site/assets/ProCache',
'/site/assets/pwpc-*',
'/site/assets/sessions',
];
}
public function addRobots()
{
if (!$this->robots()) return;
$this->echo("Hiding site from search engines via robots.txt");
$release = $this->paths->release;
$src = __DIR__ . "/robots.txt";
$this->exec("cp -f $src $release/robots.txt");
}
/**
* chown files based on root folder
* @return void
*/
protected function chown()
{
if (!$this->chown) {
$this->echo("chown was disabled in deploy.php");
return;
}
$root = $this->paths->root;
$owner = fileowner($root);
$group = filegroup($root);
$this->echo("Setting owner and group based on $root...");
$this->exec("chown -R $owner:$group $root", true);
$this->echo("Done");
}
/**
* Delete files from release
* @return void
*/
public function delete($files = null, $reset = false)
{
if (is_array($files)) {
if ($reset) $this->delete = [];
$this->delete = array_merge($files, $this->delete);
} elseif ($files === null) {
// execute deletion
$this->echo("Deleting files...");
foreach ($this->delete as $file) {
$file = trim(Paths::normalizeSeparators($file), "/");
$this->echo(" $file");
$this->exec("rm -rf $file");
}
$this->echo("Done");
}
}
/**
* Cleanup old releases and keep given number
*
* keep=2 means we keep current + 2 old releases (overall 3)
*
* This does also rename old release folders to make symlinks aware of the
* change without rebooting the server or reloading php-fpm
*/
public function deleteOldReleases($keep = null, $rename = true)
{
if (!$keep) $keep = self::keep;
$this->echo("Cleaning up old releases...");
$folders = glob($this->paths->root . "/release-*");
rsort($folders);
$cnt = 0;
$revert = "You can revert like this:";
$revert .= "\n cd {$this->paths->root}";
foreach ($folders as $folder) {
$cnt++;
$base = basename($folder);
if ($cnt > $keep + 1) {
$this->echo("delete $base", 2);
$this->exec("rm -rf $folder");
continue;
}
if ($rename) {
if ($cnt > 1) {
$arrow = str_pad(">", 10 - $cnt, " ", STR_PAD_LEFT);
$this->echo("rename $base $arrow $base-", 2);
$date = date("Y-m-d H:i:s", filemtime($folder));
$revert .= "\n $date >> ln -snf $base- current";
$this->exec("mv $folder $folder-");
$folder = "$folder-";
$base = "$base-";
} else $this->echo("create $base", 2);
}
}
$folders = glob($this->paths->root . "/tmp-release-*");
if (count($folders)) $this->echo("Deleting tmp folders...");
foreach ($folders as $folder) {
$base = basename($folder);
$this->echo("delete $base", 2);
$this->exec("rm -rf $folder");
}
$this->echo($revert);
$this->echo("Done");
}
public function dry($flag = true)
{
$this->dry = $flag;
}
/**
* Create DB dump
*/
public function dumpDB()
{
if ($this->dry) return $this->echo("Dry run - skipping dumpDB()...");
$current = $this->paths->root . "/current";
$configFile = "$current/site/config.php";
if (!is_file($configFile)) {
return $this->echo("No current release - skipping dumpDB()...");
}
try {
$this->echo("Trying to create a DB dump of old release...");
// load config
$config = new Config();
include $configFile;
$dir = "$current/site/assets/backups/database";
$sql = "$dir/rm-deploy.sql";
$this->exec("
mkdir -p $dir
mysqldump -h'{$config->dbHost}' -P'{$config->dbPort}' -u'{$config->dbUser}' -p'{$config->dbPass}' {$config->dbName} > $sql
");
$this->echo("old: " . realpath($sql), 2);
$this->echo("new: " . str_replace("/site/", "-/site/", realpath($sql)), 2);
$this->echo("Done");
} catch (\Throwable $th) {
$this->echo($th->getMessage());
}
}
/**
* Echo message to stout
*/
public function echo($msg = '', $indent = 0)
{
if (is_int($indent)) $indent = str_pad('', $indent);
if (is_string($msg)) {
echo "{$indent}$msg\n";
} elseif (is_array($msg)) {
if (count($msg)) echo print_r($msg, true) . "\n";
}
}
/**
* Execute command and echo output
*/
public function exec($cmd, $echoCmd = false)
{
if ($this->dry) $echoCmd = true;
if ($echoCmd) $this->echo($cmd);
if ($this->dry) return;
exec($cmd, $out);
$this->echo($out);
return $out;
}
/**
* Finish deployment
* This removes the tmp- prefix from the deployment folder
* and updates the "current" symlink
* @return void
*/
public function finish($keep = null)
{
if ($this->dry) {
$this->echo("Dry run - skipping finish()...");
return;
}
$oldPath = $this->paths->release;
$newName = substr(basename($oldPath), 4);
$this->echo("Finishing deployment - updating symlink...");
$this->exec("mv $oldPath {$this->paths->root}/$newName");
$this->exec("
cd {$this->paths->root}
ln -snf $newName current
");
$this->deleteOldReleases($keep);
}
public function exit($msg)
{
$this->echo($msg);
exit($msg);
}
public function hello()
{
$this->echo("
#########################################
RockMigrations Deployment by baumrock.com
#########################################
");
$this->echo("Creating new release at {$this->paths->release}\n");
$this->echo("Root folder name: " . $this->rootFolderName());
}
/**
* Run RockMigrations
*/
public function migrate()
{
$release = $this->paths->release;
$file = "$release/site/modules/RockMigrations/migrate.php";
if (!is_file($file)) return $this->echo("RockMigrations not found...");
$this->echo("Trigger RockMigrations...");
$php = $this->php();
try {
$out = $this->exec("$php $file", true);
} catch (\Throwable $th) {
$this->exit($th->getMessage());
}
if (!is_array($out)) return $this->exit("migrate.php failed");
}
/**
* Print paths
*/
protected function paths()
{
$this->echo($this->paths->getArray());
}
/**
* Get or set php command that will be used to trigger the migrate script
* This needs to be configurable in case the CLI php version is different
* than the webroot (eg 7.4 vs. 8.1)
*
* Example:
* $deploy->php('keyhelp-php81');
*
* @return string
*/
public function php($phpCommand = '')
{
if ($phpCommand) $this->php = $phpCommand;
return $this->php;
}
/**
* Push folder to shared folder and create symlink
*
* Usage:
* $deploy->push('/site/assets/files/123');
*/
public function push($folder)
{
return $this->share([$folder => 'push']);
}
/**
* Get or set robots flag
*
* TRUE means that the deny-all robots.txt will be written to root
*
* By default it will be FALSE for master and main branch and TRUE for
* all other branches
*/
public function robots($val = null)
{
if ($val === null) {
if ($this->robots === null) {
if ($this->branch == 'main') $this->robots = false;
elseif ($this->branch == 'master') $this->robots = false;
else $this->robots = true;
}
return $this->robots;
}
$this->robots = $val;
}
/**
* Add robots.txt to deny all robot requests
*
* This method is the same as robots() but the name is more verbose so the
* deployment script gets better readable.
*
* Usage:
* $deploy->robotsDenyAll(true); // overwrite robots.txt (deny all)
*
* $deploy->robotsDenyAll(false); // dont overwrite robots.txt
*
* deny robot requests of the root folder name is NOT yoursite.com
* that means if the root folder name is staging.yoursite.com all robot
* requests will be denied because on deploy RM will copy the deny-all
* robots.txt to the root folder of your staging site
* $deploy->robotsDenyAll(
* $deploy->rootFolderName() != 'yoursite.com'
* );
*/
public function robotsDenyAll($bool = false)
{
return $this->robots(!!$bool);
}
public function rootFolderName(): string
{
return basename($this->paths->root);
}
/**
* Run default actions
*/
public function run($keep = null)
{
$this->hello();
$this->share();
$this->delete();
$this->secure();
$this->dumpDB();
$this->migrate();
$this->addRobots();
$this->finish($keep);
$this->chown(); // must be after finish to affect current symlink!
$folders = glob($this->paths->root . "/tmp-release-*");
if (count($folders)) {
$this->exit("Found some tmp-folders. It seems something went wrong...");
} else {
$this->echo("
########################
deployment successful :)
########################
");
}
}
/**
* Secure file and folder permissions
* @return void
*/
public function secure()
{
$release = $this->paths->release;
$shared = $this->paths->shared;
$this->echo("Securing file and folder permissions...");
$this->exec(" find $release -type d -exec chmod 755 {} \;
find $release -type f -exec chmod 644 {} \;
chmod 440 $release/site/config.php
chmod 440 $shared/site/config-local.php", true);
$this->echo("Done");
}
/**
* Share files and folders across releases
* Shared assets will be symlinked: releases folder --> shared folder
*
* Usage:
* $deploy->share("/your/file.txt");
*
* $deploy->share([
* // symlink the foo folder
* '/foo',
*
* // push data from repo to shared folder and then create a symlink
* '/site/assets/files/123' => 'push',
* ]);
* @return void
*/
public function share($files = null, $reset = false)
{
if (is_string($files)) $files = [$files];
if (is_array($files)) {
if ($reset) $this->share = [];
$this->share = array_merge($files, $this->share);
} elseif ($files === null) {
$this->echo("Setting up shared files...");
$release = $this->paths->release;
$shared = $this->paths->shared;
$this->echo($this->share);
foreach ($this->share as $k => $v) {
$file = $v;
// push to shared folder or just create link?
$type = 'link';
if (is_string($k)) {
$file = $k;
$type = $v;
}
// prepare the src path
$file = trim(Paths::normalizeSeparators($file), "/");
$from = Paths::normalizeSeparators("$release/$file");
$toAbs = Paths::normalizeSeparators("$shared/$file");
$isfile = !!pathinfo($from, PATHINFO_EXTENSION);
$toDir = dirname($toAbs);
// we create relative symlinks
$level = substr_count($file, "/");
$to = "shared/$file";
for ($i = 0; $i <= $level; $i++) $to = "../$to";
if ($isfile) {
$this->echo(" file $from");
$this->exec("ln -sf $to $from");
} else {
$this->echo(" directory $from");
// push means we only push files to the shared folder
// but we do not create a symlink. This can be used to push site
// translations where the files folder itself is already symlinked
if ($type == 'push') {
$this->exec("
rm -rf $toAbs
mkdir -p $toDir
mv $from $toDir
", $this->isVerbose);
} else {
$this->exec("
mkdir -p $toAbs
rm -rf $from
ln -snf $to $from
", $this->isVerbose);
}
}
}
$this->echo("Done");
}
}
/**
* Make output more verbose
*/
public function verbose()
{
$this->isVerbose = true;
}
}