[Nostromo] Initial Commit

This commit is contained in:
Andrés Montañez
2016-12-31 03:52:25 -03:00
commit 233bd58d48
39 changed files with 3169 additions and 0 deletions
@@ -0,0 +1,52 @@
<?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\TarGz;
use Mage\Runtime\Exception\DeploymentException;
use Symfony\Component\Process\Process;
use Mage\Task\AbstractTask;
/**
* TarGz Task - Delete temporal Tar
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class CleanupTask extends AbstractTask
{
public function getName()
{
return 'deploy/targz/cleanup';
}
public function getDescription()
{
return '[Deploy] Cleanup TarGZ file';
}
public function execute()
{
if (!$this->runtime->getEnvironmentConfig('releases', false)) {
throw new DeploymentException('This task is only available with releases enabled', 400);
}
$tarGzLocal = $this->runtime->getVar('targz_local');
$cmdDeleteTarGz = sprintf('rm %s', $tarGzLocal);
/** @var Process $process */
$process = $this->runtime->runLocalCommand($cmdDeleteTarGz);
if ($process->isSuccessful()) {
return true;
}
return false;
}
}
@@ -0,0 +1,69 @@
<?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\TarGz;
use Mage\Runtime\Exception\DeploymentException;
use Symfony\Component\Process\Process;
use Mage\Task\AbstractTask;
/**
* TarGz Task - Copy Tar
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class CopyTask extends AbstractTask
{
public function getName()
{
return 'deploy/targz/copy';
}
public function getDescription()
{
return '[Deploy] Copying files with TarGZ';
}
public function execute()
{
if (!$this->runtime->getEnvironmentConfig('releases', false)) {
throw new DeploymentException('This task is only available with releases enabled', 400);
}
$user = $this->runtime->getEnvironmentConfig('user');
$host = $this->runtime->getWorkingHost();
$scpFlags = $this->runtime->getEnvironmentConfig('scp', '-P 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no');
$hostPath = rtrim($this->runtime->getEnvironmentConfig('host_path'), '/');
$currentReleaseId = $this->runtime->getReleaseId();
$targetDir = sprintf('%s/releases/%s', $hostPath, $currentReleaseId);
$tarGzLocal = $this->runtime->getVar('targz_local');
$tarGzRemote = basename($tarGzLocal);
$cmdCopy = sprintf('scp %s %s %s@%s:%s/%s', $scpFlags, $tarGzLocal, $user, $host, $targetDir, $tarGzRemote);
/** @var Process $process */
$process = $this->runtime->runLocalCommand($cmdCopy, 300);
if ($process->isSuccessful()) {
$cmdUntar = sprintf('cd %s && tar xfz %s', $targetDir, $tarGzRemote);
$process = $this->runtime->runRemoteCommand($cmdUntar, false, 600);
if ($process->isSuccessful()) {
$cmdDelete = sprintf('rm %s/%s', $targetDir, $tarGzRemote);
$process = $this->runtime->runRemoteCommand($cmdDelete, false);
if ($process->isSuccessful()) {
return true;
}
}
}
return false;
}
}
@@ -0,0 +1,62 @@
<?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\TarGz;
use Mage\Runtime\Exception\DeploymentException;
use Symfony\Component\Process\Process;
use Mage\Task\AbstractTask;
/**
* TarGz Task - Create temporal Tar
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class PrepareTask extends AbstractTask
{
public function getName()
{
return 'deploy/targz/prepare';
}
public function getDescription()
{
return '[Deploy] Preparing TarGz file';
}
public function execute()
{
if (!$this->runtime->getEnvironmentConfig('releases', false)) {
throw new DeploymentException('This task is only available with releases enabled', 400);
}
$tarGzLocal = tempnam(sys_get_temp_dir(), 'mage');
$this->runtime->setVar('targz_local', $tarGzLocal);
$excludes = $this->getExcludes();
$cmdTarGz = sprintf('tar cfz %s %s ./', $tarGzLocal, $excludes);
/** @var Process $process */
$process = $this->runtime->runLocalCommand($cmdTarGz, 300);
return $process->isSuccessful();
}
protected function getExcludes()
{
$excludes = $this->runtime->getEnvironmentConfig('exclude', []);
$excludes = array_merge(['.git'], $excludes);
foreach ($excludes as &$exclude) {
$exclude = '--exclude=' . $exclude;
}
return implode(' ', $excludes);
}
}