2016-12-31 07:52:25 +01:00
|
|
|
<?php
|
2022-04-10 06:20:03 +02:00
|
|
|
|
2016-12-31 07:52:25 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the Magallanes package.
|
|
|
|
*
|
|
|
|
* (c) Andrés Montañez <andres@andresmontanez.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Mage\Task\BuiltIn\Deploy;
|
|
|
|
|
2017-01-07 05:53:57 +01:00
|
|
|
use Mage\Task\Exception\ErrorException;
|
2016-12-31 07:52:25 +01:00
|
|
|
use Symfony\Component\Process\Process;
|
|
|
|
use Mage\Task\AbstractTask;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rsync Task - Copy files with Rsync
|
|
|
|
*
|
|
|
|
* @author Andrés Montañez <andresmontanez@gmail.com>
|
|
|
|
*/
|
|
|
|
class RsyncTask extends AbstractTask
|
|
|
|
{
|
2022-04-10 06:20:03 +02:00
|
|
|
public function getName(): string
|
2016-12-31 07:52:25 +01:00
|
|
|
{
|
|
|
|
return 'deploy/rsync';
|
|
|
|
}
|
|
|
|
|
2022-04-10 06:20:03 +02:00
|
|
|
public function getDescription(): string
|
2016-12-31 07:52:25 +01:00
|
|
|
{
|
|
|
|
return '[Deploy] Copying files with Rsync';
|
|
|
|
}
|
|
|
|
|
2022-04-10 06:20:03 +02:00
|
|
|
public function execute(): bool
|
2016-12-31 07:52:25 +01:00
|
|
|
{
|
2017-01-15 02:27:52 +01:00
|
|
|
$flags = $this->runtime->getEnvOption('rsync', '-avz');
|
2017-01-03 04:34:12 +01:00
|
|
|
$sshConfig = $this->runtime->getSSHConfig();
|
2017-01-12 02:22:21 +01:00
|
|
|
$user = $this->runtime->getEnvOption('user', $this->runtime->getCurrentUser());
|
2018-03-29 22:33:20 +02:00
|
|
|
$host = $this->runtime->getHostName();
|
2017-01-12 02:22:21 +01:00
|
|
|
$hostPath = rtrim($this->runtime->getEnvOption('host_path'), '/');
|
2016-12-31 07:52:25 +01:00
|
|
|
$targetDir = rtrim($hostPath, '/');
|
|
|
|
|
2017-01-12 02:22:21 +01:00
|
|
|
if ($this->runtime->getEnvOption('releases', false)) {
|
2017-01-15 02:36:15 +01:00
|
|
|
throw new ErrorException('Can\'t be used with Releases, use "deploy/tar/copy"');
|
2016-12-31 07:52:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$excludes = $this->getExcludes();
|
2017-03-14 15:17:34 +01:00
|
|
|
$from = $this->runtime->getEnvOption('from', './');
|
2022-04-10 06:20:03 +02:00
|
|
|
$cmdRsync = sprintf(
|
|
|
|
'rsync -e "ssh -p %d %s" %s %s %s %s@%s:%s',
|
|
|
|
$sshConfig['port'],
|
|
|
|
$sshConfig['flags'],
|
|
|
|
$flags,
|
|
|
|
$excludes,
|
|
|
|
$from,
|
|
|
|
$user,
|
|
|
|
$host,
|
|
|
|
$targetDir
|
|
|
|
);
|
2016-12-31 07:52:25 +01:00
|
|
|
|
|
|
|
/** @var Process $process */
|
2022-04-10 06:20:03 +02:00
|
|
|
$process = $this->runtime->runLocalCommand($cmdRsync, 0);
|
2016-12-31 07:52:25 +01:00
|
|
|
return $process->isSuccessful();
|
|
|
|
}
|
|
|
|
|
2022-04-10 06:20:03 +02:00
|
|
|
protected function getExcludes(): string
|
2016-12-31 07:52:25 +01:00
|
|
|
{
|
2017-04-14 21:29:22 +02:00
|
|
|
$excludes = $this->runtime->getMergedOption('exclude', []);
|
2017-02-05 00:26:56 +01:00
|
|
|
$excludes = array_merge(['.git'], array_filter($excludes));
|
2016-12-31 07:52:25 +01:00
|
|
|
|
|
|
|
foreach ($excludes as &$exclude) {
|
|
|
|
$exclude = '--exclude=' . $exclude;
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode(' ', $excludes);
|
|
|
|
}
|
|
|
|
}
|