deployer/recipe/deploy/shared.php

66 lines
2.2 KiB
PHP
Raw Normal View History

2016-11-19 15:13:32 +07:00
<?php
/* (c) Anton Medvedev <anton@medv.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Deployer;
2017-01-18 13:27:08 +07:00
use Deployer\Exception\ConfigurationException;
2016-11-19 15:13:32 +07:00
desc('Creating symlinks for shared files and dirs');
task('deploy:shared', function () {
$sharedPath = "{{deploy_path}}/shared";
2017-01-18 13:27:08 +07:00
// Validate shared_dir, find duplicates
foreach (get('shared_dirs') as $a) {
foreach (get('shared_dirs') as $b) {
if ($a !== $b && strpos(rtrim($a, '/') . '/', rtrim($b, '/') . '/') === 0) {
2017-01-18 13:27:08 +07:00
throw new ConfigurationException("Can not share same dirs `$a` and `$b`.");
}
}
}
2016-11-19 15:13:32 +07:00
foreach (get('shared_dirs') as $dir) {
// Check if shared dir does not exists.
if (!test("[ -d $sharedPath/$dir ]")) {
// Create shared dir if it does not exist.
run("mkdir -p $sharedPath/$dir");
2016-11-22 21:46:17 -02:00
// If release contains shared dir, copy that dir from release to shared.
if (test("[ -d $(echo {{release_path}}/$dir) ]")) {
run("cp -rv {{release_path}}/$dir $sharedPath/" . dirname($dir));
}
}
2016-11-19 15:13:32 +07:00
// Remove from source.
run("rm -rf {{release_path}}/$dir");
2016-11-19 15:13:32 +07:00
// Create path to shared dir in release dir if it does not exist.
// Symlink will not create the path and will fail otherwise.
2016-11-19 15:13:32 +07:00
run("mkdir -p `dirname {{release_path}}/$dir`");
// Symlink shared dir to release dir
run("{{bin/symlink}} $sharedPath/$dir {{release_path}}/$dir");
}
foreach (get('shared_files') as $file) {
$dirname = dirname($file);
// Remove from source.
run("if [ -f $(echo {{release_path}}/$file) ]; then rm -rf {{release_path}}/$file; fi");
// Ensure dir is available in release
run("if [ ! -d $(echo {{release_path}}/$dirname) ]; then mkdir -p {{release_path}}/$dirname;fi");
// Create dir of shared file
run("mkdir -p $sharedPath/" . $dirname);
// Touch shared
run("touch $sharedPath/$file");
// Symlink shared dir to release dir
run("{{bin/symlink}} $sharedPath/$file {{release_path}}/$file");
}
});