Skip to content

Commit

Permalink
Auto update #449 (#545)
Browse files Browse the repository at this point in the history
* Add custom repository type for updates

* Direct to self update module

* Formatting
  • Loading branch information
nabeelio authored Feb 10, 2020
1 parent 3fcd378 commit 99118da
Show file tree
Hide file tree
Showing 14 changed files with 279 additions and 47 deletions.
41 changes: 30 additions & 11 deletions app/Http/Controllers/Admin/MaintenanceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Contracts\Controller;
use App\Repositories\KvpRepository;
use App\Services\CronService;
use App\Services\VersionService;
use Codedge\Updater\UpdaterManager;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;
Expand All @@ -16,23 +17,26 @@ class MaintenanceController extends Controller
private $cronSvc;
private $kvpRepo;
private $updateManager;
private $versionSvc;

public function __construct(
CronService $cronSvc,
KvpRepository $kvpRepo,
UpdaterManager $updateManager
UpdaterManager $updateManager,
VersionService $versionSvc
) {
$this->cronSvc = $cronSvc;
$this->kvpRepo = $kvpRepo;
$this->updateManager = $updateManager;
$this->versionSvc = $versionSvc;
}

public function index()
{
return view('admin.maintenance.index', [
'cron_path' => $this->cronSvc->getCronExecString(),
'cron_problem_exists' => $this->cronSvc->cronProblemExists(),
'new_version' => $this->kvpRepo->get('new_version_available', false),
'new_version' => true, //$this->kvpRepo->get('new_version_available', false),
'new_version_tag' => $this->kvpRepo->get('latest_version_tag'),
]);
}
Expand Down Expand Up @@ -71,26 +75,41 @@ public function cache(Request $request)
}

/**
* Update the phpVMS install
* Force an update check
*
* @param \Illuminate\Http\Request $request
*
* @return mixed
*/
public function update(Request $request)
public function forcecheck(Request $request)
{
$this->versionSvc->isNewVersionAvailable();

$new_version_avail = $this->kvpRepo->get('new_version_available', false);
$new_version_tag = $this->kvpRepo->get('latest_version_tag');

Log::info('Force check, available='.$new_version_avail.', tag='.$new_version_tag);

if (!$new_version_avail) {
Flash::error('A newer version is not available!');
return redirect(route('admin.maintenance.index'));
Flash::success('No new version available');
} else {
Flash::success('New version available: '.$new_version_tag);
}

return redirect(route('admin.maintenance.index'));
}

/**
* Update the phpVMS install
*
* @param \Illuminate\Http\Request $request
*
* @return mixed
*/
public function update(Request $request)
{
$new_version_tag = $this->kvpRepo->get('latest_version_tag');
Log::info('Attempting to update to '.$new_version_tag);

$this->updateManager->source()->update($new_version_tag);

Flash::success('phpVMS was updated!');
return redirect(route('/update'));
return redirect('/update/downloader');
}
}
4 changes: 4 additions & 0 deletions app/Http/Routes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ static function () {
->name('maintenance.update')
->middleware('ability:admin,maintenance');

Route::match(['post'], 'maintenance/forcecheck', 'MaintenanceController@forcecheck')
->name('maintenance.forcecheck')
->middleware('ability:admin,maintenance');

// subfleet
Route::get('subfleets/export', 'SubfleetController@export')
->name('subfleets.export')
Expand Down
11 changes: 2 additions & 9 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
use App\Notifications\EventHandler;
use Codedge\Updater\Events\UpdateAvailable;
use Codedge\Updater\Events\UpdateSucceeded;
use Codedge\Updater\Listeners\SendUpdateAvailableNotification;
use Codedge\Updater\Listeners\SendUpdateSucceededNotification;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
Expand All @@ -38,13 +36,8 @@ class EventServiceProvider extends ServiceProvider
AwardListener::class,
],

UpdateAvailable::class => [
SendUpdateAvailableNotification::class,
],

UpdateSucceeded::class => [
SendUpdateSucceededNotification::class,
],
UpdateAvailable::class => [],
UpdateSucceeded::class => [],

];

