Merge pull request #32 from denisdulici/updates

Improved updates
This commit is contained in:
Denis Duliçi 2017-10-02 16:42:04 +03:00 committed by GitHub
commit be69b6ec41
11 changed files with 155 additions and 57 deletions

View File

@ -0,0 +1,26 @@
<?php
namespace App\Events;
class UpdateFinished
{
public $alias;
public $old;
public $new;
/**
* Create a new event instance.
*
* @param $alias
* @param $old
* @param $new
*/
public function __construct($alias, $old, $new)
{
$this->alias = $alias;
$this->old = $old;
$this->new = $new;
}
}

View File

@ -3,9 +3,10 @@
namespace App\Http\Controllers\Install; namespace App\Http\Controllers\Install;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Models\Module\Module as Model; use App\Events\UpdateFinished;
use App\Utilities\Updater; use App\Utilities\Updater;
use App\Utilities\Versions; use App\Utilities\Versions;
use Artisan;
use Module; use Module;
class Updates extends Controller class Updates extends Controller
@ -73,17 +74,50 @@ class Updates extends Controller
/** /**
* Update the core or modules. * Update the core or modules.
* *
* @param $alias
* @param $version
* @return Response * @return Response
*/ */
public function update($alias, $version) public function update($alias, $version)
{ {
set_time_limit(600); // 10 minutes set_time_limit(600); // 10 minutes
$status = Updater::update($alias, $version); if (Updater::update($alias, $version)) {
return redirect('install/updates/post/' . $alias . '/' . version('short') . '/' . $version);
}
// Clear cache in order to check for updates again flash(trans('updates.error'))->error();
Updater::clear();
return redirect()->back(); return redirect()->back();
} }
/**
* Final actions post update.
*
* @param $alias
* @param $old
* @param $new
* @return Response
*/
public function post($alias, $old, $new)
{
// Check if the file mirror was successful
if (($alias == 'core') && (version('short') != $new)) {
flash(trans('updates.error'))->error();
return redirect('install/updates');
}
// Clear cache after update
Artisan::call('cache:clear');
// Update database
Artisan::call('migrate', ['--force' => true]);
event(new UpdateFinished($alias, $old, $new));
flash(trans('updates.success'))->success();
return redirect('install/updates');
}
} }

View File

