Skip to content
This repository was archived by the owner on Dec 28, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions config/laravel-deploy-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
'stages' => [
'production' => [
'git' => [
'http' => ''
'http' => '',
],

'connection' => [
Expand Down Expand Up @@ -32,8 +32,8 @@
'storage',
],
'files' => [
'.env'
]
'.env',
],
],

'config' => [
Expand All @@ -43,6 +43,6 @@
],
'keep' => 4,
],
]
]
],
],
];
29 changes: 15 additions & 14 deletions src/Console/Commands/Deploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function __construct()
* Execute the console command.
*
* @throws \Exception on failure
*
* @return mixed
*/
public function handle()
Expand All @@ -54,9 +55,9 @@ public function handle()
if ($this->option('stage') === null) {
throw new \Exception('The argument "--stage=" is required!', 128);
} else {
if (!is_array(config('laravel-deploy-helper.stages.' . $this->option('stage')))) {
throw new \Exception('The stage "' . $this->option('stage')
. '" does not exist!', 128);
if (!is_array(config('laravel-deploy-helper.stages.'.$this->option('stage')))) {
throw new \Exception('The stage "'.$this->option('stage')
.'" does not exist!', 128);
}
}

Expand All @@ -65,12 +66,12 @@ public function handle()
}

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

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

// Check for lockfile
Expand All @@ -80,22 +81,22 @@ public function handle()
}

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

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

return false;
}
Expand All @@ -106,10 +107,10 @@ public function handle()
$this->ldh = Deployer::doDeploy($ssh, $this->option('stage'), $this->option('branch'), $this->ldh);

// Write to config
$ssh->putString(SSH::home($this->option('stage')) . '/ldh.json', json_encode($this->ldh));
$ssh->putString(SSH::home($this->option('stage')).'/ldh.json', json_encode($this->ldh));

// Done
verbose('[' . $this->option('stage') . '] Deploy successfull!');
verbose('['.$this->option('stage').'] Deploy successfull!');
Locker::unlock($ssh, $this->option('stage'));
}
}
17 changes: 9 additions & 8 deletions src/Console/Commands/Locktest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function __construct()
* Execute the console command.
*
* @throws \Exception on failure
*
* @return mixed
*/
public function handle()
Expand All @@ -45,21 +46,21 @@ public function handle()
if ($this->option('stage') === null) {
throw new \Exception('The argument "--stage=" is required!', 128);
} else {
if (!is_array(config('laravel-deploy-helper.stages.' . $this->option('stage')))) {
throw new \Exception('The stage "' . $this->option('stage')
. '" does not exist!', 128);
if (!is_array(config('laravel-deploy-helper.stages.'.$this->option('stage')))) {
throw new \Exception('The stage "'.$this->option('stage')
.'" does not exist!', 128);
}
}

$stage = $this->option('stage');

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

verbose('Setting lock: ' . (Locker::lock($ssh, $stage) ? 'OK' : 'Error'));
verbose('Getting lock: ' . (Locker::verify($ssh, $stage) ? 'OK' : 'Error'));
verbose('Path to lock: ' . Locker::getLockPath($ssh, $stage));
verbose('Destroy lock: ' . (Locker::unlock($ssh, $stage) ? 'OK' : 'Error'));
verbose('Setting lock: '.(Locker::lock($ssh, $stage) ? 'OK' : 'Error'));
verbose('Getting lock: '.(Locker::verify($ssh, $stage) ? 'OK' : 'Error'));
verbose('Path to lock: '.Locker::getLockPath($ssh, $stage));
verbose('Destroy lock: '.(Locker::unlock($ssh, $stage) ? 'OK' : 'Error'));
}
}
26 changes: 13 additions & 13 deletions src/Console/Commands/Rollback.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class Rollback extends Command

