magallanes/src/Task/BuiltIn/Deploy/Tar/CopyTask.php

80 lines
2.4 KiB
PHP
Raw Normal View History

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\Tar;
2016-12-31 07:52:25 +01:00
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;
/**
* Tar Task - Copy Tar
2016-12-31 07:52:25 +01:00
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class CopyTask extends AbstractTask
{
2022-04-10 06:20:03 +02:00
public function getName(): string
2016-12-31 07:52:25 +01:00
{
return 'deploy/tar/copy';
2016-12-31 07:52:25 +01:00
}
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 Tar';
2016-12-31 07:52:25 +01:00
}
2022-04-10 06:20:03 +02:00
public function execute(): bool
2016-12-31 07:52:25 +01:00
{
2017-01-12 02:22:21 +01:00
if (!$this->runtime->getEnvOption('releases', false)) {
2017-01-06 02:14:35 +01:00
throw new ErrorException('This task is only available with releases enabled', 40);
2016-12-31 07:52:25 +01:00
}
2017-01-12 02:22:21 +01:00
$user = $this->runtime->getEnvOption('user', $this->runtime->getCurrentUser());
$host = $this->runtime->getHostName();
$sshConfig = $sshConfig = $this->runtime->getSSHConfig();
2017-01-12 02:22:21 +01:00
$hostPath = rtrim($this->runtime->getEnvOption('host_path'), '/');
2016-12-31 07:52:25 +01:00
$currentReleaseId = $this->runtime->getReleaseId();
2017-07-22 22:14:36 +02:00
$tarPath = $this->runtime->getEnvOption('tar_extract_path', 'tar');
2017-01-15 02:27:52 +01:00
$flags = $this->runtime->getEnvOption('tar_extract', 'xfzop');
2016-12-31 07:52:25 +01:00
$targetDir = sprintf('%s/releases/%s', $hostPath, $currentReleaseId);
$tarLocal = $this->runtime->getVar('tar_local');
$tarRemote = basename($tarLocal);
2016-12-31 07:52:25 +01:00
2022-04-10 06:20:03 +02:00
$cmdCopy = sprintf(
'scp -P %d %s %s %s@%s:%s/%s',
$sshConfig['port'],
$sshConfig['flags'],
$tarLocal,
$user,
$host,
$targetDir,
$tarRemote
);
2016-12-31 07:52:25 +01:00
/** @var Process $process */
2022-04-10 06:20:03 +02:00
$process = $this->runtime->runLocalCommand($cmdCopy, intval($sshConfig['timeout']));
2016-12-31 07:52:25 +01:00
if ($process->isSuccessful()) {
2017-07-22 22:14:36 +02:00
$cmdUnTar = sprintf('cd %s && %s %s %s', $targetDir, $tarPath, $flags, $tarRemote);
$process = $this->runtime->runRemoteCommand($cmdUnTar, false, 600);
2016-12-31 07:52:25 +01:00
if ($process->isSuccessful()) {
$cmdDelete = sprintf('rm %s/%s', $targetDir, $tarRemote);
2016-12-31 07:52:25 +01:00
$process = $this->runtime->runRemoteCommand($cmdDelete, false);
2017-01-06 17:10:37 +01:00
return $process->isSuccessful();
2016-12-31 07:52:25 +01:00
}
}
return false;
}
}