updates automatically handled on the background

This commit is contained in:
Čarodej
2022-04-05 16:43:12 +02:00
parent 3649b6b7cf
commit 993dfc8fa1
21 changed files with 141 additions and 243 deletions

View File

@@ -1,6 +1,6 @@
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:zJANxvpTkxQwXRdrlTxjTYyFQ0vTv0DZRE7yNtVXmIA=
APP_KEY=base64:qpZihuLxIkDkK3rsQND5Ksv10zVWQ3yih2KiuY6IfXk=
APP_DEBUG=true
APP_URL=http://localhost
APP_DEMO=false

View File

@@ -9,6 +9,7 @@
- [Installation](#installation)
- [Nginx Configuration](#nginx-configuration)
- [Apache Configuration](#apache-configuration)
- [Updating Application](#updating-application)
- [Developers](#developers)
- [Running Environment On Your Localhost](#running-environment-on-your-localhost)
- [Express Installation](#express-installation)
@@ -163,6 +164,11 @@ Make sure you have enabled mod_rewrite. There is an example config for running V
</VirtualHost>
```
# Updating Application
1. Replace all files where the app is located except `/storage` folder and `.env` file.
2. Clear the application cache (Admin / Settings / Application).
3. In 5 minutes the app update stuff automatically on the background if needed.
# Developers
## Running Environment On Your Localhost

View File

@@ -16,7 +16,7 @@
"/chunks/not-found.js": "/chunks/not-found.js?id=9f6ce23ce5d969f1",
"/chunks/temporary-unavailable.js": "/chunks/temporary-unavailable.js?id=f564565faa09d6d6",
"/chunks/admin.js": "/chunks/admin.js?id=8032540fddb832c5",
"/chunks/dashboard.js": "/chunks/dashboard.js?id=fd1d266e493a79a8",
"/chunks/dashboard.js": "/chunks/dashboard.js?id=a41b0f486b1f8d36",
"/chunks/invoices.js": "/chunks/invoices.js?id=1416cbf6d1a593ac",
"/chunks/subscriptions.js": "/chunks/subscriptions.js?id=5bf6704f5b599f36",
"/chunks/pages.js": "/chunks/pages.js?id=c8380d571e91e8be",

View File

@@ -53,16 +53,6 @@
</div>
</div>
<!--New language strings alert-->
<AlertBox v-if="data.app.shouldUpgrade" @click.native.once="upgradeSystem" color="green" :is-loading="isUpgradingApp">
There is a new update that needs to upgrade some stuff on your backend. Please click on this box to upgrade.
</AlertBox>
<!--New language strings alert-->
<AlertBox v-if="data.app.shouldUpgradeTranslations" @click.native.once="upgradeTranslations" color="green" :is-loading="isUpgradingLanguages">
We detect new language strings. You should <b class="dark:text-green-500 text-green-600 text-sm font-bold underline">upgrade your translations</b>. After that, you can find new translations at the bottom page of your translations in language editor. Please click on this box.
</AlertBox>
<!--Create metered plan alert-->
<AlertBox v-if="config.subscriptionType === 'metered' && config.isEmptyPlans" color="rose">
As you installed app with metered subscription type, you have to <router-link :to="{ name: 'CreateMeteredPlan' }" class="dark:text-rose-500 text-sm font-bold underline">create your plan</router-link> as soon as possible to prevent new user registration without automatically assigned subscription plan.
@@ -218,52 +208,10 @@ export default {
},
data() {
return {
isUpgradingLanguages: false,
isUpgradingApp: false,
isLoading: false,
data: undefined,
}
},
methods: {
upgradeTranslations() {
this.isUpgradingLanguages = true
axios.get('/upgrade/translations')
.then(() => {
this.data.app.shouldUpgradeTranslations = false
events.$emit('toaster', {
type: 'success',
message: this.$t('Your translations was upgraded successfully.'),
})
})
.catch(() => {
events.$emit('alert:open', {
title: this.$t('popup_error.title'),
message: this.$t('popup_error.message'),
})
})
},
upgradeSystem() {
this.isUpgradingApp = true
axios.get('/upgrade/system')
.then(() => {
this.data.app.shouldUpgrade = false
events.$emit('toaster', {
type: 'success',
message: this.$t('Your app was upgraded successfully.'),
})
})
.catch(() => {
events.$emit('alert:open', {
title: this.$t('popup_error.title'),
message: this.$t('popup_error.message'),
})
})
}
},
created() {
axios
.get('/api/admin/dashboard')

View File

@@ -1,15 +1,8 @@
<?php
use Domain\Maintenance\Controllers\UpgradeSystemController;
use Domain\Maintenance\Controllers\MaintenanceModeController;
use Domain\Maintenance\Controllers\UpgradeTranslationsController;
Route::group(['middleware' => ['auth:sanctum']], function () {
Route::get('/down', [MaintenanceModeController::class, 'down']);
Route::get('/up', [MaintenanceModeController::class, 'up']);
Route::group(['prefix' => 'upgrade'], function () {
Route::get('/translations', UpgradeTranslationsController::class);
Route::get('/system', UpgradeSystemController::class);
});
});

View File

@@ -5,13 +5,14 @@ use Illuminate\Console\Scheduling\Schedule;
use App\Console\Commands\SetupDevEnvironment;
use App\Console\Commands\SetupProdEnvironment;
use Support\Scheduler\Actions\ReportUsageAction;
use Support\Demo\Actions\DeleteAllSharedLinksAction;
use Support\Demo\Actions\DeleteAllDemoSharedLinksAction;
use Support\Scheduler\Actions\DeleteFailedFilesAction;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Support\Scheduler\Actions\DeleteUnverifiedUsersAction;
use Support\Scheduler\Actions\DeleteExpiredShareLinksAction;
use App\Console\Commands\GenerateDemoSubscriptionContentCommand;
use Support\Scheduler\Actions\ExpireUnfilledUploadRequestAction;
use Support\Upgrading\Actions\UpdateSystemAction;
class Kernel extends ConsoleKernel
{
@@ -42,7 +43,7 @@ class Kernel extends ConsoleKernel
if (is_demo()) {
$schedule->call(
fn () => resolve(DeleteAllSharedLinksAction::class)()
fn () => resolve(DeleteAllDemoSharedLinksAction::class)()
)->daily()->at('00:00');
}
@@ -54,6 +55,10 @@ class Kernel extends ConsoleKernel
fn () => resolve(ExpireUnfilledUploadRequestAction::class)()
)->hourly();
$schedule->call(
fn () => resolve(UpdateSystemAction::class)()
)->everyMinute();
$schedule->call(
fn () => resolve(DeleteUnverifiedUsersAction::class)()
)->daily()->at('00:05');

View File

@@ -1,27 +1,17 @@
<?php
namespace Domain\Admin\Controllers\Dashboard;
use Schema;
use ByteUnits\Metric;
use App\Users\Models\User;
use Illuminate\Http\Response;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Domain\Maintenance\Models\AppUpdate;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Routing\ResponseFactory;
use VueFileManager\Subscription\Domain\Subscriptions\Models\Subscription;
class GetDashboardDataController extends Controller
{
public function __invoke(): Application|ResponseFactory|Response
public function __invoke(): JsonResponse
{
// Get app update data
$shouldUpgrade = $this->getUpgradeData();
// Get translations data
list($originalTranslations, $activeTranslations) = $this->countTranslations();
// Get bandwidth data
list($upload, $download, $uploadTotal, $downloadTotal, $storageUsage) = $this->getDiskData();
@@ -31,7 +21,7 @@ class GetDashboardDataController extends Controller
->where('type', 'charge')
->sum('amount');
return response([
return response()->json([
'users' => [
'total' => User::count(),
'usersPremiumTotal' => Subscription::count(),
@@ -48,8 +38,6 @@ class GetDashboardDataController extends Controller
],
],
'app' => [
'shouldUpgrade' => count($shouldUpgrade) > 0,
'shouldUpgradeTranslations' => $activeTranslations !== $originalTranslations,
'isRunningCron' => isRunningCron(),
'license' => get_settings('license'),
'version' => config('vuefilemanager.version'),
@@ -111,40 +99,4 @@ class GetDashboardDataController extends Controller
return [$upload, $download, $uploadTotal, $downloadTotal, $storageUsage];
}
private function countTranslations(): array
{
$default_translations = [
'extended' => collect([
config('language-translations.extended'),
config('language-translations.regular'),
config('custom-language-translations'),
])->collapse(),
'regular' => collect([
config('language-translations.regular'),
config('custom-language-translations'),
])->collapse(),
];
$originalTranslationCount = count($default_translations[get_settings('license')]);
$activeTranslationsCount = DB::table('language_translations')
->where('lang', 'en')
->count();
return [$originalTranslationCount, $activeTranslationsCount];
}
private function getUpgradeData(): array
{
// Get already updated versions
$alreadyUpdated = Schema::hasTable('app_updates')
? AppUpdate::all()
->pluck('version')
->toArray()
: [];
// Get versions which has to be upgraded
return array_diff(config('vuefilemanager.updates'), $alreadyUpdated);
}
}

View File

@@ -1,10 +1,9 @@
<?php
namespace Domain\Localization\Actions;
use DB;
class DeleteLanguageStringsAction
class DeleteLanguageTranslationsAction
{
public function __invoke(array $list): void
{

View File

@@ -1,18 +0,0 @@
<?php
namespace Domain\Localization\Actions;
use DB;
class UpdateLanguageStringsAction
{
public function __invoke(array $list): void
{
collect($list)
->each(fn(...$item) => DB::table('language_translations')
->where('lang', 'en')
->where('key', $item[1])
->update(['value' => $item[0]])
);
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Domain\Localization\Actions;
use DB;
class UpdateLanguageTranslationsAction
{
public function __invoke(array $list): void
{
collect($list)
->each(
fn (...$item) => DB::table('language_translations')
->where('lang', 'en')
->where('key', $item[1])
->update(['value' => $item[0]])
);
}
}

View File

@@ -1,16 +1,12 @@
<?php
namespace Domain\Maintenance\Actions;
use Gate;
use Artisan;
class UpgradeDatabaseAction
{
public function __invoke(): bool
{
// Check admin permission
Gate::authorize('maintenance');
return Artisan::call('migrate', [
'--force' => true,
]);

View File

@@ -1,29 +0,0 @@
<?php
namespace Domain\Maintenance\Controllers;
use Gate;
use Illuminate\Http\Response;
use App\Http\Controllers\Controller;
use Domain\Localization\Actions\UpgradeLanguageTranslationsAction;
class UpgradeTranslationsController extends Controller
{
public function __construct(
public UpgradeLanguageTranslationsAction $upgradeLanguageTranslations,
) {
}
/**
* Get new language translations from default translations
* and insert it into database
*/
public function __invoke(): Response
{
// Check admin permission
Gate::authorize('maintenance');
($this->upgradeLanguageTranslations)();
return response('Done.', 201);
}
}

View File

@@ -1,5 +1,4 @@
<?php
namespace Domain\Settings\Requests;
use Illuminate\Foundation\Http\FormRequest;

View File

@@ -1,10 +1,10 @@
<?php
namespace Support\Demo\Actions;
use App\Users\Models\User;
use DB;
use App\Users\Models\User;
class DeleteAllSharedLinksAction
class DeleteAllDemoSharedLinksAction
{
public function __invoke()
{

View File

@@ -0,0 +1,80 @@
<?php
namespace Support\Upgrading\Actions;
use DB;
use Schema;
use Artisan;
use Domain\Maintenance\Models\AppUpdate;
use Support\Upgrading\Controllers\UpgradingVersionsController;
use Domain\Localization\Actions\UpgradeLanguageTranslationsAction;
class UpdateSystemAction extends UpgradingVersionsController
{
public function __invoke(): void
{
ini_set('max_execution_time', -1);
// Upgrade the language translations
if ($this->shouldUpdateTranslations()) {
resolve(UpgradeLanguageTranslationsAction::class)();
}
// Check if there are some version to upgrade
$shouldUpgradeSystem = $this->shouldUpgradeSystem();
// Upgrade the app
if (! empty($shouldUpgradeSystem)) {
foreach ($shouldUpgradeSystem as $version) {
// Get method name
$method = "upgrade_to_$version";
if (method_exists($this, $method)) {
// Run update
$this->{$method}();
// Store update record
AppUpdate::create(['version' => $version]);
}
}
// Clear config
Artisan::call('config:clear');
}
}
private function shouldUpgradeSystem(): array
{
// Get already updated versions
$alreadyUpdated = Schema::hasTable('app_updates')
? AppUpdate::all()
->pluck('version')
->toArray()
: [];
// Get versions which has to be upgraded
return array_diff(config('vuefilemanager.updates'), $alreadyUpdated);
}
private function shouldUpdateTranslations(): bool
{
$default_translations = [
'extended' => collect([
config('language-translations.extended'),
config('language-translations.regular'),
config('custom-language-translations'),
])->collapse(),
'regular' => collect([
config('language-translations.regular'),
config('custom-language-translations'),
])->collapse(),
];
$originalTranslationCount = count($default_translations[get_settings('license')]);
$activeTranslationsCount = DB::table('language_translations')
->where('lang', 'en')
->count();
return $activeTranslationsCount !== $originalTranslationCount;
}
}

View File

@@ -1,69 +1,28 @@
<?php
namespace Domain\Maintenance\Controllers;
namespace Support\Upgrading\Controllers;
use DB;
use Domain\Localization\Actions\DeleteLanguageStringsAction;
use Domain\Localization\Actions\UpdateLanguageStringsAction;
use Schema;
use Storage;
use Artisan;
use Storage;
use App\Users\Models\User;
use Illuminate\Support\Arr;
use Illuminate\Http\Request;
use Domain\Files\Models\File;
use Illuminate\Http\Response;
use Domain\Folders\Models\Folder;
use App\Http\Controllers\Controller;
use Domain\Maintenance\Models\AppUpdate;
use Domain\Maintenance\Actions\UpgradeDatabaseAction;
use VueFileManager\Subscription\Domain\Plans\Models\Plan;
use Domain\Localization\Actions\DeleteLanguageTranslationsAction;
use Domain\Localization\Actions\UpdateLanguageTranslationsAction;
class UpgradeSystemController extends Controller
class UpgradingVersionsController
{
public function __construct(
public UpgradeDatabaseAction $upgradeDatabase,
public DeleteLanguageStringsAction $deleteLanguageStrings,
public UpdateLanguageStringsAction $updateLanguageStrings,
public DeleteLanguageTranslationsAction $deleteLanguageStrings,
public UpdateLanguageTranslationsAction $updateLanguageStrings,
) {
}
public function __invoke(Request $request): Response
{
ini_set('max_execution_time', -1);
// Clear config
Artisan::call('config:clear');
// Get already updated versions
$alreadyUpdated = Schema::hasTable('app_updates')
? AppUpdate::all()
->pluck('version')
->toArray()
: [];
// Get versions which has to be upgraded
$needToUpgrade = array_diff(config('vuefilemanager.updates'), $alreadyUpdated);
// Iterate and upgrade
foreach ($needToUpgrade as $version) {
// Get method name
$method = "upgrade_to_$version";
if (method_exists($this, $method)) {
// Run update
$this->{$method}($request);
// Store update record
AppUpdate::create(['version' => $version]);
return response('Done', 201);
}
}
return response('Whooops, something went wrong!', 500);
}
private function upgrade_to_2_0_10(): void
public function upgrade_to_2_0_10(): void
{
($this->upgradeDatabase)();
@@ -131,7 +90,7 @@ class UpgradeSystemController extends Controller
]));
}
private function upgrade_to_2_0_13(): void
public function upgrade_to_2_0_13(): void
{
// Force plan synchronization
if (get_settings('license') === 'extended' && Plan::count() !== 0) {
@@ -139,7 +98,7 @@ class UpgradeSystemController extends Controller
}
}
private function upgrade_to_2_0_14(): void
public function upgrade_to_2_0_14(): void
{
($this->upgradeDatabase)();
@@ -148,7 +107,7 @@ class UpgradeSystemController extends Controller
->each(fn ($user) => $user->forceFill(['two_factor_confirmed_at' => now()])->save());
($this->deleteLanguageStrings)([
'popup_2fa.disappear_qr'
'popup_2fa.disappear_qr',
]);
($this->updateLanguageStrings)([

View File

@@ -157,6 +157,7 @@ class UserAccountTest extends TestCase
'role' => $user->role,
'socialite_account' => false,
'two_factor_authentication' => false,
'two_factor_confirmed_at' => null,
'storage' => [
'used' => 0,
'used_formatted' => '0%',

View File

@@ -25,8 +25,6 @@ class DashboardTest extends TestCase
->assertStatus(200)
->assertJsonFragment([
'app' => [
'shouldUpgrade' => true,
'shouldUpgradeTranslations' => true,
'earnings' => '$0.00',
'isRunningCron' => false,
'license' => 'extended',

View File

@@ -5,6 +5,7 @@ use DB;
use Tests\TestCase;
use App\Users\Models\User;
use Illuminate\Support\Str;
use Support\Upgrading\Actions\UpdateSystemAction;
class AppUpgradeTest extends TestCase
{
@@ -39,10 +40,7 @@ class AppUpgradeTest extends TestCase
]);
});
$this
->actingAs($user)
->get('/upgrade/translations')
->assertStatus(201);
resolve(UpdateSystemAction::class)();
collect(['en', 'sk'])
->map(function ($locale) {
@@ -65,13 +63,7 @@ class AppUpgradeTest extends TestCase
*/
public function it_upgrade_app()
{
$user = User::factory()
->create(['role' => 'admin']);
$this
->actingAs($user)
->get('/upgrade/system')
->assertStatus(201);
resolve(UpdateSystemAction::class)();
$this->assertDatabaseHas('app_updates', [
'version' => '2_0_10',

View File

@@ -1,11 +1,10 @@
<?php
namespace Tests\Support\Demo;
use Tests\TestCase;
use App\Users\Models\User;
use Domain\Sharing\Models\Share;
use Support\Demo\Actions\DeleteAllSharedLinksAction;
use Tests\TestCase;
use Support\Demo\Actions\DeleteAllDemoSharedLinksAction;
class DemoTest extends TestCase
{
@@ -30,7 +29,7 @@ class DemoTest extends TestCase
Share::factory()
->create(['user_id' => $howdy->id]);
resolve(DeleteAllSharedLinksAction::class)();
resolve(DeleteAllDemoSharedLinksAction::class)();
$this->assertDatabaseHas('shares', [
'user_id' => $user->id,