/**
  * Cleanup old releases by listing all releases and keeping a configurable
  * number of old releases (application option "keepReleases"). The current
  * and previous release (if one exists) are protected from removal.
  *
  * Example configuration:
  *
  *     $application->setOption('keepReleases', 2);
  *
  * Note: There is no rollback for this cleanup, so we have to be sure not to delete any
  *       live or referenced releases.
  *
  * @param \TYPO3\Deploy\Domain\Model\Node $node
  * @param \TYPO3\Deploy\Domain\Model\Application $application
  * @param \TYPO3\Deploy\Domain\Model\Deployment $deployment
  * @param array $options
  * @return void
  */
 public function execute(Node $node, Application $application, Deployment $deployment, array $options = array())
 {
     if (!$application->hasOption('keepReleases')) {
         $deployment->getLogger()->log(($deployment->isDryRun() ? 'Would keep' : 'Keeping') . ' all releases for "' . $application->getName() . '"', LOG_DEBUG);
         return;
     }
     $keepReleases = $application->getOption('keepReleases');
     $releasesPath = $application->getDeploymentPath() . '/releases';
     $currentReleaseIdentifier = $deployment->getReleaseIdentifier();
     $previousReleasePath = $application->getDeploymentPath() . '/releases/previous';
     $previousReleaseIdentifier = trim($this->shell->execute("if [ -h {$previousReleasePath} ]; then basename `readlink {$previousReleasePath}` ; fi", $node, $deployment));
     $allReleasesList = $this->shell->execute("find {$releasesPath}/. -maxdepth 1 -type d -exec basename {} \\;", $node, $deployment);
     $allReleases = preg_split('/\\s+/', $allReleasesList, -1, PREG_SPLIT_NO_EMPTY);
     $removableReleases = array();
     foreach ($allReleases as $release) {
         if ($release !== '.' && $release !== $currentReleaseIdentifier && $release !== $previousReleaseIdentifier && $release !== 'current' && $release !== 'previous') {
             $removableReleases[] = trim($release);
         }
     }
     sort($removableReleases);
     $removeReleases = array_slice($removableReleases, 0, count($removableReleases) - $keepReleases);
     $removeCommand = '';
     foreach ($removeReleases as $removeRelease) {
         $removeCommand .= "rm -rf {$releasesPath}/{$removeRelease};rm -f {$releasesPath}/{$removeRelease}REVISION;";
     }
     if (count($removeReleases) > 0) {
         $deployment->getLogger()->log(($deployment->isDryRun() ? 'Would remove' : 'Removing') . ' releases ' . implode(', ', $removeReleases));
         $this->shell->executeOrSimulate($removeCommand, $node, $deployment);
     } else {
         $deployment->getLogger()->log('No releases to remove', LOG_DEBUG);
     }
 }
