2014-07-12 14:23:22 +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.
|
|
|
|
*/
|
|
|
|
|
2015-02-15 15:28:44 +03:00
|
|
|
/**
|
|
|
|
* Common parameters.
|
|
|
|
*/
|
2015-01-03 00:04:00 +03:00
|
|
|
set('env', 'prod');
|
|
|
|
set('keep_releases', 3);
|
2015-02-15 15:28:44 +03:00
|
|
|
set('shared_dirs', []);
|
|
|
|
set('shared_files', []);
|
2015-02-15 23:24:08 +03:00
|
|
|
set('writable_dirs', []);
|
2015-02-20 15:26:31 +03:00
|
|
|
set('writable_use_sudo', true); // Using sudo in writable commands?
|
2015-02-24 17:13:10 +03:00
|
|
|
set('env_vars', ''); // Like SYMFONY_ENV=prod
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default arguments
|
|
|
|
*/
|
|
|
|
argument('stage', \Symfony\Component\Console\Input\InputArgument::OPTIONAL, 'Run tasks only on this server or group of servers.');
|
2015-01-03 00:04:00 +03:00
|
|
|
|
2014-07-12 14:23:22 +04:00
|
|
|
/**
|
|
|
|
* Rollback to previous release.
|
|
|
|
*/
|
|
|
|
task('rollback', function () {
|
2015-01-03 14:48:38 +03:00
|
|
|
$releases = env('releases_list');
|
2014-07-12 14:23:22 +04:00
|
|
|
|
|
|
|
if (isset($releases[1])) {
|
2015-01-03 14:48:38 +03:00
|
|
|
$releaseDir = "{deploy_path}/releases/{$releases[1]}";
|
|
|
|
|
2014-07-12 14:23:22 +04:00
|
|
|
// Symlink to old release.
|
2015-02-15 23:30:11 +03:00
|
|
|
run("cd {deploy_path} && ln -nfs $releaseDir current");
|
2014-07-12 14:23:22 +04:00
|
|
|
|
|
|
|
// Remove release
|
2015-01-03 14:48:38 +03:00
|
|
|
run("rm -rf {deploy_path}/releases/{$releases[0]}");
|
|
|
|
|
|
|
|
if (isVerbose()) {
|
|
|
|
writeln("Rollback to `{$releases[1]}` release was successful.");
|
|
|
|
}
|
2014-07-12 14:23:22 +04:00
|
|
|
} else {
|
|
|
|
writeln("<comment>No more releases you can revert to.</comment>");
|
|
|
|
}
|
|
|
|
})->desc('Rollback to previous release');
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Preparing server for deployment.
|
|
|
|
*/
|
|
|
|
task('deploy:prepare', function () {
|
|
|
|
// Create releases dir.
|
2015-01-03 00:04:00 +03:00
|
|
|
run("cd {deploy_path} && if [ ! -d releases ]; then mkdir releases; fi");
|
2014-07-12 14:23:22 +04:00
|
|
|
|
|
|
|
// Create shared dir.
|
2015-01-03 00:04:00 +03:00
|
|
|
run("cd {deploy_path} && if [ ! -d shared ]; then mkdir shared; fi");
|
2014-07-12 14:23:22 +04:00
|
|
|
})->desc('Preparing server for deploy');
|
|
|
|
|
2015-01-03 00:04:00 +03:00
|
|
|
/**
|
|
|
|
* Return release path.
|
|
|
|
*/
|
|
|
|
env('release_path', function () {
|
|
|
|
return str_replace("\n", '', run("readlink {deploy_path}/release"));
|
|
|
|
});
|
2014-07-12 14:23:22 +04:00
|
|
|
|
|
|
|
/**
|
2015-01-03 00:04:00 +03:00
|
|
|
* Release
|
2014-07-12 14:23:22 +04:00
|
|
|
*/
|
2015-01-03 00:04:00 +03:00
|
|
|
task('deploy:release', function () {
|
2015-02-12 21:07:41 -05:00
|
|
|
$release = date('YmdHis');
|
2014-07-12 14:23:22 +04:00
|
|
|
|
2015-01-03 00:04:00 +03:00
|
|
|
$releasePath = "{deploy_path}/releases/$release";
|
2014-07-12 14:23:22 +04:00
|
|
|
|
2015-02-15 23:24:08 +03:00
|
|
|
$i = 0;
|
|
|
|
while (is_dir(env()->parse($releasePath)) && $i < 42) {
|
|
|
|
$releasePath .= '.' . ++$i;
|
|
|
|
}
|
|
|
|
|
2015-01-03 00:04:00 +03:00
|
|
|
run("mkdir $releasePath");
|
2014-07-12 14:23:22 +04:00
|
|
|
|
2015-01-03 00:04:00 +03:00
|
|
|
run("cd {deploy_path} && if [ -e release ]; then rm release; fi");
|
|
|
|
|
|
|
|
run("ln -s $releasePath {deploy_path}/release");
|
|
|
|
})->desc('Prepare release');
|
2014-07-12 14:23:22 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2015-01-03 00:04:00 +03:00
|
|
|
* Update project code
|
2014-07-12 14:23:22 +04:00
|
|
|
*/
|
2015-01-03 00:04:00 +03:00
|
|
|
task('deploy:update_code', function () {
|
2015-01-03 15:01:48 +03:00
|
|
|
$repository = get('repository');
|
2015-02-06 12:27:44 -02:00
|
|
|
run("git clone --depth 1 --recursive -q $repository {release_path} 2>&1");
|
2015-01-03 00:04:00 +03:00
|
|
|
run("chmod -R g+w {release_path}");
|
|
|
|
})->desc('Updating code');
|
2014-07-12 14:23:22 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2015-01-03 00:04:00 +03:00
|
|
|
* Create symlinks for shared directories and files.
|
2014-07-12 14:23:22 +04:00
|
|
|
*/
|
|
|
|
task('deploy:shared', function () {
|
2015-01-03 00:04:00 +03:00
|
|
|
$sharedPath = "{deploy_path}/shared";
|
2014-07-12 14:23:22 +04:00
|
|
|
|
2015-02-15 15:28:44 +03:00
|
|
|
foreach (get('shared_dirs') as $dir) {
|
2015-01-03 00:04:00 +03:00
|
|
|
// Remove from source
|
2015-02-15 15:28:44 +03:00
|
|
|
run("if [ -d $(echo {release_path}/$dir) ]; then rm -rf {release_path}/$dir; fi");
|
2014-07-12 14:23:22 +04:00
|
|
|
|
|
|
|
// Create shared dir if does not exist
|
2015-02-15 15:28:44 +03:00
|
|
|
run("mkdir -p $sharedPath/$dir");
|
2014-07-12 14:23:22 +04:00
|
|
|
|
|
|
|
// Symlink shared dir to release dir
|
2015-02-15 15:28:44 +03:00
|
|
|
run("ln -nfs $sharedPath/$dir {release_path}/$dir");
|
2014-07-12 14:23:22 +04:00
|
|
|
}
|
|
|
|
|
2015-01-03 00:04:00 +03:00
|
|
|
foreach (get('shared_files') as $file) {
|
|
|
|
// Remove from source
|
|
|
|
run("if [ -d $(echo {release_path}/$file) ]; then rm -rf {release_path}/$file; fi");
|
2014-07-12 14:23:22 +04:00
|
|
|
|
|
|
|
// Create dir of shared file
|
|
|
|
run("mkdir -p $sharedPath/" . dirname($file));
|
|
|
|
|
2015-01-03 00:04:00 +03:00
|
|
|
// Touch shared
|
2014-07-12 14:23:22 +04:00
|
|
|
run("touch $sharedPath/$file");
|
|
|
|
|
2015-01-03 00:04:00 +03:00
|
|
|
// Symlink shared dir to release dir
|
2015-02-15 15:28:44 +03:00
|
|
|
run("ln -nfs $sharedPath/$file {release_path}/$file");
|
2014-07-12 14:23:22 +04:00
|
|
|
}
|
|
|
|
})->desc('Creating symlinks for shared files');
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2015-02-15 23:24:08 +03:00
|
|
|
* Make writable dirs.
|
2014-07-12 14:23:22 +04:00
|
|
|
*/
|
2015-02-15 23:24:08 +03:00
|
|
|
task('deploy:writable', function () {
|
|
|
|
$dirs = join(' ', get('writable_dirs'));
|
2015-02-20 15:26:31 +03:00
|
|
|
$sudo = get('writable_use_sudo') ? 'sudo' : '';
|
2015-02-15 23:24:08 +03:00
|
|
|
|
|
|
|
if(!empty($dirs)) {
|
|
|
|
|
2015-02-20 15:26:31 +03:00
|
|
|
$httpUser = run("ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\ -f1")->toString();
|
2015-02-15 23:24:08 +03:00
|
|
|
|
2015-02-15 23:41:52 +03:00
|
|
|
cd(env('release_path'));
|
|
|
|
|
2015-02-15 23:24:08 +03:00
|
|
|
if (strpos(run("chmod 2>&1; true"), '+a') !== false) {
|
|
|
|
|
2015-02-20 15:26:31 +03:00
|
|
|
if (!empty($httpUser)) {
|
|
|
|
run("$sudo chmod +a \"$httpUser allow delete,write,append,file_inherit,directory_inherit\" $dirs");
|
|
|
|
}
|
|
|
|
|
|
|
|
run("$sudo chmod +a \"`whoami` allow delete,write,append,file_inherit,directory_inherit\" $dirs");
|
2015-02-15 23:24:08 +03:00
|
|
|
|
|
|
|
} elseif (commandExist('setfacl')) {
|
|
|
|
|
2015-02-20 15:26:31 +03:00
|
|
|
if (!empty($httpUser)) {
|
|
|
|
run("$sudo setfacl -R -m u:\"$httpUser\":rwX -m u:`whoami`:rwX $dirs");
|
|
|
|
run("$sudo setfacl -dR -m u:\"$httpUser\":rwX -m u:`whoami`:rwX $dirs");
|
|
|
|
} else {
|
|
|
|
run("$sudo chmod 777 $dirs");
|
|
|
|
}
|
2015-02-15 23:24:08 +03:00
|
|
|
|
|
|
|
|
2015-02-20 15:26:31 +03:00
|
|
|
} else {
|
|
|
|
run("$sudo chmod 777 $dirs");
|
2015-02-15 23:24:08 +03:00
|
|
|
}
|
2014-07-12 14:23:22 +04:00
|
|
|
}
|
2015-02-15 23:24:08 +03:00
|
|
|
|
2015-02-20 15:26:31 +03:00
|
|
|
})->desc('Make writable dirs');
|
2014-07-12 14:23:22 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2015-01-03 00:04:00 +03:00
|
|
|
* Installing vendors tasks.
|
2014-07-12 14:23:22 +04:00
|
|
|
*/
|
|
|
|
task('deploy:vendors', function () {
|
2015-02-15 15:28:44 +03:00
|
|
|
$envVars = get('env_vars');
|
|
|
|
|
2015-02-15 23:24:08 +03:00
|
|
|
if (commandExist('composer')) {
|
|
|
|
$composer = 'composer';
|
|
|
|
} else {
|
2015-01-03 00:04:00 +03:00
|
|
|
run("cd {release_path} && curl -s http://getcomposer.org/installer | php");
|
2015-02-15 15:28:44 +03:00
|
|
|
$composer = 'php composer.phar';
|
2014-07-12 14:23:22 +04:00
|
|
|
}
|
|
|
|
|
2015-02-15 15:28:44 +03:00
|
|
|
run("cd {release_path} && $envVars $composer install --no-dev --verbose --prefer-dist --optimize-autoloader --no-progress --no-scripts");
|
2014-07-12 14:23:22 +04:00
|
|
|
|
|
|
|
})->desc('Installing vendors');
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2015-01-03 00:04:00 +03:00
|
|
|
* Create symlink to last release.
|
2014-07-12 14:23:22 +04:00
|
|
|
*/
|
|
|
|
task('deploy:symlink', function () {
|
|
|
|
|
2015-02-15 15:28:44 +03:00
|
|
|
run("cd {deploy_path} && mv -f release current");
|
2014-07-12 14:23:22 +04:00
|
|
|
|
|
|
|
})->desc('Creating symlink to release');
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2015-01-03 00:04:00 +03:00
|
|
|
* Return list of releases on server.
|
|
|
|
*/
|
|
|
|
env('releases_list', function () {
|
2015-02-15 23:24:08 +03:00
|
|
|
$list = run('ls {deploy_path}/releases')->toArray();
|
2015-01-03 00:04:00 +03:00
|
|
|
|
|
|
|
rsort($list);
|
|
|
|
|
|
|
|
return $list;
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return current release path.
|
|
|
|
*/
|
|
|
|
env('current', function () {
|
2015-02-15 23:24:08 +03:00
|
|
|
return run("readlink {deploy_path}/current")->toString();
|
2015-01-03 00:04:00 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show current release number.
|
|
|
|
*/
|
|
|
|
task('current', function () {
|
|
|
|
writeln('Current release: ' . basename(env('current')));
|
|
|
|
})->desc('Show current release.');
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cleanup old releases.
|
2014-07-12 14:23:22 +04:00
|
|
|
*/
|
|
|
|
task('cleanup', function () {
|
2015-01-03 00:04:00 +03:00
|
|
|
$releases = env('releases_list');
|
2014-07-12 14:23:22 +04:00
|
|
|
|
2015-01-03 00:04:00 +03:00
|
|
|
$keep = get('keep_releases');
|
2014-07-12 14:23:22 +04:00
|
|
|
|
|
|
|
while ($keep > 0) {
|
|
|
|
array_shift($releases);
|
|
|
|
--$keep;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($releases as $release) {
|
2015-01-03 00:04:00 +03:00
|
|
|
run("rm -rf {deploy_path}/releases/$release");
|
2014-07-12 14:23:22 +04:00
|
|
|
}
|
|
|
|
|
2015-01-03 14:48:38 +03:00
|
|
|
run("cd {deploy_path} && if [ -e release ]; then rm release; fi");
|
2015-02-15 23:24:08 +03:00
|
|
|
run("cd {deploy_path} && if [ -h release ]; then rm release; fi");
|
2015-01-03 14:48:38 +03:00
|
|
|
|
2014-07-12 14:23:22 +04:00
|
|
|
})->desc('Cleaning up old releases');
|