Improve the update process

This commit is contained in:
cuneytsenturk 2018-09-27 17:08:00 +03:00
parent 070300005e
commit 98e8a8072f
5 changed files with 427 additions and 7 deletions

View File

@ -6,8 +6,10 @@ use App\Http\Controllers\Controller;
use App\Events\UpdateFinished; use App\Events\UpdateFinished;
use App\Utilities\Updater; use App\Utilities\Updater;
use App\Utilities\Versions; use App\Utilities\Versions;
use Illuminate\Http\Request;
use Artisan; use Artisan;
use Module; use Module;
use File;
class Updates extends Controller class Updates extends Controller
{ {
@ -80,15 +82,20 @@ class Updates extends Controller
*/ */
public function update($alias, $version) public function update($alias, $version)
{ {
set_time_limit(600); // 10 minutes if ($alias == 'core') {
$name = 'Akaunting v' . $version;
if (Updater::update($alias, $version)) { $installed = version('short');
return redirect('install/updates/post/' . $alias . '/' . version('short') . '/' . $version); } else {
// Get module instance
$module = Module::findByAlias($alias);
$name = $module->get('name');
$installed = $module->get('version');
} }
flash(trans('updates.error'))->error()->important(); return view('install.updates.edit', compact('alias', 'name', 'installed', 'version'));
return redirect()->back();
} }
/** /**
@ -117,4 +124,231 @@ class Updates extends Controller
return redirect('install/updates'); return redirect('install/updates');
} }
/**
* Show the form for viewing the specified resource.
*
* @param $request
*
* @return Response
*/
public function steps(Request $request)
{
$this->checkApiToken();
$json = [];
$json['step'] = [];
$name = $request['name'];
$version = $request['version'];
// Download
$json['step'][] = [
'text' => trans('modules.installation.download', ['module' => $name]),
'url' => url('install/updates/download')
];
// Unzip
$json['step'][] = [
'text' => trans('modules.installation.unzip', ['module' => $name]),
'url' => url('install/updates/unzip')
];
// File Copy
$json['step'][] = [
'text' => trans('modules.installation.file_copy', ['module' => $name]),
'url' => url('install/updates/file-copy')
];
// Migrate DB and trigger event UpdateFinish event
$json['step'][] = [
'text' => trans('modules.installation.migrate', ['module' => $name]),
'url' => url('install/updates/migrate')
];
// redirect update page
$json['step'][] = [
'text' => trans('modules.installation.finish'),
'url' => url('install/updates/finish')
];
return response()->json($json);
}
/**
* Show the form for viewing the specified resource.
*
* @param $request
*
* @return Response
*/
public function download(Request $request)
{
set_time_limit(600); // 10 minutes
$status = true;
if ($request['alias'] != 'core') {
$this->checkApiToken();
}
// Download file
if (!$data = Updater::download($request['alias'], $request['version'])) {
$status = false;
$message = trans('modules.errors.download', ['module' => $request['name']]);
}
// Create temp directory
$path = 'temp-' . md5(mt_rand());
$temp_path = storage_path('app/temp') . '/' . $path;
if (!File::isDirectory($temp_path)) {
File::makeDirectory($temp_path);
}
$file = $temp_path . '/upload.zip';
// Add content to the Zip file
$uploaded = is_int(file_put_contents($file, $data)) ? true : false;
if (!$uploaded) {
return false;
$message = trans('modules.errors.upload', ['module' => $request['name']]);
}
$json = [
'success' => ($status) ? true : false,
'errors' => (!$status) ? $message : false,
'data' => [
'path' => $path
]
];
return response()->json($json);
}
/**
* Show the form for viewing the specified resource.
*
* @param $request
*
* @return Response
*/
public function unzip(Request $request)
{
set_time_limit(600); // 10 minutes
if ($request['alias'] != 'core') {
$this->checkApiToken();
}
$path = storage_path('app/temp') . '/' . $request['path'];
$file = $path . '/upload.zip';
$result = Updater::unzip($file, $path);
$json = [
'success' => ($result) ? true : false,
'errors' => (!$result) ? trans('modules.errors.unzip', ['module' => $request['name']]) : false,
'data' => [
'path' => $request['path']
]
];
return response()->json($json);
}
/**
* Show the form for viewing the specified resource.
*
* @param $request
*
* @return Response
*/
public function fileCopy(Request $request)
{
set_time_limit(600); // 10 minutes
if ($request['alias'] != 'core') {
$this->checkApiToken();
}
$path = storage_path('app/temp') . '/' . $request['path'];
$result = Updater::fileCopy($request['alias'], $path, $request['version']);
$json = [
'success' => ($result) ? true : false,
'errors' => (!$result) ? trans('modules.errors.file_copy', ['module' => $request['name']]) : false,
'data' => [
'path' => $request['path']
]
];
return response()->json($json);
}
/**
* Show the form for viewing the specified resource.
*
* @param $request
*
* @return Response
*/
public function migrate(Request $request)
{
// Check if the file mirror was successful
if (($request['alias'] == 'core') && (version('short') != $request['version'])) {
$json = [
'success' => false,
'errors' => trans('modules.errors.migrate core', ['module' => $request['name']]),
'data' => []
];
return response()->json($json);
}
// Clear cache after update
Artisan::call('cache:clear');
try {
event(new UpdateFinished($request['alias'], $request['installed'], $request['version']));
$json = [
'success' => true,
'errors' => false,
'data' => []
];
} catch (\Exception $e) {
$json = [
'success' => false,
'errors' => trans('modules.errors.migrate', ['module' => $request['name']]),
'data' => []
];
}
return response()->json($json);
}
/**
* Show the form for viewing the specified resource.
*
* @param $request
*
* @return Response
*/
public function finish(Request $request)
{
$json = [
'success' => true,
'errors' => false,
'redirect' => url("install/updates"),
'data' => [],
];
return response()->json($json);
}
} }

