Separate symfony recipe to common and symfony 💨

This commit is contained in:
Elfet 2014-07-12 14:23:22 +04:00
parent 4867322d71
commit af3a14f4d3
2 changed files with 209 additions and 195 deletions

201
recipe/common.php Normal file
View File

@ -0,0 +1,201 @@
<?php
/* (c) Anton Medvedev <anton@elfet.ru>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Rollback to previous release.
*/
task('rollback', function () {
$basePath = config()->getPath();
$releases = env()->getReleases();
$currentReleasePath = env()->getReleasePath();
if (isset($releases[1])) {
$releaseDir = "{$basePath}/releases/{$releases[1]}";
// Symlink to old release.
run("rm -f current");
run("ln -s $releaseDir current");
// Remove release
run("rm -rf $currentReleasePath");
} else {
writeln("<comment>No more releases you can revert to.</comment>");
}
})->desc('Rollback to previous release');
/**
* Preparing server for deployment.
*/
task('deploy:prepare', function () {
$basePath = config()->getPath();
// Check if base path exist.
run("if [ ! -d \"$basePath\" ]; then mkdir $basePath; fi", true);
// Create releases dir.
run("if [ ! -d \"releases\" ]; then mkdir releases; fi");
// Create shared dir.
run("if [ ! -d \"shared\" ]; then mkdir shared; fi");
})->desc('Preparing server for deploy');
/**
* Update project code
*/
task('deploy:update_code', function () {
$basePath = config()->getPath();
$repository = get('repository', false);
if (false === $repository) {
throw new \RuntimeException('You have to specify repository.');
}
$release = date('Ymd') . substr((string)time(), -5);
$releasePath = "$basePath/releases/$release";
env()->setReleasePath($releasePath);
env()->set('is_new_release', true);
run("git clone -q $repository $releasePath");
run("chmod -R g+w $releasePath");
})->desc('Updating code');
/**
* Delete new release if something goes wrong
*/
task('deploy:rollback', function () {
if (env()->get('is_new_release', false)) {
$server = config()->getName();
writeln("<error>Rolling back to previous release on server $server</error>");
$releasePath = env()->getReleasePath();
// Remove release
run("rm -rf $releasePath");
} else {
writeln("<comment>If you want to rollback run \"rollback\" task</comment>");
}
});
/**
* Create symlinks for shared directories and files
*/
task('deploy:shared', function () {
$basePath = config()->getPath();
$sharedPath = "$basePath/shared";
$releasePath = env()->getReleasePath();
// User specified shared directories
$sharedDirs = (array)get('shared_dirs', []);
foreach ($sharedDirs as $dir) {
// Remove dir from source
run("if [ -d \"$releasePath/$dir\" ]; then rm -rf $releasePath/$dir; fi");
// Create shared dir if does not exist
run("mkdir -p $sharedPath/$dir");
// Symlink shared dir to release dir
run("ln -nfs $sharedPath/$dir $releasePath/$dir");
}
// User specified shared files
$sharedFiles = (array)get('shared_files', []);
foreach ($sharedFiles as $file) {
// Create dir of shared file
run("mkdir -p $sharedPath/" . dirname($file));
// Touch shared file
run("touch $sharedPath/$file");
// Symlink shared file to release file
run("ln -nfs $sharedPath/$file $releasePath/$file");
}
})->desc('Creating symlinks for shared files');
/**
* Make writeable dirs
*/
task('deploy:writeable_dirs', function () {
$user = config()->getUser();
$wwwUser = config()->getWwwUser();
$releasePath = env()->getReleasePath();
cd($releasePath);
// User specified writeable dirs
$dirs = (array)get('writeable_dirs', []);
foreach ($dirs as $dir) {
run("chmod -R 0777 $dir");
run("chmod -R g+w $dir");
}
})->desc('Make writeable dirs');
/**
* Vendors
*/
task('deploy:vendors', function () {
$releasePath = env()->getReleasePath();
cd($releasePath);
$prod = get('env', 'prod');
$isComposer = run("if [ -e $releasePath/composer.phar ]; then echo 'true'; fi");
if ('true' !== $isComposer) {
run("curl -s http://getcomposer.org/installer | php");
}
run("SYMFONY_ENV=$prod php composer.phar install --no-dev --verbose --prefer-dist --optimize-autoloader --no-progress");
})->desc('Installing vendors');
/**
* Create symlink to last release
*/
task('deploy:symlink', function () {
$releasePath = env()->getReleasePath();
$releaseDir = get('release_dir', null);
run("rm -f current && ln -s $releasePath current");
})->desc('Creating symlink to release');
/**
* Cleanup old releases
*/
task('cleanup', function () {
$releases = env()->getReleases();
$keep = get('keep_releases', 3);
while ($keep > 0) {
array_shift($releases);
--$keep;
}
foreach ($releases as $release) {
run("rm -rf releases/$release");
}
})->desc('Cleaning up old releases');
/**
* Helper tasks
*/
task('deploy:start', function () {
});
task('deploy:end', function () {
});

View File

