Refactor installer.

This commit is contained in:
James Cole
2019-03-30 11:03:39 +01:00
parent 5b1fb5354e
commit c07ef3658b
18 changed files with 230 additions and 327 deletions

View File

@@ -31,6 +31,7 @@ use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Support\Facades\Preferences;
use FireflyIII\Support\Http\Controllers\GetConfigurationData;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Laravel\Passport\Passport;
use Log;
@@ -50,6 +51,11 @@ class InstallController extends Controller
public const BASEDIR_ERROR = 'Firefly III cannot execute the upgrade commands. It is not allowed to because of an open_basedir restriction.';
/** @var string Other errors */
public const OTHER_ERROR = 'An unknown error prevented Firefly III from executing the upgrade commands. Sorry.';
/** @var array All upgrade commands. */
private $upgradeCommands;
/** @noinspection MagicMethodsValidityInspection */
/** @noinspection PhpMissingParentConstructorInspection */
/**
@@ -58,41 +64,39 @@ class InstallController extends Controller
public function __construct()
{
// empty on purpose.
$this->upgradeCommands = [
// there are x initial commands
'migrate' => ['--seed' => true, '--force' => true],
'firefly:decrypt-all' => [],
'generate-keys' => [], // an exception :(
// there are 10 upgrade commands.
'firefly-iii:transaction-identifiers' => [],
'firefly-iii:account-currencies' => [],
'firefly-iii:journal-currencies' => [],
'firefly-iii:migrate-notes' => [],
'firefly-iii:migrate-attachments' => [],
'firefly-iii:bills-to-rules' => [],
'firefly-iii:bl-currency' => [],
'firefly-iii:cc-liabilities' => [],
'firefly-iii:migrate-to-groups' => [],
'firefly-iii:back-to-journals' => [],
// there are 12 verify commands.
'firefly-iii:fix-piggies' => [],
'firefly-iii:create-link-types' => [],
'firefly-iii:create-access-tokens' => [],
'firefly-iii:remove-bills' => [],
'firefly-iii:enable-currencies' => [],
'firefly-iii:fix-transfer-budgets' => [],
'firefly-iii:fix-uneven-amount' => [],
'firefly-iii:delete-zero-amount' => [],
'firefly-iii:delete-orphaned-transactions' => [],
'firefly-iii:delete-empty-journals' => [],
'firefly-iii:delete-empty-groups' => [],
'firefly-iii:fix-account-types' => [],
];
}
/**
* Do database decrypt.
*
* @return \Illuminate\Http\JsonResponse
*/
public function decrypt(): JsonResponse
{
if ($this->hasForbiddenFunctions()) {
return response()->json(['error' => true, 'message' => self::FORBIDDEN_ERROR]);
}
try {
Log::debug('Am now calling decrypt database routine...');
Artisan::call('firefly:decrypt-all');
Log::debug(Artisan::output());
} catch (Exception $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
if (strpos($e->getMessage(), 'open_basedir restriction in effect')) {
Cache::clear();
return response()->json(['error' => true, 'message' => self::BASEDIR_ERROR]);
}
return response()->json(['error' => true, 'message' => self::OTHER_ERROR . ' ' . $e->getMessage()]);
}
// clear cache as well.
Cache::clear();
Preferences::mark();
return response()->json(['error' => false, 'message' => 'OK']);
}
/**
* Show index.
*
@@ -108,16 +112,9 @@ class InstallController extends Controller
/**
* Create specific RSA keys.
*
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function keys()
public function keys(): void
{
if ($this->hasForbiddenFunctions()) {
return response()->json(['error' => true, 'message' => self::FORBIDDEN_ERROR]);
}
// create keys manually because for some reason the passport namespace
// does not exist
$rsa = new RSA();
$keys = $rsa->createKey(4096);
@@ -127,113 +124,77 @@ class InstallController extends Controller
];
if (file_exists($publicKey) || file_exists($privateKey)) {
return response()->json(['error' => false, 'message' => 'OK']);
return;
}
file_put_contents($publicKey, Arr::get($keys, 'publickey'));
file_put_contents($privateKey, Arr::get($keys, 'privatekey'));
// clear cache as well.
Cache::clear();
Preferences::mark();
return response()->json(['error' => false, 'message' => 'OK']);
}
/**
* Run migration commands.
* @param Request $request
*
* @return JsonResponse
*/
public function migrate(): JsonResponse
public function runCommand(Request $request): JsonResponse
{
if ($this->hasForbiddenFunctions()) {
return response()->json(['error' => true, 'message' => self::FORBIDDEN_ERROR]);
}
$requestIndex = (int)$request->get('index');
$response = [
'hasNextCommand' => false,
'done' => true,
'next' => 0,
'previous' => null,
'error' => false,
'errorMessage' => null,
];
try {
Log::debug('Am now calling migrate routine...');
Artisan::call('migrate', ['--seed' => true, '--force' => true]);
Log::debug(Artisan::output());
} catch (Exception $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
if (strpos($e->getMessage(), 'open_basedir restriction in effect')) {
return response()->json(['error' => true, 'message' => self::BASEDIR_ERROR]);
Log::debug(sprintf('Will now run commands. Request index is %d', $requestIndex));
$index = 0;
foreach ($this->upgradeCommands as $command => $args) {
Log::debug(sprintf('Current command is "%s", index is %d', $command, $index));
if ($index < $requestIndex) {
Log::debug('Will not execute.');
$index++;
continue;
}
if ($index >= $requestIndex) {
Log::debug(sprintf('%d >= %d, will execute the command.', $index, $requestIndex));
Log::debug(sprintf('Will now call command %s with args.', $command), $args);
try {
if ('generate-keys' === $command) {
$this->keys();
}
if ('generate-keys' !== $command) {
Artisan::call($command, $args);
Log::debug(Artisan::output());
}
} catch (Exception $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
if (strpos($e->getMessage(), 'open_basedir restriction in effect')) {
$response['error'] = true;
$response['errorMessage'] = self::BASEDIR_ERROR;
return response()->json(['error' => true, 'message' => self::OTHER_ERROR]);
}
// clear cache as well.
Cache::clear();
Preferences::mark();
return response()->json($response);
}
$response['error'] = true;
$response['errorMessage'] = self::OTHER_ERROR . ' ' . $e->getMessage();
return response()->json(['error' => false, 'message' => 'OK']);
}
return response()->json($response);
}
// clear cache as well.
Cache::clear();
Preferences::mark();
/**
* Do database upgrade.
*
* @return \Illuminate\Http\JsonResponse
*/
public function upgrade(): JsonResponse
{
if ($this->hasForbiddenFunctions()) {
return response()->json(['error' => true, 'message' => self::FORBIDDEN_ERROR]);
}
try {
Log::debug('Am now calling upgrade database routine...');
Artisan::call('firefly:upgrade-database');
Log::debug(Artisan::output());
} catch (Exception $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
if (strpos($e->getMessage(), 'open_basedir restriction in effect')) {
return response()->json(['error' => true, 'message' => self::BASEDIR_ERROR]);
$index++;
$response['hasNextCommand'] = true;
$response['previous'] = $command;
break;
}
return response()->json(['error' => true, 'message' => self::OTHER_ERROR . ' ' . $e->getMessage()]);
}
// clear cache as well.
Cache::clear();
Preferences::mark();
$response['next'] = $index;
return response()->json(['error' => false, 'message' => 'OK']);
return response()->json($response);
}
/**
* Do database verification.
*
* @return \Illuminate\Http\JsonResponse
*/
public function verify(): JsonResponse
{
if ($this->hasForbiddenFunctions()) {
return response()->json(['error' => true, 'message' => self::FORBIDDEN_ERROR]);
}
try {
Log::debug('Am now calling verify database routine...');
Artisan::call('firefly:verify');
Log::debug(Artisan::output());
} catch (Exception $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
if (strpos($e->getMessage(), 'open_basedir restriction in effect')) {
return response()->json(['error' => true, 'message' => self::BASEDIR_ERROR]);
}
return response()->json(['error' => true, 'message' => self::OTHER_ERROR . ' ' . $e->getMessage()]);
}
// clear cache as well.
Cache::clear();
Preferences::mark();
return response()->json(['error' => false, 'message' => 'OK']);
}
}