add check version and refactpr auto update

This commit is contained in:
jayvirsinh_gohil
2019-11-14 21:00:17 +05:30
parent 0d9ca2a0fd
commit 917b618ac7
7 changed files with 37 additions and 19 deletions

View File

@ -14,8 +14,6 @@ class UpdateFinished
{ {
use Dispatchable; use Dispatchable;
public $alias;
public $new; public $new;
public $old; public $old;
@ -25,9 +23,8 @@ class UpdateFinished
* *
* @return void * @return void
*/ */
public function __construct($alias, $old, $new) public function __construct($old, $new)
{ {
$this->alias = $alias;
$this->old = $old; $this->old = $old;
$this->new = $new; $this->new = $new;
} }

View File

@ -4,6 +4,7 @@ namespace Laraspace\Http\Controllers;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Laraspace\Space\Updater; use Laraspace\Space\Updater;
use Laraspace\Space\SiteApi;
class UpdateController extends Controller class UpdateController extends Controller
{ {
@ -11,7 +12,16 @@ class UpdateController extends Controller
{ {
set_time_limit(600); // 10 minutes set_time_limit(600); // 10 minutes
$json = Updater::update($request->alias, $request->installed, $request->version); $json = Updater::update($request->installed, $request->version);
return response()->json($json);
}
public function checkLatestVersion(Request $request)
{
set_time_limit(600); // 10 minutes
$json = Updater::checkForUpdate();
return response()->json($json); return response()->json($json);
} }

View File

@ -4,8 +4,6 @@ namespace Laraspace\Listeners\Updates;
class Listener class Listener
{ {
const ALIAS = '';
const VERSION = ''; const VERSION = '';
/** /**
@ -16,11 +14,6 @@ class Listener
*/ */
protected function check($event) 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 // Do not apply to the same or newer versions
// if (version_compare($event->old, static::VERSION, '>=')) { // if (version_compare($event->old, static::VERSION, '>=')) {
// return false; // return false;

View File

@ -11,8 +11,6 @@ use Laraspace\Setting;
class Version101 extends Listener class Version101 extends Listener
{ {
const ALIAS = 'core';
const VERSION = '1.0.1'; const VERSION = '1.0.1';
/** /**

View File

@ -11,7 +11,7 @@ trait SiteApi
protected static function getRemote($url, $data = array()) protected static function getRemote($url, $data = array())
{ {
$base = 'http://download-test.test/'; $base = 'http://crater-main.test/';
$client = new Client(['verify' => false, 'base_uri' => $base]); $client = new Client(['verify' => false, 'base_uri' => $base]);

View File

@ -7,17 +7,18 @@ use Artisan;
use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Exception\RequestException;
use Laraspace\Space\SiteApi; use Laraspace\Space\SiteApi;
use Laraspace\Events\UpdateFinished; use Laraspace\Events\UpdateFinished;
use Laraspace\Setting;
class Updater class Updater
{ {
use SiteApi; use SiteApi;
public static function update($alias, $installed, $version) public static function update($installed, $version)
{ {
$data = null; $data = null;
$path = null; $path = null;
$url = '/api/download'; $url = '/download/'.$version;
$response = static::getRemote($url, ['timeout' => 100, 'track_redirects' => true]); $response = static::getRemote($url, ['timeout' => 100, 'track_redirects' => true]);
@ -68,7 +69,7 @@ class Updater
// Delete zip file // Delete zip file
File::delete($file); File::delete($file);
if (!File::copyDirectory($temp_path2, base_path())) { if (!File::copyDirectory($temp_path2.'/crater', base_path())) {
return false; return false;
} }
@ -77,7 +78,7 @@ class Updater
File::deleteDirectory($temp_path2); File::deleteDirectory($temp_path2);
try { try {
event(new UpdateFinished($alias, $installed, $version)); event(new UpdateFinished($installed, $version));
return [ return [
'success' => true, 'success' => true,
@ -92,4 +93,18 @@ class Updater
]; ];
} }
} }
public static function checkForUpdate()
{
$data = null;
$url = '/check/latest/download/'.Setting::getSetting('version');
$response = static::getRemote($url, ['timeout' => 100, 'track_redirects' => true]);
if ($response && ($response->getStatusCode() == 200)) {
$data = $response->getBody()->getContents();
}
return json_decode($data);
}
} }

View File

@ -129,6 +129,11 @@ Route::group(['middleware' => 'api'], function () {
'uses' => 'UpdateController@update' 'uses' => 'UpdateController@update'
]); ]);
Route::get('/check/update', [
'as' => 'check.update',
'uses' => 'UpdateController@checkLatestVersion'
]);
Route::get('/bootstrap', [ Route::get('/bootstrap', [
'as' => 'bootstrap', 'as' => 'bootstrap',
'uses' => 'UsersController@getBootstrap' 'uses' => 'UsersController@getBootstrap'