Add a /update to update an install #164

This commit is contained in:
Nabeel Shahzad 2018-02-05 16:16:24 -06:00
parent eebf7871cb
commit 282421deb8
21 changed files with 232 additions and 20 deletions

View File

@ -19,6 +19,7 @@ use App\Http\Controllers\Controller;
use Modules\Installer\Services\DatabaseService;
use Modules\Installer\Services\ConfigService;
use Modules\Installer\Services\MigrationService;
use Modules\Installer\Services\RequirementsService;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
@ -29,6 +30,7 @@ class InstallerController extends Controller
$analyticsSvc,
$dbService,
$envService,
$migrationSvc,
$reqService,
$userService;
@ -37,6 +39,7 @@ class InstallerController extends Controller
AnalyticsService $analyticsSvc,
DatabaseService $dbService,
ConfigService $envService,
MigrationService $migrationSvc,
RequirementsService $reqService,
UserService $userService
) {
@ -44,6 +47,7 @@ class InstallerController extends Controller
$this->analyticsSvc = $analyticsSvc;
$this->dbService = $dbService;
$this->envService = $envService;
$this->migrationSvc = $migrationSvc;
$this->reqService = $reqService;
$this->userService = $userService;
}
@ -56,7 +60,7 @@ class InstallerController extends Controller
return view('installer::errors/already-installed');
}
return view('installer::index-start');
return view('installer::install/index-start');
}
protected function testDb(Request $request)
@ -127,7 +131,7 @@ class InstallerController extends Controller
# Make sure there are no false values
$passed = !\in_array(false, $statuses, true);
return view('installer::steps/step1-requirements', [
return view('installer::install/steps/step1-requirements', [
'php' => $php_version,
'extensions' => $extensions,
'directories' => $directories,
@ -141,7 +145,7 @@ class InstallerController extends Controller
public function step2(Request $request)
{
$db_types = ['mysql' => 'mysql', 'sqlite' => 'sqlite'];
return view('installer::steps/step2-db', [
return view('installer::install/steps/step2-db', [
'db_types' => $db_types,
]);
}
@ -204,6 +208,7 @@ class InstallerController extends Controller
try {
$console_out .= $this->dbService->setupDB();
$console_out .= $this->migrationSvc->runAllMigrations();
} catch(QueryException $e) {
flash()->error($e->getMessage());
return redirect(route('installer.step2'))->withInput();
@ -211,7 +216,7 @@ class InstallerController extends Controller
$console_out = trim($console_out);
return view('installer::steps/step2a-db_output', [
return view('installer::install/steps/step2a-db_output', [
'console_output' => $console_out
]);
}
@ -221,7 +226,7 @@ class InstallerController extends Controller
*/
public function step3(Request $request)
{
return view('installer::steps/step3-user', []);
return view('installer::install/steps/step3-user', []);
}
/**
@ -283,7 +288,7 @@ class InstallerController extends Controller
$this->analyticsSvc->sendInstall();
return view('installer::steps/step3a-completed', []);
return view('installer::install/steps/step3a-completed', []);
}
/**

View File

@ -0,0 +1,75 @@
<?php
namespace Modules\Installer\Http\Controllers;
use Log;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Modules\Installer\Services\MigrationService;
class UpdaterController extends Controller
{
protected $migrationSvc;
public function __construct(
MigrationService $migrationSvc
) {
$this->migrationSvc = $migrationSvc;
}
/**
* Display a listing of the resource.
*/
public function index()
{
return view('installer::update/index-start');
}
/**
* Step 1. Check if there's an update available. Check if there
* are any unrun migrations
*/
public function step1(Request $request)
{
$migrations = $this->migrationSvc->migrationsAvailable();
if(\count($migrations) > 0) {
return view('installer::update/steps/step1-update-available');
}
Log::info('No migrations found');
return view('installer::update/steps/step1-no-update');
}
/**
* Step 2 Run all of the migrations
* @param Request $request
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function run_migrations(Request $request)
{
Log::info('Update: run_migrations', $request->post());
$migrations = $this->migrationSvc->migrationsAvailable();
if(\count($migrations) === 0) {
return redirect(route('update.complete'));
}
$output = $this->migrationSvc->runAllMigrations();
return view('installer::update/steps/step2-migrations-done', [
'console_output' => $output,
]);
}
/**
* Final step
* @param Request $request
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function complete(Request $request)
{
return redirect('/login');
}
}

View File

@ -1,3 +1,9 @@
<?php
Route::get('/update', 'InstallerController@index');
Route::get('/', 'UpdaterController@index')->name('index');
Route::get('/step1', 'UpdaterController@step1')->name('step1');
Route::post('/step1', 'UpdaterController@step1')->name('step1');
Route::post('/run-migrations', 'UpdaterController@run_migrations')->name('run_migrations');
Route::get('/complete', 'UpdaterController@complete')->name('complete');

View File

@ -44,8 +44,8 @@ class InstallerServiceProvider extends ServiceProvider
});
Route::group([
'as' => 'installer.',
'prefix' => 'install',
'as' => 'update.',
'prefix' => 'update',
'middleware' => ['web'],
'namespace' => 'Modules\Installer\Http\Controllers'
], function () {

View File

@ -0,0 +1,12 @@
@extends('installer::app')
@section('title', 'Update phpVMS')
@section('content')
<h2>phpvms updater</h2>
<p>Press continue to check if there are any updates available.</p>
{!! Form::open(['route' => 'update.step1', 'method' => 'post']) !!}
<p style="text-align: right">
{!! Form::submit('Start >>', ['class' => 'btn btn-success']) !!}
</p>
{!! Form::close() !!}
@endsection

View File

@ -0,0 +1,13 @@
@extends('installer::app')
@section('title', 'Update phpVMS')
@section('content')
<h2>phpvms updater</h2>
<p>It seems like you're up to date!</p>
{!! Form::open(['route' => 'update.complete', 'method' => 'GET']) !!}
<p style="text-align: right">
{!! Form::submit('Complete >>', ['class' => 'btn btn-success']) !!}
</p>
{!! Form::close() !!}
@endsection

View File

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

View File

@ -0,0 +1,18 @@
@extends('installer::app')
@section('title', 'Update Completed')
@section('content')
<div style="align-content: center;">
{!! Form::open(['route' => 'update.complete', 'method' => 'GET']) !!}
<pre class="lang-sh">
<code class="lang-sh">
{!! $console_output !!}
</code>
</pre>
<p style="text-align: right">
{!! Form::submit('Complete >>', ['class' => 'btn btn-success']) !!}
</p>
{!! Form::close() !!}
</div>
@endsection

View File

@ -51,12 +51,6 @@ class DatabaseService {
$output .= \Artisan::output();
}
\Artisan::call('migrate');
$output .= trim(\Artisan::output());
\Artisan::call('db:seed');
$output .= trim(\Artisan::output());
return trim($output);
}
}

View File

@ -0,0 +1,77 @@
<?php
namespace Modules\Installer\Services;
use Log;
use Nwidart\Modules\Facades\Module;
/**
* Class MigrationsService
* @package Modules\Installer\Services
*/
class MigrationService
{
/**
* @return \Illuminate\Database\Migrations\Migrator
*/
protected function getMigrator()
{
$m = app('migrator');
$m->setConnection(config('database.default'));
return $m;
}
/**
* Find all of the possible paths that migrations exist.
* Include looking in all of the modules Database/migrations directories
* @return array
*/
public function getMigrationPaths(): array
{
$paths = [
'core' => \App::databasePath() . '/migrations'
];
$modules = Module::enabled();
foreach ($modules as $module) {
$module_path = $module->getPath() . '/Database/migrations';
if(file_exists($module_path)) {
$paths[$module->getName()] = $module_path;
}
}
Log::info('Update - migration paths', $paths);
return $paths;
}
/**
* Return what migrations are available
*/
public function migrationsAvailable(): array
{
$migrator = $this->getMigrator();
$migration_dirs = $this->getMigrationPaths();
$files = $migrator->getMigrationFiles(array_values($migration_dirs));
$availMigrations = array_diff(array_keys($files), $migrator->getRepository()->getRan());
Log::info('Migrations available:', $availMigrations);
return $availMigrations;
}
/**
* Run all of the migrations that are available. Just call artisan since
* it looks into all of the module directories, etc
*/
public function runAllMigrations()
{
$output = '';
\Artisan::call('migrate');
$output .= trim(\Artisan::output());
return $output;
}
}

View File

@ -1,8 +1,8 @@
<?php
use App\Models\Migrations\Migration;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class $CLASS$ extends Migration
{

View File

@ -1,8 +1,8 @@
<?php
use App\Models\Migrations\Migration;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class $CLASS$ extends Migration
{

View File

@ -1,8 +1,8 @@
<?php
use App\Models\Migrations\Migration;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class $CLASS$ extends Migration
{

View File

@ -1,8 +1,8 @@
<?php
use App\Models\Migrations\Migration;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class $CLASS$ extends Migration
{

View File

@ -1,8 +1,8 @@
<?php
use App\Models\Migrations\Migration;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class $CLASS$ extends Migration
{