Skip to content
This repository has been archived by the owner on Dec 28, 2021. It is now read-only.

Commit

Permalink
Added better check for dep versions.
Browse files Browse the repository at this point in the history
  • Loading branch information
RamonSmit committed Jul 18, 2017
1 parent 3cfd452 commit efbd567
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 70 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"require": {
"php": ">=5.6.0",
"illuminate/support": "~5.2",
"laravelcollective/remote": "5.4.*"
"laravelcollective/remote": "5.4.*",
"composer/semver": "^1.4"
},
"require-dev": {
"sebastiaanluca/laravel-helpers": "^1.0",
Expand Down
101 changes: 52 additions & 49 deletions src/Helpers/Deployer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static function freshInit(Connection $connection, $stage)
{
// Init fresh remote repo
$connection->define('init', [
'cd '.config('laravel-deploy-helper.stages.'.$stage.'.remote.root'),
'cd ' . config('laravel-deploy-helper.stages.' . $stage . '.remote.root'),
'mkdir releases',
'mkdir patches',
'mkdir shared',
Expand All @@ -24,22 +24,22 @@ public static function freshInit(Connection $connection, $stage)
}

/**
* @param \Collective\Remote\Connection $connection
* @param $stage
* @param $branch
* @param $ldh
* @param $stage
* @param $branch
* @param $ldh
*
* @return int
* @return array
* @throws \Exception
*/
public static function doDeploy($stage, $branch, $ldh)
{
// Some stuff that does not change in runtime
$releaseName = time();
$home = config('laravel-deploy-helper.stages.'.$stage.'.remote.root');
$shared = config('laravel-deploy-helper.stages.'.$stage.'.shared');
$commands = config('laravel-deploy-helper.stages.'.$stage.'.commands');
$versions = config('laravel-deploy-helper.stages.'.$stage.'.config.dependencies');
$keep = config('laravel-deploy-helper.stages.'.$stage.'.config.keep');
$home = config('laravel-deploy-helper.stages.' . $stage . '.remote.root');
$shared = config('laravel-deploy-helper.stages.' . $stage . '.shared');
$commands = config('laravel-deploy-helper.stages.' . $stage . '.commands');
$versions = config('laravel-deploy-helper.stages.' . $stage . '.config.dependencies');
$keep = config('laravel-deploy-helper.stages.' . $stage . '.config.keep');

// Check what releases are old and can be removed
// Adding the array fixed #1
Expand All @@ -55,75 +55,78 @@ public static function doDeploy($stage, $branch, $ldh)

// Check versions
// Operators: http://php.net/manual/en/function.version-compare.php
verbose('['.$stage.'] Checking dependencies. Might take a minute.');
verbose('[' . $stage . '] Checking dependencies. Might take a minute.');
foreach ($versions as $app => $version) {
SSH::checkAppVersion($connection, $app, $version);
if (SSH::checkAppVersion($connection, $app, $version) == '-1') {
throw new \Exception('Version of ' . $app . ' does not match your requirements');
}
}

// Define the deploy
verbose('['.$stage.'] Creating new release directory and pulling from remote');
return $ldh;

$url = config('laravel-deploy-helper.stages.'.$stage.'.git.http');
// Define the deploy
verbose('[' . $stage . '] Creating new release directory and pulling from remote');
// Fixes https://github.com/DALTCORE/laravel-deploy-helper/issues/6#issuecomment-315124310
$url = config('laravel-deploy-helper.stages.' . $stage . '.git.http');
if ($url === null) {
$url = config('laravel-deploy-helper.stages.'.$stage.'.git');
$url = config('laravel-deploy-helper.stages.' . $stage . '.git');
}

SSH::execute($stage, [
'mkdir '.$home.'/releases/'.$releaseName,
'cd '.$home.'/releases/'.$releaseName,
'git clone -b '.$branch.' '."'".$url."'".' .',
'mkdir ' . $home . '/releases/' . $releaseName,
'cd ' . $home . '/releases/' . $releaseName,
'git clone -b ' . $branch . ' ' . "'" . $url . "'" . ' .',
]);

// Pre-flight for shared stuff
$items['directories'] = [];
foreach ($shared['directories'] as $share) {
verbose('['.$stage.'] About to share directory "'.$home.'/current/'.$share.'"');
$items['directories'][] = '[ -e '.$home.'/current/'.$share.' ] && cp -R -p '.$home.'/current/'
.$share.' '.$home.'/shared/'.$share;
$items['directories'][] = '[ -e '.$home.'/shared/'.$share.' ] && cp -R -p '.$home.'/shared/'.
$share.' '.$home.'/releases/'.$releaseName;
verbose('[' . $stage . '] About to share directory "' . $home . '/current/' . $share . '"');
$items['directories'][] = '[ -e ' . $home . '/current/' . $share . ' ] && cp -R -p ' . $home . '/current/'
. $share . ' ' . $home . '/shared/' . $share;
$items['directories'][] = '[ -e ' . $home . '/shared/' . $share . ' ] && cp -R -p ' . $home . '/shared/' .
$share . ' ' . $home . '/releases/' . $releaseName;
}
// Pre-flight for shared stuff
$items['files'] = [];
foreach ($shared['files'] as $share) {
verbose('['.$stage.'] About to share file "'.$home.'/current/'.$share.'"');
$items['files'][] = '[ -e '.$home.'/current/'.$share.' ] && cp -p '.$home.'/current/'.$share
.' '.$home.'/shared/'.$share;
$items['files'][] = '[ -e '.$home.'/shared/'.$share.' ] && cp -p '.$home.'/shared/'.$share.
' '.$home.'/releases/'.$releaseName.'/'.$share;
verbose('[' . $stage . '] About to share file "' . $home . '/current/' . $share . '"');
$items['files'][] = '[ -e ' . $home . '/current/' . $share . ' ] && cp -p ' . $home . '/current/' . $share
. ' ' . $home . '/shared/' . $share;
$items['files'][] = '[ -e ' . $home . '/shared/' . $share . ' ] && cp -p ' . $home . '/shared/' . $share .
' ' . $home . '/releases/' . $releaseName . '/' . $share;
}

// Define shared files
verbose('['.$stage.'] Syncing shared files');
verbose('[' . $stage . '] Syncing shared files');
SSH::execute($stage, $items['files']);

// Define shared directories
verbose('['.$stage.'] Syncing shared directories');
verbose('[' . $stage . '] Syncing shared directories');
SSH::execute($stage, $items['directories']);

$items = [];
foreach ($commands as $command) {
$items[] = 'cd '.$home.'/releases/'.$releaseName.' && '.$command;
$items[] = 'cd ' . $home . '/releases/' . $releaseName . ' && ' . $command;
}
// Define commands
verbose('['.$stage.'] Executing custom commands');
verbose('[' . $stage . '] Executing custom commands');
SSH::execute($stage, $items);

// Define post deploy actions
verbose('['.$stage.'] Linking new release to /current directory and removing temp');
verbose('[' . $stage . '] Linking new release to /current directory and removing temp');
SSH::execute($stage, [
'ln -sfn '.$home.'/releases/'.$releaseName.' '.$home.'/current',
'rm -rf '.$home.'/shared/*',
'ln -sfn ' . $home . '/releases/' . $releaseName . ' ' . $home . '/current',
'rm -rf ' . $home . '/shared/*',
]);

// Remove old deploys
if (isset($toRemove) && is_array($toRemove)) {
$items = [];
foreach ($toRemove as $dir => $val) {
$items[] = 'echo "Removing release '.$dir.'" && rm -rf '.$home.'/releases/'.$dir;
$items[] = 'echo "Removing release ' . $dir . '" && rm -rf ' . $home . '/releases/' . $dir;
}
verbose('['.$stage.'] Cleaning up old releases');
verbose('[' . $stage . '] Cleaning up old releases');
SSH::execute($stage, $items);
}

Expand All @@ -137,15 +140,15 @@ public static function doDeploy($stage, $branch, $ldh)
*/
public static function doRollback(Connection $connection, $stage, $ldh, $dirs)
{
$home = config('laravel-deploy-helper.stages.'.$stage.'.remote.root');
$home = config('laravel-deploy-helper.stages.' . $stage . '.remote.root');

// Define post deploy actions
$connection->define('preformRollback', [
'ln -sfn '.$home.'/releases/'.$dirs[1].' '.$home.'/current',
'rm -rf '.$home.'/releases/'.$dirs[0],
'ln -sfn ' . $home . '/releases/' . $dirs[1] . ' ' . $home . '/current',
'rm -rf ' . $home . '/releases/' . $dirs[0],
]);

verbose("\t".'Hold my beer, We\'re rolling back');
verbose("\t" . 'Hold my beer, We\'re rolling back');
$connection->task('preformRollback');

unset($dirs[0]);
Expand All @@ -165,26 +168,26 @@ public static function doRollback(Connection $connection, $stage, $ldh, $dirs)
*/
public static function doPatch($stage, $branch)
{
$home = config('laravel-deploy-helper.stages.'.$stage.'.remote.root');
$home = config('laravel-deploy-helper.stages.' . $stage . '.remote.root');

// setup ssh connection to remote
$connection = SSH::instance()->into($stage);

$connection->define('preformPatch', [
Command::builder('cd', [$home.'/current']),
Command::builder('cd', [$home . '/current']),
Command::builder('ls', ['-haml']),

Command::builder('git', ['config', 'user.email', '[email protected]']),
Command::builder('git', ['config', 'user.name', 'LDH']),

Command::builder('git', ['fetch']),
Command::builder('git',
['format-patch', '-1', 'origin/'.$branch, 'FETCH_HEAD', '-o', $home.'/patches']),
'git apply --reject --whitespace=fix '.$home.'/patches/*',
Command::builder('rm', ['-rf', $home.'/patches']),
['format-patch', '-1', 'origin/' . $branch, 'FETCH_HEAD', '-o', $home . '/patches']),
'git apply --reject --whitespace=fix ' . $home . '/patches/*',
Command::builder('rm', ['-rf', $home . '/patches']),
]);

verbose("\t".'Hold on tight, trying to patch!');
verbose("\t" . 'Hold on tight, trying to patch!');
$connection->task('preformPatch');
}
}
35 changes: 18 additions & 17 deletions src/Helpers/SSH.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace DALTCORE\LaravelDeployHelper\Helpers;

use Collective\Remote\Connection;
use Composer\Semver\Semver;
use DALTCORE\LaravelDeployHelper\Remote\RemoteManager;

class SSH
Expand All @@ -22,7 +23,7 @@ public static function instance()
*/
public static function home($stage)
{
return config('laravel-deploy-helper.stages.'.$stage.'.remote.root');
return config('laravel-deploy-helper.stages.' . $stage . '.remote.root');
}

/**
Expand Down Expand Up @@ -86,7 +87,7 @@ public static function checkAppVersion(Connection $connection, $app, $requestedV
if ($requestedVersion === true) {
$connection->run(Command::builder($app, []), function ($response) use ($app) {
if (stripos($response, 'command not found') !== false) {
throw new \Exception('ERROR: '.$app.' is not installed on the server.');
throw new \Exception('ERROR: ' . $app . ' is not installed on the server.');
}
});
verbose("\t => Checking $app is available");
Expand All @@ -99,13 +100,13 @@ public static function checkAppVersion(Connection $connection, $app, $requestedV
*/
$connection->run(Command::builder($app, []), function ($response) use ($app) {
if (stripos($response, 'command not found') !== false) {
throw new \Exception('ERROR: '.$app.' is not installed on the server.');
throw new \Exception('ERROR: ' . $app . ' is not installed on the server.');
}
});

verbose("\t => Checking $app version $currVer $operator $version");

return version_compare($currVer, $version, $operator);
return (!empty(Semver::satisfies($version, $requestedVersion)));
}

/**
Expand Down Expand Up @@ -140,9 +141,9 @@ public static function preFlight($instance, $stage, $branch = false)
if ($stage === null) {
throw new \Exception('The argument "--stage=" is required!', 128);
} else {
if (!is_array(config('laravel-deploy-helper.stages.'.$stage))) {
throw new \Exception('The stage "'.$stage
.'" does not exist!', 128);
if (!is_array(config('laravel-deploy-helper.stages.' . $stage))) {
throw new \Exception('The stage "' . $stage
. '" does not exist!', 128);
}
}

Expand All @@ -152,13 +153,13 @@ public static function preFlight($instance, $stage, $branch = false)
}

if (in_array($branch, Git::getBranches()) == false) {
throw new \Exception('The branch "'.$branch
.'" does not exists locally? Please `git pull`!', 128);
throw new \Exception('The branch "' . $branch
. '" does not exists locally? Please `git pull`!', 128);
}
}

// Connecting to remote server
verbose('['.$stage.'] Trying to login into remote SSH');
verbose('[' . $stage . '] Trying to login into remote SSH');
$ssh = self::instance()->into($stage);

// Check for lockfile
Expand All @@ -168,22 +169,22 @@ public static function preFlight($instance, $stage, $branch = false)
}

// Trying to read file
verbose('['.$stage.'] Reading config file from remote server');
$config = $ssh->exists(self::home($stage).'/ldh.json');
verbose('[' . $stage . '] Reading config file from remote server');
$config = $ssh->exists(self::home($stage) . '/ldh.json');

// Check if config exists
if ($config == false) {
error('['.$stage.'] ldh.json does not exists.');
error('[' . $stage . '] ldh.json does not exists.');
if ($instance->confirm('Do you want to initialize LDH here?')) {
Deployer::freshInit($ssh, $stage);
} else {
return false;
}
} else {
verbose('['.$stage.'] Found config. Checking directories.');
$config = $ssh->getString(self::home($stage).'/ldh.json');
verbose('[' . $stage . '] Found config. Checking directories.');
$config = $ssh->getString(self::home($stage) . '/ldh.json');
if ($config == false) {
error('['.$stage.'] Config file is empty... Something is wrong.');
error('[' . $stage . '] Config file is empty... Something is wrong.');

return false;
}
Expand All @@ -201,6 +202,6 @@ public static function performLanding($stage)
{
$ssh = self::instance()->into($stage);
Locker::unlock($ssh, $stage);
verbose('['.$stage.'] Changes are successfull!');
verbose('[' . $stage . '] Changes are successfull!');
}
}
6 changes: 3 additions & 3 deletions src/Helpers/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function verbose($message)
{
$console = new ConsoleOutput();
$console->getFormatter()->setStyle('info', new OutputFormatterStyle('blue', null));
$console->writeln('<info>'.$message.'</info>');
$console->writeln('<info>' . $message . '</info>');
}
}

Expand All @@ -31,7 +31,7 @@ function error($message)
{
$console = new ConsoleOutput();
$console->getFormatter()->setStyle('error', new OutputFormatterStyle('white', 'red'));
$console->writeln('<error>'.$message.'</error>');
$console->writeln('<error>' . $message . '</error>');
}
}

Expand All @@ -55,6 +55,6 @@ function cli_header()
\t\________|\_______/ \__| \__|
\t Laravel Deploy Helper
\tRamon Smit <[email protected]>
\t\t 1.1.2\n";
\t\t 1.1.3\n";
}
}

0 comments on commit efbd567

Please sign in to comment.