Auto update #449 (#545)

* Add custom repository type for updates

* Direct to self update module

* Formatting
This commit is contained in:
Nabeel S
2020-02-10 17:47:46 -05:00
committed by GitHub
parent 3fcd378f91
commit 99118daad9
14 changed files with 281 additions and 49 deletions

View File

@@ -3,9 +3,11 @@
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;
@@ -13,22 +15,30 @@ 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;
}
/**
@@ -94,4 +104,36 @@ class UpdateController extends Controller
{
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');
}
}

View File

@@ -0,0 +1,124 @@
<?php
namespace Modules\Updater\Lib;
use App\Services\VersionService;
use Codedge\Updater\Contracts\GithubRepositoryTypeContract;
use Codedge\Updater\SourceRepositoryTypes\GithubRepositoryType;
use Codedge\Updater\Traits\SupportPrivateAccessToken;
use Codedge\Updater\Traits\UseVersionFile;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Psr\Http\Message\ResponseInterface;
/**
* Use SourceRepositoryTypes/GithubRepositoryTypes/GithubTagType.php as a reference
* Just replace the new update checks, etc, with the VersionService stubs. They're
* essentially the same except for the current version checks and all that. Adds some
* additional logging too, but the base update method is from GithubRepositoryType
*/
final class VmsRepositoryType extends GithubRepositoryType implements GithubRepositoryTypeContract
{
use UseVersionFile;
use SupportPrivateAccessToken;
/**
* @var Client
*/
protected $client;
/**
* @var VersionService
*/
protected $versionSvc;
public function __construct(array $config, Client $client)
{
$this->config = $config;
$this->client = $client;
$this->storagePath = Str::finish($this->config['download_path'], DIRECTORY_SEPARATOR);
$this->versionSvc = app(VersionService::class);
}
/**
* Check repository if a newer version than the installed one is available.
*
* @param string $currentVersion
*
* @throws InvalidArgumentException
* @throws Exception
*
* @return bool
*/
public function isNewVersionAvailable(string $currentVersion = ''): bool
{
return $this->versionSvc->isNewVersionAvailable($currentVersion);
}
/**
* Get the latest version that has been published in a certain repository.
* Example: 2.6.5 or v2.6.5.
*
* @param string $prepend Prepend a string to the latest version
* @param string $append Append a string to the latest version
*
* @throws Exception
*
* @return string
*/
public function getVersionAvailable(string $prepend = '', string $append = ''): string
{
return $this->versionSvc->getLatestVersion();
}
/**
* Fetches the latest version. If you do not want the latest version, specify one and pass it.
*
* @param string $version
*
* @throws Exception
*
* @return void
*/
public function fetch($version = ''): void
{
$response = $this->getRepositoryReleases();
$releaseCollection = collect(\GuzzleHttp\json_decode($response->getBody()->getContents()));
if ($releaseCollection->isEmpty()) {
throw new Exception('Cannot find a release to update. Please check the repository you\'re pulling from');
}
if (!File::exists($this->storagePath)) {
File::makeDirectory($this->storagePath, 493, true, true);
}
if (!empty($version)) {
$release = $releaseCollection->where('name', $version)->first();
} else {
$release = $releaseCollection->first();
}
Log::info('Found release='.$release->name.', path='.$release->zipball_url);
$storageFolder = $this->storagePath.$release->name.'-'.now()->timestamp;
$storageFilename = $storageFolder.'.zip';
if (!$this->isSourceAlreadyFetched($release->name)) {
$this->downloadRelease($this->client, $release->zipball_url, $storageFilename);
$this->unzipArchive($storageFilename, $storageFolder);
$this->createReleaseFolder($storageFolder, $release->name);
}
}
protected function getRepositoryReleases(): ResponseInterface
{
$url = self::GITHUB_API_URL
.'/repos/'.$this->config['repository_vendor']
.'/'.$this->config['repository_name']
.'/tags';
$headers = [];
return $this->client->request('GET', $url, ['headers' => $headers]);
}
}

View File

@@ -33,6 +33,10 @@ class UpdateServiceProvider extends ServiceProvider
Route::post('/run-migrations', 'UpdateController@run_migrations')->name('run_migrations');
Route::get('/complete', 'UpdateController@complete')->name('complete');
// Routes for the update downloader
Route::get('/downloader', 'UpdateController@updater')->name('updater');
Route::post('/downloader', 'UpdateController@update_download')->name('update_download');
});
}

View File

@@ -0,0 +1,12 @@
@extends('installer::app')
@section('title', 'Update phpVMS')
@section('content')
<h2>phpvms updater</h2>
<p>Click run to complete the update to version {{ $version }}</p>
{{ Form::open(['route' => 'update.update_download', 'method' => 'post']) }}
<p style="text-align: right">
{{ Form::submit('Run >>', ['class' => 'btn btn-success']) }}
</p>
{{ Form::close() }}
@endsection