Expand Down
2 changes: 2 additions & 0 deletions app/Services/VersionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ private function getLatestVersionGithub()
foreach ($releases as $release) {
if ($release['prerelease'] === true) {
if ($include_prerelease) {
Log::info('Found latest pre-release of '.$release['tag_name']);
return $this->setLatestRelease(
$release['tag_name'],
$this->getGithubAsset($release)
Expand All @@ -104,6 +105,7 @@ private function getLatestVersionGithub()
continue;
}

Log::info('Found latest release of '.$release['tag_name']);
return $this->setLatestRelease(
$release['tag_name'],
$this->getGithubAsset($release)
Expand Down
17 changes: 17 additions & 0 deletions app/Support/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,21 @@ public function get($uri, array $opts = [])

return $body;
}

/**
* Download a file to a given path
*
* @param $uri
* @param $local_path
*
* @return string
*/
public function download($uri, $local_path)
{
$response = $this->httpClient->request('GET', $uri, [
'sink' => $local_path,
]);

return $response;
}
}
1 change: 1 addition & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
'Session' => Illuminate\Support\Facades\Session::class,
'Storage' => Illuminate\Support\Facades\Storage::class,
'Theme' => Igaster\LaravelTheme\Facades\Theme::class,
'Updater' => Codedge\Updater\UpdaterFacade::class,
'URL' => Illuminate\Support\Facades\URL::class,
'Utils' => App\Facades\Utils::class,
'Validator' => Illuminate\Support\Facades\Validator::class,
Expand Down
5 changes: 5 additions & 0 deletions config/phpvms.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@
*/
'version_file' => 'https://api.github.com/repos/nabeelio/phpvms/releases',

/**
* The URL to download the latest phpVMS version from
*/
'distrib_url' => 'http://downloads.phpvms.net/phpvms-{VERSION}.zip',

/*
* Where the KVP file is stored
*/
Expand Down
22 changes: 11 additions & 11 deletions config/self-update.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
<?php

return [
'default' => 'http',
'version_installed' => env('SELF_UPDATER_VERSION_INSTALLED', ''),
'default' => 'vms',
'version_installed' => '',

'repository_types' => [
'github' => [
'type' => 'github',
'repository_vendor' => 'nabeelio',
'repository_name' => 'phpvms',
'repository_url' => 'https://github.com/nabeelio/phpvms',
'download_path' => env('SELF_UPDATER_DOWNLOAD_PATH', '/tmp'),
'download_path' => storage_path('app'),
'private_access_token' => '',
'use_branch' => 'master',
],
'http' => [
'type' => 'http',
'repository_url' => 'http://downloads.phpvms.net',
'pkg_filename_format' => 'phpvms-_VERSION_.zip',
'download_path' => env('SELF_UPDATER_DOWNLOAD_PATH', '/tmp'),
'private_access_token' => env('SELF_UPDATER_HTTP_PRIVATE_ACCESS_TOKEN', ''),
'use_branch' => '',
],
],

'mail_to' => [
'address' => 'no-reply@phpvms.net',
'name' => 'no name',
'subject_update_available' => 'Update available',
'subject_update_succeeded' => 'Update succeeded',
],

'exclude_folders' => [
'node_modules',
'bootstrap/cache',
Expand Down
44 changes: 43 additions & 1 deletion modules/Updater/Http/Controllers/UpdateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,42 @@
namespace Modules\Updater\Http\Controllers;

use App\Contracts\Controller;
use App\Repositories\KvpRepository;
use App\Services\Installer\InstallerService;
use App\Services\Installer\MigrationService;
use App\Services\Installer\SeederService;
use Codedge\Updater\UpdaterManager;
use function count;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class UpdateController extends Controller
{
private $installerSvc;
private $kvpRepo;
private $migrationSvc;
private $seederSvc;
private $updateManager;

/**
* @param InstallerService $installerSvc
* @param MigrationService $migrationSvc
* @param SeederService $seederSvc
* @param KvpRepository $kvpRepo
* @param UpdaterManager $updateManager
*/
public function __construct(
InstallerService $installerSvc,
KvpRepository $kvpRepo,
MigrationService $migrationSvc,
SeederService $seederSvc
SeederService $seederSvc,
UpdaterManager $updateManager
) {
$this->migrationSvc = $migrationSvc;
$this->seederSvc = $seederSvc;
$this->installerSvc = $installerSvc;
$this->kvpRepo = $kvpRepo;
$this->updateManager = $updateManager;
}

/**
Expand Down Expand Up @@ -94,4 +104,36 @@ public function complete(Request $request)
{
return redirect('/login');
}

/**
* Show the update page with the latest version
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function updater(Request $request)
{
$version = $this->kvpRepo->get('latest_version_tag');

return view('updater::downloader/downloader', [
'version' => $version,
]);
}

/**
* Download the actual update and then forward the user to the updater page
*
* @param \Illuminate\Http\Request $request
*
* @return mixed
*/
public function update_download(Request $request)
{
$version = $this->kvpRepo->get('latest_version_tag');
$this->updateManager->source('github')->update($version);

Log::info('Update completed to '.$version.', redirecting');
return redirect('/update');
}
}
Loading

0 comments on commit 99118da

Please sign in to comment.