misc_pterodactyl-panel/app/Console/Commands/Environment/DatabaseSettingsCommand.php

153 lines
5.1 KiB
PHP
Raw Permalink Normal View History

2017-09-23 02:19:57 +00:00
<?php
2017-09-26 02:43:01 +00:00
/**
2017-09-23 02:19:57 +00:00
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
2017-09-26 02:43:01 +00:00
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
2017-09-23 02:19:57 +00:00
*/
namespace Pterodactyl\Console\Commands\Environment;
use PDOException;
use Illuminate\Console\Command;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Database\DatabaseManager;
use Pterodactyl\Traits\Commands\EnvironmentWriterTrait;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
class DatabaseSettingsCommand extends Command
{
use EnvironmentWriterTrait;
/**
* @var \Illuminate\Contracts\Config\Repository
*/
protected $config;
/**
* @var \Illuminate\Contracts\Console\Kernel
*/
protected $console;
/**
* @var \Illuminate\Database\DatabaseManager
*/
protected $database;
/**
* @var string
*/
protected $description = 'Configure database settings for the Panel.';
/**
* @var string
*/
protected $signature = 'p:environment:database
{--host= : The connection address for the MySQL server.}
2017-11-04 22:41:52 +00:00
{--port= : The connection port for the MySQL server.}
{--database= : The database to use.}
{--username= : Username to use when connecting.}
{--password= : Password to use for this database.}';
2017-09-23 02:19:57 +00:00
/**
* @var array
*/
protected $variables = [];
/**
* DatabaseSettingsCommand constructor.
*/
public function __construct(ConfigRepository $config, DatabaseManager $database, Kernel $console)
{
parent::__construct();
$this->config = $config;
$this->console = $console;
$this->database = $database;
}
/**
* Handle command execution.
*
* @return int
*
* @throws \Pterodactyl\Exceptions\PterodactylException
*/
public function handle()
{
$this->output->note(trans('command/messages.environment.database.host_warning'));
$this->variables['DB_HOST'] = $this->option('host') ?? $this->ask(
2021-01-23 20:33:34 +00:00
trans('command/messages.environment.database.host'),
$this->config->get('database.connections.mysql.host', '127.0.0.1')
);
2017-09-23 02:19:57 +00:00
$this->variables['DB_PORT'] = $this->option('port') ?? $this->ask(
2021-01-23 20:33:34 +00:00
trans('command/messages.environment.database.port'),
$this->config->get('database.connections.mysql.port', 3306)
);
2017-09-23 02:19:57 +00:00
2017-11-04 22:41:52 +00:00
$this->variables['DB_DATABASE'] = $this->option('database') ?? $this->ask(
2021-01-23 20:33:34 +00:00
trans('command/messages.environment.database.database'),
$this->config->get('database.connections.mysql.database', 'panel')
);
2017-09-23 02:19:57 +00:00
$this->output->note(trans('command/messages.environment.database.username_warning'));
2017-11-04 22:41:52 +00:00
$this->variables['DB_USERNAME'] = $this->option('username') ?? $this->ask(
2021-01-23 20:33:34 +00:00
trans('command/messages.environment.database.username'),
$this->config->get('database.connections.mysql.username', 'pterodactyl')
);
2017-09-23 02:19:57 +00:00
$askForMySQLPassword = true;
2021-01-23 20:33:34 +00:00
if (!empty($this->config->get('database.connections.mysql.password')) && $this->input->isInteractive()) {
2017-09-23 02:19:57 +00:00
$this->variables['DB_PASSWORD'] = $this->config->get('database.connections.mysql.password');
$askForMySQLPassword = $this->confirm(trans('command/messages.environment.database.password_defined'));
}
if ($askForMySQLPassword) {
$this->variables['DB_PASSWORD'] = $this->option('password') ?? $this->secret(trans('command/messages.environment.database.password'));
}
try {
$this->testMySQLConnection();
} catch (PDOException $exception) {
$this->output->error(trans('command/messages.environment.database.connection_error', ['error' => $exception->getMessage()]));
$this->output->error(trans('command/messages.environment.database.creds_not_saved'));
if ($this->confirm(trans('command/messages.environment.database.try_again'))) {
$this->database->disconnect('_pterodactyl_command_test');
return $this->handle();
}
return 1;
}
$this->writeToEnvironment($this->variables);
$this->info($this->console->output());
return 0;
}
/**
* Test that we can connect to the provided MySQL instance and perform a selection.
*/
private function testMySQLConnection()
{
$this->config->set('database.connections._pterodactyl_command_test', [
2019-09-06 04:32:57 +00:00
'driver' => 'mysql',
'host' => $this->variables['DB_HOST'],
'port' => $this->variables['DB_PORT'],
'database' => $this->variables['DB_DATABASE'],
'username' => $this->variables['DB_USERNAME'],
'password' => $this->variables['DB_PASSWORD'],
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'strict' => true,
2017-09-23 02:19:57 +00:00
]);
$this->database->connection('_pterodactyl_command_test')->getPdo();
}
}