@ -7,20 +7,11 @@ use Illuminate\Auth\Events\Login as ILogin;
class Login class Login
{ {
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/** /**
* Handle the event. * Handle the event.
* *
* @param Logout $event * @param ILogin $event
* @return void * @return void
*/ */
public function handle(ILogin $event) public function handle(ILogin $event)

View File

@ -7,20 +7,11 @@ use Illuminate\Auth\Events\Logout as ILogout;
class Logout class Logout
{ {
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/** /**
* Handle the event. * Handle the event.
* *
* @param Logout $event * @param ILogout $event
* @return void * @return void
*/ */
public function handle(ILogout $event) public function handle(ILogout $event)

View File

@ -0,0 +1,31 @@
<?php
namespace App\Listeners\Updates;
class Listener
{
const ALIAS = '';
const VERSION = '';
/**
* Check if should listen.
*
* @param $event
* @return boolean
*/
protected function check($event)
{
// Apply only to the specified alias
if ($event->alias != static::ALIAS) {
return false;
}
// Do not apply to the same or newer versions
if ($event->old >= static::VERSION) {
return false;
}
return true;
}
}

View File

@ -0,0 +1,33 @@
<?php
namespace App\Listeners\Updates;
//use App\Models\Auth\Permission;
use App\Events\UpdateFinished;
class Version104 extends Listener
{
const ALIAS = 'core';
const VERSION = '1.0.4';
/**
* Handle the event.
*
* @param $event
* @return void
*/
public function handle(UpdateFinished $event)
{
// Check if should listen
if (!$this->check($event)) {
return;
}
/*Permission::create([
'name' => 'john-doe',
'display_name' => 'John Doe',
'description' => 'John Doe',
]);*/
}
}

View File

@ -13,8 +13,8 @@ class EventServiceProvider extends ServiceProvider
* @var array * @var array
*/ */
protected $listen = [ protected $listen = [
'App\Events\Event' => [ 'App\Events\UpdateFinished' => [
'App\Listeners\EventListener', 'App\Listeners\Updates\Version104',
], ],
'Illuminate\Auth\Events\Login' => [ 'Illuminate\Auth\Events\Login' => [
'App\Listeners\Auth\Login', 'App\Listeners\Auth\Login',

View File

@ -5,7 +5,6 @@ namespace App\Utilities;
use App\Models\Module\Module as Model; use App\Models\Module\Module as Model;
use App\Models\Module\ModuleHistory as ModelHistory; use App\Models\Module\ModuleHistory as ModelHistory;
use App\Traits\SiteApi; use App\Traits\SiteApi;
use Artisan;
use Cache; use Cache;
use Date; use Date;
use File; use File;
@ -28,20 +27,21 @@ class Updater
// Update // Update
public static function update($alias, $version) public static function update($alias, $version)
{ {
// Download file
if (!$data = static::download($alias, $version)) { if (!$data = static::download($alias, $version)) {
return false; return false;
} }
// Create temp directory
$path = 'temp-' . md5(mt_rand()); $path = 'temp-' . md5(mt_rand());
$temp_path = storage_path('app/temp') . '/' . $path; $temp_path = storage_path('app/temp') . '/' . $path;
$file = $temp_path . '/upload.zip';
// Create tmp directory
if (!File::isDirectory($temp_path)) { if (!File::isDirectory($temp_path)) {
File::makeDirectory($temp_path); File::makeDirectory($temp_path);
} }
$file = $temp_path . '/upload.zip';
// Add content to the Zip file // Add content to the Zip file
$uploaded = is_int(file_put_contents($file, $data)) ? true : false; $uploaded = is_int(file_put_contents($file, $data)) ? true : false;
@ -58,37 +58,20 @@ class Updater
$zip->close(); $zip->close();
// Remove Zip
File::delete($file);
if ($alias == 'core') { if ($alias == 'core') {
// Move all files/folders from temp path then delete it // Move all files/folders from temp path
File::copyDirectory($temp_path, base_path()); if (!File::copyDirectory($temp_path, base_path())) {
File::deleteDirectory($temp_path);
// Clear cache after update
Artisan::call('cache:clear');
// Update database
Artisan::call('migrate', ['--force' => true]);
// Check if the file mirror was successful
/*if (version('short') != $version) {
return false; return false;
}*/ }
} else { } else {
// Get module instance
$module = Module::get($alias); $module = Module::get($alias);
$model = Model::where('alias', $alias)->first(); $model = Model::where('alias', $alias)->first();
// Move all files/folders from temp path then delete it // Move all files/folders from temp path
File::copyDirectory($temp_path, module_path($module->get('name'))); if (!File::copyDirectory($temp_path, module_path($module->get('name')))) {
File::deleteDirectory($temp_path); return false;
}
// Clear cache after update
Artisan::call('cache:clear');
// Update database
Artisan::call('migrate', ['--force' => true]);
// Add history // Add history
ModelHistory::create([ ModelHistory::create([
@ -100,6 +83,9 @@ class Updater
]); ]);
} }
// Delete temp directory
File::deleteDirectory($temp_path);
return true; return true;
} }

View File

@ -9,5 +9,7 @@ return [
'check' => 'Check', 'check' => 'Check',
'new_core' => 'An updated version of Akaunting is available.', 'new_core' => 'An updated version of Akaunting is available.',
'latest_core' => 'Congratulations! You have the latest version of Akaunting. Future security updates will be applied automatically.', 'latest_core' => 'Congratulations! You have the latest version of Akaunting. Future security updates will be applied automatically.',
'success' => 'Update process has been completed successfully.',
'error' => 'Update process has failed, please, try again.',
]; ];

View File

@ -138,8 +138,11 @@
@permission('read-install-updates') @permission('read-install-updates')
<!-- Updates: style can be found in dropdown.less --> <!-- Updates: style can be found in dropdown.less -->
<li> <li>
<a href="{{ url('install/updates') }}" title="{{ $updates }} Updates Available"> <a href="{{ url('install/updates') }}" data-toggle="tooltip" data-placement="bottom" title="{{ $updates }} Updates Available">
<i class="fa fa-refresh"></i> <i class="fa fa-refresh"></i>
@if ($updates)
<span class="label label-danger">{{ $updates }}</span>
@endif
</a> </a>
</li> </li>
@endpermission @endpermission

View File

@ -104,7 +104,8 @@ Route::group(['middleware' => ['auth', 'language', 'adminmenu', 'permission:read
Route::group(['prefix' => 'install'], function () { Route::group(['prefix' => 'install'], function () {
Route::get('updates/changelog', 'Install\Updates@changelog'); Route::get('updates/changelog', 'Install\Updates@changelog');
Route::get('updates/check', 'Install\Updates@check'); Route::get('updates/check', 'Install\Updates@check');
Route::get('updates/update/{id}/{version}', 'Install\Updates@update'); Route::get('updates/update/{alias}/{version}', 'Install\Updates@update');
Route::get('updates/post/{alias}/{old}/{new}', 'Install\Updates@post');
Route::resource('updates', 'Install\Updates'); Route::resource('updates', 'Install\Updates');
}); });
}); });