Last active
February 24, 2016 01:41
-
-
Save DavidSporer/7485da2e83421cb06292 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Brainswarm\SurfDeployment\Task\Transfer; | |
/* * | |
* This script belongs to the TYPO3 Flow package "TYPO3.Surf". * | |
* * | |
* */ | |
use TYPO3\Flow\Annotations as Flow; | |
use TYPO3\Flow\Utility\Files; | |
use TYPO3\Surf\Domain\Model\Node; | |
use TYPO3\Surf\Domain\Model\Application; | |
use TYPO3\Surf\Domain\Model\Deployment; | |
/** | |
* A scp transfer task | |
* | |
* Copies the application assets from the application workspace to the node using scp. | |
*/ | |
class ScpTask extends \TYPO3\Surf\Domain\Model\Task | |
{ | |
/** | |
* @Flow\Inject | |
* @var \TYPO3\Surf\Domain\Service\ShellCommandService | |
*/ | |
protected $shell; | |
/** | |
* @Flow\Inject | |
* @var \TYPO3\Flow\Package\PackageManagerInterface | |
*/ | |
protected $packageManager; | |
/** | |
* Execute this task | |
* | |
* @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()) | |
{ | |
$localPackagePath = $deployment->getWorkspacePath($application); | |
$releasePath = $deployment->getApplicationReleasePath($application); | |
$remotePath = Files::concatenatePaths(array($application->getDeploymentPath(), 'cache', 'transfer')); | |
// make sure there is a remote .cache folder | |
$command = 'mkdir -p ' . $remotePath; | |
$this->shell->executeOrSimulate($command, $node, $deployment); | |
$username = $node->hasOption('username') ? $node->getOption('username') . '@' : ''; | |
$hostname = $node->getHostname(); | |
$destinationArgument = ($node->isLocalhost() ? $remotePath : "{$username}{$hostname}:{$remotePath}"); | |
// escape whitespaces | |
$localPackagePath = preg_replace('/\s+/', '\ ', $localPackagePath); | |
$destinationArgument = preg_replace('/\s+/', '\ ', $destinationArgument); | |
$localhost = new Node('localhost'); | |
$localhost->setHostname('localhost'); | |
$this->createTarballForTransfer($localPackagePath, $localhost, $deployment); | |
// copy tarball to target server | |
$command = "scp " . $localPackagePath . "/transfer.tar.gz " . $destinationArgument; | |
$this->shell->executeOrSimulate($command, $localhost, $deployment); | |
$command = strtr("cp -RPp $remotePath/. $releasePath", "\t\n", " "); | |
// TODO Copy revision file (if it exists) for application to deployment path with release identifier | |
$this->shell->executeOrSimulate($command, $node, $deployment); | |
// extract tarball on server | |
$command = "ssh " . $username . $hostname . " 'cd " . $remotePath . "; tar -zxvf " . $remotePath . "/transfer.tar.gz'"; | |
echo $command; | |
$this->shell->executeOrSimulate($command, $localhost, $deployment); | |
// delete tarball on server | |
$command = 'ssh ' . $username . $hostname . ' rm -rf ' . $remotePath . '/transfer.tar.gz'; | |
$this->shell->executeOrSimulate($command, $localhost, $deployment); | |
$this->deleteTarballForTransfer($localPackagePath, $localhost, $deployment); | |
} | |
protected function createTarballForTransfer($localPackagePath, $node, $deployment) | |
{ | |
// delete tarball if it exists to avoid errors | |
$command = 'rm -rf ' . $localPackagePath . '/transfer.tar.gz'; | |
$this->shell->executeOrSimulate($command, $node, $deployment); | |
$command = "cd " . $localPackagePath . "/; tar -zcf transfer.tar.gz " . $localPackagePath . "/*"; | |
$this->shell->executeOrSimulate($command, $node, $deployment); | |
} | |
protected function deleteTarballForTransfer($localPackagePath, $node, $deployment) | |
{ | |
$command = 'rm -rf ' . $localPackagePath . '/transfer.tar.gz'; | |
$this->shell->executeOrSimulate($command, $node, $deployment); | |
} | |
/** | |
* Simulate this task | |
* | |
* @param Node $node | |
* @param Application $application | |
* @param Deployment $deployment | |
* @param array $options | |
* @return void | |
*/ | |
public function simulate(Node $node, Application $application, Deployment $deployment, array $options = array()) | |
{ | |
$this->execute($node, $application, $deployment, $options); | |
} | |
/** | |
* Rollback this task | |
* | |
* @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 rollback(Node $node, Application $application, Deployment $deployment, array $options = array()) | |
{ | |
$releasePath = $deployment->getApplicationReleasePath($application); | |
$this->shell->execute('rm -Rf ' . $releasePath, $node, $deployment, TRUE); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment