Skip to content

Commit 8722571

Browse files
committed
Finish console command cleanup
1 parent 68cc71e commit 8722571

File tree

7 files changed

+376
-380
lines changed

7 files changed

+376
-380
lines changed
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
<?php
2+
/*
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Pterodactyl\Console\Commands\Environment;
26+
27+
use Ramsey\Uuid\Uuid;
28+
use Illuminate\Console\Command;
29+
use Illuminate\Contracts\Console\Kernel;
30+
use Pterodactyl\Traits\Commands\EnvironmentWriterTrait;
31+
use Illuminate\Contracts\Config\Repository as ConfigRepository;
32+
33+
class AppSettingsCommand extends Command
34+
{
35+
use EnvironmentWriterTrait;
36+
37+
/**
38+
* @var \Illuminate\Contracts\Console\Kernel
39+
*/
40+
protected $command;
41+
42+
/**
43+
* @var \Illuminate\Contracts\Config\Repository
44+
*/
45+
protected $config;
46+
47+
/**
48+
* @var string
49+
*/
50+
protected $description = 'Configure basic environment settings for the Panel.';
51+
52+
/**
53+
* @var string
54+
*/
55+
protected $signature = 'p:environment:setup
56+
{--url= : The URL that this Panel is running on.}
57+
{--timezone= : The timezone to use for Panel times.}
58+
{--cache= : The cache driver backend to use.}
59+
{--session= : The session driver backend to use.}
60+
{--redis-host= : Redis host to use for connections.}
61+
{--redis-pass= : Password used to connect to redis.}
62+
{--redis-port= : Port to connect to redis over.}';
63+
64+
/**
65+
* @var array
66+
*/
67+
protected $variables = [];
68+
69+
/**
70+
* AppSettingsCommand constructor.
71+
*
72+
* @param \Illuminate\Contracts\Config\Repository $config
73+
* @param \Illuminate\Contracts\Console\Kernel $command
74+
*/
75+
public function __construct(ConfigRepository $config, Kernel $command)
76+
{
77+
parent::__construct();
78+
79+
$this->command = $command;
80+
$this->config = $config;
81+
}
82+
83+
/**
84+
* Handle command execution.
85+
*
86+
* @throws \Pterodactyl\Exceptions\PterodactylException
87+
*/
88+
public function handle()
89+
{
90+
if (is_null($this->config->get('pterodactyl.service.author'))) {
91+
$this->variables['SERVICE_AUTHOR'] = Uuid::uuid4()->toString();
92+
}
93+
94+
$this->output->comment(trans('command/messages.environment.app.app_url_help'));
95+
$this->variables['APP_URL'] = $this->option('url') ?? $this->ask(
96+
trans('command/messages.environment.app.app_url'), $this->config->get('app.url', 'http://example.org')
97+
);
98+
99+
$this->output->comment(trans('command/messages.environment.app.timezone_help'));
100+
$this->variables['APP_TIMEZONE'] = $this->option('timezone') ?? $this->ask(
101+
trans('command/messages.environment.app.timezone'), $this->config->get('app.timezone')
102+
);
103+
104+
$this->variables['CACHE_DRIVER'] = $this->option('cache') ?? $this->choice(
105+
trans('command/messages.environment.app.cache_driver'), [
106+
'redis' => 'Redis (recommended)',
107+
'memcached' => 'Memcached',
108+
], $this->config->get('cache.default', 'redis')
109+
);
110+
111+
$this->variables['SESSION_DRIVER'] = $this->option('session') ?? $this->choice(
112+
trans('command/messages.environment.app.session_driver'), [
113+
'redis' => 'Redis (recommended)',
114+
'memcached' => 'Memcached',
115+
'mysql' => 'MySQL Database',
116+
'file' => 'Filesystem',
117+
'cookie' => 'Cookie',
118+
], $this->config->get('session.driver', 'redis')
119+
);
120+
121+
$this->variables['QUEUE_DRIVER'] = $this->option('session') ?? $this->choice(
122+
trans('command/messages.environment.app.session_driver'), [
123+
'redis' => 'Redis (recommended)',
124+
'database' => 'MySQL Database',
125+
'sync' => 'Sync',
126+
], $this->config->get('queue.driver', 'redis')
127+
);
128+
129+
$this->checkForRedis();
130+
$this->writeToEnvironment($this->variables);
131+
132+
$this->command->call('config:cache');
133+
$this->info($this->command->output());
134+
}
135+
136+
/**
137+
* Check if redis is selected, if so, request connection details and verify them.
138+
*/
139+
private function checkForRedis()
140+
{
141+
$items = collect($this->variables)->filter(function ($item) {
142+
return $item === 'redis';
143+
});
144+
145+
// Redis was not selected, no need to continue.
146+
if (count($items) === 0) {
147+
return;
148+
}
149+
150+
$this->output->note(trans('command/messages.environment.app.using_redis'));
151+
$this->variables['REDIS_HOST'] = $this->option('redis-host') ?? $this->ask(
152+
trans('command/messages.environment.app.redis_host'), $this->config->get('database.redis.default.host')
153+
);
154+
155+
$askForRedisPassword = true;
156+
if (! empty($this->config->get('database.redis.default.password'))) {
157+
$this->variables['REDIS_PASSWORD'] = $this->config->get('database.redis.default.password');
158+
$askForRedisPassword = $this->confirm(trans('command/messages.environment.app.redis_pass_defined'));
159+
}
160+
161+
if ($askForRedisPassword) {
162+
$this->output->comment(trans('command/messages.environment.app.redis_pass_help'));
163+
$this->variables['REDIS_PASSWORD'] = $this->option('redis-pass') ?? $this->output->askHidden(
164+
trans('command/messages.environment.app.redis_password'), function () {
165+
return true;
166+
}
167+
);
168+
}
169+
170+
$this->variables['REDIS_PORT'] = $this->option('redis-port') ?? $this->ask(
171+
trans('command/messages.environment.app.redis_port'), $this->config->get('database.redis.default.port')
172+
);
173+
}
174+
}
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?php
2+
/*
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Pterodactyl\Console\Commands\Environment;
26+
27+
use PDOException;
28+
use Illuminate\Console\Command;
29+
use Illuminate\Contracts\Console\Kernel;
30+
use Illuminate\Database\DatabaseManager;
31+
use Pterodactyl\Traits\Commands\EnvironmentWriterTrait;
32+
use Illuminate\Contracts\Config\Repository as ConfigRepository;
33+
34+
class DatabaseSettingsCommand extends Command
35+
{
36+
use EnvironmentWriterTrait;
37+
38+
/**
39+
* @var \Illuminate\Contracts\Config\Repository
40+
*/
41+
protected $config;
42+
43+
/**
44+
* @var \Illuminate\Contracts\Console\Kernel
45+
*/
46+
protected $console;
47+
48+
/**
49+
* @var \Illuminate\Database\DatabaseManager
50+
*/
51+
protected $database;
52+
53+
/**
54+
* @var string
55+
*/
56+
protected $description = 'Configure database settings for the Panel.';
57+
58+
/**
59+
* @var string
60+
*/
61+
protected $signature = 'p:environment:database
62+
{--host= : The connection address for the MySQL server.}
63+
{--port= : The connection port for the MySQL server.}';
64+
65+
/**
66+
* @var array
67+
*/
68+
protected $variables = [];
69+
70+
/**
71+
* DatabaseSettingsCommand constructor.
72+
*
73+
* @param \Illuminate\Contracts\Config\Repository $config
74+
* @param \Illuminate\Database\DatabaseManager $database
75+
* @param \Illuminate\Contracts\Console\Kernel $console
76+
*/
77+
public function __construct(ConfigRepository $config, DatabaseManager $database, Kernel $console)
78+
{
79+
parent::__construct();
80+
81+
$this->config = $config;
82+
$this->console = $console;
83+
$this->database = $database;
84+
}
85+
86+
/**
87+
* Handle command execution.
88+
*
89+
* @return int
90+
*
91+
* @throws \Pterodactyl\Exceptions\PterodactylException
92+
*/
93+
public function handle()
94+
{
95+
$this->output->note(trans('command/messages.environment.database.host_warning'));
96+
$this->variables['DB_HOST'] = $this->option('host') ?? $this->ask(
97+
trans('command/messages.environment.database.host'), $this->config->get('database.connections.mysql.host', '127.0.0.1')
98+
);
99+
100+
$this->variables['DB_PORT'] = $this->option('port') ?? $this->ask(
101+
trans('command/messages.environment.database.port'), $this->config->get('database.connections.mysql.port', 3306)
102+
);
103+
104+
$this->variables['DB_DATABASE'] = $this->option('port') ?? $this->ask(
105+
trans('command/messages.environment.database.database'), $this->config->get('database.connections.mysql.database', 'panel')
106+
);
107+
108+
$this->output->note(trans('command/messages.environment.database.username_warning'));
109+
$this->variables['DB_USERNAME'] = $this->option('port') ?? $this->ask(
110+
trans('command/messages.environment.database.username'), $this->config->get('database.connections.mysql.username', 'pterodactyl')
111+
);
112+
113+
$askForMySQLPassword = true;
114+
if (! empty($this->config->get('database.connections.mysql.password')) && $this->input->isInteractive()) {
115+
$this->variables['DB_PASSWORD'] = $this->config->get('database.connections.mysql.password');
116+
$askForMySQLPassword = $this->confirm(trans('command/messages.environment.database.password_defined'));
117+
}
118+
119+
if ($askForMySQLPassword) {
120+
$this->variables['DB_PASSWORD'] = $this->option('password') ?? $this->secret(trans('command/messages.environment.database.password'));
121+
}
122+
123+
try {
124+
$this->testMySQLConnection();
125+
} catch (PDOException $exception) {
126+
$this->output->error(trans('command/messages.environment.database.connection_error', ['error' => $exception->getMessage()]));
127+
$this->output->error(trans('command/messages.environment.database.creds_not_saved'));
128+
129+
if ($this->confirm(trans('command/messages.environment.database.try_again'))) {
130+
$this->database->disconnect('_pterodactyl_command_test');
131+
132+
return $this->handle();
133+
}
134+
135+
return 1;
136+
}
137+
138+
$this->writeToEnvironment($this->variables);
139+
140+
$this->console->call('config:cache');
141+
$this->info($this->console->output());
142+
143+
return 0;
144+
}
145+
146+
/**
147+
* Test that we can connect to the provided MySQL instance and perform a selection.
148+
*/
149+
private function testMySQLConnection()
150+
{
151+
$this->config->set('database.connections._pterodactyl_command_test', [
152+
'driver' => 'mysql',
153+
'host' => $this->variables['DB_HOST'],
154+
'port' => $this->variables['DB_PORT'],
155+
'database' => $this->variables['DB_DATABASE'],
156+
'username' => $this->variables['DB_USERNAME'],
157+
'password' => $this->variables['DB_PASSWORD'],
158+
'charset' => 'utf8mb4',
159+
'collation' => 'utf8mb4_unicode_ci',
160+
'strict' => true,
161+
]);
162+
163+
$this->database->connection('_pterodactyl_command_test')->getPdo();
164+
}
165+
}

0 commit comments

Comments
 (0)