Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Console/Commands/Environment/DatabaseSettingsCommand.php
7461 views
1
<?php
2
3
namespace Pterodactyl\Console\Commands\Environment;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Contracts\Console\Kernel;
7
use Illuminate\Database\DatabaseManager;
8
use Pterodactyl\Traits\Commands\EnvironmentWriterTrait;
9
10
class DatabaseSettingsCommand extends Command
11
{
12
use EnvironmentWriterTrait;
13
14
protected $description = 'Configure database settings for the Panel.';
15
16
protected $signature = 'p:environment:database
17
{--host= : The connection address for the MySQL server.}
18
{--port= : The connection port for the MySQL server.}
19
{--database= : The database to use.}
20
{--username= : Username to use when connecting.}
21
{--password= : Password to use for this database.}';
22
23
protected array $variables = [];
24
25
/**
26
* DatabaseSettingsCommand constructor.
27
*/
28
public function __construct(private DatabaseManager $database, private Kernel $console)
29
{
30
parent::__construct();
31
}
32
33
/**
34
* Handle command execution.
35
*
36
* @throws \Pterodactyl\Exceptions\PterodactylException
37
*/
38
public function handle(): int
39
{
40
$this->output->note('It is highly recommended to not use "localhost" as your database host as we have seen frequent socket connection issues. If you want to use a local connection you should be using "127.0.0.1".');
41
$this->variables['DB_HOST'] = $this->option('host') ?? $this->ask(
42
'Database Host',
43
config('database.connections.mysql.host', '127.0.0.1')
44
);
45
46
$this->variables['DB_PORT'] = $this->option('port') ?? $this->ask(
47
'Database Port',
48
config('database.connections.mysql.port', 3306)
49
);
50
51
$this->variables['DB_DATABASE'] = $this->option('database') ?? $this->ask(
52
'Database Name',
53
config('database.connections.mysql.database', 'panel')
54
);
55
56
$this->output->note('Using the "root" account for MySQL connections is not only highly frowned upon, it is also not allowed by this application. You\'ll need to have created a MySQL user for this software.');
57
$this->variables['DB_USERNAME'] = $this->option('username') ?? $this->ask(
58
'Database Username',
59
config('database.connections.mysql.username', 'pterodactyl')
60
);
61
62
$askForMySQLPassword = true;
63
if (!empty(config('database.connections.mysql.password')) && $this->input->isInteractive()) {
64
$this->variables['DB_PASSWORD'] = config('database.connections.mysql.password');
65
$askForMySQLPassword = $this->confirm('It appears you already have a MySQL connection password defined, would you like to change it?');
66
}
67
68
if ($askForMySQLPassword) {
69
$this->variables['DB_PASSWORD'] = $this->option('password') ?? $this->secret('Database Password');
70
}
71
72
try {
73
$this->testMySQLConnection();
74
} catch (\PDOException $exception) {
75
$this->output->error(sprintf('Unable to connect to the MySQL server using the provided credentials. The error returned was "%s".', $exception->getMessage()));
76
$this->output->error('Your connection credentials have NOT been saved. You will need to provide valid connection information before proceeding.');
77
78
if ($this->confirm('Go back and try again?')) {
79
$this->database->disconnect('_pterodactyl_command_test');
80
81
return $this->handle();
82
}
83
84
return 1;
85
}
86
87
$this->writeToEnvironment($this->variables);
88
89
$this->info($this->console->output());
90
91
return 0;
92
}
93
94
/**
95
* Test that we can connect to the provided MySQL instance and perform a selection.
96
*/
97
private function testMySQLConnection()
98
{
99
config()->set('database.connections._pterodactyl_command_test', [
100
'driver' => 'mysql',
101
'host' => $this->variables['DB_HOST'],
102
'port' => $this->variables['DB_PORT'],
103
'database' => $this->variables['DB_DATABASE'],
104
'username' => $this->variables['DB_USERNAME'],
105
'password' => $this->variables['DB_PASSWORD'],
106
'charset' => 'utf8mb4',
107
'collation' => 'utf8mb4_unicode_ci',
108
'strict' => true,
109
]);
110
111
$this->database->connection('_pterodactyl_command_test')->getPdo();
112
}
113
}
114
115