Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  Fix tests.
  Improve error message on timeout.
  Fix tests.
  Fix database restore.
  Fix for filesystems.
  Return command results.
  Fix call to undefined function.
  Fix tests.
  Bugfixes for remote directory & databasecommand.
  Fix docblock.
  Add option to change to the physical remote directory, ignoring symlinks.
  Improve docblock.
  Added typehinting.
  Add gordalina/cachetool to require-dev.
  Fix tests.
  Fix test names.
  Fix typo in traitname.
  Fix code style issues.
  Fix code style issues.
  Added opcache & cleandir tasks & commands.
  • Loading branch information
Jelle-S committed Feb 24, 2017
2 parents 5f11957 + 1ec4368 commit d94df44
Show file tree
Hide file tree
Showing 22 changed files with 809 additions and 68 deletions.
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
},
"require-dev": {
"phpunit/phpunit": "~4.4",
"codeclimate/php-test-reporter": "dev-master"
"codeclimate/php-test-reporter": "dev-master",
"gordalina/cachetool": "^2.1"
},
"autoload": {
"psr-4": {
Expand All @@ -27,6 +28,9 @@
"scripts": {
"test": "robo --ansi test"
},
"suggest": {
"gordalina/cachetool": "Required if the clear OPcache task is used."
},
"license": "MIT",
"authors": [
{
Expand Down
13 changes: 11 additions & 2 deletions src/BackupManager/Compressors/TarCompressor.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,24 @@ public function handles($type)
*/
public function getCompressCommandLine($inputPath)
{
return 'tar -zcf ' . escapeshellarg($inputPath);
return 'tar -zcf '
. escapeshellarg($this->getCompressedPath($inputPath))
. ' --directory=' . escapeshellarg(dirname($inputPath))
. ' ' . escapeshellarg(basename($inputPath))
. ' && rm -rf ' . escapeshellarg($inputPath);
}

/**
* {@inheritdoc}
*/
public function getDecompressCommandLine($outputPath)
{
return 'tar -zxf ' . escapeshellarg($outputPath);
return 'tar -zxf '
. escapeshellarg($outputPath)
. ' --directory=' . escapeshellarg(dirname($outputPath))
. ' --transform=' . escapeshellarg('s,.*,' . $this->getDecompressedPath(basename($outputPath)) . ',')
. ' && rm -rf ' . escapeshellarg($outputPath);

}

/**
Expand Down
15 changes: 15 additions & 0 deletions src/BackupManager/Databases/MysqlDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,21 @@ public function getDumpCommandLine($outputPath)
);
}

/**
* {@inheritdoc}
*/
public function getRestoreCommandLine($inputPath)
{
return sprintf('mysql --host=%s --port=%s --user=%s --password=%s %s -e "source %s"',
escapeshellarg($this->config['host']),
escapeshellarg($this->config['port']),
escapeshellarg($this->config['user']),
escapeshellarg($this->config['pass']),
escapeshellarg($this->config['database']),
$inputPath
);
}

/**
* Get the credential options for the command.
*
Expand Down
2 changes: 1 addition & 1 deletion src/BackupManager/Factory/BackupManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ public static function create($filesystemConfig, $dbConfig)
// Add all default compressors.
$compressorProvider = CompressorProviderFactory::create();

return new BackupManagerAdapter(Manager($filesystemProvider, $databaseProvider, $compressorProvider));
return new BackupManagerAdapter(new Manager($filesystemProvider, $databaseProvider, $compressorProvider));
}
}
81 changes: 81 additions & 0 deletions src/ClearOpCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace DigipolisGent\Robo\Task\Deploy;

use Robo\Contract\CommandInterface;
use Robo\Result;
use Robo\Task\BaseTask;

class ClearOpCache extends BaseTask implements CommandInterface
{
use \Robo\Common\ExecCommand;

const ENV_FCGI = 'fcgi';
const ENV_CLI = 'cli';

/**
* The host type (cli or fcgi).
*
* @var string
*/
protected $environment;

/**
* The fcgi host (if the environment is fcgi).
*
* @var string
*/
protected $host;

/**
* Creates a new ClearOpCache task.
*
* @param string $environment
* One of the ClearOpCache::ENV_* constants.
* @param string $host
* If the environment is FCGI, the host (path to socket or ip:port).
*/
public function __construct($environment = self::ENV_FCGI, $host = null)
{
$this->environment = $environment;
$this->host = $host;
}

/**
* {@inheritdoc}
*/
public function run()
{
if (!class_exists('\\CacheTool\\CacheTool')) {
return Result::errorMissingPackage($this, '\\CacheTool\\CacheTool', 'gordalina/cachetool');
}

$adapter = null;
switch ($this->environment) {
case static::ENV_CLI:
$adapter = new \CacheTool\Adapter\Cli();
break;

case static::ENV_FCGI:
default:
$adapter = new \CacheTool\Adapter\FastCGI($this->host);
break;
}
$cachetool = \CacheTool\CacheTool::factory($adapter);
$cachetool->opcache_reset();
return Result::success($this);
}

/**
* {@inheritdoc}
*/
public function getCommand()
{
$tool = $this->findExecutable('cachetool');
$cmd = $tool . ' opcache:reset --' . $this->environment;
if ($this->environment === static::ENV_FCGI && $this->host) {
$cmd .= '=' . $this->host;
}
return $cmd;
}
}
28 changes: 28 additions & 0 deletions src/Commands/ClearOpCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace DigipolisGent\Robo\Task\Deploy\Commands;

use DigipolisGent\Robo\Task\Deploy\ClearOpCache as ClearOpCacheTask;

trait ClearOpCache
{

use \DigipolisGent\Robo\Task\Deploy\Traits\ClearOpCacheTrait;

/**
* Command digipolis:database-backup.
*
* @param string $environment
* The environment.
* @param array $opts
* The command options.
*/
public function digipolisClearOpCache(
$environment = ClearOpCacheTask::ENV_FCGI,
$opts = [
'host' => null,
]
) {
$this->taskClearOpCache($environment, $opts['host'])->run();
}
}
17 changes: 15 additions & 2 deletions src/Commands/DatabaseBackup.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,29 @@ trait DatabaseBackup
public function digipolisDatabaseBackup($database = 'default', $opts = [
'file-system-config|fsconf' => null,
'database-config|dbconf' => null,
'compression|c' => 'tar',
'compression|c' => 'gzip',
'destination|d' => null,
'destination-type|dtype' => 'local',
'drupal' => false,
])
{
if (is_callable([$this, 'readProperties']))
{
$this->readProperties();
}
$destination = is_null($opts['destination'])
? realpath(getcwd()) . '/project.tar.gz'
: $opts['destination'];
$this->createDbTask('taskDatabaseBackup', $database, $opts)
if (is_null($opts['file-system-config'])) {
$opts['file-system-config'] = [
$opts['destination-type'] => [
'type' => ucfirst($opts['destination-type']),
'root' => realpath(dirname($destination)),
],
];
$destination = basename($destination);
}
return $this->createDbTask('taskDatabaseBackup', $database, $opts)
->destination($destination, $opts['destination-type'])
->run();
}
Expand Down
17 changes: 15 additions & 2 deletions src/Commands/DatabaseRestore.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,29 @@ trait DatabaseRestore
public function digipolisDatabaseRestore($database = 'default', $opts = [
'file-system-config|fsconf' => null,
'database-config|dbconf' => null,
'compression|c' => 'tar',
'compression|c' => 'gzip',
'source|s' => null,
'source-type|stype' => 'local',
'drupal' => false,
])
{
if (is_callable([$this, 'readProperties']))
{
$this->readProperties();
}
$source = is_null($opts['source'])
? realpath(getcwd()) . '/project.tar.gz'
: $opts['source'];
$this->createDbTask('taskDatabaseRestore', $database, $opts)
if (is_null($opts['file-system-config'])) {
$opts['file-system-config'] = [
$opts['source-type'] => [
'type' => ucfirst($opts['source-type']),
'root' => realpath(dirname($source)),
],
];
$source = basename($source);
}
return $this->createDbTask('taskDatabaseRestore', $database, $opts)
->source($source, $opts['source-type'])
->run();
}
Expand Down
37 changes: 37 additions & 0 deletions src/Commands/PartialCleanDirs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace DigipolisGent\Robo\Task\Deploy\Commands;

use DigipolisGent\Robo\Task\Deploy\PartialCleanDirs as PartialCleanDirsTask;

trait PartialCleanDirs
{

use \DigipolisGent\Robo\Task\Deploy\Traits\PartialCleanDirsTrait;

/**
* Partially clean directories.
*
* @param array $dirs
* Comma separated list of directories to clean. Each dir is optionally
* followed by a colon and the number of items to preserve in that dir.
* Defaults to 5 items.
* @param array $opts
* The command options.
*/
public function digipolisCleanDir($dirs, $opts = ['sort' => PartialCleanDirsTask::SORT_NAME])
{
$dirsArg = array();
foreach (array_map('trim', explode(',', $dirs)) as $dir) {
$dirParts = explode(':', $dir);
if (count($dirParts) > 1) {
$dirsArg[$dirParts[0]] = $dirParts[1];
continue;
}
$dirsArg[] = $dirParts[0];
}
$this->taskPartialCleanDirs($dirsArg)
->sortBy($opts['sort'])
->run();
}
}
2 changes: 2 additions & 0 deletions src/Commands/loadCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ trait loadCommands
DatabaseBackup::createDbTask insteadof DatabaseRestore;
}
use PushPackage;
use ClearOpCache;
use PartialCleanDirs;
}
8 changes: 5 additions & 3 deletions src/Common/DatabaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@ protected function defaultDbConfig($drupal = false)

protected function parseDrupalDbConfig()
{
$webDir = $this->getConfig()->get('digipolis.project.web', false);
$webDir = $this->getConfig()->get('digipolis.root.web', false);
if (!$webDir) {
return false;
}

$finder = new \Symfony\Component\Finder\Finder();
$finder->in($webDir . '/sites')->files()->name('settings.php');
foreach ($finder as $settingsFile) {
$site_path = null;
$app_root = null;
include_once $settingsFile->getRealpath();
break;
}
Expand All @@ -57,7 +59,7 @@ protected function parseDrupalDbConfig()
'host' => $config['host'],
'port' => isset($config['port']) ? $config['port'] : '3306',
'user' => $config['username'],
'pass' => $config['pass'],
'pass' => $config['password'],
'database' => $config['database'],
'structureTables' => [
'batch',
Expand Down Expand Up @@ -99,7 +101,7 @@ protected function createDbTask($task, $database, $opts)
: $this->defaultDbConfig($opts['drupal']);

return $this->{$task}($filesystemConfig, $dbConfig)
->compression($opts['comporession'])
->compression($opts['compression'])
->database($database);
}
}
10 changes: 5 additions & 5 deletions src/DatabaseBackup.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,12 @@ public function compression($compression)
*/
public function run()
{
/**
* The backup manager
* @var \DigipolisGent\Robo\Task\Deploy\BackupManager\Adapter\BackupManagerAdapterInterface
*/
$manager = call_user_func([$this->backupManagerFactory, 'create'], $this->filesystemConfig, $this->dbConfig);
try {
/**
* The backup manager
* @var \DigipolisGent\Robo\Task\Deploy\BackupManager\Adapter\BackupManagerAdapterInterface
*/
$manager = call_user_func([$this->backupManagerFactory, 'create'], $this->filesystemConfig, $this->dbConfig);
if (empty($this->destinations)) {
$this->destination(getcwd());
}
Expand Down
10 changes: 5 additions & 5 deletions src/DatabaseRestore.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,12 @@ public function compression($compression)
*/
public function run()
{
/**
* The backup manager
* @var \DigipolisGent\Robo\Task\Deploy\BackupManager\Adapter\BackupManagerAdapterInterface
*/
$manager = call_user_func([$this->backupManagerFactory, 'create'], $this->filesystemConfig, $this->dbConfig);
try {
/**
* The backup manager
* @var \DigipolisGent\Robo\Task\Deploy\BackupManager\Adapter\BackupManagerAdapterInterface
*/
$manager = call_user_func([$this->backupManagerFactory, 'create'], $this->filesystemConfig, $this->dbConfig);
if (!isset($this->sourcePath)) {
$this->source(getcwd());
}
Expand Down
Loading

0 comments on commit d94df44

Please sign in to comment.