From 3d0df818cf4744b16846f8c114ef44f978e2beb8 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Mon, 2 Oct 2023 15:51:44 +0200 Subject: [PATCH] wip #6278 @2 --- .../Operations/Tools/DockerWebContainer.php | 154 ++++++++++-------- .../tools/dockerwebcontainer/elasticsearch | 12 ++ .../template/images/php-fpm/Dockerfile | 10 +- 3 files changed, 102 insertions(+), 74 deletions(-) create mode 100644 resources/tools/dockerwebcontainer/elasticsearch diff --git a/app/Http/Controllers/Admin/Operations/Tools/DockerWebContainer.php b/app/Http/Controllers/Admin/Operations/Tools/DockerWebContainer.php index 0ab21f978..aef6300ae 100644 --- a/app/Http/Controllers/Admin/Operations/Tools/DockerWebContainer.php +++ b/app/Http/Controllers/Admin/Operations/Tools/DockerWebContainer.php @@ -6,6 +6,8 @@ use App\Notifications\DownloadReady; use App\Notifications\ToolboxNotification; use Cubist\Backpack\Magic\Fields\Checkbox; use Cubist\Backpack\Magic\Fields\SelectFromArray; +use Cubist\Backpack\Magic\Fields\SelectFromArrayMultiple; +use Cubist\Backpack\Magic\Fields\Tags; use Cubist\Backpack\Magic\Fields\Text; use Cubist\Backpack\Magic\Form; use Cubist\Util\Files\Files; @@ -26,21 +28,19 @@ trait DockerWebContainer $form->setSubmitIcon('lab la-docker'); $form->addField('containername', Text::class, __('Nom du container'), ['prefix' => '/docker/']); $form->addField('domain', Text::class, __('Nom du domaine ou sous-domaine'), ['placeholder' => 'containername.dev.cubedesigners.com']); + $form->addField('env', SelectFromArrayMultiple::class, __('Environnements'), ['default' => json_encode(['production', 'dev']), 'options' => ['production' => 'production', 'dev' => 'dev', 'staging' => 'staging', 'preprod' => 'preprod']]); $form->addField('phpversion', SelectFromArray::class, __('Version de PHP'), ["options" => ['none' => '-', '5.6' => '5.6', -// '7.0' => '7.0', -// '7.1' => '7.1', -// '7.2' => '7.2', -// '7.3' => '7.3', '7.4' => '7.4', -// '8.0' => '8.0', '8.1' => '8.1', '8.2' => '8.2', ], 'value' => '8.2']); $form->addField('mysql', Checkbox::class, __('Serveur Mariadb') . ' (MySQL)', ['default' => true]); $form->addField('redis', Checkbox::class, __('Serveur Redis'), ['default' => true]); $form->addField('matomo', Checkbox::class, __('Serveur Matomo'), ['default' => true]); + $form->addField('elasticsearch', Checkbox::class, __('Serveur ElasticSearch'), ['default' => false]); + return view('tools.form', ['form' => $form]); } @@ -49,80 +49,96 @@ trait DockerWebContainer { $request = request(); $name = Str::slug($request->get('containername', 'containername')); - $variables = ['$name' => $name, - '$portadminer' => rand(10000, 60000), - '$portmatomo' => rand(10000, 60000), - '$matomodbpassword' => Str::random(16), - '$port' => rand(10000, 60000), - '$sshport' => rand(10000, 60000), - '$domain' => $request->get('domain', ''), - '$dbpassword' => Str::random(16), - '$phpfpmimage' => 'php-fpm', - '$locale' => 'fr_FR', - '$localeshort' => 'fr', - '$sshpassword' => Str::random(16), - ]; - - if (!$variables['$domain']) { - $variables['$domain'] = $name . '.dev.cubedesigners.com'; - } + + $envs = $request->get('env', ['production']); $tmp = Files::tmpdir(); - $dir = Files::mkdir($tmp . '/' . $name); - $source = resource_path('tools/dockerwebcontainer/template'); - `cp -r $source/* $dir/`; - - $compose = ['base']; - $variables['$phpversion'] = $request->get('phpversion', '8.1'); - if ($variables['$phpversion'] !== 'none') { - $compose[] = 'php'; - if (version_compare($variables['$phpversion'], '7.3', '<=')) { - $variables['$phpfpmimage'] = 'php-' . $variables['$phpversion'] . '-fpm'; + $notification = __('Le container web :name a été crée avec les informations suivantes', ['name' => $name]) . " : \n\n"; + + foreach ($envs as $env) { + $notification .= '---- Environnement : ' . $env . " ----\n\n"; + $subDomain = $env === 'production' ? 'www.' : $env . '.'; + $dockerSuffix = $env === 'production' ? '' : '-' . $env; + + + $variables = ['$name' => $name.$dockerSuffix, + '$portadminer' => rand(10000, 60000), + '$portmatomo' => rand(10000, 60000), + '$matomodbpassword' => Str::random(16), + '$port' => rand(10000, 60000), + '$sshport' => rand(10000, 60000), + '$domain' => $request->get('domain', ''), + '$dbpassword' => Str::random(16), + '$phpfpmimage' => 'php-fpm', + '$locale' => 'fr_FR', + '$localeshort' => 'fr', + '$sshpassword' => Str::random(16), + ]; + + if (!$variables['$domain']) { + $variables['$domain'] = $name . '.dev.cubedesigners.com'; } - } - if ($request->get('mysql', true)) { - $compose[] = 'mysql'; - } - if ($request->get('matomo', true)) { - $compose[] = 'matomo'; - } - $compose[] = 'network'; - $composeFileContent = ''; - foreach ($compose as $item) { - $composeFileContent .= file_get_contents(resource_path('tools/dockerwebcontainer/' . $item)); - } - file_put_contents($dir . '/docker-compose.yml', $composeFileContent); + $variables['$domain'] = $subDomain . $variables['$domain']; + + $dir = Files::mkdir($tmp . '/' . $name . $dockerSuffix); + $source = resource_path('tools/dockerwebcontainer/template'); + `cp -r $source/* $dir/`; + + $compose = ['base']; + $variables['$phpversion'] = $request->get('phpversion', '8.1'); + if ($variables['$phpversion'] !== 'none') { + $compose[] = 'php'; + if (version_compare($variables['$phpversion'], '7.3', '<=')) { + $variables['$phpfpmimage'] = 'php-' . $variables['$phpversion'] . '-fpm'; + } + } + if ($request->get('mysql', true)) { + $compose[] = 'mysql'; + } + if ($request->get('matomo', true)) { + $compose[] = 'matomo'; + } + if ($request->get('elasticsearch', false)) { + $compose[] = 'elasticsearch'; + } + $compose[] = 'network'; + $composeFileContent = ''; + foreach ($compose as $item) { + $composeFileContent .= file_get_contents(resource_path('tools/dockerwebcontainer/' . $item)); + } + file_put_contents($dir . '/docker-compose.yml', $composeFileContent); - Files::recursiveReplaceStringInDir($dir, $variables); + $infos = [ + 'URL' => 'https://' . $variables['$domain'] . '/']; + + if ($request->get('mysql', true)) { + $infos = array_merge($infos, + [ + __('Hôte de base de données') => $variables['$name'] . '-mariadb', + __('Nom de base de données') => $variables['$name'], + __('Utilisateur de la base de données') => 'root', + __('Mot de passe de la base de données') => $variables['$dbpassword'], + 'Adminer' => 'https://adminer.' . $variables['$domain'] . '/', + __('Serveur SSH/SFTP') => $variables['$domain'], + __('Port') => $variables['$sshport'], + __('Utilisateur') => $variables['$name'], + __('Mot de passe') => $variables['$sshpassword'], + ]); + } + foreach ($infos as $k => $v) { + $notification .= '* ' . $k . ' : ' . $v . "\n"; + } + $notification.="\n"; + + Files::recursiveReplaceStringInDir($dir, $variables); + } $tmpfile = Files::tempnam() . '.zip'; Zip::archive($tmp, $tmpfile); Files::rmdir($tmp); $res = response()->download($tmpfile, $name . '.zip')->deleteFileAfterSend(true); - $notification = __('Le container web :name a été crée avec les informations suivantes', ['name' => $variables['$name']]) . "\n"; - $infos = [ - 'URL' => 'https://' . $variables['$domain'] . '/']; - - if ($request->get('mysql', true)) { - $infos = array_merge($infos, - [ - __('Hôte de base de données') => $variables['$name'] . '-mariadb', - __('Nom de base de données') => $variables['$name'], - __('Utilisateur de la base de données') => 'root', - __('Mot de passe de la base de données') => $variables['$dbpassword'], - 'Adminer' => 'https://adminer.' . $variables['$domain'] . '/', - __('Serveur SSH/SFTP') => $variables['$domain'], - __('Port') => $variables['$sshport'], - __('Utilisateur') => $variables['$name'], - __('Mot de passe') => $variables['$sshpassword'], - ]); - } - foreach ($infos as $k => $v) { - $notification .= '* ' . $k . ' : ' . $v . "\n"; - } - - backpack_user()->notify(new ToolboxNotification(__('Container :name web prêt', ['name' => $variables['$name']]), $notification, [], true)); + backpack_user()->notify(new ToolboxNotification(__('Container :name web prêt', ['name' => $name]), $notification, [], true)); return $res; } } diff --git a/resources/tools/dockerwebcontainer/elasticsearch b/resources/tools/dockerwebcontainer/elasticsearch new file mode 100644 index 000000000..e5d76ceff --- /dev/null +++ b/resources/tools/dockerwebcontainer/elasticsearch @@ -0,0 +1,12 @@ + elasticsearch: + container_name: $name-elasticsearch + image: docker.elastic.co/elasticsearch/elasticsearch:8.10.2 + volumes: + - ./esdata:/usr/share/elasticsearch/data + environment: + - bootstrap.memory_lock=true + - "ES_JAVA_OPTS=-Xms512m -Xmx512m" + - discovery.type=single-node + networks: + - $name + restart: unless-stopped diff --git a/resources/tools/dockerwebcontainer/template/images/php-fpm/Dockerfile b/resources/tools/dockerwebcontainer/template/images/php-fpm/Dockerfile index 706b929df..12ba4acdf 100644 --- a/resources/tools/dockerwebcontainer/template/images/php-fpm/Dockerfile +++ b/resources/tools/dockerwebcontainer/template/images/php-fpm/Dockerfile @@ -14,10 +14,13 @@ ENV DEBIAN_FRONTEND=noninteractive # Add Ondrej PHP repository RUN apt update \ - && apt install -y --no-install-recommends gnupg bash curl \ + && apt install -y --no-install-recommends ca-certificates gnupg bash curl \ && echo "deb http://ppa.launchpad.net/ondrej/php/ubuntu jammy main" > /etc/apt/sources.list.d/ondrej-php.list \ && apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 4F4EA0AAE5267A6C +RUN curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg +RUN echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list + RUN apt update RUN apt -y --no-install-recommends install \ @@ -48,13 +51,10 @@ RUN apt -y --no-install-recommends install \ php$phpversion-curl \ php$phpversion-mcrypt \ php$phpversion-fpm \ - less nano wget + less nano wget nodejs RUN apt -y --no-install-recommends install nodejs sudo openssh-server rsyslog cron mariadb-client -# Install nodejs repository -RUN curl -fsSL https://deb.nodesource.com/setup_19.x | bash - - RUN apt -y --no-install-recommends install locales RUN sed -i '/$locale.UTF-8/s/^# //g' /etc/locale.gen && \ locale-gen -- 2.39.5