koel/app/Console/Commands/InitCommand.php

288 lines
8.9 KiB
PHP
Raw Normal View History

2015-12-14 22:37:46 +00:00
<?php
namespace App\Console\Commands;
2017-12-03 16:54:11 +00:00
use App\Models\Setting;
2016-01-15 02:16:58 +00:00
use App\Models\User;
use App\Repositories\SettingRepository;
2018-08-19 14:56:56 +00:00
use App\Services\MediaCacheService;
2016-04-02 13:16:09 +00:00
use Exception;
2015-12-14 22:37:46 +00:00
use Illuminate\Console\Command;
2018-08-19 21:01:01 +00:00
use Illuminate\Contracts\Console\Kernel as Artisan;
use Illuminate\Contracts\Hashing\Hasher as Hash;
use Illuminate\Database\DatabaseManager as DB;
use Jackiedo\DotenvEditor\DotenvEditor;
2015-12-14 22:37:46 +00:00
2018-08-19 21:01:01 +00:00
class InitCommand extends Command
2015-12-14 22:37:46 +00:00
{
2016-01-15 02:16:58 +00:00
protected $signature = 'koel:init';
protected $description = 'Install or upgrade Koel';
2018-08-19 14:56:56 +00:00
2018-08-19 21:01:01 +00:00
private $mediaCacheService;
private $artisan;
private $dotenvEditor;
private $hash;
private $db;
private $settingRepository;
2018-08-19 21:01:01 +00:00
public function __construct(
MediaCacheService $mediaCacheService,
SettingRepository $settingRepository,
2018-08-19 21:01:01 +00:00
Artisan $artisan,
Hash $hash,
DotenvEditor $dotenvEditor,
DB $db
2018-08-19 21:03:21 +00:00
) {
2018-08-19 14:56:56 +00:00
parent::__construct();
$this->mediaCacheService = $mediaCacheService;
2018-08-19 21:01:01 +00:00
$this->artisan = $artisan;
$this->dotenvEditor = $dotenvEditor;
$this->hash = $hash;
$this->db = $db;
$this->settingRepository = $settingRepository;
2018-08-19 14:56:56 +00:00
}
2015-12-14 22:37:46 +00:00
2018-08-24 15:27:19 +00:00
public function handle(): void
2015-12-14 22:37:46 +00:00
{
2016-01-15 02:16:58 +00:00
$this->comment('Attempting to install or upgrade Koel.');
$this->comment('Remember, you can always install/upgrade manually following the guide here:');
2017-03-01 11:06:07 +00:00
$this->info('📙 '.config('koel.misc.docs_url').PHP_EOL);
2015-12-14 22:37:46 +00:00
if ($this->inNoInteractionMode()) {
$this->info('Running in no-interaction mode');
}
2018-08-23 06:51:16 +00:00
$this->maybeGenerateAppKey();
$this->maybeGenerateJwtSecret();
$this->maybeSetUpDatabase();
$this->migrateDatabase();
$this->maybeSeedDatabase();
$this->maybeSetMediaPath();
$this->compileFrontEndAssets();
2015-12-14 22:37:46 +00:00
2017-12-03 16:54:11 +00:00
$this->comment(PHP_EOL.'🎆 Success! Koel can now be run from localhost with `php artisan serve`.');
2018-08-19 21:01:01 +00:00
2018-08-31 13:47:15 +00:00
if (Setting::get('media_path')) {
2017-12-03 16:54:11 +00:00
$this->comment('You can also scan for media with `php artisan koel:sync`.');
}
2018-08-19 21:01:01 +00:00
2016-01-15 02:16:58 +00:00
$this->comment('Again, for more configuration guidance, refer to');
2017-03-01 11:06:07 +00:00
$this->info('📙 '.config('koel.misc.docs_url'));
2017-12-03 16:54:11 +00:00
$this->comment('or open the .env file in the root installation folder.');
2017-03-01 11:06:07 +00:00
$this->comment('Thanks for using Koel. You rock!');
2015-12-14 22:37:46 +00:00
}
2017-12-03 16:54:11 +00:00
/**
* Prompt user for valid database credentials and set up the database.
*/
2018-08-24 15:27:19 +00:00
private function setUpDatabase(): void
2017-12-03 16:54:11 +00:00
{
$config = [
'DB_CONNECTION' => '',
'DB_HOST' => '',
'DB_PORT' => '',
'DB_DATABASE' => '',
'DB_USERNAME' => '',
2017-12-03 16:54:34 +00:00
'DB_PASSWORD' => '',
2017-12-03 16:54:11 +00:00
];
$config['DB_CONNECTION'] = $this->choice(
'Your DB driver of choice',
[
'mysql' => 'MySQL/MariaDB',
'pgsql' => 'PostgreSQL',
2017-12-03 16:54:11 +00:00
'sqlsrv' => 'SQL Server',
2017-12-03 16:54:34 +00:00
'sqlite-e2e' => 'SQLite',
2017-12-03 16:54:11 +00:00
],
'mysql'
);
2018-08-19 21:03:21 +00:00
2017-12-03 16:54:11 +00:00
if ($config['DB_CONNECTION'] === 'sqlite-e2e') {
$config['DB_DATABASE'] = $this->ask('Absolute path to the DB file');
} else {
$config['DB_HOST'] = $this->anticipate('DB host', ['127.0.0.1', 'localhost']);
2019-06-30 14:22:53 +00:00
$config['DB_PORT'] = (string) $this->ask('DB port (leave empty for default)');
2017-12-03 16:54:11 +00:00
$config['DB_DATABASE'] = $this->anticipate('DB name', ['koel']);
$config['DB_USERNAME'] = $this->anticipate('DB user', ['koel']);
2019-06-30 14:22:53 +00:00
$config['DB_PASSWORD'] = (string) $this->ask('DB password');
2017-12-03 16:54:11 +00:00
}
foreach ($config as $key => $value) {
2018-08-19 21:01:01 +00:00
$this->dotenvEditor->setKey($key, $value);
2017-12-03 16:54:11 +00:00
}
2018-08-19 21:01:01 +00:00
$this->dotenvEditor->save();
2017-12-03 16:54:11 +00:00
// Set the config so that the next DB attempt uses refreshed credentials
config([
'database.default' => $config['DB_CONNECTION'],
"database.connections.{$config['DB_CONNECTION']}.host" => $config['DB_HOST'],
"database.connections.{$config['DB_CONNECTION']}.port" => $config['DB_PORT'],
"database.connections.{$config['DB_CONNECTION']}.database" => $config['DB_DATABASE'],
"database.connections.{$config['DB_CONNECTION']}.username" => $config['DB_USERNAME'],
"database.connections.{$config['DB_CONNECTION']}.password" => $config['DB_PASSWORD'],
]);
}
private function inNoInteractionMode(): bool
{
return (bool) $this->option('no-interaction');
}
2018-08-24 15:27:19 +00:00
private function setUpAdminAccount(): void
2017-12-03 16:54:11 +00:00
{
$this->info("Let's create the admin account.");
2018-08-19 21:01:01 +00:00
[$name, $email, $password] = $this->gatherAdminAccountCredentials();
2017-12-03 16:54:11 +00:00
User::create([
'name' => $name,
'email' => $email,
2018-08-19 21:01:01 +00:00
'password' => $this->hash->make($password),
2017-12-03 16:54:11 +00:00
'is_admin' => true,
]);
}
2018-08-24 15:27:19 +00:00
private function maybeSetMediaPath(): void
2017-12-03 16:54:11 +00:00
{
2018-08-31 13:47:15 +00:00
if (Setting::get('media_path')) {
2018-08-23 06:51:16 +00:00
return;
}
if ($this->inNoInteractionMode()) {
$this->setMediaPathFromEnvFile();
return;
}
2017-12-03 16:54:11 +00:00
$this->info('The absolute path to your media directory. If this is skipped (left blank) now, you can set it later via the web interface.');
while (true) {
$path = $this->ask('Media path', config('koel.media_path'));
2018-08-19 21:01:01 +00:00
if (!$path) {
2017-12-03 16:54:11 +00:00
return;
}
if ($this->isValidMediaPath($path)) {
2017-12-03 16:54:11 +00:00
Setting::set('media_path', $path);
2017-12-03 16:54:34 +00:00
2017-12-03 16:54:11 +00:00
return;
}
$this->error('The path does not exist or not readable. Try again.');
}
}
2018-08-23 06:51:16 +00:00
2018-08-24 15:27:19 +00:00
private function maybeGenerateAppKey(): void
2018-08-23 06:51:16 +00:00
{
if (!config('app.key')) {
$this->info('Generating app key');
$this->artisan->call('key:generate');
} else {
$this->comment('App key exists -- skipping');
}
}
2018-08-24 15:27:19 +00:00
private function maybeGenerateJwtSecret(): void
2018-08-23 06:51:16 +00:00
{
if (!config('jwt.secret')) {
$this->info('Generating JWT secret');
$this->artisan->call('koel:generate-jwt-secret');
} else {
$this->comment('JWT secret exists -- skipping');
}
}
2018-08-24 15:27:19 +00:00
private function maybeSeedDatabase(): void
2018-08-23 06:51:16 +00:00
{
if (!User::count()) {
$this->setUpAdminAccount();
$this->info('Seeding initial data');
$this->artisan->call('db:seed', ['--force' => true]);
} else {
$this->comment('Data seeded -- skipping');
}
}
2018-08-24 15:27:19 +00:00
private function maybeSetUpDatabase(): void
2018-08-23 06:51:16 +00:00
{
$dbSetUp = false;
while (!$dbSetUp) {
try {
// Make sure the config cache is cleared before another attempt.
$this->artisan->call('config:clear');
$this->db->reconnect()->getPdo();
$dbSetUp = true;
} catch (Exception $e) {
$this->error($e->getMessage());
$this->warn(PHP_EOL."Koel cannot connect to the database. Let's set it up.");
2018-08-23 06:51:16 +00:00
$this->setUpDatabase();
}
}
}
2018-08-24 15:27:19 +00:00
private function migrateDatabase(): void
2018-08-23 06:51:16 +00:00
{
$this->info('Migrating database');
$this->artisan->call('migrate', ['--force' => true]);
// Clear the media cache, just in case we did any media-related migration
$this->mediaCacheService->clear();
}
2018-08-24 15:27:19 +00:00
private function compileFrontEndAssets(): void
2018-08-23 06:51:16 +00:00
{
$this->info('Compiling front-end stuff');
system('yarn install');
}
/** @return array<string> */
private function gatherAdminAccountCredentials(): array
{
if ($this->inNoInteractionMode()) {
return [config('koel.admin.name'), config('koel.admin.email'), config('koel.admin.password')];
}
$name = $this->ask('Your name', config('koel.admin.name'));
$email = $this->ask('Your email address', config('koel.admin.email'));
$passwordConfirmed = false;
$password = null;
while (!$passwordConfirmed) {
$password = $this->secret('Your desired password');
$confirmation = $this->secret('Again, just to make sure');
if ($confirmation !== $password) {
$this->error("That doesn't match. Let's try again.");
} else {
$passwordConfirmed = true;
}
}
return [$name, $email, $password];
}
private function isValidMediaPath(string $path): bool
{
return is_dir($path) && is_readable($path);
}
private function setMediaPathFromEnvFile(): void
{
with(config('koel.media_path'), function (?string $path): void {
if (!$path) {
return;
}
if ($this->isValidMediaPath($path)) {
Setting::set('media_path', $path);
} else {
$this->warn(sprintf('The path %s does not exist or not readable. Skipping.', $path));
}
});
}
2015-12-14 22:37:46 +00:00
}