mirror of
https://github.com/andres-montanez/Magallanes
synced 2024-06-15 20:15:04 +02:00
Merge pull request #97 from JoeSimsen/master
Allow a release to be zipped
This commit is contained in:
commit
585fda2f50
|
@ -51,200 +51,260 @@ abstract class AbstractTask
|
||||||
* Configuration
|
* Configuration
|
||||||
* @var Config;
|
* @var Config;
|
||||||
*/
|
*/
|
||||||
protected $config = null;
|
protected $config = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Indicates if the Task is running in a Rollback
|
* Indicates if the Task is running in a Rollback
|
||||||
* @var boolean
|
* @var boolean
|
||||||
*/
|
*/
|
||||||
protected $inRollback = false;
|
protected $inRollback = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Indicates the Stage the Task is running ing
|
* Indicates the Stage the Task is running ing
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $stage = null;
|
protected $stage = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extra parameters
|
* Extra parameters
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $parameters = array();
|
protected $parameters = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the Title of the Task
|
* Returns the Title of the Task
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public abstract function getName();
|
public abstract function getName();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Runs the task
|
* Runs the task
|
||||||
*
|
*
|
||||||
* @return boolean
|
* @return boolean
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
* @throws ErrorWithMessageException
|
* @throws ErrorWithMessageException
|
||||||
* @throws SkipException
|
* @throws SkipException
|
||||||
*/
|
*/
|
||||||
public abstract function run();
|
public abstract function run();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Task Constructor
|
* Task Constructor
|
||||||
*
|
*
|
||||||
* @param Config $config
|
* @param Config $config
|
||||||
* @param boolean $inRollback
|
* @param boolean $inRollback
|
||||||
* @param string $stage
|
* @param string $stage
|
||||||
* @param array $parameters
|
* @param array $parameters
|
||||||
*/
|
*/
|
||||||
public final function __construct(Config $config, $inRollback = false, $stage = null, $parameters = array())
|
public final function __construct(Config $config, $inRollback = false, $stage = null, $parameters = array())
|
||||||
{
|
{
|
||||||
$this->config = $config;
|
$this->config = $config;
|
||||||
$this->inRollback = $inRollback;
|
$this->inRollback = $inRollback;
|
||||||
$this->stage = $stage;
|
$this->stage = $stage;
|
||||||
$this->parameters = $parameters;
|
$this->parameters = $parameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Indicates if the Task is running in a Rollback operation
|
* Indicates if the Task is running in a Rollback operation
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public function inRollback()
|
public function inRollback()
|
||||||
{
|
{
|
||||||
return $this->inRollback;
|
return $this->inRollback;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the Stage of the Deployment:
|
* Gets the Stage of the Deployment:
|
||||||
* - pre-deploy
|
* - pre-deploy
|
||||||
* - deploy
|
* - deploy
|
||||||
* - post-deploy
|
* - post-deploy
|
||||||
* - post-release
|
* - post-release
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getStage()
|
public function getStage()
|
||||||
{
|
{
|
||||||
return $this->stage;
|
return $this->stage;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the Configuration
|
* Gets the Configuration
|
||||||
* @return Config;
|
* @return Config;
|
||||||
*/
|
*/
|
||||||
public function getConfig()
|
public function getConfig()
|
||||||
{
|
{
|
||||||
return $this->config;
|
return $this->config;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the Task, optional to implement
|
* Initializes the Task, optional to implement
|
||||||
*/
|
*/
|
||||||
public function init()
|
public function init()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a Parameter, or a default if not found
|
* Returns a Parameter, or a default if not found
|
||||||
*
|
*
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param mixed $default
|
* @param mixed $default
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function getParameter($name, $default = null)
|
public function getParameter($name, $default = null)
|
||||||
{
|
{
|
||||||
return $this->getConfig()->getParameter($name, $default, $this->getParameters());
|
return $this->getConfig()->getParameter($name, $default, $this->getParameters());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function getParameters()
|
protected function getParameters()
|
||||||
{
|
{
|
||||||
return $this->parameters;
|
return $this->parameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Runs a Shell Command Localy
|
* Runs a Shell Command Localy
|
||||||
* @param string $command
|
* @param string $command
|
||||||
* @param string $output
|
* @param string $output
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
protected final function runCommandLocal($command, &$output = null)
|
protected final function runCommandLocal($command, &$output = null)
|
||||||
{
|
{
|
||||||
return Console::executeCommand($command, $output);
|
return Console::executeCommand($command, $output);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Runs a Shell Command on the Remote Host
|
* Runs a Shell Command on the Remote Host
|
||||||
* @param string $command
|
* @param string $command
|
||||||
* @param string $output
|
* @param string $output
|
||||||
* @param boolean $cdToDirectoryFirst
|
* @param boolean $cdToDirectoryFirst
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
protected final function runCommandRemote($command, &$output = null, $cdToDirectoryFirst = true)
|
protected final function runCommandRemote($command, &$output = null, $cdToDirectoryFirst = true)
|
||||||
{
|
{
|
||||||
if ($this->getConfig()->release('enabled', false) == true) {
|
if ($this->getConfig()->release('enabled', false) == true) {
|
||||||
if ($this instanceOf IsReleaseAware) {
|
if ($this instanceOf IsReleaseAware) {
|
||||||
$releasesDirectory = '';
|
$releasesDirectory = '';
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
$releasesDirectory = '/'
|
$releasesDirectory = '/'
|
||||||
. $this->getConfig()->release('directory', 'releases')
|
. $this->getConfig()->release('directory', 'releases')
|
||||||
. '/'
|
. '/'
|
||||||
. $this->getConfig()->getReleaseId();
|
. $this->getConfig()->getReleaseId();
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
$releasesDirectory = '';
|
$releasesDirectory = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
// if general.yml includes "ssy_needs_tty: true", then add "-t" to the ssh command
|
// if general.yml includes "ssy_needs_tty: true", then add "-t" to the ssh command
|
||||||
$needs_tty = ($this->getConfig()->general('ssh_needs_tty',false) ? '-t' : '');
|
$needs_tty = ($this->getConfig()->general('ssh_needs_tty',false) ? '-t' : '');
|
||||||
|
|
||||||
$localCommand = 'ssh ' . $this->getConfig()->getHostIdentityFileOption() . $needs_tty . ' -p ' . $this->getConfig()->getHostPort() . ' '
|
$localCommand = 'ssh ' . $this->getConfig()->getHostIdentityFileOption() . $needs_tty . ' -p ' . $this->getConfig()->getHostPort() . ' '
|
||||||
. '-q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no '
|
. '-q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no '
|
||||||
. $this->getConfig()->deployment('user') . '@' . $this->getConfig()->getHostName();
|
. $this->getConfig()->deployment('user') . '@' . $this->getConfig()->getHostName();
|
||||||
|
|
||||||
$remoteCommand = str_replace('"', '\"', $command);
|
$remoteCommand = str_replace('"', '\"', $command);
|
||||||
if($cdToDirectoryFirst){
|
if($cdToDirectoryFirst){
|
||||||
$remoteCommand = 'cd ' . rtrim($this->getConfig()->deployment('to'), '/') . $releasesDirectory . ' && ' . $remoteCommand;
|
$remoteCommand = 'cd ' . rtrim($this->getConfig()->deployment('to'), '/') . $releasesDirectory . ' && ' . $remoteCommand;
|
||||||
}
|
}
|
||||||
$localCommand .= ' ' . '"sh -c \"' . $remoteCommand . '\""';
|
$localCommand .= ' ' . '"sh -c \"' . $remoteCommand . '\""';
|
||||||
|
|
||||||
Console::log('Run remote command ' . $remoteCommand);
|
Console::log('Run remote command ' . $remoteCommand);
|
||||||
|
|
||||||
return $this->runCommandLocal($localCommand, $output);
|
return $this->runCommandLocal($localCommand, $output);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Runs a Shell Command Localy or in the Remote Host based on the Task Stage.
|
* Runs a Shell Command Localy or in the Remote Host based on the Task Stage.
|
||||||
* If the stage is "deploy" then it will be executed in the remote host.
|
* If the stage is "deploy" then it will be executed in the remote host.
|
||||||
* @param string $command
|
* @param string $command
|
||||||
* @param string $output
|
* @param string $output
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
protected final function runCommand($command, &$output = null)
|
protected final function runCommand($command, &$output = null)
|
||||||
{
|
{
|
||||||
if ($this->getStage() == self::STAGE_DEPLOY) {
|
if ($this->getStage() == self::STAGE_DEPLOY) {
|
||||||
return $this->runCommandRemote($command, $output);
|
return $this->runCommandRemote($command, $output);
|
||||||
} else {
|
} else {
|
||||||
return $this->runCommandLocal($command, $output);
|
return $this->runCommandLocal($command, $output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* adds a cd to the needed release if we work with releases.
|
* adds a cd to the needed release if we work with releases.
|
||||||
*
|
*
|
||||||
* @param string $command
|
* @param string $command
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function getReleasesAwareCommand($command)
|
protected function getReleasesAwareCommand($command)
|
||||||
{
|
{
|
||||||
if ($this->getConfig()->release('enabled', false) == true) {
|
if ($this->getConfig()->release('enabled', false) == true) {
|
||||||
$releasesDirectory = $this->getConfig()->release('directory', 'releases');
|
$releasesDirectory = $this->getConfig()->release('directory', 'releases');
|
||||||
|
|
||||||
$deployToDirectory = $releasesDirectory . '/' . $this->getConfig()->getReleaseId();
|
$deployToDirectory = $releasesDirectory . '/' . $this->getConfig()->getReleaseId();
|
||||||
return 'cd ' . $deployToDirectory . ' && ' . $command;
|
return 'cd ' . $deployToDirectory . ' && ' . $command;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $command;
|
return $command;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $releasesDirectory
|
||||||
|
* @param $releaseId
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected function tarRelease($releaseId)
|
||||||
|
{
|
||||||
|
$result = true;
|
||||||
|
// for given release, check if tarred
|
||||||
|
$output = '';
|
||||||
|
$releasesDirectory = $this->getConfig()->release('directory', 'releases');
|
||||||
|
|
||||||
|
$currentReleaseDirectory = $releasesDirectory . '/' . $releaseId;
|
||||||
|
$currentReleaseDirectoryTemp = $currentReleaseDirectory . '_tmp/';
|
||||||
|
$currentRelease = $currentReleaseDirectory . '/' . $releaseId . '.tar.gz';
|
||||||
|
|
||||||
|
$command = 'test -e ' . $currentRelease . ' && echo "true" || echo ""';
|
||||||
|
$this->runCommandRemote($command, $output);
|
||||||
|
|
||||||
|
// if not, do so
|
||||||
|
if (!$output) {
|
||||||
|
$commands = array();
|
||||||
|
$commands[] = 'mv ' . $currentReleaseDirectory . ' ' . $currentReleaseDirectoryTemp;
|
||||||
|
$commands[] = 'mkdir ' . $currentReleaseDirectory;
|
||||||
|
$commands[] = 'tar cfz ' . $currentRelease . ' ' . $currentReleaseDirectoryTemp;
|
||||||
|
$commands[] = 'rm -rf ' . $currentReleaseDirectoryTemp;
|
||||||
|
$command = implode(' && ', $commands);
|
||||||
|
$result = $this->runCommandRemote($command, $output);
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function untarRelease($releaseId)
|
||||||
|
{
|
||||||
|
$result = true;
|
||||||
|
// for given release, check if tarred
|
||||||
|
$output = '';
|
||||||
|
$releasesDirectory = $this->getConfig()->release('directory', 'releases');
|
||||||
|
|
||||||
|
$currentReleaseDirectory = $releasesDirectory . '/' . $releaseId;
|
||||||
|
$currentReleaseDirectoryTemp = $currentReleaseDirectory . '_tmp/';
|
||||||
|
$currentRelease = $currentReleaseDirectory . '/' . $releaseId . '.tar.gz';
|
||||||
|
|
||||||
|
$command = 'test -e ' . $currentRelease . ' && echo "true" || echo ""';
|
||||||
|
$this->runCommandRemote($command, $output);
|
||||||
|
|
||||||
|
// if tarred, untar now
|
||||||
|
if ($output) {
|
||||||
|
$commands = array();
|
||||||
|
$commands[] = 'tar xfz ' . $currentRelease;
|
||||||
|
$commands[] = 'rm -rf ' . $currentReleaseDirectory;
|
||||||
|
$commands[] = 'mv ' .$currentReleaseDirectoryTemp . ' ' . $currentReleaseDirectory;
|
||||||
|
$command = implode(' && ', $commands);
|
||||||
|
$result = $this->runCommandRemote($command, $output);
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,73 +25,82 @@ class ReleaseTask extends AbstractTask implements IsReleaseAware, SkipOnOverride
|
||||||
* (non-PHPdoc)
|
* (non-PHPdoc)
|
||||||
* @see \Mage\Task\AbstractTask::getName()
|
* @see \Mage\Task\AbstractTask::getName()
|
||||||
*/
|
*/
|
||||||
public function getName()
|
public function getName()
|
||||||
{
|
{
|
||||||
return 'Releasing [built-in]';
|
return 'Releasing [built-in]';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Releases a Deployment: points the current symbolic link to the release directory
|
* Releases a Deployment: points the current symbolic link to the release directory
|
||||||
* @see \Mage\Task\AbstractTask::run()
|
* @see \Mage\Task\AbstractTask::run()
|
||||||
*/
|
*/
|
||||||
public function run()
|
public function run()
|
||||||
{
|
{
|
||||||
if ($this->getConfig()->release('enabled', false) == true) {
|
if ($this->getConfig()->release('enabled', false) == true) {
|
||||||
$releasesDirectory = $this->getConfig()->release('directory', 'releases');
|
$releasesDirectory = $this->getConfig()->release('directory', 'releases');
|
||||||
$symlink = $this->getConfig()->release('symlink', 'current');
|
$symlink = $this->getConfig()->release('symlink', 'current');
|
||||||
|
|
||||||
if (substr($symlink, 0, 1) == '/') {
|
if (substr($symlink, 0, 1) == '/') {
|
||||||
$releasesDirectory = rtrim($this->getConfig()->deployment('to'), '/') . '/' . $releasesDirectory;
|
$releasesDirectory = rtrim($this->getConfig()->deployment('to'), '/') . '/' . $releasesDirectory;
|
||||||
}
|
}
|
||||||
|
|
||||||
$currentCopy = $releasesDirectory . '/' . $this->getConfig()->getReleaseId();
|
$releaseId = $this->getConfig()->getReleaseId();
|
||||||
|
|
||||||
//Check if target user:group is specified
|
if ($this->getConfig()->release('compressreleases', false) == true) {
|
||||||
$userGroup = $this->getConfig()->deployment('owner');
|
// Tar.gz releases
|
||||||
// Fetch the user and group from base directory; defaults usergroup to 33:33
|
$result = $this->tarReleases() && $result;
|
||||||
if(empty($userGroup)){
|
// Untar new release
|
||||||
$user = '33';
|
$result = $this->untarRelease($releaseId) && $result;
|
||||||
$group = '33';
|
}
|
||||||
$directoryInfos = '';
|
|
||||||
// Get raw directory info and parse it in php.
|
|
||||||
// "stat" command don't behave the same on different systems, ls output format also varies
|
|
||||||
// and awk parameters need special care depending on the executing shell
|
|
||||||
$resultFetch = $this->runCommandRemote("ls -ld .", $directoryInfos);
|
|
||||||
if(!empty($directoryInfos)){
|
|
||||||
//uniformize format as it depends on the system deployed on
|
|
||||||
$directoryInfos = trim(str_replace(array(" ", "\t"), ' ', $directoryInfos));
|
|
||||||
$infoArray = explode(' ', $directoryInfos);
|
|
||||||
if(!empty($infoArray[2])) {
|
|
||||||
$user = $infoArray[2];
|
|
||||||
}
|
|
||||||
if(!empty($infoArray[3])) {
|
|
||||||
$group = $infoArray[3];
|
|
||||||
}
|
|
||||||
$userGroup = $user . ':' . $group;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove symlink if exists; create new symlink and change owners
|
$currentCopy = $releasesDirectory . '/' . $releaseId;
|
||||||
$command = 'rm -f ' . $symlink
|
|
||||||
. ' ; '
|
|
||||||
. 'ln -sf ' . $currentCopy . ' ' . $symlink;
|
|
||||||
|
|
||||||
if ($resultFetch && $userGroup != '') {
|
//Check if target user:group is specified
|
||||||
$command .= ' && '
|
$userGroup = $this->getConfig()->deployment('owner');
|
||||||
. 'chown -h ' . $userGroup . ' ' . $symlink
|
// Fetch the user and group from base directory; defaults usergroup to 33:33
|
||||||
. ' && '
|
if(empty($userGroup)){
|
||||||
. 'chown -R ' . $userGroup . ' ' . $currentCopy
|
$user = '33';
|
||||||
. ' && '
|
$group = '33';
|
||||||
. 'chown ' . $userGroup . ' ' . $releasesDirectory;
|
$directoryInfos = '';
|
||||||
}
|
// Get raw directory info and parse it in php.
|
||||||
|
// "stat" command don't behave the same on different systems, ls output format also varies
|
||||||
|
// and awk parameters need special care depending on the executing shell
|
||||||
|
$resultFetch = $this->runCommandRemote("ls -ld .", $directoryInfos);
|
||||||
|
if(!empty($directoryInfos)){
|
||||||
|
//uniformize format as it depends on the system deployed on
|
||||||
|
$directoryInfos = trim(str_replace(array(" ", "\t"), ' ', $directoryInfos));
|
||||||
|
$infoArray = explode(' ', $directoryInfos);
|
||||||
|
if(!empty($infoArray[2])) {
|
||||||
|
$user = $infoArray[2];
|
||||||
|
}
|
||||||
|
if(!empty($infoArray[3])) {
|
||||||
|
$group = $infoArray[3];
|
||||||
|
}
|
||||||
|
$userGroup = $user . ':' . $group;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$result = $this->runCommandRemote($command);
|
// Remove symlink if exists; create new symlink and change owners
|
||||||
|
$command = 'rm -f ' . $symlink
|
||||||
|
. ' ; '
|
||||||
|
. 'ln -sf ' . $currentCopy . ' ' . $symlink;
|
||||||
|
|
||||||
return $result;
|
if ($resultFetch && $userGroup != '') {
|
||||||
|
$command .= ' && '
|
||||||
|
. 'chown -h ' . $userGroup . ' ' . $symlink
|
||||||
|
. ' && '
|
||||||
|
. 'chown -R ' . $userGroup . ' ' . $currentCopy
|
||||||
|
. ' && '
|
||||||
|
. 'chown ' . $userGroup . ' ' . $releasesDirectory;
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
$result = $this->runCommandRemote($command);
|
||||||
return false;
|
|
||||||
}
|
return $result;
|
||||||
}
|
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
|
|
||||||
namespace Mage\Task\BuiltIn\Deployment\Strategy;
|
namespace Mage\Task\BuiltIn\Deployment\Strategy;
|
||||||
|
|
||||||
|
use Mage\Console;
|
||||||
use Mage\Task\BuiltIn\Deployment\Strategy\BaseStrategyTaskAbstract;
|
use Mage\Task\BuiltIn\Deployment\Strategy\BaseStrategyTaskAbstract;
|
||||||
use Mage\Task\Releases\IsReleaseAware;
|
use Mage\Task\Releases\IsReleaseAware;
|
||||||
|
|
||||||
|
@ -67,19 +68,19 @@ class TarGzTask extends BaseStrategyTaskAbstract implements IsReleaseAware
|
||||||
}
|
}
|
||||||
|
|
||||||
$command = 'tar cfz ' . $localTarGz . '.tar.gz ' . $excludeCmd . ' -C ' . $this->getConfig()->deployment('from') . ' .';
|
$command = 'tar cfz ' . $localTarGz . '.tar.gz ' . $excludeCmd . ' -C ' . $this->getConfig()->deployment('from') . ' .';
|
||||||
$result = $this->runCommandLocal($command);
|
$result = $this->runCommandLocal($command);
|
||||||
|
|
||||||
// Copy Tar Gz to Remote Host
|
// Copy Tar Gz to Remote Host
|
||||||
$command = 'scp ' . $this->getConfig()->getHostIdentityFileOption() . '-P ' . $this->getConfig()->getHostPort() . ' ' . $localTarGz . '.tar.gz '
|
$command = 'scp ' . $this->getConfig()->getHostIdentityFileOption() . '-P ' . $this->getConfig()->getHostPort() . ' ' . $localTarGz . '.tar.gz '
|
||||||
. $this->getConfig()->deployment('user') . '@' . $this->getConfig()->getHostName() . ':' . $deployToDirectory;
|
. $this->getConfig()->deployment('user') . '@' . $this->getConfig()->getHostName() . ':' . $deployToDirectory;
|
||||||
$result = $this->runCommandLocal($command) && $result;
|
$result = $this->runCommandLocal($command) && $result;
|
||||||
|
|
||||||
// Extract Tar Gz
|
// Extract Tar Gz
|
||||||
$this->getReleasesAwareCommand('tar xfz ' . $remoteTarGz . '.tar.gz');
|
$command = $this->getReleasesAwareCommand('tar xfz ' . $remoteTarGz . '.tar.gz');
|
||||||
$result = $this->runCommandRemote($command) && $result;
|
$result = $this->runCommandRemote($command) && $result;
|
||||||
|
|
||||||
// Delete Tar Gz from Remote Host
|
// Delete Tar Gz from Remote Host
|
||||||
$this->getReleasesAwareCommand('rm ' . $remoteTarGz . '.tar.gz');
|
$command = $this->getReleasesAwareCommand('rm ' . $remoteTarGz . '.tar.gz');
|
||||||
$result = $this->runCommandRemote($command) && $result;
|
$result = $this->runCommandRemote($command) && $result;
|
||||||
|
|
||||||
// Delete Tar Gz from Local
|
// Delete Tar Gz from Local
|
||||||
|
|
|
@ -27,168 +27,175 @@ class RollbackTask extends AbstractTask implements IsReleaseAware
|
||||||
* The Relase ID to Rollback To
|
* The Relase ID to Rollback To
|
||||||
* @var integer
|
* @var integer
|
||||||
*/
|
*/
|
||||||
protected $release = null;
|
protected $release = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* (non-PHPdoc)
|
* (non-PHPdoc)
|
||||||
* @see \Mage\Task\AbstractTask::getName()
|
* @see \Mage\Task\AbstractTask::getName()
|
||||||
*/
|
*/
|
||||||
public function getName()
|
public function getName()
|
||||||
{
|
{
|
||||||
return 'Rollback release [built-in]';
|
return 'Rollback release [built-in]';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the Release ID to Rollback To
|
* Sets the Release ID to Rollback To
|
||||||
* @param integer $releaseId
|
* @param integer $releaseId
|
||||||
* @return \Mage\Task\BuiltIn\Releases\RollbackTask
|
* @return \Mage\Task\BuiltIn\Releases\RollbackTask
|
||||||
*/
|
*/
|
||||||
public function setRelease($releaseId)
|
public function setRelease($releaseId)
|
||||||
{
|
{
|
||||||
$this->release = $releaseId;
|
$this->release = $releaseId;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the Release ID to Rollback To
|
* Gets the Release ID to Rollback To
|
||||||
* @return integer
|
* @return integer
|
||||||
*/
|
*/
|
||||||
public function getRelease()
|
public function getRelease()
|
||||||
{
|
{
|
||||||
return $this->release;
|
return $this->release;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs a Rollback Operation
|
* Performs a Rollback Operation
|
||||||
* @see \Mage\Task\AbstractTask::run()
|
* @see \Mage\Task\AbstractTask::run()
|
||||||
*/
|
*/
|
||||||
public function run()
|
public function run()
|
||||||
{
|
{
|
||||||
if ($this->getConfig()->release('enabled', false) == true) {
|
if ($this->getConfig()->release('enabled', false) == true) {
|
||||||
$releasesDirectory = $this->getConfig()->release('directory', 'releases');
|
$releasesDirectory = $this->getConfig()->release('directory', 'releases');
|
||||||
$symlink = $this->getConfig()->release('symlink', 'current');
|
$symlink = $this->getConfig()->release('symlink', 'current');
|
||||||
|
|
||||||
$output = '';
|
$output = '';
|
||||||
$result = $this->runCommandRemote('ls -1 ' . $releasesDirectory, $output);
|
$result = $this->runCommandRemote('ls -1 ' . $releasesDirectory, $output);
|
||||||
$releases = ($output == '') ? array() : explode(PHP_EOL, $output);
|
$releases = ($output == '') ? array() : explode(PHP_EOL, $output);
|
||||||
|
|
||||||
if (count($releases) == 0) {
|
if (count($releases) == 0) {
|
||||||
Console::output('Release are not available for <dark_gray>' . $this->getConfig()->getHost() . '</dark_gray> ... <red>FAIL</red>');
|
Console::output('Release are not available for <dark_gray>' . $this->getConfig()->getHost() . '</dark_gray> ... <red>FAIL</red>');
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
rsort($releases);
|
rsort($releases);
|
||||||
|
|
||||||
$releaseIsAvailable = false;
|
$releaseIsAvailable = false;
|
||||||
if ($this->getRelease() == '') {
|
if ($this->getRelease() == '') {
|
||||||
$releaseId = $releases[0];
|
$releaseId = $releases[0];
|
||||||
$releaseIsAvailable = true;
|
$releaseIsAvailable = true;
|
||||||
|
|
||||||
} else if ($this->getRelease() <= 0) {
|
} else if ($this->getRelease() <= 0) {
|
||||||
$index = $this->getRelease() * -1;
|
$index = $this->getRelease() * -1;
|
||||||
if (isset($releases[$index])) {
|
if (isset($releases[$index])) {
|
||||||
$releaseId = $releases[$index];
|
$releaseId = $releases[$index];
|
||||||
$releaseIsAvailable = true;
|
$releaseIsAvailable = true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (in_array($this->getRelease(), $releases)) {
|
if (in_array($this->getRelease(), $releases)) {
|
||||||
$releaseId = $this->getRelease();
|
$releaseId = $this->getRelease();
|
||||||
$releaseIsAvailable = true;
|
$releaseIsAvailable = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$releaseIsAvailable) {
|
if (!$releaseIsAvailable) {
|
||||||
Console::output('Release <dark_gray>' . $this->getRelease() . '</dark_gray> is invalid or unavailable for <dark_gray>' . $this->getConfig()->getHost() . '</dark_gray> ... <red>FAIL</red>');
|
Console::output('Release <dark_gray>' . $this->getRelease() . '</dark_gray> is invalid or unavailable for <dark_gray>' . $this->getConfig()->getHost() . '</dark_gray> ... <red>FAIL</red>');
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
Console::output('Rollback release on <dark_gray>' . $this->getConfig()->getHost() . '</dark_gray>');
|
Console::output('Rollback release on <dark_gray>' . $this->getConfig()->getHost() . '</dark_gray>');
|
||||||
$rollbackTo = $releasesDirectory . '/' . $releaseId;
|
$rollbackTo = $releasesDirectory . '/' . $releaseId;
|
||||||
|
|
||||||
// Tasks
|
// Tasks
|
||||||
$tasks = 1;
|
$tasks = 1;
|
||||||
$completedTasks = 0;
|
$completedTasks = 0;
|
||||||
$tasksToRun = $this->getConfig()->getTasks();
|
$tasksToRun = $this->getConfig()->getTasks();
|
||||||
$this->getConfig()->setReleaseId($releaseId);
|
$this->getConfig()->setReleaseId($releaseId);
|
||||||
|
|
||||||
// Run Deploy Tasks
|
// Run Deploy Tasks
|
||||||
foreach ($tasksToRun as $taskData) {
|
foreach ($tasksToRun as $taskData) {
|
||||||
$task = Factory::get($taskData, $this->getConfig(), true, self::STAGE_DEPLOY);
|
$task = Factory::get($taskData, $this->getConfig(), true, self::STAGE_DEPLOY);
|
||||||
$task->init();
|
$task->init();
|
||||||
Console::output('Running <purple>' . $task->getName() . '</purple> ... ', 2, false);
|
Console::output('Running <purple>' . $task->getName() . '</purple> ... ', 2, false);
|
||||||
|
|
||||||
if ($task instanceOf RollbackAware) {
|
if ($task instanceOf RollbackAware) {
|
||||||
$tasks++;
|
$tasks++;
|
||||||
$result = $task->run();
|
$result = $task->run();
|
||||||
|
|
||||||
if ($result == true) {
|
|
||||||
Console::output('<green>OK</green>', 0);
|
|
||||||
$completedTasks++;
|
|
||||||
} else {
|
|
||||||
Console::output('<red>FAIL</red>', 0);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Console::output('<yellow>SKIPPED</yellow>', 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Changing Release
|
if ($result == true) {
|
||||||
Console::output('Running <purple>Rollback Release [id=' . $releaseId . ']</purple> ... ', 2, false);
|
Console::output('<green>OK</green>', 0);
|
||||||
|
$completedTasks++;
|
||||||
|
} else {
|
||||||
|
Console::output('<red>FAIL</red>', 0);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Console::output('<yellow>SKIPPED</yellow>', 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$userGroup = '';
|
if ($this->getConfig()->release('compressreleases', false) == true) {
|
||||||
$resultFetch = $this->runCommandRemote('ls -ld ' . $rollbackTo . ' | awk \'{print \$3":"\$4}\'', $userGroup);
|
// Tar the current
|
||||||
$command = 'rm -f ' . $symlink
|
$result = $this->tarReleases() && $result;
|
||||||
. ' && '
|
// Untar the rollbackto
|
||||||
. 'ln -sf ' . $rollbackTo . ' ' . $symlink;
|
$result = $this->untarRelease($releaseId) && $result;
|
||||||
|
}
|
||||||
|
|
||||||
if ($resultFetch) {
|
// Changing Release
|
||||||
$command .= ' && chown -h ' . $userGroup . ' ' . $symlink;
|
Console::output('Running <purple>Rollback Release [id=' . $releaseId . ']</purple> ... ', 2, false);
|
||||||
}
|
|
||||||
|
|
||||||
$result = $this->runCommandRemote($command);
|
$userGroup = '';
|
||||||
|
$resultFetch = $this->runCommandRemote('ls -ld ' . $rollbackTo . ' | awk \'{print \$3":"\$4}\'', $userGroup);
|
||||||
|
$command = 'rm -f ' . $symlink
|
||||||
|
. ' && '
|
||||||
|
. 'ln -sf ' . $rollbackTo . ' ' . $symlink;
|
||||||
|
|
||||||
if ($result) {
|
if ($resultFetch) {
|
||||||
Console::output('<green>OK</green>', 0);
|
$command .= ' && chown -h ' . $userGroup . ' ' . $symlink;
|
||||||
$completedTasks++;
|
}
|
||||||
} else {
|
|
||||||
Console::output('<red>FAIL</red>', 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run Post Release Tasks
|
$result = $this->runCommandRemote($command);
|
||||||
$tasksToRun = $this->getConfig()->getTasks(AbstractTask::STAGE_POST_DEPLOY);
|
|
||||||
foreach ($tasksToRun as $taskData) {
|
|
||||||
$task = Factory::get($taskData, $this->getConfig(), true, self::STAGE_POST_DEPLOY);
|
|
||||||
$task->init();
|
|
||||||
Console::output('Running <purple>' . $task->getName() . '</purple> ... ', 2, false);
|
|
||||||
|
|
||||||
if ($task instanceOf RollbackAware) {
|
if ($result) {
|
||||||
$tasks++;
|
Console::output('<green>OK</green>', 0);
|
||||||
$result = $task->run();
|
$completedTasks++;
|
||||||
|
} else {
|
||||||
|
Console::output('<red>FAIL</red>', 0);
|
||||||
|
}
|
||||||
|
|
||||||
if ($result == true) {
|
// Run Post Release Tasks
|
||||||
Console::output('<green>OK</green>', 0);
|
$tasksToRun = $this->getConfig()->getTasks(AbstractTask::STAGE_POST_DEPLOY);
|
||||||
$completedTasks++;
|
foreach ($tasksToRun as $taskData) {
|
||||||
} else {
|
$task = Factory::get($taskData, $this->getConfig(), true, self::STAGE_POST_DEPLOY);
|
||||||
Console::output('<red>FAIL</red>', 0);
|
$task->init();
|
||||||
}
|
Console::output('Running <purple>' . $task->getName() . '</purple> ... ', 2, false);
|
||||||
} else {
|
|
||||||
Console::output('<yellow>SKIPPED</yellow>', 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($completedTasks == $tasks) {
|
if ($task instanceOf RollbackAware) {
|
||||||
$tasksColor = 'green';
|
$tasks++;
|
||||||
} else {
|
$result = $task->run();
|
||||||
$tasksColor = 'red';
|
|
||||||
}
|
|
||||||
|
|
||||||
Console::output('Release rollback on <dark_gray>' . $this->getConfig()->getHost() . '</dark_gray> compted: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.', 1, 3);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $result;
|
if ($result == true) {
|
||||||
|
Console::output('<green>OK</green>', 0);
|
||||||
|
$completedTasks++;
|
||||||
|
} else {
|
||||||
|
Console::output('<red>FAIL</red>', 0);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Console::output('<yellow>SKIPPED</yellow>', 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
if ($completedTasks == $tasks) {
|
||||||
return false;
|
$tasksColor = 'green';
|
||||||
}
|
} else {
|
||||||
}
|
$tasksColor = 'red';
|
||||||
|
}
|
||||||
|
|
||||||
|
Console::output('Release rollback on <dark_gray>' . $this->getConfig()->getHost() . '</dark_gray> compted: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.', 1, 3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in a new issue