View File

@ -121,6 +121,56 @@ class Updater
return $file; return $file;
} }
public static function unzip($file, $temp_path)
{
// Unzip the file
$zip = new ZipArchive();
if (($zip->open($file) !== true) || !$zip->extractTo($temp_path)) {
return false;
}
$zip->close();
// Delete zip file
File::delete($file);
return true;
}
public static function fileCopy($alias, $temp_path, $version)
{
if ($alias == 'core') {
// Move all files/folders from temp path
if (!File::copyDirectory($temp_path, base_path())) {
return false;
}
} else {
// Get module instance
$module = Module::findByAlias($alias);
$model = Model::where('alias', $alias)->first();
// Move all files/folders from temp path
if (!File::copyDirectory($temp_path, module_path($module->get('name')))) {
return false;
}
// Add history
ModelHistory::create([
'company_id' => session('company_id'),
'module_id' => $model->id,
'category' => $module->get('category'),
'version' => $version,
'description' => trans('modules.history.updated', ['module' => $module->get('name')]),
]);
}
// Delete temp directory
File::deleteDirectory($temp_path);
return true;
}
public static function all() public static function all()
{ {
// Get data from cache // Get data from cache
@ -159,4 +209,4 @@ class Updater
return $data; return $data;
} }
} }

View File

@ -39,9 +39,21 @@ return [
'header' => 'App Installation', 'header' => 'App Installation',
'download' => 'Downloading :module file.', 'download' => 'Downloading :module file.',
'unzip' => 'Extracting :module files.', 'unzip' => 'Extracting :module files.',
'file_copy' => 'Copying :module files.',
'migrate' => 'Applying :module updates.',
'finish' => 'The update was successfully installed. You will be redirect Update Center.',
'install' => 'Installing :module files.', 'install' => 'Installing :module files.',
], ],
'errors' => [
'download' => ':module can not download!',
'upload' => 'Downloaded :module can not saved!',
'unzip' => ':module can not unzip!',
'file_copy' => ':module files can not copy!',
'migrate' => ':module migrate broken!',
'migrate core' => ':module already latest version so then yon can not update.',
],
'badge' => [ 'badge' => [
'installed' => 'Installed', 'installed' => 'Installed',
], ],

View File

