2014-07-12 14:23:22 +04:00
< ? php
2016-01-11 12:51:59 +07:00
/* ( c ) Anton Medvedev < anton @ medv . io >
2014-07-12 14:23:22 +04:00
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2016-01-11 14:00:45 +07:00
namespace Deployer ;
2016-11-05 17:50:18 +07:00
use Deployer\Task\Context ;
2016-11-05 12:22:56 +07:00
use Symfony\Component\Console\Input\InputArgument ;
use Symfony\Component\Console\Input\InputOption ;
2016-11-05 17:50:18 +07:00
/**
* Configuration
*/
2016-11-05 15:44:09 +07:00
set ( 'keep_releases' , 5 );
2016-11-05 17:50:18 +07:00
2016-11-05 17:58:06 +07:00
set ( 'repository' , '' ); // Repository to deploy.
2016-11-05 17:50:18 +07:00
set ( 'branch' , '' ); // Branch to deploy.
2015-02-15 15:28:44 +03:00
set ( 'shared_dirs' , []);
set ( 'shared_files' , []);
2016-11-05 17:50:18 +07:00
2015-07-05 14:53:49 +02:00
set ( 'copy_dirs' , []);
2016-11-05 17:50:18 +07:00
2015-02-15 23:24:08 +03:00
set ( 'writable_dirs' , []);
2016-11-07 13:57:38 +07:00
set ( 'writable_mode' , 'chgrp' ); // chgrp or acl.
2016-11-07 20:08:38 +07:00
set ( 'writable_use_sudo' , false ); // Using sudo in writable commands?
2016-11-05 17:50:18 +07:00
set ( 'http_user' , false );
2015-09-08 11:48:07 +07:00
set ( 'clear_paths' , []); // Relative path from deploy_path
2016-11-10 12:25:06 +07:00
set ( 'clear_use_sudo' , false ); // Using sudo in clean commands?
2016-06-05 12:58:34 -04:00
2016-11-05 17:50:18 +07:00
set ( 'use_relative_symlink' , true );
2016-06-05 12:58:34 -04:00
2016-11-05 17:50:18 +07:00
set ( 'composer_action' , 'install' );
set ( 'composer_options' , '{{composer_action}} --verbose --prefer-dist --no-progress --no-interaction --no-dev --optimize-autoloader' );
2016-06-05 12:58:34 -04:00
2016-11-05 17:50:18 +07:00
set ( 'env_vars' , '' ); // Variable assignment before cmds (for example, SYMFONY_ENV={{set}})
2016-06-05 12:58:34 -04:00
2016-11-05 17:50:18 +07:00
set ( 'git_cache' , function () { //whether to use git cache - faster cloning by borrowing objects from existing clones.
2016-04-19 10:24:46 +07:00
$gitVersion = run ( '{{bin/git}} version' );
2015-08-24 08:37:18 +07:00
$regs = [];
if ( preg_match ( '/((\d+\.?)+)/' , $gitVersion , $regs )) {
$version = $regs [ 1 ];
} else {
$version = " 1.0.0 " ;
}
return version_compare ( $version , '2.3' , '>=' );
2015-06-16 11:31:49 +02:00
});
2016-11-05 17:50:18 +07:00
set ( 'timezone' , 'UTC' );
set ( 'release_name' , function () {
2016-05-19 08:20:28 +07:00
// Set the deployment timezone
2016-11-05 17:50:18 +07:00
if ( ! date_default_timezone_set ( get ( 'timezone' ))) {
2016-05-19 08:20:28 +07:00
date_default_timezone_set ( 'UTC' );
}
2016-11-09 21:26:49 +07:00
return date ( 'YmdHis' );
2016-05-19 08:20:28 +07:00
}); // name of folder in releases
2015-02-24 17:13:10 +03:00
2016-11-09 21:26:49 +07:00
/**
* Return list of releases on server .
*/
set ( 'releases_list' , function () {
// Will list only dirs in releases and sort them by mtime in reverse.
$list = run ( 'cd {{deploy_path}}/releases && ls -t -d */' ) -> toArray ();
// filter out anything that does not look like a release
foreach ( $list as $key => $item ) {
$item = basename ( rtrim ( $item , '/' )); // strip path returned from find
$name_match = '[0-9_\.]+' ;
$extension_match = '\.[0-9]+' ;
if ( ! preg_match ( " /^ $name_match ( $extension_match )* $ / " , $item )) {
unset ( $list [ $key ]); // dir name does not match pattern, throw it out
continue ;
}
$list [ $key ] = $item ; // $item was changed
}
return $list ;
});
2016-11-10 12:25:06 +07:00
/**
* Return release path .
*/
set ( 'release_path' , function () {
$releaseExists = run ( " if [ -h { { deploy_path}}/release ]; then echo 'true'; fi " ) -> toBool ();
if ( ! $releaseExists ) {
throw new \RuntimeException (
" Release path does not found. \n " .
" Run deploy:release to create new release. "
);
}
$link = run ( " readlink { { deploy_path}}/release " ) -> toString ();
return substr ( $link , 0 , 1 ) === '/' ? $link : get ( 'deploy_path' ) . '/' . $link ;
});
/**
* Return current release path .
*/
set ( 'current_path' , function () {
$link = run ( " readlink { { deploy_path}}/current " ) -> toString ();
return substr ( $link , 0 , 1 ) === '/' ? $link : get ( 'deploy_path' ) . '/' . $link ;
});
2016-04-04 16:22:00 +07:00
/**
* Custom bins .
*/
2016-11-05 17:50:18 +07:00
set ( 'bin/php' , function () {
2016-04-04 16:22:00 +07:00
return run ( 'which php' ) -> toString ();
});
2016-11-05 17:50:18 +07:00
set ( 'bin/git' , function () {
2016-04-04 16:22:00 +07:00
return run ( 'which git' ) -> toString ();
});
2016-11-05 17:50:18 +07:00
set ( 'bin/composer' , function () {
2016-04-04 16:22:00 +07:00
if ( commandExist ( 'composer' )) {
$composer = run ( 'which composer' ) -> toString ();
}
if ( empty ( $composer )) {
2016-04-12 03:06:44 +02:00
run ( " cd { { release_path}} && curl -sS https://getcomposer.org/installer | { { bin/php}} " );
2016-04-11 11:18:16 +02:00
$composer = '{{bin/php}} {{release_path}}/composer.phar' ;
2016-04-04 16:22:00 +07:00
}
return $composer ;
});
2016-11-05 17:50:18 +07:00
set ( 'bin/symlink' , function () {
if ( get ( 'use_relative_symlink' )) {
2016-11-07 12:47:29 +07:00
// Check if target system supports relative symlink.
2016-11-05 15:42:44 +07:00
if ( run ( 'if [[ "$(man ln)" =~ "--relative" ]]; then echo "true"; fi' ) -> toBool ()) {
2016-11-05 13:40:12 +07:00
return 'ln -nfs --relative' ;
}
}
return 'ln -nfs' ;
});
2015-02-24 17:13:10 +03:00
/**
2015-02-24 18:47:48 +03:00
* Default arguments and options .
2015-02-24 17:13:10 +03:00
*/
2016-11-05 12:22:56 +07:00
argument ( 'stage' , InputArgument :: OPTIONAL , 'Run tasks only on this server or group of servers.' );
option ( 'tag' , null , InputOption :: VALUE_OPTIONAL , 'Tag to deploy.' );
option ( 'revision' , null , InputOption :: VALUE_OPTIONAL , 'Revision to deploy.' );
option ( 'branch' , null , InputOption :: VALUE_OPTIONAL , 'Branch to deploy.' );
2015-01-03 00:04:00 +03:00
2016-06-05 12:58:34 -04:00
2014-07-12 14:23:22 +04:00
/**
* Rollback to previous release .
*/
task ( 'rollback' , function () {
2016-11-05 17:50:18 +07:00
$releases = get ( 'releases_list' );
2014-07-12 14:23:22 +04:00
if ( isset ( $releases [ 1 ])) {
2015-03-23 09:19:12 +07:00
$releaseDir = " { { deploy_path}}/releases/ { $releases [ 1 ] } " ;
2015-01-03 14:48:38 +03:00
2014-07-12 14:23:22 +04:00
// Symlink to old release.
2016-11-05 13:40:12 +07:00
run ( " cd { { deploy_path}} && { { bin/symlink}} $releaseDir current " );
2014-07-12 14:23:22 +04:00
// Remove release
2015-03-23 09:19:12 +07:00
run ( " rm -rf { { deploy_path}}/releases/ { $releases [ 0 ] } " );
2015-01-03 14:48:38 +03:00
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' );
2016-11-05 12:56:53 +07:00
/**
* Lock deploy .
*/
task ( 'deploy:lock' , function () {
$locked = run ( " if [ -f { { deploy_path}}/deploy.lock ]; then echo 'true'; fi " ) -> toBool ();
if ( $locked ) {
throw new \RuntimeException (
" Deploy locked. \n " .
" Run deploy:unlock command to unlock. "
);
} else {
run ( " touch { { deploy_path}}/deploy.lock " );
}
}) -> desc ( 'Lock deploy' );
/**
* Unlock deploy .
*/
task ( 'deploy:unlock' , function () {
run ( " rm { { deploy_path}}/deploy.lock " );
}) -> desc ( 'Unlock deploy' );
2014-07-12 14:23:22 +04:00
/**
* Preparing server for deployment .
*/
task ( 'deploy:prepare' , function () {
2015-05-13 19:00:12 +07:00
// Check if shell is POSIX-compliant
2015-05-12 00:11:24 +07:00
try {
2015-05-19 14:58:32 +07:00
cd ( '' ); // To run command as raw.
2015-09-04 00:10:48 +07:00
$result = run ( 'echo $0' ) -> toString ();
if ( $result == 'stdin: is not a tty' ) {
2016-01-11 14:36:42 +07:00
throw new \RuntimeException (
2015-09-04 00:10:48 +07:00
" Looks like ssh inside another ssh. \n " .
" Help: http://goo.gl/gsdLt9 "
);
}
2015-05-12 00:11:24 +07:00
} catch ( \RuntimeException $e ) {
2016-11-05 12:22:56 +07:00
$formatter = Deployer :: get () -> getHelper ( 'formatter' );
2015-05-13 16:52:34 +07:00
$errorMessage = [
" Shell on your server is not POSIX-compliant. Please change to sh, bash or similar. " ,
" Usually, you can change your shell to bash by running: chsh -s /bin/bash " ,
];
write ( $formatter -> formatBlock ( $errorMessage , 'error' , true ));
2015-05-13 13:26:48 +07:00
throw $e ;
2015-05-12 00:11:24 +07:00
}
2015-08-24 00:31:29 +07:00
2015-07-27 14:58:47 +07:00
run ( 'if [ ! -d {{deploy_path}} ]; then mkdir -p {{deploy_path}}; fi' );
2015-05-11 14:04:14 +01:00
2016-03-14 12:31:01 +01:00
// Check for existing /current directory (not symlink)
2016-03-14 13:02:56 +01:00
$result = run ( 'if [ ! -L {{deploy_path}}/current ] && [ -d {{deploy_path}}/current ]; then echo true; fi' ) -> toBool ();
2016-03-14 12:33:59 +01:00
if ( $result ) {
2016-11-05 17:50:18 +07:00
throw new \RuntimeException ( 'There already is a directory (not symlink) named "current" in ' . get ( 'deploy_path' ) . '. Remove this directory so it can be replaced with a symlink for atomic deployments.' );
2016-03-14 12:31:01 +01:00
}
2014-07-12 14:23:22 +04:00
// Create releases dir.
2015-03-23 09:19:12 +07:00
run ( " cd { { deploy_path}} && if [ ! -d releases ]; then mkdir releases; fi " );
2014-07-12 14:23:22 +04:00
// Create shared dir.
2015-03-23 09:19:12 +07: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
* Release
2014-07-12 14:23:22 +04:00
*/
2015-01-03 00:04:00 +03:00
task ( 'deploy:release' , function () {
2016-11-05 12:39:52 +07:00
// Clean up if there is unfinished release.
$previousReleaseExist = run ( " cd { { deploy_path}} && if [ -h release ]; then echo 'true'; fi " ) -> toBool ();
if ( $previousReleaseExist ) {
2016-11-09 22:03:04 +07:00
run ( 'cd {{deploy_path}} && rm -rf "{{release_path}}"' ); // Delete release.
2016-11-05 12:39:52 +07:00
run ( 'cd {{deploy_path}} && rm release' ); // Delete symlink.
}
2014-07-12 14:23:22 +04:00
2016-11-05 17:50:18 +07:00
$releasePath = Context :: get () -> getEnvironment () -> parse ( " { { deploy_path}}/releases/ { { release_name}} " );
2015-02-15 23:24:08 +03:00
$i = 0 ;
2016-05-19 08:20:28 +07:00
while ( run ( " if [ -d $releasePath ]; then echo 'true'; fi " ) -> toBool ()) {
2015-02-15 23:24:08 +03:00
$releasePath .= '.' . ++ $i ;
}
2016-11-05 12:39:52 +07:00
// Make new release.
2015-01-03 00:04:00 +03:00
run ( " mkdir $releasePath " );
2016-11-05 13:40:12 +07:00
run ( " { { bin/symlink}} $releasePath { { deploy_path}}/release " );
2015-01-03 00:04:00 +03:00
}) -> 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 () {
2016-04-19 10:24:46 +07:00
$repository = trim ( get ( 'repository' ));
2016-11-05 17:50:18 +07:00
$branch = get ( 'branch' );
$git = get ( 'bin/git' );
$gitCache = get ( 'git_cache' );
2015-06-16 13:15:36 +02:00
$depth = $gitCache ? '' : '--depth 1' ;
2015-09-07 11:50:48 +02:00
2016-11-05 12:22:56 +07:00
// If option `branch` is set.
if ( input () -> hasOption ( 'branch' )) {
$branch = input () -> getOption ( 'branch' );
}
2016-11-05 17:50:18 +07:00
// Branch may come from option or from configuration.
2016-06-01 13:45:21 +07:00
$at = '' ;
if ( ! empty ( $branch )) {
$at = " -b $branch " ;
}
// If option `tag` is set
2015-02-24 18:47:48 +03:00
if ( input () -> hasOption ( 'tag' )) {
$tag = input () -> getOption ( 'tag' );
2016-06-01 13:45:21 +07:00
if ( ! empty ( $tag )) {
$at = " -b $tag " ;
}
2015-02-25 12:20:04 +03:00
}
2016-06-01 13:45:21 +07:00
// If option `tag` is not set and option `revision` is set
if ( empty ( $tag ) && input () -> hasOption ( 'revision' )) {
$revision = input () -> getOption ( 'revision' );
2016-10-04 08:06:24 +04:00
if ( ! empty ( $revision )) {
$depth = '' ;
}
2015-02-25 12:20:04 +03:00
}
2015-02-25 12:36:25 -05:00
2016-11-05 17:50:18 +07:00
$releases = get ( 'releases_list' );
2015-09-07 11:50:48 +02:00
2016-10-04 08:06:24 +04:00
if ( $gitCache && isset ( $releases [ 1 ])) {
2015-06-17 19:30:25 +02:00
try {
2016-04-04 16:22:00 +07:00
run ( " $git clone $at --recursive -q --reference { { deploy_path}}/releases/ { $releases [ 1 ] } --dissociate $repository { { release_path}} 2>&1 " );
2016-01-11 14:36:42 +07:00
} catch ( \RuntimeException $exc ) {
2015-06-17 19:30:25 +02:00
// If {{deploy_path}}/releases/{$releases[1]} has a failed git clone, is empty, shallow etc, git would throw error and give up. So we're forcing it to act without reference in this situation
2016-04-04 16:22:00 +07:00
run ( " $git clone $at --recursive -q $repository { { release_path}} 2>&1 " );
2015-06-17 19:30:25 +02:00
}
} else {
// if we're using git cache this would be identical to above code in catch - full clone. If not, it would create shallow clone.
2016-04-04 16:22:00 +07:00
run ( " $git clone $at $depth --recursive -q $repository { { release_path}} 2>&1 " );
2015-06-16 11:31:49 +02:00
}
2016-10-04 08:06:24 +04:00
if ( ! empty ( $revision )) {
run ( " cd { { release_path}} && $git checkout $revision " );
}
2015-01-03 00:04:00 +03:00
}) -> desc ( 'Updating code' );
2014-07-12 14:23:22 +04:00
2015-07-05 14:53:49 +02:00
/**
2016-11-05 17:57:14 +07:00
* Copy directories .
2015-07-05 14:53:49 +02:00
*/
task ( 'deploy:copy_dirs' , function () {
$dirs = get ( 'copy_dirs' );
2015-07-05 14:59:40 +02:00
foreach ( $dirs as $dir ) {
2016-03-19 22:27:33 +07:00
// Delete directory if exists.
2015-07-05 15:06:00 +02:00
run ( " if [ -d $ (echo { { release_path}}/ $dir ) ]; then rm -rf { { release_path}}/ $dir ; fi " );
2015-07-05 14:53:49 +02:00
2016-03-19 22:27:33 +07:00
// Copy directory.
2015-07-05 15:06:00 +02:00
run ( " if [ -d $ (echo { { deploy_path}}/current/ $dir ) ]; then cp -rpf { { deploy_path}}/current/ $dir { { release_path}}/ $dir ; fi " );
2015-07-05 14:53:49 +02:00
}
}) -> desc ( 'Copy directories' );
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-03-23 09:19:12 +07: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 ) {
2016-03-19 22:27:33 +07:00
// Remove from source.
2015-03-23 09:19:12 +07:00
run ( " if [ -d $ (echo { { release_path}}/ $dir ) ]; then rm -rf { { release_path}}/ $dir ; fi " );
2014-07-12 14:23:22 +04:00
2016-03-19 22:27:33 +07:00
// Create shared dir if it does not exist.
2015-02-15 15:28:44 +03:00
run ( " mkdir -p $sharedPath / $dir " );
2014-07-12 14:23:22 +04:00
2016-03-19 22:27:33 +07:00
// Create path to shared dir in release dir if it does not exist.
2015-02-25 12:36:25 -05:00
// (symlink will not create the path and will fail otherwise)
2015-03-23 09:19:12 +07:00
run ( " mkdir -p `dirname { { release_path}}/ $dir ` " );
2015-02-25 12:36:25 -05:00
2014-07-12 14:23:22 +04:00
// Symlink shared dir to release dir
2016-11-05 13:40:12 +07:00
run ( " { { bin/symlink}} $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 ) {
2016-01-20 10:23:47 +01:00
$dirname = dirname ( $file );
2016-03-19 22:27:33 +07:00
// Remove from source.
2015-06-04 14:22:37 +03:00
run ( " if [ -f $ (echo { { release_path}}/ $file ) ]; then rm -rf { { release_path}}/ $file ; fi " );
2016-01-20 10:23:47 +01:00
// Ensure dir is available in release
run ( " if [ ! -d $ (echo { { release_path}}/ $dirname ) ]; then mkdir -p { { release_path}}/ $dirname ;fi " );
2014-07-12 14:23:22 +04:00
// Create dir of shared file
2016-01-20 10:23:47 +01:00
run ( " mkdir -p $sharedPath / " . $dirname );
2014-07-12 14:23:22 +04:00
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
2016-11-05 13:40:12 +07:00
run ( " { { bin/symlink}} $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' ));
2016-11-07 13:57:38 +07:00
$mode = get ( 'writable_mode' );
2015-02-20 15:26:31 +03:00
$sudo = get ( 'writable_use_sudo' ) ? 'sudo' : '' ;
2016-11-05 17:50:18 +07:00
$httpUser = get ( 'http_user' , false );
2015-02-15 23:24:08 +03:00
2016-11-07 13:57:38 +07:00
if ( empty ( $dirs )) {
return ;
}
2015-02-15 23:24:08 +03:00
2016-11-07 20:53:14 +07:00
if ( $httpUser === false && $mode !== '777' ) {
2016-11-07 13:57:38 +07:00
// Detect http user in process list.
$httpUser = run ( " ps axo user,comm | 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:41:52 +03:00
2016-11-07 13:57:38 +07:00
if ( empty ( $httpUser )) {
throw new \RuntimeException (
" Can't detect http user name. \n " .
" Please setup `http_user` config parameter. "
);
}
}
try {
cd ( '{{release_path}}' );
if ( $mode === 'chgrp' ) {
// Change group ownership.
// -R operate on files and directories recursively
// -H if a command line argument is a symbolic link to a directory, traverse it
run ( " $sudo chgrp -RH $httpUser $dirs " );
2016-11-07 14:12:09 +07:00
} elseif ( $mode === 'acl' ) {
2015-05-13 16:52:34 +07:00
if ( strpos ( run ( " chmod 2>&1; true " ), '+a' ) !== false ) {
2016-11-07 13:57:38 +07:00
// Try OS-X specific setting of access-rights
2015-02-25 12:36:25 -05:00
2016-11-07 13:57:38 +07:00
run ( " $sudo chmod +a \" $httpUser allow delete,write,append,file_inherit,directory_inherit \" $dirs " );
2015-05-13 16:52:34 +07:00
run ( " $sudo chmod +a \" `whoami` allow delete,write,append,file_inherit,directory_inherit \" $dirs " );
2016-11-07 14:12:09 +07:00
} elseif ( commandExist ( 'setfacl' )) {
2016-11-07 13:57:38 +07:00
if ( ! empty ( $sudo )) {
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 {
// When running without sudo, exception may be thrown
// if executing setfacl on files created by http user (in directory that has been setfacl before).
// These directories/files should be skipped.
// Now, we will check each directory for ACL and only setfacl for which has not been set before.
$writeableDirs = get ( 'writable_dirs' );
foreach ( $writeableDirs as $dir ) {
// Check if ACL has been set or not
$hasfacl = run ( " getfacl -p $dir | grep \" ^user: $httpUser :.*w \" | wc -l " ) -> toString ();
// Set ACL for directory if it has not been set before
if ( ! $hasfacl ) {
run ( " setfacl -R -m u: \" $httpUser\ " : rwX - m u : `whoami` : rwX $dir " );
run ( " setfacl -dR -m u: \" $httpUser\ " : rwX - m u : `whoami` : rwX $dir " );
2015-08-24 11:37:26 +07:00
}
}
2015-05-13 16:52:34 +07:00
}
2015-02-20 15:26:31 +03:00
} else {
2016-11-07 13:57:38 +07:00
// If we are not on OS-X and have no ACL installed.
// Maybe it's better to throw an exception.
2016-08-31 15:59:55 +02:00
run ( " $sudo chmod -R 777 $dirs " );
2015-02-20 15:26:31 +03:00
}
2016-11-07 20:53:14 +07:00
} elseif ( $mode === '777' ) {
run ( " $sudo chmod -R 777 $dirs " );
} else {
throw new \RuntimeException ( " Unknown writable_mode ` $mode `. " );
2016-11-07 13:57:38 +07:00
}
} catch ( \RuntimeException $e ) {
$formatter = Deployer :: get () -> getHelper ( 'formatter' );
2015-05-13 16:52:34 +07:00
2016-11-07 13:57:38 +07:00
$errorMessage = [
" Unable to setup correct permissions for writable dirs. " ,
" You need to configure sudo's sudoers files to not prompt for password, " ,
" or setup correct permissions manually. " ,
];
write ( $formatter -> formatBlock ( $errorMessage , 'error' , true ));
2015-02-15 23:24:08 +03:00
2016-11-07 13:57:38 +07:00
throw $e ;
2014-07-12 14:23:22 +04: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 () {
2016-06-05 12:58:34 -04:00
run ( 'cd {{release_path}} && {{env_vars}} {{bin/composer}} {{composer_options}}' );
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 () {
2016-11-05 15:42:44 +07:00
if ( run ( 'if [[ "$(man mv)" =~ "--no-target-directory" ]]; then echo "true"; fi' ) -> toBool ()) {
run ( " mv -T { { deploy_path}}/release { { deploy_path}}/current " );
} else {
// Atomic symlink does not supported.
// Will use simple≤ two steps switch.
run ( " cd { { deploy_path}} && { { bin/symlink}} { { release_path}} current " ); // Atomic override symlink.
run ( " cd { { deploy_path}} && rm release " ); // Remove release link.
}
2014-07-12 14:23:22 +04:00
}) -> desc ( 'Creating symlink to release' );
2015-01-03 00:04:00 +03:00
/**
* Show current release number .
*/
task ( 'current' , function () {
2016-11-09 22:03:04 +07:00
writeln ( 'Current release: ' . basename ( get ( 'current_path' )));
2015-01-03 00:04:00 +03:00
}) -> desc ( 'Show current release.' );
/**
* Cleanup old releases .
2014-07-12 14:23:22 +04:00
*/
task ( 'cleanup' , function () {
2016-11-05 17:50:18 +07:00
$releases = get ( '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-03-23 09:19:12 +07:00
run ( " rm -rf { { deploy_path}}/releases/ $release " );
2014-07-12 14:23:22 +04:00
}
2015-03-23 09:19:12 +07:00
run ( " cd { { deploy_path}} && if [ -e release ]; then rm release; fi " );
run ( " cd { { deploy_path}} && if [ -h release ]; then rm release; fi " );
2014-07-12 14:23:22 +04:00
}) -> desc ( 'Cleaning up old releases' );
2015-02-25 12:55:56 +03:00
2015-09-08 11:48:07 +07:00
/**
* Cleanup files and directories
*/
task ( 'deploy:clean' , function () {
$paths = get ( 'clear_paths' );
$sudo = get ( 'clear_use_sudo' ) ? 'sudo' : '' ;
foreach ( $paths as $path ) {
run ( " $sudo rm -rf { { deploy_path}}/ $path " );
}
}) -> desc ( 'Cleaning up files and/or directories' );
2015-02-25 12:55:56 +03:00
/**
* Success message
*/
task ( 'success' , function () {
writeln ( " <info>Successfully deployed!</info> " );
})
-> once ()
-> setPrivate ();