Skip to content

Commit 7f0a0d6

Browse files
committed
Merge branch 'develop'
Merge details: (43a2391) Added docblock to `getTimeoutSetting`. by Jelle Sebreghts on Wed Sep 26 11:49:14 2018 (5368096) Refactoring: Split `getTimeoutSetting` in separate methods. by Jelle Sebreghts on Wed Sep 26 11:39:26 2018 (d744581) Ensure properties are read before getting timeout settings. by Jelle Sebreghts on Wed Sep 26 11:08:53 2018 (9ddd6bb) Fixes #12: Make time outs configurable. by Jelle Sebreghts on Fri Sep 21 14:50:56 2018 (85b3ff5) Issue #12: Make time outs configurable. by Jelle Sebreghts on Fri Sep 21 14:50:56 2018 (c84efca) #23: Throw an exception when we cant't determine the user home directory or realpath. by Jelle Sebreghts on Fri Sep 21 12:04:03 2018 (a73e44e) Fixes #23: `readlink -e` option not recognized on MacOS. by Jelle Sebreghts on Fri Sep 21 11:48:27 2018
2 parents 7965545 + 2ecef09 commit 7f0a0d6

File tree

1 file changed

+89
-12
lines changed

1 file changed

+89
-12
lines changed

src/AbstractRoboFile.php