@ -5,81 +5,7 @@
* file that was distributed with this source code.
*/
/**
* Rollback to previous release.
*/
task('rollback', function () {
$basePath = config()->getPath();
$releases = env()->getReleases();
$currentReleasePath = env()->getReleasePath();
if (isset($releases[1])) {
$releaseDir = "{$basePath}/releases/{$releases[1]}";
// Symlink to old release.
run("rm -f current");
run("ln -s $releaseDir current");
// Remove release
run("rm -rf $currentReleasePath");
} else {
writeln("<comment>No more releases you can revert to.</comment>");
}
})->desc('Rollback to previous release');
/**
* Preparing server for deployment.
*/
task('deploy:prepare', function () {
$basePath = config()->getPath();
// Check if base path exist.
run("if [ ! -d \"$basePath\" ]; then mkdir $basePath; fi", true);
// Create releases dir.
run("if [ ! -d \"releases\" ]; then mkdir releases; fi");
// Create shared dir.
run("if [ ! -d \"shared\" ]; then mkdir shared; fi");
})->desc('Preparing server for deploy');
/**
* Update project code
*/
task('deploy:update_code', function () {
$basePath = config()->getPath();
$repository = get('repository', false);
if (false === $repository) {
throw new \RuntimeException('You have to specify repository.');
}
$release = date('Ymd') . substr((string)time(), -5);
$releasePath = "$basePath/releases/$release";
env()->setReleasePath($releasePath);
env()->set('is_new_release', true);
run("git clone -q $repository $releasePath");
run("chmod -R g+w $releasePath");
})->desc('Updating code');
/**
* Delete new release if something goes wrong
*/
task('deploy:rollback', function () {
if (env()->get('is_new_release', false)) {
$server = config()->getName();
writeln("<error>Rolling back to previous release on server $server</error>");
$releasePath = env()->getReleasePath();
// Remove release
run("rm -rf $releasePath");
} else {
writeln("<comment>If you want to rollback run \"rollback\" task</comment>");
}
});
require_once __DIR__ . '/common.php';
/**
* Create cache dir
@ -99,65 +25,6 @@ task('deploy:create_cache_dir', function () {
after('deploy:update_code', 'deploy:create_cache_dir');
/**
* Create symlinks for shared directories and files
*/
task('deploy:shared', function () {
$basePath = config()->getPath();
$sharedPath = "$basePath/shared";
$releasePath = env()->getReleasePath();
// User specified shared directories
$sharedDirs = (array)get('shared_dirs', ['app/logs']);
foreach ($sharedDirs as $dir) {
// Remove dir from source
run("if [ -d \"$releasePath/$dir\" ]; then rm -rf $releasePath/$dir; fi");
// Create shared dir if does not exist
run("mkdir -p $sharedPath/$dir");
// Symlink shared dir to release dir
run("ln -nfs $sharedPath/$dir $releasePath/$dir");
}
// User specified shared files
$sharedFiles = (array)get('shared_dirs', ['app/config/parameters.yml']);
foreach ($sharedFiles as $file) {
// Create dir of shared file
run("mkdir -p $sharedPath/" . dirname($file));
// Touch shared file
run("touch $sharedPath/$file");
// Symlink shared file to release file
run("ln -nfs $sharedPath/$file $releasePath/$file");
}
})->desc('Creating symlinks for shared files');
/**
* Make writeable dirs
*/
task('deploy:writeable_dirs', function () {
$user = config()->getUser();
$wwwUser = config()->getWwwUser();
$releasePath = env()->getReleasePath();
cd($releasePath);
// User specified writeable dirs
$dirs = (array)get('writeable_dirs', ['app/cache', 'app/logs']);
foreach ($dirs as $dir) {
run("chmod -R 0777 $dir");
run("chmod -R g+w $dir");
}
})->desc('Make writeable dirs');
/**
* Set right permissions
*/
@ -201,25 +68,6 @@ task('deploy:assets', function () {
})->desc('Normalizing asset timestamps');
/**
* Install vendors
*/
task('deploy:vendors', function () {
$releasePath = env()->getReleasePath();
cd($releasePath);
$prod = get('env', 'prod');
$isComposer = run("if [ -e $releasePath/composer.phar ]; then echo 'true'; fi");
if ('true' !== $isComposer) {
run("curl -s http://getcomposer.org/installer | php");
}
run("SYMFONY_ENV=$prod php composer.phar install --no-dev --verbose --prefer-dist --optimize-autoloader --no-progress");
})->desc('Installing vendors');
/**
* Dump all assets to the filesystem
*/
@ -281,50 +129,14 @@ task('deploy:clear_controllers', function () {
after('deploy:update_code', 'deploy:clear_controllers');
/**
* Create symlink to last release
*/
task('deploy:symlink', function () {
$releasePath = env()->getReleasePath();
$releaseDir = get('release_dir', null);
// Symfony shared dirs
set('shared_dirs', ['app/logs']);
run("rm -f current && ln -s $releasePath current");
// Symfony shared files
set('shared_files', ['app/config/parameters.yml']);
})->desc('Creating symlink to release');
/**
* Cleanup old releases
*/
task('cleanup', function () {
$releases = env()->getReleases();
$keep = get('keep_releases', 3);
while ($keep > 0) {
array_shift($releases);
--$keep;
}
foreach ($releases as $release) {
run("rm -rf releases/$release");
}
})->desc('Cleaning up old releases');
/**
* Helper task
*/
task('deploy:start', function () {
});
/**
* Helper task
*/
task('deploy:end', function () {
});
// Symfony writeable dirs
set('writeable_dirs', ['app/cache', 'app/logs']);
/**
@ -346,6 +158,7 @@ task('deploy', [
'deploy:end'
])->desc('Deploy your project');
/**
* Success message
*/