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;
|
2018-08-19 14:56:56 +00:00
|
|
|
use App\Services\MediaCacheService;
|
2016-01-15 07:28:34 +00:00
|
|
|
use DB;
|
2016-04-02 13:16:09 +00:00
|
|
|
use Exception;
|
2015-12-14 22:37:46 +00:00
|
|
|
use Illuminate\Console\Command;
|
|
|
|
use Illuminate\Support\Facades\Artisan;
|
2017-12-03 16:54:11 +00:00
|
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
use Jackiedo\DotenvEditor\Facades\DotenvEditor;
|
2015-12-14 22:37:46 +00:00
|
|
|
|
|
|
|
class Init extends Command
|
|
|
|
{
|
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
|
|
|
private $mediaCacheService;
|
|
|
|
|
|
|
|
public function __construct(MediaCacheService $mediaCacheService)
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->mediaCacheService = $mediaCacheService;
|
|
|
|
}
|
2015-12-14 22:37:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
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
|
|
|
|
2016-08-21 15:19:03 +00:00
|
|
|
if (!config('app.key')) {
|
2016-01-15 02:16:58 +00:00
|
|
|
$this->info('Generating app key');
|
|
|
|
Artisan::call('key:generate');
|
|
|
|
} else {
|
|
|
|
$this->comment('App key exists -- skipping');
|
|
|
|
}
|
2015-12-14 22:37:46 +00:00
|
|
|
|
2016-08-21 15:19:03 +00:00
|
|
|
if (!config('jwt.secret')) {
|
2016-01-15 02:16:58 +00:00
|
|
|
$this->info('Generating JWT secret');
|
|
|
|
Artisan::call('koel:generate-jwt-secret');
|
|
|
|
} else {
|
|
|
|
$this->comment('JWT secret exists -- skipping');
|
|
|
|
}
|
2016-01-15 07:28:34 +00:00
|
|
|
|
2017-12-03 16:54:11 +00:00
|
|
|
$dbSetUp = false;
|
|
|
|
while (!$dbSetUp) {
|
|
|
|
try {
|
|
|
|
// Make sure the config cache is cleared before another attempt.
|
|
|
|
Artisan::call('config:clear');
|
|
|
|
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.');
|
|
|
|
$this->setUpDatabase();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 02:16:58 +00:00
|
|
|
$this->info('Migrating database');
|
2015-12-14 22:37:46 +00:00
|
|
|
Artisan::call('migrate', ['--force' => true]);
|
2018-08-19 14:56:56 +00:00
|
|
|
|
2017-01-06 03:04:08 +00:00
|
|
|
// Clean the media cache, just in case we did any media-related migration
|
2018-08-19 14:56:56 +00:00
|
|
|
$this->mediaCacheService->clear();
|
2015-12-14 22:37:46 +00:00
|
|
|
|
2016-01-15 02:16:58 +00:00
|
|
|
if (!User::count()) {
|
2017-12-03 16:54:11 +00:00
|
|
|
$this->setUpAdminAccount();
|
2016-01-15 02:16:58 +00:00
|
|
|
$this->info('Seeding initial data');
|
|
|
|
Artisan::call('db:seed', ['--force' => true]);
|
|
|
|
} else {
|
|
|
|
$this->comment('Data seeded -- skipping');
|
|
|
|
}
|
2015-12-14 22:37:46 +00:00
|
|
|
|
2017-12-03 16:54:11 +00:00
|
|
|
if (!Setting::get('media_path')) {
|
|
|
|
$this->setMediaPath();
|
|
|
|
}
|
|
|
|
|
2017-03-27 10:09:05 +00:00
|
|
|
$this->info('Compiling front-end stuff');
|
2016-12-02 09:39:31 +00:00
|
|
|
system('yarn install');
|
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`.');
|
|
|
|
if (Setting::get('media_path')) {
|
|
|
|
$this->comment('You can also scan for media with `php artisan koel:sync`.');
|
|
|
|
}
|
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.
|
|
|
|
*/
|
|
|
|
private function setUpDatabase()
|
|
|
|
{
|
|
|
|
$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',
|
2017-12-11 20:50:32 +00:00
|
|
|
'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'
|
|
|
|
);
|
|
|
|
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']);
|
|
|
|
$config['DB_PORT'] = (string) $this->ask('DB port (leave empty for default)', false);
|
|
|
|
$config['DB_DATABASE'] = $this->anticipate('DB name', ['koel']);
|
|
|
|
$config['DB_USERNAME'] = $this->anticipate('DB user', ['koel']);
|
|
|
|
$config['DB_PASSWORD'] = (string) $this->ask('DB password', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($config as $key => $value) {
|
|
|
|
DotenvEditor::setKey($key, $value);
|
|
|
|
}
|
|
|
|
DotenvEditor::save();
|
|
|
|
|
|
|
|
// 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'],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set up the admin account.
|
|
|
|
*/
|
|
|
|
private function setUpAdminAccount()
|
|
|
|
{
|
|
|
|
$this->info("Let's create the admin account.");
|
2017-12-03 16:54:34 +00:00
|
|
|
$name = $this->ask('Your name');
|
2017-12-03 16:54:11 +00:00
|
|
|
$email = $this->ask('Your email address');
|
|
|
|
$passwordConfirmed = false;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
User::create([
|
|
|
|
'name' => $name,
|
|
|
|
'email' => $email,
|
|
|
|
'password' => Hash::make($password),
|
|
|
|
'is_admin' => true,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the media path via the console.
|
|
|
|
*/
|
|
|
|
private function setMediaPath()
|
|
|
|
{
|
|
|
|
$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', false);
|
|
|
|
if ($path === false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_dir($path) && is_readable($path)) {
|
|
|
|
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.');
|
|
|
|
}
|
|
|
|
}
|
2015-12-14 22:37:46 +00:00
|
|
|
}
|