Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Jelle-S committed Sep 26, 2018
2 parents 7965545 + 2ecef09 commit 7f0a0d6
Showing 1 changed file with 89 additions and 12 deletions.
101 changes: 89 additions & 12 deletions src/AbstractRoboFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

abstract class AbstractRoboFile extends \Robo\Tasks implements DigipolisPropertiesAwareInterface, ConfigAwareInterface
{
const DEFAULT_TIMEOUT = 10;

use \DigipolisGent\Robo\Task\Package\Commands\loadCommands;
use \DigipolisGent\Robo\Task\General\loadTasks;
use \DigipolisGent\Robo\Task\General\Common\DigipolisPropertiesAware;
Expand Down Expand Up @@ -350,7 +352,7 @@ protected function preSymlinkTask($worker, AbstractAuth $auth, $remote)
$collection = $this->collectionBuilder();
$collection->taskSsh($worker, $auth)
->remoteDirectory($projectRoot, true)
->timeout(60);
->timeout($this->getTimeoutSetting('presymlink_mirror_dir'));
foreach ($remote['symlinks'] as $symlink) {
list($target, $link) = explode(':', $symlink);
if ($link === $remote['currentdir']) {
Expand Down Expand Up @@ -520,7 +522,7 @@ protected function syncTask(

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

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

$collection->addTask(
$this->taskSsh($sourceHost, $sourceAuth)
->timeout(1800)
->timeout($this->getTimeoutSetting('synctask_rsync'))
->exec($rsync)
);
}
Expand Down Expand Up @@ -677,7 +679,7 @@ protected function backupTask(
. ' ' . ($this->fileBackupSubDirs ? implode(' ', $this->fileBackupSubDirs) : '*');
$collection->taskSsh($worker, $auth)
->remoteDirectory($remote['filesdir'])
->timeout(300)
->timeout($this->getTimeoutSetting('backup_files'))
->exec($filesBackup);
}

Expand All @@ -687,7 +689,7 @@ protected function backupTask(
. '--destination=' . $backupDir . '/' . $dbBackupFile;
$collection->taskSsh($worker, $auth)
->remoteDirectory($currentProjectRoot, true)
->timeout(300)
->timeout($this->getTimeoutSetting('backup_database'))
->exec($dbBackup);
}
return $collection;
Expand Down Expand Up @@ -716,7 +718,7 @@ protected function removeBackupTask(

$collection = $this->collectionBuilder();
$collection->taskSsh($worker, $auth)
->timeout(300)
->timeout($this->getTimeoutSetting('remove_backup'))
->exec('rm -rf ' . $backupDir);

return $collection;
Expand Down Expand Up @@ -762,7 +764,7 @@ protected function restoreBackupTask(
$collection
->taskSsh($worker, $auth)
->remoteDirectory($remote['filesdir'], true)
->timeout(300)
->timeout($this->getTimeoutSetting('restore_files_backup'))
->exec('tar -xkzf ' . $backupDir . '/' . $filesBackupFile);
}

Expand All @@ -774,7 +776,7 @@ protected function restoreBackupTask(
$collection
->taskSsh($worker, $auth)
->remoteDirectory($currentProjectRoot, true)
->timeout(60)
->timeout($this->getTimeoutSetting('restore_db_backup'))
->exec($dbRestore);
}
return $collection;
Expand Down Expand Up @@ -816,7 +818,7 @@ protected function preRestoreBackupTask(
return $this->taskSsh($worker, $auth)
->remoteDirectory($remote['filesdir'], true)
// Files dir can be pretty big on large sites.
->timeout(300)
->timeout($this->getTimeoutSetting('pre_restore_remove_files'))
->exec($removeFiles);
}

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

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

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

$rsync = $this->taskRsync()
->rawArg('--rsh "ssh -o StrictHostKeyChecking=no -i `readlink -e ' . $keyFile . '`"')
->rawArg('--rsh "ssh -o StrictHostKeyChecking=no -i ' . $this->realpath($keyFile) . '"')
->fromHost($host)
->fromUser($user)
->fromPath($remote['filesdir'] . '/' . $dir)
Expand Down Expand Up @@ -1314,4 +1316,79 @@ protected function getLocalSettings($app = null, $timestamp = null)
);
return $this->tokenReplace($this->getConfig()->get('local'), $replacements) + $defaults;
}

/**
* Timeouts can be overwritten in properties.yml under the `timeout` key.
*
* @param string $setting
*
* @return int
*/
protected function getTimeoutSetting($setting)
{
$timeoutSettings = $this->getTimeoutSettings();
return isset($timeoutSettings[$setting]) ? $timeoutSettings[$setting] : static::DEFAULT_TIMEOUT;
}

protected function getTimeoutSettings()
{
$this->readProperties();
return $this->getConfig()->get('timeouts', []) + $this->getDefaultTimeoutSettings();
}

protected function getDefaultTimeoutSettings()
{
return [
'presymlink_mirror_dir' => 60,
'synctask_rsync' => 1800,
'backup_files' => 300,
'backup_database' => 300,
'remove_backup' => 300,
'restore_files_backup' => 300,
'restore_db_backup' => 60,
'pre_restore_remove_files' => 300,
'clean_dir' => 30,
];
}

/**
* PHP's realpath can't handle tilde (~), so we have to write a wrapper
* for it.
*/
protected function realpath($path)
{
$realpath = $path;
if (strpos($realpath, '~') === 0 && ($homedir = $this->getUserHomeDir())) {
$realpath = $homedir . substr($realpath, 1);
}
$realpath = realpath($realpath);

if ($realpath === false) {
throw new \Exception(sprintf('Could not determine real path of %s.', $path));
}

return $realpath;
}

/**
* Get the home directory for the current user.
*/
protected function getUserHomeDir()
{
// getenv('HOME') isn't set on Windows.
$home = getenv('HOME');
if (!empty($home)) {
// home should never end with a trailing slash.
return rtrim($home, '/');
}
if (!empty($_SERVER['HOMEDRIVE']) && !empty($_SERVER['HOMEPATH'])) {
// home on windows
$home = $_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH'];
// If HOMEPATH is a root directory the path can end with a slash.
// Make sure that doesn't happen.
return rtrim($home, '\\/');
}

throw new \Exception('Could not determine the current user\'s home directory.');
}
}

0 comments on commit 7f0a0d6

Please sign in to comment.