2014-07-06 14:02:23 +04:00
|
|
|
<?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.
|
|
|
|
*/
|
2014-07-06 18:30:21 +04:00
|
|
|
use Deployer\Server;
|
2014-07-06 14:02:23 +04:00
|
|
|
|
2014-07-06 22:28:15 +04:00
|
|
|
function releases()
|
|
|
|
{
|
|
|
|
$releases = run("ls releases");
|
|
|
|
$releases = explode("\n", $releases);
|
|
|
|
rsort($releases);
|
|
|
|
|
|
|
|
return array_filter($releases, function ($release) {
|
|
|
|
$release = trim($release);
|
|
|
|
return !empty($release);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-07-06 18:30:21 +04:00
|
|
|
task('rollback', function () {
|
2014-07-06 22:28:15 +04:00
|
|
|
info('Rollback to previous release');
|
|
|
|
|
|
|
|
$basePath = Server\Current::getServer()->getConfiguration()->getPath();
|
|
|
|
|
|
|
|
$releases = releases();
|
|
|
|
|
|
|
|
if (count($releases) >= 2) {
|
|
|
|
$releaseDir = "releases/" . $releases[1];
|
|
|
|
run("rm -f current");
|
|
|
|
run("ln -s $basePath/$releaseDir current");
|
|
|
|
} else {
|
|
|
|
writeln("<comment>No more releases you can revert to.</comment>");
|
|
|
|
}
|
|
|
|
|
|
|
|
ok();
|
2014-07-06 18:30:21 +04:00
|
|
|
})->description('Rollback to previous release');
|
|
|
|
|
2014-07-06 22:28:15 +04:00
|
|
|
task('deploy:prepare', function () {
|
|
|
|
info('Preparing server for deploy');
|
|
|
|
|
2014-07-06 18:30:21 +04:00
|
|
|
$basePath = Server\Current::getServer()->getConfiguration()->getPath();
|
|
|
|
|
|
|
|
// Check if base dir 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");
|
|
|
|
|
2014-07-06 22:28:15 +04:00
|
|
|
ok();
|
|
|
|
})->description('Prepare server for deploy');
|
2014-07-06 18:30:21 +04:00
|
|
|
|
|
|
|
task('deploy:update_code', function () {
|
2014-07-06 22:28:15 +04:00
|
|
|
info('Updating code');
|
|
|
|
|
2014-07-06 18:30:21 +04:00
|
|
|
$repository = get('repository', null);
|
|
|
|
|
|
|
|
if (null === $repository) {
|
|
|
|
throw new \RuntimeException('You have to specify repository.');
|
|
|
|
}
|
|
|
|
|
2014-07-06 22:28:15 +04:00
|
|
|
$release = date('Ymd') . substr((string)time(), -5);
|
2014-07-06 18:30:21 +04:00
|
|
|
$releaseDir = "releases/$release";
|
2014-07-06 22:28:15 +04:00
|
|
|
|
|
|
|
set('release', $release);
|
|
|
|
set('release_dir', $releaseDir);
|
2014-07-06 18:30:21 +04:00
|
|
|
|
|
|
|
run("git clone -q $repository $releaseDir");
|
|
|
|
run("chmod -R g+w $releaseDir");
|
2014-07-06 22:28:15 +04:00
|
|
|
run("touch $releaseDir/$release.release");
|
|
|
|
|
|
|
|
ok();
|
|
|
|
})->description('Update code');
|
2014-07-06 18:30:21 +04:00
|
|
|
|
|
|
|
task('deploy:create_cache_dir', function () {
|
2014-07-06 22:28:15 +04:00
|
|
|
info('Creating cache dir');
|
|
|
|
|
|
|
|
$releaseDir = get('release_dir', null);
|
2014-07-06 18:30:21 +04:00
|
|
|
|
|
|
|
if (null !== $releaseDir) {
|
|
|
|
$cacheDir = "$releaseDir/app/cache";
|
|
|
|
|
2014-07-06 22:28:15 +04:00
|
|
|
set('cache_dir', $cacheDir);
|
|
|
|
|
2014-07-06 18:30:21 +04:00
|
|
|
run("if [ -d \"$cacheDir\" ]; then rm -rf $cacheDir; fi");
|
|
|
|
|
|
|
|
run("mkdir -p $cacheDir");
|
|
|
|
run("chmod -R 0777 $cacheDir");
|
|
|
|
run("chmod -R g+w $cacheDir");
|
|
|
|
}
|
2014-07-06 22:28:15 +04:00
|
|
|
|
|
|
|
ok();
|
2014-07-06 18:30:21 +04:00
|
|
|
})->description('Create cache dir');
|
|
|
|
|
|
|
|
after('deploy:update_code', 'deploy:create_cache_dir');
|
|
|
|
|
|
|
|
task('deploy:shared', function () {
|
2014-07-06 22:28:15 +04:00
|
|
|
info('Creating symlinks for shared files');
|
|
|
|
|
2014-07-06 18:30:21 +04:00
|
|
|
$basePath = Server\Current::getServer()->getConfiguration()->getPath();
|
|
|
|
$sharedDirBase = "$basePath/shared";
|
|
|
|
|
2014-07-06 22:28:15 +04:00
|
|
|
$releaseDir = get('release_dir', null);
|
2014-07-06 18:30:21 +04:00
|
|
|
if (null !== $releaseDir) {
|
|
|
|
// Shared dirs
|
|
|
|
$sharedDirs = (array)get('shared_dirs', ['app/logs']);
|
|
|
|
foreach ($sharedDirs as $dir) {
|
|
|
|
// Remove dir from source
|
|
|
|
run("if [ -d \"$releaseDir/$dir\" ]; then rm -rf $releaseDir/$dir; fi");
|
|
|
|
|
|
|
|
// Create shared dir
|
|
|
|
run("mkdir -p $sharedDirBase/$dir");
|
|
|
|
|
|
|
|
// Symlink
|
|
|
|
run("ln -nfs $sharedDirBase/$dir $releaseDir/$dir");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shared files
|
|
|
|
$sharedFiles = (array)get('shared_dirs', ['app/config/parameters.yml']);
|
|
|
|
foreach ($sharedFiles as $file) {
|
|
|
|
// Create dir of shared file
|
|
|
|
run("mkdir -p $sharedDirBase/" . dirname($file));
|
|
|
|
|
|
|
|
// Touch shared file
|
|
|
|
run("touch $sharedDirBase/$file");
|
|
|
|
|
|
|
|
// Symlink
|
|
|
|
run("ln -nfs $sharedDirBase/$file $releaseDir/$file");
|
|
|
|
}
|
|
|
|
}
|
2014-07-06 22:28:15 +04:00
|
|
|
|
|
|
|
ok();
|
|
|
|
})->description('Create symlinks for shared directories and files');
|
|
|
|
|
|
|
|
task('deploy:assets', function () {
|
|
|
|
info('Normalizing asset timestamps');
|
|
|
|
|
|
|
|
$basePath = Server\Current::getServer()->getConfiguration()->getPath();
|
|
|
|
$releaseDir = get('release_dir', null);
|
|
|
|
|
|
|
|
if (null !== $releaseDir) {
|
|
|
|
$assets = get('assets', ['web/css', 'web/images', 'web/js']);
|
|
|
|
|
|
|
|
$assets = array_map(function ($asset) use ($releaseDir, $basePath) {
|
|
|
|
return "$basePath/$releaseDir/$asset";
|
|
|
|
}, $assets);
|
|
|
|
$assets = implode(' ', $assets);
|
|
|
|
|
|
|
|
$time = date('Ymdhi.s');
|
|
|
|
|
|
|
|
run("find $assets -exec touch -t $time {} ';' &> /dev/null || true");
|
|
|
|
}
|
|
|
|
|
|
|
|
ok();
|
|
|
|
})->description('Normalize asset timestamps');
|
|
|
|
|
|
|
|
task('deploy:vendors', function () {
|
|
|
|
info('Installing vendors');
|
|
|
|
|
|
|
|
$releaseDir = get('release_dir', null);
|
|
|
|
|
|
|
|
if (null !== $releaseDir) {
|
|
|
|
$isComposer = run("if [ -e $releaseDir/composer.phar ]; then echo 'true'; fi");
|
|
|
|
|
|
|
|
if ('true' !== $isComposer) {
|
|
|
|
run("cd $releaseDir && curl -s http://getcomposer.org/installer | php");
|
|
|
|
}
|
|
|
|
|
|
|
|
$prod = get('env', 'prod');
|
|
|
|
|
|
|
|
run("cd $releaseDir && SYMFONY_ENV=$prod php composer.phar install --no-dev --verbose --prefer-dist --optimize-autoloader --no-progress");
|
|
|
|
}
|
|
|
|
|
|
|
|
ok();
|
|
|
|
})->description('Install vendors');
|
|
|
|
|
|
|
|
task('deploy:assetic:dump', function () {
|
|
|
|
info('Dumping assets');
|
|
|
|
|
|
|
|
$prod = get('env', 'prod');
|
|
|
|
run("php app/console assetic:dump --env=$prod --no-debug");
|
|
|
|
|
|
|
|
ok();
|
|
|
|
})->description('Dump all assets to the filesystem');
|
|
|
|
|
|
|
|
task('deploy:cache:warmup', function () {
|
|
|
|
info('Warming up cache');
|
|
|
|
|
|
|
|
$cacheDir = get('cache_dir', null);
|
|
|
|
|
|
|
|
if (null !== $cacheDir) {
|
|
|
|
$prod = get('env', 'prod');
|
|
|
|
run("php app/console cache:warmup --env=$prod --no-debug");
|
|
|
|
run("chmod -R g+w $cacheDir");
|
|
|
|
}
|
|
|
|
|
|
|
|
ok();
|
|
|
|
})->description('Warm up cache');
|
|
|
|
|
|
|
|
task('database:migrate', function () {
|
|
|
|
info('Migrating database');
|
|
|
|
|
|
|
|
$prod = get('env', 'prod');
|
|
|
|
run("php app/console doctrine:migrations:migrate --env=$prod --no-debug --no-interaction");
|
|
|
|
|
|
|
|
ok();
|
|
|
|
})->description('Migrate database');
|
|
|
|
|
|
|
|
task('deploy:clear_controllers', function () {
|
|
|
|
run("rm -f web/app_*.php");
|
|
|
|
})->description('Remove app_dev.php files');
|
|
|
|
|
|
|
|
after('deploy:update_code', 'deploy:clear_controllers');
|
|
|
|
|
|
|
|
task('deploy:symlink', function () {
|
|
|
|
$basePath = Server\Current::getServer()->getConfiguration()->getPath();
|
|
|
|
$releaseDir = get('release_dir', null);
|
|
|
|
|
|
|
|
if (null !== $releaseDir) {
|
|
|
|
run("rm -f current");
|
|
|
|
run("ln -s $basePath/$releaseDir current");
|
|
|
|
}
|
|
|
|
})->description('Create symlink to last release');
|
|
|
|
|
|
|
|
task('cleanup', function () {
|
|
|
|
info('Cleaning up old releases');
|
|
|
|
|
|
|
|
$releases = releases();
|
|
|
|
|
|
|
|
$keep = get('keep_releases', 3);
|
|
|
|
|
|
|
|
while ($keep > 0) {
|
|
|
|
array_shift($releases);
|
|
|
|
--$keep;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($releases as $release) {
|
|
|
|
run("rm -rf releases/$release");
|
|
|
|
}
|
|
|
|
|
|
|
|
ok();
|
|
|
|
})->description('Cleanup old releases');
|
2014-07-06 18:30:21 +04:00
|
|
|
|
|
|
|
task('deploy', [
|
2014-07-06 22:28:15 +04:00
|
|
|
'deploy:prepare',
|
2014-07-06 18:30:21 +04:00
|
|
|
'deploy:update_code',
|
2014-07-06 22:28:15 +04:00
|
|
|
'deploy:shared',
|
|
|
|
'deploy:assets',
|
|
|
|
'deploy:vendors',
|
|
|
|
'deploy:assetic:dump',
|
|
|
|
(get('automigrate', false) ? 'migrate' : function () {
|
|
|
|
}),
|
|
|
|
'deploy:cache:warmup',
|
|
|
|
'deploy:symlink',
|
|
|
|
'cleanup'
|
|
|
|
])->description('Deploy your project');
|
2014-07-06 18:30:21 +04:00
|
|
|
|
|
|
|
after('deploy', function () {
|
|
|
|
$host = Server\Current::getServer()->getConfiguration()->getHost();
|
2014-07-06 22:28:15 +04:00
|
|
|
writeln("<info>Successfully deployed on</info> <fg=cyan>$host</fg=cyan>");
|
2014-07-06 18:30:21 +04:00
|
|
|
});
|
2014-07-06 22:28:15 +04:00
|
|
|
|
|
|
|
|
2014-07-06 14:02:23 +04:00
|
|
|
|