From 4e42a56a7f86a8da69fa3c27b3e6d18c44e92fab Mon Sep 17 00:00:00 2001 From: Shyim Date: Thu, 26 Apr 2018 17:50:11 +0200 Subject: [PATCH] Simplify installation --- README.md | 8 +-- src/Command/SetupCommand.php | 104 +++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 src/Command/SetupCommand.php diff --git a/README.md b/README.md index ecbcb01..6625967 100644 --- a/README.md +++ b/README.md @@ -7,12 +7,12 @@ ReCast is a multi platform streaming tool written in PHP and uses nginx RTMP. Yo ## Installation * You have to install a nginx server [Tutorial](https://github.com/recastin/panel/wiki/Install-Nginx-RTMP) -* Checkout this project, copy .env.dist to .env and adjust the settings +* Checkout this project * Run ```composer install --no-dev -o``` -* Generate JWT Keys, follow [Documentation](https://github.com/lexik/LexikJWTAuthenticationBundle/blob/HEAD/Resources/doc/index.md#installation) -* Create the database ```php bin/console doctrine:migrations:migrate``` -* Create a new crontab entry which runs every minute ```php bin/console recast:cron``` +* Run ``php bin/console recast:setup`` +* Create the tables ```php bin/console doctrine:migrations:migrate``` * Create a new user with ```php bin/console recast:create:user``` +* Create a new crontab entry which runs every minute ```php bin/console recast:cron``` * Environment variable ``APP_HOST`` should point to a http server, nginx rtmp does not support https. ### Environment variable overview diff --git a/src/Command/SetupCommand.php b/src/Command/SetupCommand.php new file mode 100644 index 0000000..9e1cb3a --- /dev/null +++ b/src/Command/SetupCommand.php @@ -0,0 +1,104 @@ + + */ +class SetupCommand extends Command +{ + private const OPENSSL_TOKEN_CONFIG = [ + 'digest_alg' => 'aes256', + 'private_key_bits' => 4096, + 'private_key_type' => OPENSSL_KEYTYPE_RSA, + ]; + + /** + * @author Soner Sayakci + */ + public function configure() + { + $this + ->setName('recast:setup') + ->setDescription('Configures a new recast instance'); + } + + /** + * @param InputInterface $input + * @param OutputInterface $output + * @return int|null|void + * @author Soner Sayakci + * @throws \Exception + */ + public function execute(InputInterface $input, OutputInterface $output) + { + $io = new SymfonyStyle($input, $output); + + $host = $io->ask('MySQL Host', 'localhost'); + $hostPort = $io->ask('MySQL Port', '3306'); + $user = $io->ask('MySQL User', 'recast'); + $password = $io->ask('MySQL Password', 'iLoveReCast'); + $dbName = $io->ask('MySQL Database', 'recast'); + $appHost = $io->ask('Please specify a http url where recast will be available', 'http://app.recast.in'); + $nginxFolder = $io->ask('Please specify the nginx folder where nginx rtmp is installed', '/opt/nginx-rtmp/conf/'); + $nginxReloadCommand = $io->ask('Please specify the command that should be executed to reload nginx rtmp', 'systemctl reload nginx-rtmp'); + + if (parse_url($appHost, PHP_URL_SCHEME) !== 'http') { + throw new \RuntimeException('URL must be http due nginx-rtmp limitations'); + } + + $envs = []; + $envs['APP_ENV'] = 'prod'; + $envs['APP_SECRET'] = $this->generateRandomString(); + $envs['MAILER_URL'] = 'null://localhost'; + $envs['JWT_PRIVATE_KEY_PATH'] = 'config/jwt/private.pem'; + $envs['JWT_PUBLIC_KEY_PATH'] = 'config/jwt/public.pem'; + $envs['JWT_PASSPHRASE'] = 'a758fddfbc878122f8b37259b8ea14c3'; + $envs['DATABASE_URL'] = sprintf('mysql://%s:%s@%s:%s/%s', $user, $password, $host, $hostPort, $dbName); + $envs['NGINX_CONFIG_DIR'] = $nginxFolder; + $envs['APP_HOST'] = $appHost; + $envs['NGINX_RESTART_COMMAND'] = $nginxReloadCommand; + + $stringEnv = []; + foreach ($envs as $env => $value) { + $stringEnv[] = $env . '="' . $value . '"'; + } + + file_put_contents('.env', implode("\n", $stringEnv)); + + $io->success('Environment configuration generated'); + + $res = openssl_pkey_new(self::OPENSSL_TOKEN_CONFIG); + openssl_pkey_export($res, $privKey); + $pubKey = openssl_pkey_get_details($res); + $pubKey = $pubKey['key']; + + file_put_contents($envs['JWT_PRIVATE_KEY_PATH'], $privKey); + file_put_contents($envs['JWT_PUBLIC_KEY_PATH'], $pubKey); + + $io->success('JWT Tokens generated'); + } + + /** + * @param int $length + * @return string + * @author Soner Sayakci + * @throws \Exception + */ + private function generateRandomString($length = 32) : string + { + $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + $charactersLength = \strlen($characters); + $randomString = ''; + for ($i = 0; $i < $length; $i++) { + $randomString .= $characters[random_int(0, $charactersLength - 1)]; + } + return $randomString; + } +} \ No newline at end of file