magallanes/src/Task/BuiltIn/Deploy/RsyncTask.php

67 lines
1.9 KiB
PHP
Raw Normal View History

2016-12-31 07:52:25 +01:00
<?php
/*
* 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
{
public function getName()
{
return 'deploy/rsync';
}
public function getDescription()
{
return '[Deploy] Copying files with Rsync';
}
public function execute()
{
2017-01-15 02:27:52 +01:00
$flags = $this->runtime->getEnvOption('rsync', '-avz');
$sshConfig = $this->runtime->getSSHConfig();
2017-01-12 02:22:21 +01:00
$user = $this->runtime->getEnvOption('user', $this->runtime->getCurrentUser());
2016-12-31 07:52:25 +01:00
$host = $this->runtime->getWorkingHost();
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-01 07:55:09 +01:00
throw new ErrorException('Can\'t be used with Releases, use "deploy/targz/copy"');
2016-12-31 07:52:25 +01:00
}
$excludes = $this->getExcludes();
$cmdRsync = sprintf('rsync -e "ssh -p %d %s" %s %s ./ %s@%s:%s', $sshConfig['port'], $sshConfig['flags'], $flags, $excludes, $user, $host, $targetDir);
2016-12-31 07:52:25 +01:00
/** @var Process $process */
$process = $this->runtime->runLocalCommand($cmdRsync, 600);
return $process->isSuccessful();
}
protected function getExcludes()
{
2017-01-12 02:22:21 +01:00
$excludes = $this->runtime->getEnvOption('exclude', []);
2016-12-31 07:52:25 +01:00
$excludes = array_merge(['.git'], $excludes);
foreach ($excludes as &$exclude) {
$exclude = '--exclude=' . $exclude;
}
return implode(' ', $excludes);
}
}