@ -0,0 +1,117 @@
@extends('layouts.admin')
@section('title', trans_choice('general.updates', 2))
@section('new_button')
<span class="new-button"><a href="{{ url('install/updates/check') }}" class="btn btn-warning btn-sm"><span class="fa fa-history"></span> &nbsp;{{ trans('updates.check') }}</a></span>
@endsection
@section('content')
<!-- Default box -->
<div class="box box-success">
<div class="box-header with-border">
<i class="fa fa-gear"></i>
<h3 class="box-title">{{ $name }}</h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<p>
<div class="progress">
<div id="progress-bar" class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%">
<span class="sr-only">{{ trans('modules.installation.start', ['module' => $name]) }}</span>
</div>
</div>
<div id="progress-text"></div>
</p>
</div>
<!-- /.box-body -->
</div>
<!-- /.box -->
@endsection
@push('scripts')
<script type="text/javascript">
var step = new Array();
var total = 0;
var path = '';
$(document).ready(function() {
$.ajax({
url: '{{ url("install/updates/steps") }}',
type: 'post',
dataType: 'json',
data: {name: '{{ $name }}', version: '{{ $version }}'},
headers: { 'X-CSRF-TOKEN': '{{ csrf_token() }}' },
success: function(json) {
if (json['errorr']) {
$('#progress-bar').addClass('progress-bar-danger');
$('#progress-text').html('<div class="text-danger">' + json['error'] + '</div>');
}
if (json['step']) {
step = json['step'];
total = step.length;
next();
}
}
});
});
function next() {
data = step.shift();
if (data) {
$('#progress-bar').css('width', (100 - (step.length / total) * 100) + '%');
$.each($('#progress-text .text-default'), function( index, value ) {
// Remove Loading font
$(this).find('.update-spin').remove();
// Remove Check font
$(this).find('.update-check').remove();
// Add Check font
$(this).append(' <i class="fa fa-check update-check text-success"></i>');
});
$('#progress-text').append('<span class="text-default"><i class="fa fa-spinner fa-spin update-spin"></i> ' + data['text'] + '</span> </br>');
setTimeout(function() {
$.ajax({
url: data.url,
type: 'post',
dataType: 'json',
data: {path: path, alias: '{{ $alias }}', installed: '{{ $installed }}', version: '{{ $version }}'},
headers: { 'X-CSRF-TOKEN': '{{ csrf_token() }}' },
success: function(json) {
if (json['errors']) {
$('#progress-bar').addClass('progress-bar-danger');
$('#progress-text').append('<div class="text-danger"><i class="fa fa-times update-error"></i> ' + json['errors'] + '</div>');
}
if (json['success']) {
$('#progress-bar').removeClass('progress-bar-danger');
$('#progress-bar').addClass('progress-bar-success');
}
if (json['data']['path']) {
path = json['data']['path'];
}
if (!json['errors'] && !json['redirect']) {
next();
}
if (json['redirect']) {
window.location = json['redirect'];
}
},
error: function(xhr, ajaxOptions, thrownError) {
alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
}, 800);
}
}
</script>
@endpush

View File

@ -161,6 +161,13 @@ Route::group(['middleware' => 'language'], function () {
Route::get('updates/check', 'Install\Updates@check'); Route::get('updates/check', 'Install\Updates@check');
Route::get('updates/update/{alias}/{version}', 'Install\Updates@update'); Route::get('updates/update/{alias}/{version}', 'Install\Updates@update');
Route::get('updates/post/{alias}/{old}/{new}', 'Install\Updates@post'); Route::get('updates/post/{alias}/{old}/{new}', 'Install\Updates@post');
Route::post('updates/steps', 'Install\Updates@steps');
Route::post('updates/download', 'Install\Updates@download');
Route::post('updates/download', 'Install\Updates@download');
Route::post('updates/unzip', 'Install\Updates@unzip');
Route::post('updates/file-copy', 'Install\Updates@fileCopy');
Route::post('updates/migrate', 'Install\Updates@migrate');
Route::post('updates/finish', 'Install\Updates@finish');
Route::resource('updates', 'Install\Updates'); Route::resource('updates', 'Install\Updates');
}); });