Skip to content

Commit

Permalink
added update command
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdulici committed Dec 3, 2019
1 parent 066ac38 commit e2f73ec
Show file tree
Hide file tree
Showing 13 changed files with 429 additions and 234 deletions.
49 changes: 49 additions & 0 deletions app/Console/Commands/FinishUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Console\Commands;

use App\Events\Install\UpdateFinished;
use Illuminate\Console\Command;

class FinishUpdate extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'update:finish {alias} {company_id} {new} {old}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Finish the update process through CLI';

/**
* Create a new command instance.
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
set_time_limit(900); // 15 minutes

$this->info('Finishing update...');

session(['company_id' => $this->argument('company_id')]);

$this->call('cache:clear');

event(new UpdateFinished($this->argument('alias'), $this->argument('new'), $this->argument('old')));
}
}
158 changes: 158 additions & 0 deletions app/Console/Commands/Update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php

namespace App\Console\Commands;

use App\Utilities\Updater;
use App\Utilities\Versions;
use Illuminate\Console\Command;

class Update extends Command
{
const CMD_SUCCESS = 0;

const CMD_ERROR = 1;

public $alias;

public $new;

public $old;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'update {alias} {company_id} {new=latest}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Allows to update Akaunting directly through CLI';

/**
* Create a new command instance.
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
set_time_limit(900); // 15 minutes

$this->alias = $this->argument('alias');

$this->new = $this->getNewVersion();

$this->old = $this->getOldVersion();

session(['company_id' => $this->argument('company_id')]);

if (!$path = $this->download()) {
return self::CMD_ERROR;
}

if (!$this->unzip($path)) {
return self::CMD_ERROR;
}

if (!$this->copyFiles($path)) {
return self::CMD_ERROR;
}

if (!$this->finish()) {
return self::CMD_ERROR;
}

return self::CMD_SUCCESS;
}

public function getNewVersion()
{
$new = $this->argument('new');

if ($new == 'latest') {
$modules = ($this->alias == 'core') ? [] : [$this->alias];

$new = Versions::latest($modules)[$this->alias];
}

return $new;
}

public function getOldVersion()
{
return ($this->alias == 'core')
? version('short')
: module($this->alias)->get('version');
}

public function download()
{
$this->info('Downloading update...');

try {
$path = Updater::download($this->alias, $this->new, $this->old);
} catch (\Exception $e) {
$this->error($e->getMessage());

return false;
}

return $path;
}

public function unzip($path)
{
$this->info('Unzipping update...');

try {
Updater::unzip($path, $this->alias, $this->new, $this->old);
} catch (\Exception $e) {
$this->error($e->getMessage());

return false;
}

return true;
}

public function copyFiles($path)
{
$this->info('Copying update files...');

try {
Updater::copyFiles($path, $this->alias, $this->new, $this->old);
} catch (\Exception $e) {
$this->error($e->getMessage());

return false;
}

return true;
}

public function finish()
{
$this->info('Finishing update...');

try {
Updater::finish($this->alias, $this->new, $this->old);
} catch (\Exception $e) {
$this->error($e->getMessage());

return false;
}

return true;
}
}
117 changes: 95 additions & 22 deletions app/Http/Controllers/Install/Updates.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use App\Utilities\Updater;
use App\Utilities\Versions;
use Illuminate\Http\Request;
use Module;

class Updates extends Controller
{
Expand Down Expand Up @@ -111,31 +110,31 @@ public function steps(Request $request)
// Download
$steps[] = [
'text' => trans('modules.installation.download', ['module' => $name]),
'url' => url('install/updates/download')
'url' => route('updates.download'),
];

// Unzip
$steps[] = [
'text' => trans('modules.installation.unzip', ['module' => $name]),
'url' => url('install/updates/unzip')
'url' => route('updates.unzip'),
];

// File Copy
// Copy files
$steps[] = [
'text' => trans('modules.installation.file_copy', ['module' => $name]),
'url' => url('install/updates/file-copy')
'url' => route('updates.copy'),
];

// Finish installation
// Finish/Apply
$steps[] = [
'text' => trans('modules.installation.finish', ['module' => $name]),
'url' => url('install/updates/finish')
'url' => route('updates.finish'),
];

// Redirect
$steps[] = [
'text' => trans('modules.installation.redirect', ['module' => $name]),
'url' => url('install/updates/redirect')
'url' => route('updates.redirect'),
];

return response()->json([
Expand All @@ -155,9 +154,27 @@ public function steps(Request $request)
*/
public function download(Request $request)
{
set_time_limit(600); // 10 minutes

$json = Updater::download($request['alias'], $request['version'], $request['installed']);
set_time_limit(900); // 15 minutes

try {
$path = Updater::download($request['alias'], $request['version'], $request['installed']);

$json = [
'success' => true,
'error' => false,
'message' => null,
'data' => [
'path' => $path,
],
];
} catch (\Exception $e) {
$json = [
'success' => false,
'error' => true,
'message' => $e->getMessage(),
'data' => [],
];
}

return response()->json($json);
}
Expand All @@ -171,9 +188,27 @@ public function download(Request $request)
*/
public function unzip(Request $request)
{
set_time_limit(600); // 10 minutes

$json = Updater::unzip($request['path'], $request['alias'], $request['version'], $request['installed']);
set_time_limit(900); // 15 minutes

try {
$path = Updater::unzip($request['path'], $request['alias'], $request['version'], $request['installed']);

$json = [
'success' => true,
'error' => false,
'message' => null,
'data' => [
'path' => $path,
],
];
} catch (\Exception $e) {
$json = [
'success' => false,
'error' => true,
'message' => $e->getMessage(),
'data' => [],
];
}

return response()->json($json);
}
Expand All @@ -185,11 +220,29 @@ public function unzip(Request $request)
*
* @return Response
*/
public function fileCopy(Request $request)
public function copyFiles(Request $request)
{
set_time_limit(600); // 10 minutes

$json = Updater::fileCopy($request['path'], $request['alias'], $request['version'], $request['installed']);
set_time_limit(900); // 15 minutes

try {
$path = Updater::copyFiles($request['path'], $request['alias'], $request['version'], $request['installed']);

$json = [
'success' => true,
'error' => false,
'message' => null,
'data' => [
'path' => $path,
],
];
} catch (\Exception $e) {
$json = [
'success' => false,
'error' => true,
'message' => $e->getMessage(),
'data' => [],
];
}

return response()->json($json);
}
Expand All @@ -203,7 +256,25 @@ public function fileCopy(Request $request)
*/
public function finish(Request $request)
{
$json = Updater::finish($request['alias'], $request['version'], $request['installed']);
set_time_limit(900); // 15 minutes

try {
Updater::finish($request['alias'], $request['version'], $request['installed']);

$json = [
'success' => true,
'error' => false,
'message' => null,
'data' => [],
];
} catch (\Exception $e) {
$json = [
'success' => false,
'error' => true,
'message' => $e->getMessage(),
'data' => [],
];
}

return response()->json($json);
}
Expand All @@ -215,13 +286,15 @@ public function finish(Request $request)
*
* @return Response
*/
public function redirect(Request $request)
public function redirect()
{
return response()->json([
$json = [
'success' => true,
'errors' => false,
'redirect' => route('updates.index'),
'data' => [],
]);
];

return response()->json($json);
}
}

0 comments on commit e2f73ec

Please sign in to comment.