/** * 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\Surf\Domain\Model\Node $node * @param \TYPO3\Surf\Domain\Model\Application $application * @param \TYPO3\Surf\Domain\Model\Deployment $deployment * @param array $options * @return void */ public function execute(Node $node, Application $application, Deployment $deployment, array $options = array()) { if (!isset($options['keepReleases'])) { $deployment->getLogger()->debug(($deployment->isDryRun() ? 'Would keep' : 'Keeping') . ' all releases for "' . $application->getName() . '"'); return; } $keepReleases = $options['keepReleases']; $releasesPath = $application->getReleasesPath(); $currentReleaseIdentifier = $deployment->getReleaseIdentifier(); $previousReleasePath = $application->getReleasesPath() . '/previous'; $previousReleaseIdentifier = trim($this->shell->execute("if [ -h {$previousReleasePath} ]; then basename `readlink {$previousReleasePath}` ; fi", $node, $deployment)); $allReleasesList = $this->shell->execute("if [ -d {$releasesPath}/. ]; then find {$releasesPath}/. -maxdepth 1 -type d -exec basename {} \\; ; fi", $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()->info(($deployment->isDryRun() ? 'Would remove' : 'Removing') . ' releases ' . implode(', ', $removeReleases)); $this->shell->executeOrSimulate($removeCommand, $node, $deployment); } else { $deployment->getLogger()->info('No releases to remove'); } }
/** * Execute or simulate a command (if the deployment is in dry run mode) * * @param string $command * @param Node $node * @param Deployment $deployment * @param bool $ignoreErrors * @param bool $logOutput TRUE if the output of the command should be logged * @return bool|mixed */ public function executeOrSimulate($command, Node $node, Deployment $deployment, $ignoreErrors = false, $logOutput = true) { if (!$deployment->isDryRun()) { return $this->execute($command, $node, $deployment, $ignoreErrors, $logOutput); } else { return $this->simulate($command, $node, $deployment); } }
/** * @param Node $node * @param Application $application * @param Deployment $deployment * @param array $options * @throws \TYPO3\Flow\Http\Client\InfiniteRedirectionException */ protected function executeOrSimulate(Node $node, Application $application, Deployment $deployment, array $options = array()) { if (empty($options['clearPhpCacheUris'])) { return; } $uris = is_array($options['clearPhpCacheUris']) ? $options['clearPhpCacheUris'] : array($options['clearPhpCacheUris']); foreach ($uris as $uri) { $deployment->getLogger()->log('... (localhost): curl "' . $uri . '"', LOG_DEBUG); if ($deployment->isDryRun() === FALSE) { $this->browser->request($uri); } } }
/** * Checks if a composer manifest exists in the directory at the given path. * * If no manifest exists, a log message is recorded. * * @param string $path * @param \TYPO3\Surf\Domain\Model\Node $node * @param \TYPO3\Surf\Domain\Model\Deployment $deployment * @return bool */ protected function composerManifestExists($path, Node $node, Deployment $deployment) { // In dry run mode, no checkout is there, this we must not assume a composer.json is there! if ($deployment->isDryRun()) { return false; } $composerJsonPath = Files::concatenatePaths(array($path, 'composer.json')); $composerJsonExists = $this->shell->executeOrSimulate('test -f ' . escapeshellarg($composerJsonPath), $node, $deployment, true); if ($composerJsonExists === false) { $deployment->getLogger()->debug('No composer.json found in path "' . $composerJsonPath . '"'); return false; } return true; }