Lines changed: 89 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
abstract class AbstractRoboFile extends \Robo\Tasks implements DigipolisPropertiesAwareInterface, ConfigAwareInterface
1313
{
14+
const DEFAULT_TIMEOUT = 10;
15+
1416
use \DigipolisGent\Robo\Task\Package\Commands\loadCommands;
1517
use \DigipolisGent\Robo\Task\General\loadTasks;
1618
use \DigipolisGent\Robo\Task\General\Common\DigipolisPropertiesAware;
@@ -350,7 +352,7 @@ protected function preSymlinkTask($worker, AbstractAuth $auth, $remote)
350352
$collection = $this->collectionBuilder();
351353
$collection->taskSsh($worker, $auth)
352354
->remoteDirectory($projectRoot, true)
353-
->timeout(60);
355+
->timeout($this->getTimeoutSetting('presymlink_mirror_dir'));
354356
foreach ($remote['symlinks'] as $symlink) {
355357
list($target, $link) = explode(':', $symlink);
356358
if ($link === $remote['currentdir']) {
@@ -520,7 +522,7 @@ protected function syncTask(
520522

521523
$collection->addTask(
522524
$this->taskRsync()
523-
->rawArg('--rsh "ssh -o StrictHostKeyChecking=no -i `readlink -e ' . $sourceKeyFile . '`"')
525+
->rawArg('--rsh "ssh -o StrictHostKeyChecking=no -i ' . $this->realpath($sourceKeyFile) . '"')
524526
->fromPath($tmpKeyFile)
525527
->toHost($sourceHost)
526528
->toUser($sourceUser)
@@ -542,7 +544,7 @@ protected function syncTask(
542544
$dir .= ($dir !== '' ? '/' : '');
543545

544546
$rsync = $this->taskRsync()
545-
->rawArg('--rsh "ssh -o StrictHostKeyChecking=no -i `readlink -e ' . $tmpKeyFile . '`"')
547+
->rawArg('--rsh "ssh -o StrictHostKeyChecking=no -i ' . $this->realpath($tmpKeyFile) . '"')
546548
->fromPath($sourceRemote['filesdir'] . '/' . $dir)
547549
->toHost($destinationHost)
548550
->toUser($destinationUser)
@@ -560,7 +562,7 @@ protected function syncTask(
560562

561563
$collection->addTask(
562564
$this->taskSsh($sourceHost, $sourceAuth)
563-
->timeout(1800)
565+
->timeout($this->getTimeoutSetting('synctask_rsync'))
564566
->exec($rsync)
565567
);
566568
}
@@ -677,7 +679,7 @@ protected function backupTask(
677679
. ' ' . ($this->fileBackupSubDirs ? implode(' ', $this->fileBackupSubDirs) : '*');
678680
$collection->taskSsh($worker, $auth)
679681
->remoteDirectory($remote['filesdir'])
680-
->timeout(300)
682+
->timeout($this->getTimeoutSetting('backup_files'))
681683
->exec($filesBackup);
682684
}
683685

@@ -687,7 +689,7 @@ protected function backupTask(
687689
. '--destination=' . $backupDir . '/' . $dbBackupFile;
688690
$collection->taskSsh($worker, $auth)
689691
->remoteDirectory($currentProjectRoot, true)
690-
->timeout(300)
692+
->timeout($this->getTimeoutSetting('backup_database'))
691693
->exec($dbBackup);
692694
}
693695
return $collection;
@@ -716,7 +718,7 @@ protected function removeBackupTask(
716718

717719
$collection = $this->collectionBuilder();
718720
$collection->taskSsh($worker, $auth)
719-
->timeout(300)
721+
->timeout($this->getTimeoutSetting('remove_backup'))
720722
->exec('rm -rf ' . $backupDir);
721723

722724
return $collection;
@@ -762,7 +764,7 @@ protected function restoreBackupTask(
762764
$collection
763765
->taskSsh($worker, $auth)
764766
->remoteDirectory($remote['filesdir'], true)
765-
->timeout(300)
767+
->timeout($this->getTimeoutSetting('restore_files_backup'))
766768
->exec('tar -xkzf ' . $backupDir . '/' . $filesBackupFile);
767769
}
768770

@@ -774,7 +776,7 @@ protected function restoreBackupTask(
774776
$collection
775777
->taskSsh($worker, $auth)
776778
->remoteDirectory($currentProjectRoot, true)
777-
->timeout(60)
779+
->timeout($this->getTimeoutSetting('restore_db_backup'))
778780
->exec($dbRestore);
779781
}
780782
return $collection;
@@ -816,7 +818,7 @@ protected function preRestoreBackupTask(
816818
return $this->taskSsh($worker, $auth)
817819
->remoteDirectory($remote['filesdir'], true)
818820
// Files dir can be pretty big on large sites.
819-
->timeout(300)
821+
->timeout($this->getTimeoutSetting('pre_restore_remove_files'))
820822
->exec($removeFiles);
821823
}
822824

@@ -1057,7 +1059,7 @@ protected function cleanDirsTask($worker, AbstractAuth $auth, $remote)
10571059

10581060
$task = $this->taskSsh($worker, $auth)
10591061
->remoteDirectory($remote['rootdir'], true)
1060-
->timeout(30)
1062+
->timeout($this->getTimeoutSetting('clean_dir'))
10611063
->exec('vendor/bin/robo digipolis:clean-dir ' . $remote['releasesdir'] . ($cleandirLimit ? ':' . $cleandirLimit : ''));
10621064

10631065
if ($remote['createbackup']) {
@@ -1125,7 +1127,7 @@ public function digipolisSyncLocal(
11251127
$dir .= ($dir !== '' ? '/' : '');
11261128

11271129
$rsync = $this->taskRsync()
1128-
->rawArg('--rsh "ssh -o StrictHostKeyChecking=no -i `readlink -e ' . $keyFile . '`"')
1130+
->rawArg('--rsh "ssh -o StrictHostKeyChecking=no -i ' . $this->realpath($keyFile) . '"')
11291131
->fromHost($host)
11301132
->fromUser($user)
11311133
->fromPath($remote['filesdir'] . '/' . $dir)
@@ -1314,4 +1316,79 @@ protected function getLocalSettings($app = null, $timestamp = null)
13141316
);
13151317
return $this->tokenReplace($this->getConfig()->get('local'), $replacements) + $defaults;
13161318
}
1319+
1320+
/**
1321+
* Timeouts can be overwritten in properties.yml under the `timeout` key.
1322+
*
1323+
* @param string $setting
1324+
*
1325+
* @return int
1326+
*/
1327+
protected function getTimeoutSetting($setting)
1328+
{
1329+
$timeoutSettings = $this->getTimeoutSettings();
1330+
return isset($timeoutSettings[$setting]) ? $timeoutSettings[$setting] : static::DEFAULT_TIMEOUT;
1331+
}
1332+
1333+
protected function getTimeoutSettings()
1334+
{
1335+
$this->readProperties();
1336+
return $this->getConfig()->get('timeouts', []) + $this->getDefaultTimeoutSettings();
1337+
}
1338+
1339+
protected function getDefaultTimeoutSettings()
1340+
{
1341+
return [
1342+
'presymlink_mirror_dir' => 60,
1343+
'synctask_rsync' => 1800,
1344+
'backup_files' => 300,
1345+
'backup_database' => 300,
1346+
'remove_backup' => 300,
1347+
'restore_files_backup' => 300,
1348+
'restore_db_backup' => 60,
1349+
'pre_restore_remove_files' => 300,
1350+
'clean_dir' => 30,
1351+
];
1352+
}
1353+
1354+
/**
1355+
* PHP's realpath can't handle tilde (~), so we have to write a wrapper
1356+
* for it.
1357+
*/
1358+
protected function realpath($path)
1359+
{
1360+
$realpath = $path;
1361+
if (strpos($realpath, '~') === 0 && ($homedir = $this->getUserHomeDir())) {
1362+
$realpath = $homedir . substr($realpath, 1);
1363+
}
1364+
$realpath = realpath($realpath);
1365+
1366+
if ($realpath === false) {
1367+
throw new \Exception(sprintf('Could not determine real path of %s.', $path));
1368+
}
1369+
1370+
return $realpath;
1371+
}
1372+
1373+
/**
1374+
* Get the home directory for the current user.
1375+
*/
1376+
protected function getUserHomeDir()
1377+
{
1378+
// getenv('HOME') isn't set on Windows.
1379+
$home = getenv('HOME');
1380+
if (!empty($home)) {
1381+
// home should never end with a trailing slash.
1382+
return rtrim($home, '/');
1383+
}
1384+
if (!empty($_SERVER['HOMEDRIVE']) && !empty($_SERVER['HOMEPATH'])) {
1385+
// home on windows
1386+
$home = $_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH'];
1387+
// If HOMEPATH is a root directory the path can end with a slash.
1388+
// Make sure that doesn't happen.
1389+
return rtrim($home, '\\/');
1390+
}
1391+
1392+
throw new \Exception('Could not determine the current user\'s home directory.');
1393+
}
13171394
}

0 commit comments

Comments
 (0)