deployer/recipe/symfony.php

171 lines
3.9 KiB
PHP
Raw Normal View History

<?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 22:28:15 +04:00
require_once __DIR__ . '/common.php';
2014-07-06 18:30:21 +04:00
2014-07-08 16:59:34 +04:00
/**
* Create cache dir
*/
2014-07-06 18:30:21 +04:00
task('deploy:create_cache_dir', function () {
2014-07-08 16:59:34 +04:00
$releasePath = env()->getReleasePath();
2014-07-06 22:28:15 +04:00
2014-07-08 16:59:34 +04:00
// Set cache dir
env()->set('cache_dir', $cacheDir = "$releasePath/app/cache");
2014-07-06 18:30:21 +04:00
2014-07-08 16:59:34 +04:00
// Remove cache dir if it exist
run("if [ -d \"$cacheDir\" ]; then rm -rf $cacheDir; fi");
2014-07-06 18:30:21 +04:00
2014-07-08 16:59:34 +04:00
// Create cache dir
run("mkdir -p $cacheDir");
})->desc('Creating cache dir');
2014-07-06 22:28:15 +04:00
2014-07-08 16:59:34 +04:00
after('deploy:update_code', 'deploy:create_cache_dir');
2014-07-06 18:30:21 +04:00
2014-07-08 16:59:34 +04:00
/**
* Set right permissions
*/
task('deploy:permissions:setfacl', function () {
2014-07-08 16:59:34 +04:00
$user = config()->getUser();
$wwwUser = config()->getWwwUser();
$releasePath = env()->getReleasePath();
$dirs = (array)get('writeable_dirs', ['app/cache', 'app/logs']);
if (empty(run("if which setfacl; then echo \"ok\"; fi"))) {
writeln('<comment>Enable ACL support and install "setfacl"</comment>');
return;
}
2014-07-08 16:59:34 +04:00
cd($releasePath);
2014-07-08 16:59:34 +04:00
foreach ($dirs as $dir) {
run("setfacl -R -m u:$wwwUser:rwX -m u:$user:rwX $dir");
run("setfacl -dR -m u:$wwwUser:rwX -m u:$user:rwX $dir");
}
2014-07-08 16:59:34 +04:00
})->desc('Setting permissions');
2014-07-08 16:59:34 +04:00
/**
* Normalize asset timestamps
*/
2014-07-06 22:28:15 +04:00
task('deploy:assets', function () {
2014-07-08 16:59:34 +04:00
$releasePath = env()->getReleasePath();
2014-07-06 22:28:15 +04:00
2014-07-08 16:59:34 +04:00
$assets = get('assets', ['web/css', 'web/images', 'web/js']);
2014-07-06 22:28:15 +04:00
2014-07-08 16:59:34 +04:00
$assets = array_map(function ($asset) use ($releasePath) {
return "$releasePath/$asset";
}, $assets);
$assets = implode(' ', $assets);
2014-07-06 22:28:15 +04:00
2014-07-08 16:59:34 +04:00
$time = date('Ymdhi.s');
2014-07-06 22:28:15 +04:00
2014-07-08 16:59:34 +04:00
run("find $assets -exec touch -t $time {} ';' &> /dev/null || true");
})->desc('Normalizing asset timestamps');
2014-07-06 22:28:15 +04:00
2014-07-08 16:59:34 +04:00
/**
* Dump all assets to the filesystem
*/
2014-07-06 22:28:15 +04:00
task('deploy:assetic:dump', function () {
2014-07-08 16:59:34 +04:00
$releasePath = env()->getReleasePath();
$prod = get('env', 'prod');
2014-07-06 22:28:15 +04:00
2014-07-08 16:59:34 +04:00
run("php $releasePath/app/console assetic:dump --env=$prod --no-debug");
2014-07-08 16:59:34 +04:00
})->desc('Dumping assets');
2014-07-06 22:28:15 +04:00
2014-07-08 16:59:34 +04:00
/**
* Warm up cache
*/
2014-07-06 22:28:15 +04:00
task('deploy:cache:warmup', function () {
2014-07-08 16:59:34 +04:00
$releasePath = env()->getReleasePath();
$cacheDir = env()->get('cache_dir', "$releasePath/app/cache");
2014-07-06 22:28:15 +04:00
2014-07-08 16:59:34 +04:00
$prod = get('env', 'prod');
2014-07-06 22:28:15 +04:00
2014-07-08 16:59:34 +04:00
run("php $releasePath/app/console cache:warmup --env=$prod --no-debug");
2014-07-06 22:28:15 +04:00
2014-07-08 16:59:34 +04:00
run("chmod -R g+w $cacheDir");
2014-07-06 22:28:15 +04:00
2014-07-08 16:59:34 +04:00
})->desc('Warming up cache');
/**
* Migrate database
*/
2014-07-06 22:28:15 +04:00
task('database:migrate', function () {
2014-07-08 16:59:34 +04:00
$releasePath = env()->getReleasePath();
$prod = get('env', 'prod');
$serverName = config()->getName();
2014-07-06 22:28:15 +04:00
$run = get('auto_migrate', false);
if (output()->isVerbose()) {
$run = askConfirmation("Run migrations on $serverName server?", $run);
}
if ($run) {
2014-07-11 10:23:01 +04:00
run("php $releasePath/app/console doctrine:migrations:migrate --env=$prod --no-debug --no-interaction");
}
2014-07-08 16:59:34 +04:00
})->desc('Migrating database');
/**
* Remove app_dev.php files
*/
2014-07-06 22:28:15 +04:00
task('deploy:clear_controllers', function () {
2014-07-08 16:59:34 +04:00
$releasePath = env()->getReleasePath();
run("rm -f $releasePath/web/app_*.php");
});
2014-07-06 22:28:15 +04:00
after('deploy:update_code', 'deploy:clear_controllers');
2014-07-08 16:59:34 +04:00
// Symfony shared dirs
set('shared_dirs', ['app/logs']);
2014-07-06 18:30:21 +04:00
// Symfony shared files
set('shared_files', ['app/config/parameters.yml']);
// Symfony writeable dirs
set('writeable_dirs', ['app/cache', 'app/logs']);
2014-07-08 16:59:34 +04:00
/**
* Main task
*/
2014-07-06 18:30:21 +04:00
task('deploy', [
'deploy:start',
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:writeable_dirs',
2014-07-06 22:28:15 +04:00
'deploy:assets',
'deploy:vendors',
'deploy:assetic:dump',
2014-07-11 10:23:01 +04:00
'database:migrate',
2014-07-06 22:28:15 +04:00
'deploy:cache:warmup',
'deploy:symlink',
'cleanup',
'deploy:end'
2014-07-08 16:59:34 +04:00
])->desc('Deploy your project');
2014-07-06 18:30:21 +04:00
2014-07-08 16:59:34 +04:00
/**
* Success message
*/
2014-07-06 18:30:21 +04:00
after('deploy', function () {
2014-07-08 16:59:34 +04:00
$host = config()->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