/**
* Create a new command instance.
*
*/
public function __construct()
{
Expand All @@ -41,6 +40,7 @@ public function __construct()
* Execute the console command.
*
* @throws \Exception on failure
*
* @return mixed
*/
public function handle()
Expand All @@ -51,14 +51,14 @@ public function handle()
if ($this->option('stage') === null) {
throw new \Exception('The argument "--stage=" is required!', 128);
} else {
if (!is_array(config('laravel-deploy-helper.stages.' . $this->option('stage')))) {
throw new \Exception('The stage "' . $this->option('stage')
. '" does not exist!', 128);
if (!is_array(config('laravel-deploy-helper.stages.'.$this->option('stage')))) {
throw new \Exception('The stage "'.$this->option('stage')
.'" does not exist!', 128);
}
}

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

// Check for lockfile
Expand All @@ -68,18 +68,18 @@ public function handle()
}

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

// Check if config exists
if ($config == false) {
error('[' . $this->option('stage') . '] ldh.json does not exists.');
error('['.$this->option('stage').'] ldh.json does not exists.');
exit(128);
} else {
verbose('[' . $this->option('stage') . '] Found config. Checking directories.');
$config = $ssh->getString(SSH::home($this->option('stage')) . '/ldh.json');
verbose('['.$this->option('stage').'] Found config. Checking directories.');
$config = $ssh->getString(SSH::home($this->option('stage')).'/ldh.json');
if ($config == false) {
error('[' . $this->option('stage') . '] Config file is empty... Something is wrong.');
error('['.$this->option('stage').'] Config file is empty... Something is wrong.');
exit(0);
}
$this->ldh = json_decode($config, true);
Expand All @@ -96,8 +96,8 @@ public function handle()
}

$this->ldh = Deployer::doRollback($ssh, $this->option('stage'), $this->ldh, $dirs);
$ssh->putString(SSH::home($this->option('stage')) . '/ldh.json', json_encode($this->ldh));
verbose('[' . $this->option('stage') . '] Rolled back!');
$ssh->putString(SSH::home($this->option('stage')).'/ldh.json', json_encode($this->ldh));
verbose('['.$this->option('stage').'] Rolled back!');
Locker::unlock($ssh, $this->option('stage'));
}
}
4 changes: 1 addition & 3 deletions src/Helpers/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class Command
{
/**
* Execute command on CLI
* Execute command on CLI.
*
* @param string $prefix utility to execute
* @param array $args the arguments to give to the prefix
Expand All @@ -18,8 +18,6 @@ class Command
*/
protected static function command($prefix, $args)
{


$process = new Process(self::builder($prefix, $args));
$process->run();

Expand Down
70 changes: 35 additions & 35 deletions src/Helpers/Deployer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ 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 shared',
'touch ldh.json'
'touch ldh.json',
]);
$connection->task('init');
}
Expand All @@ -34,11 +34,11 @@ public static function doDeploy(Connection $connection, $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
ksort($ldh);
Expand All @@ -48,35 +48,35 @@ public static function doDeploy(Connection $connection, $stage, $branch, $ldh)

// Check versions
// Operators: http://php.net/manual/en/function.version-compare.php
verbose('[' . $stage . '] Checking dependencies. Migth take a minute.');
verbose('['.$stage.'] Checking dependencies. Migth take a minute.');
foreach ($versions as $app => $version) {
SSH::checkAppVersion($connection, $app, $version);
}

// Define the deploy
$connection->define('deploy', [
'mkdir ' . $home . '/releases/' . $releaseName,
'cd ' . $home . '/releases/' . $releaseName,
'git clone -b ' . $branch . ' ' . config('laravel-deploy-helper.stages.' . $stage . '.git.http') . ' .'
'mkdir '.$home.'/releases/'.$releaseName,
'cd '.$home.'/releases/'.$releaseName,
'git clone -b '.$branch.' '.config('laravel-deploy-helper.stages.'.$stage.'.git.http').' .',
]);

// Pre-flight for shared stuff
$items['directories'] = [];
foreach ($shared['directories'] as $share) {
verbose('[' . $stage . '] About to share direcroty "' . $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 direcroty "'.$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
Expand All @@ -87,36 +87,36 @@ public static function doDeploy(Connection $connection, $stage, $branch, $ldh)

$items = [];
foreach ($commands as $command) {
$items[] = 'cd ' . $home . '/releases/' . $releaseName . ' && ' . $command;
$items[] = 'cd '.$home.'/releases/'.$releaseName.' && '.$command;
}
// Define commands
$connection->define('definedCommands', $items);

// Define post deploy actions
$connection->define('postDeploy', [
'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
$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;
}
$connection->define('removeOld', $items);

// Execute them!
verbose('[' . $stage . '] Deploying ' . $branch . ' to server');
verbose('['.$stage.'] Deploying '.$branch.' to server');
$connection->task('deploy');
verbose('[' . $stage . '] Handling shared files');
verbose('['.$stage.'] Handling shared files');
$connection->task('getSharedFiles');
verbose('[' . $stage . '] Handling shared directories');
verbose('['.$stage.'] Handling shared directories');
$connection->task('getSharedDirectories');
verbose('[' . $stage . '] Handling commands');
verbose('['.$stage.'] Handling commands');
$connection->task('definedCommands');
verbose('[' . $stage . '] Clean up and linking new instance');
verbose('['.$stage.'] Clean up and linking new instance');
$connection->task('postDeploy');
verbose('[' . $stage . '] Remove old deploys');
verbose('['.$stage.'] Remove old deploys');
$connection->task('removeOld');

$ldh[$releaseName] = true;
Expand All @@ -126,15 +126,15 @@ public static function doDeploy(Connection $connection, $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 Down
Loading