-
Notifications
You must be signed in to change notification settings - Fork 8
/
SilverStripeDeployTask.php
484 lines (385 loc) · 13.1 KB
/
SilverStripeDeployTask.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
<?php
/**
* A phing task to perform a deployment of a specified
* tarball to a remote server
*
* @author [email protected]
* @license BSD License http://silverstripe.org/bsd-license/
*/
class SilverStripeDeployTask extends SilverStripeBuildTask {
/* deployment config */
private $localpath;
private $apppath;
private $package = '';
private $apachegroup = 'apache';
private $remotepath = '';
private $incremental = false;
private $sapphirepath = 'framework';
/* SSH configuration */
private $host = "";
private $port = 22;
private $username = "";
private $password = "";
private $pubkeyfile = '';
private $privkeyfile = '';
private $privkeyfilepassphrase = '';
private $ignoreerrors = false;
// are we updating the /current release in-place, or creating a new release?
private $inplace = false;
public function main() {
if (!strlen($this->pubkeyfile) && !strlen($this->password)) {
// prompt for the password
$this->password = $this->getInput("Password for " . $this->username . '@' . $this->host);
}
$this->log("Connecting to $this->host");
$this->connect();
$currentPath = $this->remotepath . '/current';
$releasePath = $this->configureReleaseDir();
$remotePackage = $releasePath . '/' . $this->package;
$localPackage = $this->localpath . '/' . $this->package;
$this->log("Copying deployment package $localPackage");
$this->copyFile($localPackage, $remotePackage);
$this->beforeDeploy($releasePath, $currentPath);
$this->extractPackage($remotePackage, $releasePath);
$this->doDeploy($releasePath, $currentPath);
$this->postDeploy($releasePath);
@ssh2_exec($this->connection, 'exit');
}
/**
* Configure the release directory in the remote system
*
* @return string
*/
protected function configureReleaseDir() {
if ($this->inplace) {
$this->log("WARNING: Setting remote path equal to current deployment");
$releasePath = $this->remotepath . '/current';
} else {
$releasePath = $this->remotepath . '/releases/' . date('YmdHis');
$this->log("Configuring target directories at $releasePath");
$this->execute("mkdir --mode=2775 -p $releasePath/silverstripe-cache");
$this->execute("mkdir --mode=2775 -p $releasePath/assets");
}
return $releasePath;
}
/**
* Copy the existing deployment and/or relevant configuration files
* to the new location
*/
protected function beforeDeploy($releasePath, $currentPath) {
if ($this->incremental && !$this->inplace) {
$this->log("Copying existing deployment");
// we use rsync here to be able to use --excludes, and -l to preserve symlinks (ie. for assets folder)
$this->execute("rsync -rl --exclude=silverstripe-cache $currentPath/* $releasePath/");
$this->log("Copying configs");
$this->execute("cp $currentPath/.htaccess $releasePath/");
$this->execute("cp $currentPath/.env $releasePath/.env");
// These are legacy, but remain to support older projects.
$this->execute("cp $currentPath/_ss_environment.php $releasePath/");
$this->execute("cp $currentPath/$this->apppath/local.conf.php $releasePath/$this->apppath/local.conf.php");
// ---
$localConf = "$currentPath/$this->apppath/_config/local.yml";
$cmd = "if [ -f $localConf ]; then cp $localConf $releasePath/$this->apppath/_config/; fi";
$this->execute($cmd);
}
}
/**
* Extract the remote package
*
* @param type $remotePackage
* @param type $releasePath
*/
protected function extractPackage($remotePackage, $releasePath) {
$this->log("Extracting $remotePackage in $releasePath");
$this->execute("tar -zx -C $releasePath -f $remotePackage");
$this->execute("rm $remotePackage");
}
/**
* Update the new deployment with configs etc
*
* @param type $releasePath
* @param type $currentPath
*/
protected function doDeploy($releasePath, $currentPath) {
if (!$this->incremental && !$this->inplace) {
$this->log("Copying configs");
$this->execute("cp $currentPath/.htaccess $releasePath/");
$this->execute("cp $currentPath/.env $releasePath/.env");
// These are legacy, but remain to support older projects.
$this->execute("cp $currentPath/_ss_environment.php $releasePath/");
$this->execute("cp $currentPath/$this->apppath/local.conf.php $releasePath/$this->apppath/local.conf.php");
// ---
$localConf = "$currentPath/$this->apppath/_config/local.yml";
$cmd = "if [ -f $localConf ]; then cp $localConf $releasePath/$this->apppath/_config/; fi";
$this->execute($cmd);
$this->log("Copying site assets");
$this->execute("rsync -rl $currentPath/assets $releasePath/");
}
$this->log("Pre-deploy");
$this->executeOptionalPhpScript($releasePath .'/'. $this->apppath .'/scripts/pre_deploy.php');
$this->log("Backing up database");
$this->execute("php $currentPath/$this->apppath/scripts/backup_database.php");
$this->log("Saving .htaccess");
$this->execute("cp $releasePath/.htaccess $releasePath/$this->apppath/.htaccess.bak");
$this->log("Checking for maintenance mode, and switching if found");
$maintenanceHtaccess = "$releasePath/$this->apppath/.htaccess-maintenance";
$cmd = "if [ -f $maintenanceHtaccess ]; then cp $maintenanceHtaccess $currentPath/.htaccess; fi";
$this->execute($cmd);
$cmd = "if [ -f $maintenanceHtaccess ]; then cp $maintenanceHtaccess $releasePath/.htaccess; fi";
$this->log($cmd);
$this->execute($cmd);
$this->log("Executing dev/build");
// $this->execute("php $releasePath/$this->sapphirepath/cli-script.php dev/build");
if (!$this->inplace) {
$this->preLinkSwitch($releasePath);
$this->log("Changing symlinks");
$this->execute("rm $currentPath");
$this->execute("ln -s $releasePath $currentPath");
}
$this->log("Restoring .htaccess");
$htaccessBak = "$releasePath/$this->apppath/.htaccess.bak";
$cmd = "if [ -f $htaccessBak ]; then cp $htaccessBak $releasePath/.htaccess; fi";
$this->execute($cmd);
$this->log("Finalising deployment");
$this->execute("touch $releasePath/DEPLOYED");
}
/**
* If the project has a pre_deploy.php script, execute it.
*
* @param type $releasePath
* @param type $currentPath
*/
public function preLinkSwitch($releasePath) {
$this->executeOptionalPhpScript($releasePath .'/'. $this->apppath .'/scripts/pre_switch.php', dirname($releasePath));
$this->log("Setting silverstripe-cache permissions");
$this->execute("chgrp -R $this->apachegroup $releasePath/silverstripe-cache", true);
$this->execute("find $releasePath/silverstripe-cache -type f -exec chmod 664 {} \;", true);
$this->execute("find $releasePath/silverstripe-cache -type d -exec chmod 2775 {} \;", true);
}
/**
* If the project has a post_deploy.php script, execute it.
*
* @param type $releasePath
* @param type $currentPath
*/
public function postDeploy($releasePath) {
$arg = dirname($releasePath);
$this->executeOptionalPhpScript($releasePath .'/'. $this->apppath .'/scripts/post_deploy.php', $arg);
$this->log("Fixing permissions");
// force silverstripe-cache permissions first before the rest.
$this->execute("chgrp -R $this->apachegroup $releasePath/silverstripe-cache", true);
$this->execute("find $releasePath/silverstripe-cache -type f -exec chmod 664 {} \;", true);
$this->execute("find $releasePath/silverstripe-cache -type d -exec chmod 2775 {} \;", true);
$this->execute("chgrp -R $this->apachegroup $releasePath", true);
$this->execute("find $releasePath -type f -exec chmod 664 {} \;", true);
$this->execute("find $releasePath -type d -exec chmod 2775 {} \;", true);
$this->executeOptionalPhpScript($releasePath .'/'. $this->apppath .'/scripts/finalise_deployment.php', dirname($releasePath));
}
protected function executeOptionalPhpScript($script) {
$args = func_get_args();
array_shift($args);
$exe = escapeshellarg($script);
$args = array_map('escapeshellarg', $args);
$args = implode(' ', $args);
$cmd = "if [ -e $exe ]; then php $exe $args; fi";
$this->log("Executing $script with args " . $args);
$this->execute($cmd);
}
/**
* Executes a command over SSH
*
* @param string $cmd
* @return string
*/
protected function execute($cmd, $failOkay = false) {
$command = '(' . $cmd . ' 2>&1) && echo __COMPLETE';
// $command = 'sh -c '.escapeshellarg('('.$this->command.' 2>&1) && echo __COMPLETE');
$stream = ssh2_exec($this->connection, $command);
if (!$stream) {
throw new BuildException("Could not execute command!");
}
stream_set_blocking($stream, true);
$data = '';
while ($buf = fread($stream, 4096)) {
$data .= $buf;
}
fclose($stream);
if (strpos($data, '__COMPLETE') !== false || $this->ignoreerrors || $failOkay) {
$data = str_replace('__COMPLETE', '', $data);
} else {
$this->log("Command failed: $command", Project::MSG_WARN);
throw new BuildException("Failed executing command : $data");
}
return $data;
}
/**
* Copies a file to the remote system
*
* @param string $local
* @param string $remote
*/
protected function copyFile($localEndpoint, $remoteEndpoint) {
$port = $this->port ? " -P $this->port " : '';
$this->exec("scp -i $this->privkeyfile $port $localEndpoint $this->username@$this->host:$remoteEndpoint");
return;
ssh2_sftp_mkdir($this->sftp, dirname($remoteEndpoint), 2775, true);
$ret = ssh2_scp_send($this->connection, $localEndpoint, $remoteEndpoint);
if ($ret === false) {
throw new BuildException("Could not create remote file '" . $remoteEndpoint . "'");
}
}
/**
* Connects SSH stuff up
*/
protected function connect() {
if (!function_exists('ssh2_connect')) {
throw new BuildException("To use SshTask, you need to install the SSH extension.");
}
$this->connection = ssh2_connect($this->host, $this->port);
if (is_null($this->connection)) {
throw new BuildException("Could not establish connection to " . $this->host . ":" . $this->port . "!");
}
$could_auth = null;
if (strlen($this->pubkeyfile)) {
$could_auth = ssh2_auth_pubkey_file($this->connection, $this->username, $this->pubkeyfile, $this->privkeyfile, $this->privkeyfilepassphrase);
} else {
$could_auth = ssh2_auth_password($this->connection, $this->username, $this->password);
}
if (!$could_auth) {
throw new BuildException("Could not authenticate connection!");
}
$this->sftp = ssh2_sftp($this->connection);
}
public function setSapphirepath($path) {
$this->sapphirepath = $path;
}
public function setApachegroup($g) {
$this->apachegroup = $g;
}
public function getApachegroup() {
return $this->apachegroup;
}
public function setLocalpath($p) {
$this->localpath = $p;
}
public function getLocalpath() {
return $this->localpath;
}
public function setApppath($p) {
$this->apppath = $p;
}
public function getApppath() {
return $this->apppath;
}
public function setRemotepath($p) {
$this->remotepath = $p;
}
public function getRemotepath() {
return $this->remotepath;
}
public function setPackage($pkg) {
$this->package = $pkg;
}
public function getPackage() {
return $this->package;
}
public function setIncremental($v) {
if (!is_bool($v)) {
$v = $v == 'true' || $v == 1;
}
$this->incremental = $v;
}
public function getIncremental() {
return $this->incremental;
}
public function setInplace($v) {
if (!is_bool($v)) {
$v = $v == 'true' || $v == 1;
}
$this->inplace = $v;
}
public function getInplace() {
return $this->inplace;
}
public function setHost($host) {
$this->host = $host;
}
public function getHost() {
return $this->host;
}
public function setPort($port) {
if (strpos($port, '${') === false) {
$this->port = $port;
}
}
public function getPort() {
return $this->port;
}
public function setUsername($username) {
$this->username = $username;
}
public function getUsername() {
return $this->username;
}
public function setPassword($password) {
if (strpos($password, '${') === false) {
$this->password = $password;
}
}
public function getPassword() {
return $this->password;
}
/**
* Sets the public key file of the user to scp
*/
public function setPubkeyfile($pubkeyfile) {
if (strpos($pubkeyfile, '${') === false) {
$this->pubkeyfile = $pubkeyfile;
}
}
/**
* Returns the pubkeyfile
*/
public function getPubkeyfile() {
return $this->pubkeyfile;
}
/**
* Sets the private key file of the user to scp
*/
public function setPrivkeyfile($privkeyfile) {
$this->privkeyfile = $privkeyfile;
}
/**
* Returns the private keyfile
*/
public function getPrivkeyfile() {
return $this->privkeyfile;
}
/**
* Sets the private key file passphrase of the user to scp
*/
public function setPrivkeyfilepassphrase($privkeyfilepassphrase) {
$this->privkeyfilepassphrase = $privkeyfilepassphrase;
}
/**
* Returns the private keyfile passphrase
*/
public function getPrivkeyfilepassphrase($privkeyfilepassphrase) {
return $this->privkeyfilepassphrase;
}
public function setCommand($command) {
$this->command = $command;
}
public function getCommand() {
return $this->command;
}
public function setIgnoreErrors($ignore) {
if (!is_bool($ignore)) {
$ignore = $ignore == 'true' || $ignore == 1;
}
$this->ignoreerrors = $ignore;
}
public function getIgnoreErrors() {
return $this->ignoreerrors;
}
}