magallanes/Mage/Task/BuiltIn/Deployment/Strategy/TarGzTask.php

143 lines
5.5 KiB
PHP
Raw Normal View History

<?php
2013-11-05 17:12:09 +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\Deployment\Strategy;
use Mage\Task\Releases\IsReleaseAware;
/**
* Task for Sync the Local Code to the Remote Hosts via Tar GZ
*
* @author Andrés Montañez <andres@andresmontanez.com>
*/
2014-06-12 22:12:30 +02:00
class TarGzTask extends BaseStrategyTaskAbstract implements IsReleaseAware
{
2014-08-06 19:01:39 +02:00
/**
* (non-PHPdoc)
* @see \Mage\Task\AbstractTask::getName()
*/
public function getName()
{
if ($this->getConfig()->release('enabled', false) === true) {
if ($this->getConfig()->getParameter('overrideRelease', false) === true) {
return 'Deploy via TarGz (with Releases override) [built-in]';
2012-03-31 05:33:17 +02:00
} else {
return 'Deploy via TarGz (with Releases) [built-in]';
2012-03-31 05:33:17 +02:00
}
} else {
2014-08-06 19:01:39 +02:00
return 'Deploy via TarGz [built-in]';
}
}
/**
* Syncs the Local Code to the Remote Host
* @see \Mage\Task\AbstractTask::run()
*/
public function run()
{
2014-06-12 22:12:30 +02:00
$this->checkOverrideRelease();
2014-06-12 22:12:30 +02:00
$excludes = $this->getExcludes();
$excludesListFilePath = $this->getConfig()->deployment('excludes_file', '');
// If we are working with releases
$deployToDirectory = $this->getConfig()->deployment('to');
if ($this->getConfig()->release('enabled', false) === true) {
$releasesDirectory = $this->getConfig()->release('directory', 'releases');
$deployToDirectory = rtrim($this->getConfig()->deployment('to'), '/')
2014-08-06 19:01:39 +02:00
. '/' . $releasesDirectory
. '/' . $this->getConfig()->getReleaseId();
$output = null;
2014-08-06 19:01:39 +02:00
$this->runCommandRemote('mkdir -p ' . $deployToDirectory, $output, false);
}
// Create Tar Gz
$localTarGz = tempnam(sys_get_temp_dir(), 'mage');
$remoteTarGz = basename($localTarGz);
2013-11-25 11:01:22 +01:00
$excludeCmd = '';
foreach ($excludes as $excludeFile) {
if (strpos($excludeFile, '*') !== false) {
$excludeFile = '"' . $excludeFile . '"';
}
2013-11-25 11:01:22 +01:00
$excludeCmd .= ' --exclude=' . $excludeFile;
}
$excludeFromFileCmd = $this->excludesListFile($excludesListFilePath);
2014-08-07 15:49:44 +02:00
// Strategy Flags
$strategyFlags = $this->getConfig()->deployment('strategy_flags', $this->getConfig()->general('strategy_flags', array()));
if (isset($strategyFlags['targz']) && isset($strategyFlags['targz']['create'])) {
$strategyFlags = $strategyFlags['targz']['create'];
} else {
$strategyFlags = '';
}
// remove h option only if dump-symlinks is allowed in the release config part
$dumpSymlinks = $this->getConfig()->release('dump-symlinks') ? '' : 'h';
$command = 'tar cfz'. $dumpSymlinks . $strategyFlags . ' ' . $localTarGz . '.tar.gz ' . $excludeCmd . $excludeFromFileCmd . ' -C ' . $this->getConfig()->deployment('from') . ' .';
2014-08-06 19:01:39 +02:00
$result = $this->runCommandLocal($command);
2014-08-07 15:49:44 +02:00
// Strategy Flags
$strategyFlags = $this->getConfig()->deployment('strategy_flags', $this->getConfig()->general('strategy_flags', array()));
if (isset($strategyFlags['targz']) && isset($strategyFlags['targz']['exctract'])) {
$strategyFlags = $strategyFlags['targz']['exctract'];
} else {
$strategyFlags = '';
}
2014-08-06 19:01:39 +02:00
// Copy Tar Gz to Remote Host
$command = 'scp ' . $strategyFlags . ' ' . $this->getConfig()->getHostIdentityFileOption()
. $this->getConfig()->getConnectTimeoutOption() . '-P ' . $this->getConfig()->getHostPort()
. " -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "
. ' ' . $localTarGz . '.tar.gz '
. $this->getConfig()->deployment('user') . '@' . $this->getConfig()->getHostName() . ':'
. $deployToDirectory;
2014-08-06 19:01:39 +02:00
$result = $this->runCommandLocal($command) && $result;
2014-08-07 15:49:44 +02:00
// Strategy Flags
$strategyFlags = $this->getConfig()->deployment('strategy_flags', $this->getConfig()->general('strategy_flags', array()));
if (isset($strategyFlags['targz']) && isset($strategyFlags['targz']['scp'])) {
$strategyFlags = $strategyFlags['targz']['scp'];
} else {
$strategyFlags = '';
}
// Extract Tar Gz
2014-08-07 15:49:44 +02:00
$command = $this->getReleasesAwareCommand('tar xfz' . $strategyFlags . ' ' . $remoteTarGz . '.tar.gz');
$result = $this->runCommandRemote($command) && $result;
// Delete Tar Gz from Remote Host
$command = $this->getReleasesAwareCommand('rm -f ' . $remoteTarGz . '.tar.gz');
$result = $this->runCommandRemote($command) && $result;
// Delete Tar Gz from Local
$command = 'rm -f ' . $localTarGz . ' ' . $localTarGz . '.tar.gz';
$result = $this->runCommandLocal($command) && $result;
2013-07-07 01:46:29 +02:00
return $result;
}
/**
2014-11-01 21:15:39 +01:00
* Generates the Exclude from file for rsync
* @param string $excludesFile
* @return string
*/
2014-11-01 21:15:39 +01:00
protected function excludesListFile($excludesFile)
{
$excludesListFileRsync = '';
2015-04-12 21:13:06 +02:00
if (!empty($excludesFile) && file_exists($excludesFile) && is_file($excludesFile) && is_readable($excludesFile)) {
2014-11-01 21:15:39 +01:00
$excludesListFileRsync = ' --exclude-from=' . $excludesFile;
}
return $excludesListFileRsync;
}
2014-07-16 17:06:00 +02:00
}