Beispiel #2
0
 /**
  * Rollback this task
  *
  * @param \TYPO3\Deploy\Domain\Model\Node $node
  * @param \TYPO3\Deploy\Domain\Model\Application $application
  * @param \TYPO3\Deploy\Domain\Model\Deployment $deployment
  * @param array $options
  * @return void
  */
 public function rollback(Node $node, Application $application, Deployment $deployment, array $options = array())
 {
     $deploymentPath = $application->getDeploymentPath();
     $sharedPath = $application->getSharedPath();
     $releasePath = $deployment->getApplicationReleasePath($application);
     $currentPath = $application->getDeploymentPath() . '/releases/current';
     $previousPath = $application->getDeploymentPath() . '/releases/previous';
     if (!isset($options['rollbackCommand'])) {
         return;
     }
     $command = $options['rollbackCommand'];
     $command = str_replace(array('{deploymentPath}', '{sharedPath}', '{releasePath}', '{currentPath}', '{previousPath}'), array($deploymentPath, $sharedPath, $releasePath, $currentPath, $previousPath), $command);
     $this->shell->execute($command, $node, $deployment, TRUE);
 }
 /**
  * Executes this task
  *
  * @param \TYPO3\Deploy\Domain\Model\Node $node
  * @param \TYPO3\Deploy\Domain\Model\Application $application
  * @param \TYPO3\Deploy\Domain\Model\Deployment $deployment
  * @param array $options
  * @return void
  */
 public function execute(Node $node, Application $application, Deployment $deployment, array $options = array())
 {
     $this->checkOptionsForValidity($options);
     $this->shell->execute('rm -f ' . $options['targetFile'] . '; mkdir -p ' . dirname($options['targetFile']), $node, $deployment);
     $targetPath = $deployment->getApplicationReleasePath($application);
     $tarOptions = sprintf(' --transform="s,^./,%s/," ', $options['baseDirectory']);
     if (isset($options['exclude']) && is_array($options['exclude'])) {
         foreach ($options['exclude'] as $excludePattern) {
             $tarOptions .= sprintf(' --exclude="%s" ', $excludePattern);
         }
     }
     if (substr($options['targetFile'], -7) === '.tar.gz') {
         $tarOptions .= sprintf(' -czf %s --directory %s .', $options['targetFile'], $targetPath);
         $this->shell->execute(sprintf('tar %s || gnutar %s', $tarOptions, $tarOptions), $node, $deployment);
     } elseif (substr($options['targetFile'], -8) === '.tar.bz2') {
         $tarOptions .= sprintf(' -cjf %s --directory %s .', $options['targetFile'], $targetPath);
         $this->shell->execute(sprintf('tar %s || gnutar %s', $tarOptions, $tarOptions), $node, $deployment);
     } elseif (substr($options['targetFile'], -4) === '.zip') {
         $temporaryDirectory = sys_get_temp_dir() . '/' . uniqid('f3_deploy');
         $this->shell->execute(sprintf('mkdir -p %s', $temporaryDirectory), $node, $deployment);
         $tarOptions .= sprintf(' -cf %s/out.tar --directory %s . ', $temporaryDirectory, $targetPath, $options['baseDirectory']);
         $this->shell->execute(sprintf('tar %s || gnutar %s', $tarOptions, $tarOptions), $node, $deployment);
         $this->shell->execute(sprintf('cd %s; tar -xf out.tar; rm out.tar; zip --quiet -9 -r out %s', $temporaryDirectory, $options['baseDirectory']), $node, $deployment);
         $this->shell->execute(sprintf('mv %s/out.zip %s; rm -Rf %s', $temporaryDirectory, $options['targetFile'], $temporaryDirectory), $node, $deployment);
     } else {
         throw new \Exception('Unknown target file format', 1314248387);
     }
 }
 /**
  * Rollback this task
  *
  * @param \TYPO3\Deploy\Domain\Model\Node $node
  * @param \TYPO3\Deploy\Domain\Model\Application $application
  * @param \TYPO3\Deploy\Domain\Model\Deployment $deployment
  * @param array $options
  * @return void
  */
 public function rollback(Node $node, Application $application, Deployment $deployment, array $options = array())
 {
     $releasePath = $deployment->getApplicationReleasePath($application);
     $this->shell->execute('rm -f ' . $releasePath . 'REVISION', $node, $deployment, TRUE);
 }
 /**
  * Rollback this task
  *
  * @param \TYPO3\Deploy\Domain\Model\Node $node
  * @param \TYPO3\Deploy\Domain\Model\Application $application
  * @param \TYPO3\Deploy\Domain\Model\Deployment $deployment
  * @param array $options
  * @return void
  */
 public function rollback(Node $node, Application $application, Deployment $deployment, array $options = array())
 {
     $releasesPath = $application->getDeploymentPath() . '/releases';
     $this->shell->execute('cd ' . $releasesPath . ' && rm -f ./current && mv ./previous ./current', $node, $deployment, TRUE);
 }