Ver código fonte

add a composer postscript cmd to setup the env file

Olivier Massot 2 anos atrás
pai
commit
a9eae39779
15 arquivos alterados com 422 adições e 6 exclusões
  1. 1 0
      composer.json
  2. 1 0
      config/services.yaml
  3. 19 6
      doc/env.md
  4. 1 0
      env/.env.ci
  5. 1 0
      env/.env.docker
  6. 2 0
      env/.env.prod
  7. 1 0
      env/.env.staging
  8. 1 0
      env/.env.test
  9. 62 0
      env/.env.test1
  10. 62 0
      env/.env.test2
  11. 62 0
      env/.env.test3
  12. 62 0
      env/.env.test4
  13. 62 0
      env/.env.test5
  14. 3 0
      env/readme.md
  15. 82 0
      src/Commands/SetupEnvCommand.php

+ 1 - 0
composer.json

@@ -132,6 +132,7 @@
   },
   "scripts": {
     "auto-scripts": {
+      "ot:setup:env": "symfony-cmd",
       "cache:clear": "symfony-cmd",
       "assets:install %PUBLIC_DIR%": "symfony-cmd"
     },

+ 1 - 0
config/services.yaml

@@ -13,6 +13,7 @@ services:
         autowire: true      # Automatically injects dependencies in your services.
         autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
         bind:
+            $projectDir: '%kernel.project_dir%'
             $opentalentConfig: '%kernel.project_dir%%env(OPENTALENT_CONFIG)%'
             $internalFilesUploadUri: '%env(INTERNAL_FILES_DOWNLOAD_URI)%'
             $internalRequestsToken: '%env(INTERNAL_REQUESTS_TOKEN)%'

+ 19 - 6
doc/env.md

@@ -8,15 +8,28 @@ Les variables d'environnement sont définies dans les fichiers .env (dotenv)
 
 Les fichiers dotenv s'organisent ainsi :
 
-Un fichier `.env` commun à tous les environnements, il définit des valeurs par défaut.
+* Un fichier `.env` commun à tous les environnements, il définit des valeurs par défaut.
+* Un fichier `.env.local`, non versionné, qui surcharge le fichier `.env` avec les données propres à l'environnement local.
 
-Un fichier `.env.local` : il ne doit **pas** être versionné et contiendra des variables propres à l'environnement local.
-C'est lui qui définit le nom de l'environnement, par exemple : 
+Il existe d'autres manières de surcharger le fichier d'environnement, mais elles ne nous seront pas utiles ici.
 
-    APP_ENV=prod
+### Mise en pratique
 
-Un fichier `.env.<environnement>` qui ne s'appliquera qu'à l'environnement donné. Cet environnement est défini par la 
-variable d'environnement `APP_ENV` (voir plus haut).
+On trouve dans le répertoire `env` des fichiers `.env.<environnement>` correspondant aux différents environnements 
+de développement, test, production, etc.
+
+Lorsque la commande `composer install|update` est exécutée, la commande `bin/console ot:setup:env` s'exécute en post-
+script et génère un symlink `.env.local` à la racine du projet.
+
+Pour forcer la mise à jour du fichier d'environnement : 
+
+    bin/console ot:setup:env
+
+Ce symlink pointe vers le fichier d'environnement correspondant à la machine hôte.
+
+On peut forcer un nom d'hôte avec l'option `--host`. Ex : 
+
+    bin/console ot:setup:env --host=ci
 
 
 ### Déboguer

+ 1 - 0
.env.ci → env/.env.ci

@@ -1,4 +1,5 @@
 ###> symfony/framework-bundle ###
+APP_ENV=ci
 APP_DEBUG=1
 APP_SECRET=6a76497c8658bb23e2236f97a2627df3
 #TRUSTED_PROXIES=127.0.0.0/8,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16

+ 1 - 0
.env.docker → env/.env.docker

@@ -1,4 +1,5 @@
 ###> symfony/framework-bundle ###
+APP_ENV=dev
 APP_DEBUG=1
 ###< symfony/framework-bundle ###
 

+ 2 - 0
.env.prod → env/.env.prod

@@ -1,4 +1,6 @@
 ###> doctrine/doctrine-bundle ###
+APP_ENV=prod
+
 # Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
 # IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml
 DATABASE_URL=mysql://root:mysql2iopenservice369566@prod-back:3306/opentalent?serverVersion=5.7

+ 1 - 0
.env.staging → env/.env.staging

@@ -1,4 +1,5 @@
 # Fichier d'env utilisé pour les tests fonctionnels et applicatifs
+APP_ENV=staging
 APP_DEBUG=1
 
 ###> doctrine/doctrine-bundle ###

+ 1 - 0
.env.test → env/.env.test

@@ -1,4 +1,5 @@
 ###> symfony/framework-bundle ###
+APP_ENV=test
 APP_DEBUG=1
 ###< symfony/framework-bundle ###
 

+ 62 - 0
env/.env.test1

@@ -0,0 +1,62 @@
+###> symfony/framework-bundle ###
+APP_ENV=test
+APP_DEBUG=1
+###< symfony/framework-bundle ###
+
+###> doctrine/doctrine-bundle ###
+DATABASE_URL=mysql://root:mysql2iopenservice369566@localhost:3306/opentalent?serverVersion=5.7
+###< doctrine/doctrine-bundle ###
+
+###> nelmio/cors-bundle ###
+CORS_ALLOW_ORIGIN=^https?://(localhost|127\.0\.0\.1)(:[0-9]+)$
+###< nelmio/cors-bundle ###
+
+###> api v1 ###
+API_LEG_BASE_URL=https://api.test1.opentalent.fr/api
+###< files management ###
+
+###> typo3 client ###
+TYPO3_BASE_URI=http://test1.opentalent.fr/ohcluses
+###< typo3 client ###
+
+###> BlackFire configuration ###
+BLACKFIRE_CLIENT_ID=988fcba8-552d-48df-a9c2-035c76535b69
+BLACKFIRE_CLIENT_TOKEN=8cfbeb263d044da9678dc2612531504da3790c308da7448e35724a5da91c136f
+BLACKFIRE_SERVER_ID=1171e53b-459b-41da-a292-80ff68cee8c2
+BLACKFIRE_SERVER_TOKEN=dbd1cfbea015fe83cccfc189a36ca3c16f3a1b43b94f50032a15e41e53548e8b
+###< BlackFire configuration ###
+
+###> AdminAssos configuration ###
+DATABASE_ADMINASSOS_URL=mysql://root:mysql2iopenservice369566@test:3306/adminassos?serverVersion=5.7
+###< AdminAssos configuration ###
+
+###> Audit configuration ###
+DATABASE_AUDIT_URL=mysql://root:mysql2iopenservice369566@test:3306/audit?serverVersion=5.7
+###< Audit configuration ###
+
+###> symfony/mercure-bundle ###
+# See https://symfony.com/doc/current/mercure.html#configuration
+# The URL of the Mercure hub, used by the app to publish updates (can be a local URL)
+MERCURE_URL=https://mercure.test.opentalent.fr/.well-known/mercure
+# The public URL of the Mercure hub, used by the browser to connect
+MERCURE_PUBLIC_URL=https://mercure.test.opentalent.fr/.well-known/mercure
+# The secret used to sign the JWTs
+MERCURE_JWT_SECRET=NQEupdREijrfYvCmF2mnvZQFL9zLKDH9RCYter6tUWzjemPqzicffhc2fSf0yEmM
+###< symfony/mercure-bundle ###
+
+###> bindfile populate buffer file
+BIND_FILE_BUFFER_FILE=var/subdomain.txt
+###< bindfile populate buffer file
+
+###> knplabs/knp-snappy-bundle ###
+WKHTMLTOPDF_PATH=/usr/bin/wkhtmltopdf
+WKHTMLTOIMAGE_PATH=/usr/bin/wkhtmltoimage
+###< knplabs/knp-snappy-bundle ###
+
+###> filename log ###
+LOG_FILE_NAME=test
+###< filename log ###
+
+### Internal requests (@see doc/internal_requests.md)
+INTERNAL_FILES_DOWNLOAD_URI=https://api.test1.opentalent.fr/_internal/secure/files
+###

+ 62 - 0
env/.env.test2

@@ -0,0 +1,62 @@
+###> symfony/framework-bundle ###
+APP_ENV=test
+APP_DEBUG=1
+###< symfony/framework-bundle ###
+
+###> doctrine/doctrine-bundle ###
+DATABASE_URL=mysql://root:mysql2iopenservice369566@localhost:3306/opentalent?serverVersion=5.7
+###< doctrine/doctrine-bundle ###
+
+###> nelmio/cors-bundle ###
+CORS_ALLOW_ORIGIN=^https?://(localhost|127\.0\.0\.1)(:[0-9]+)$
+###< nelmio/cors-bundle ###
+
+###> api v1 ###
+API_LEG_BASE_URL=https://api.test2.opentalent.fr/api
+###< files management ###
+
+###> typo3 client ###
+TYPO3_BASE_URI=http://test2.opentalent.fr/ohcluses
+###< typo3 client ###
+
+###> BlackFire configuration ###
+BLACKFIRE_CLIENT_ID=988fcba8-552d-48df-a9c2-035c76535b69
+BLACKFIRE_CLIENT_TOKEN=8cfbeb263d044da9678dc2612531504da3790c308da7448e35724a5da91c136f
+BLACKFIRE_SERVER_ID=1171e53b-459b-41da-a292-80ff68cee8c2
+BLACKFIRE_SERVER_TOKEN=dbd1cfbea015fe83cccfc189a36ca3c16f3a1b43b94f50032a15e41e53548e8b
+###< BlackFire configuration ###
+
+###> AdminAssos configuration ###
+DATABASE_ADMINASSOS_URL=mysql://root:mysql2iopenservice369566@test:3306/adminassos?serverVersion=5.7
+###< AdminAssos configuration ###
+
+###> Audit configuration ###
+DATABASE_AUDIT_URL=mysql://root:mysql2iopenservice369566@test:3306/audit?serverVersion=5.7
+###< Audit configuration ###
+
+###> symfony/mercure-bundle ###
+# See https://symfony.com/doc/current/mercure.html#configuration
+# The URL of the Mercure hub, used by the app to publish updates (can be a local URL)
+MERCURE_URL=https://mercure.test.opentalent.fr/.well-known/mercure
+# The public URL of the Mercure hub, used by the browser to connect
+MERCURE_PUBLIC_URL=https://mercure.test.opentalent.fr/.well-known/mercure
+# The secret used to sign the JWTs
+MERCURE_JWT_SECRET=NQEupdREijrfYvCmF2mnvZQFL9zLKDH9RCYter6tUWzjemPqzicffhc2fSf0yEmM
+###< symfony/mercure-bundle ###
+
+###> bindfile populate buffer file
+BIND_FILE_BUFFER_FILE=var/subdomain.txt
+###< bindfile populate buffer file
+
+###> knplabs/knp-snappy-bundle ###
+WKHTMLTOPDF_PATH=/usr/bin/wkhtmltopdf
+WKHTMLTOIMAGE_PATH=/usr/bin/wkhtmltoimage
+###< knplabs/knp-snappy-bundle ###
+
+###> filename log ###
+LOG_FILE_NAME=test
+###< filename log ###
+
+### Internal requests (@see doc/internal_requests.md)
+INTERNAL_FILES_DOWNLOAD_URI=https://api.test2.opentalent.fr/_internal/secure/files
+###

+ 62 - 0
env/.env.test3

@@ -0,0 +1,62 @@
+###> symfony/framework-bundle ###
+APP_ENV=test
+APP_DEBUG=1
+###< symfony/framework-bundle ###
+
+###> doctrine/doctrine-bundle ###
+DATABASE_URL=mysql://root:mysql2iopenservice369566@localhost:3306/opentalent?serverVersion=5.7
+###< doctrine/doctrine-bundle ###
+
+###> nelmio/cors-bundle ###
+CORS_ALLOW_ORIGIN=^https?://(localhost|127\.0\.0\.1)(:[0-9]+)$
+###< nelmio/cors-bundle ###
+
+###> api v1 ###
+API_LEG_BASE_URL=https://api.test3.opentalent.fr/api
+###< files management ###
+
+###> typo3 client ###
+TYPO3_BASE_URI=http://test3.opentalent.fr/ohcluses
+###< typo3 client ###
+
+###> BlackFire configuration ###
+BLACKFIRE_CLIENT_ID=988fcba8-552d-48df-a9c2-035c76535b69
+BLACKFIRE_CLIENT_TOKEN=8cfbeb263d044da9678dc2612531504da3790c308da7448e35724a5da91c136f
+BLACKFIRE_SERVER_ID=1171e53b-459b-41da-a292-80ff68cee8c2
+BLACKFIRE_SERVER_TOKEN=dbd1cfbea015fe83cccfc189a36ca3c16f3a1b43b94f50032a15e41e53548e8b
+###< BlackFire configuration ###
+
+###> AdminAssos configuration ###
+DATABASE_ADMINASSOS_URL=mysql://root:mysql2iopenservice369566@test:3306/adminassos?serverVersion=5.7
+###< AdminAssos configuration ###
+
+###> Audit configuration ###
+DATABASE_AUDIT_URL=mysql://root:mysql2iopenservice369566@test:3306/audit?serverVersion=5.7
+###< Audit configuration ###
+
+###> symfony/mercure-bundle ###
+# See https://symfony.com/doc/current/mercure.html#configuration
+# The URL of the Mercure hub, used by the app to publish updates (can be a local URL)
+MERCURE_URL=https://mercure.test.opentalent.fr/.well-known/mercure
+# The public URL of the Mercure hub, used by the browser to connect
+MERCURE_PUBLIC_URL=https://mercure.test.opentalent.fr/.well-known/mercure
+# The secret used to sign the JWTs
+MERCURE_JWT_SECRET=NQEupdREijrfYvCmF2mnvZQFL9zLKDH9RCYter6tUWzjemPqzicffhc2fSf0yEmM
+###< symfony/mercure-bundle ###
+
+###> bindfile populate buffer file
+BIND_FILE_BUFFER_FILE=var/subdomain.txt
+###< bindfile populate buffer file
+
+###> knplabs/knp-snappy-bundle ###
+WKHTMLTOPDF_PATH=/usr/bin/wkhtmltopdf
+WKHTMLTOIMAGE_PATH=/usr/bin/wkhtmltoimage
+###< knplabs/knp-snappy-bundle ###
+
+###> filename log ###
+LOG_FILE_NAME=test
+###< filename log ###
+
+### Internal requests (@see doc/internal_requests.md)
+INTERNAL_FILES_DOWNLOAD_URI=https://api.test3.opentalent.fr/_internal/secure/files
+###

+ 62 - 0
env/.env.test4

@@ -0,0 +1,62 @@
+###> symfony/framework-bundle ###
+APP_ENV=test
+APP_DEBUG=1
+###< symfony/framework-bundle ###
+
+###> doctrine/doctrine-bundle ###
+DATABASE_URL=mysql://root:mysql2iopenservice369566@localhost:3306/opentalent?serverVersion=5.7
+###< doctrine/doctrine-bundle ###
+
+###> nelmio/cors-bundle ###
+CORS_ALLOW_ORIGIN=^https?://(localhost|127\.0\.0\.1)(:[0-9]+)$
+###< nelmio/cors-bundle ###
+
+###> api v1 ###
+API_LEG_BASE_URL=https://api.test4.opentalent.fr/api
+###< files management ###
+
+###> typo3 client ###
+TYPO3_BASE_URI=http://test4.opentalent.fr/ohcluses
+###< typo3 client ###
+
+###> BlackFire configuration ###
+BLACKFIRE_CLIENT_ID=988fcba8-552d-48df-a9c2-035c76535b69
+BLACKFIRE_CLIENT_TOKEN=8cfbeb263d044da9678dc2612531504da3790c308da7448e35724a5da91c136f
+BLACKFIRE_SERVER_ID=1171e53b-459b-41da-a292-80ff68cee8c2
+BLACKFIRE_SERVER_TOKEN=dbd1cfbea015fe83cccfc189a36ca3c16f3a1b43b94f50032a15e41e53548e8b
+###< BlackFire configuration ###
+
+###> AdminAssos configuration ###
+DATABASE_ADMINASSOS_URL=mysql://root:mysql2iopenservice369566@test:3306/adminassos?serverVersion=5.7
+###< AdminAssos configuration ###
+
+###> Audit configuration ###
+DATABASE_AUDIT_URL=mysql://root:mysql2iopenservice369566@test:3306/audit?serverVersion=5.7
+###< Audit configuration ###
+
+###> symfony/mercure-bundle ###
+# See https://symfony.com/doc/current/mercure.html#configuration
+# The URL of the Mercure hub, used by the app to publish updates (can be a local URL)
+MERCURE_URL=https://mercure.test.opentalent.fr/.well-known/mercure
+# The public URL of the Mercure hub, used by the browser to connect
+MERCURE_PUBLIC_URL=https://mercure.test.opentalent.fr/.well-known/mercure
+# The secret used to sign the JWTs
+MERCURE_JWT_SECRET=NQEupdREijrfYvCmF2mnvZQFL9zLKDH9RCYter6tUWzjemPqzicffhc2fSf0yEmM
+###< symfony/mercure-bundle ###
+
+###> bindfile populate buffer file
+BIND_FILE_BUFFER_FILE=var/subdomain.txt
+###< bindfile populate buffer file
+
+###> knplabs/knp-snappy-bundle ###
+WKHTMLTOPDF_PATH=/usr/bin/wkhtmltopdf
+WKHTMLTOIMAGE_PATH=/usr/bin/wkhtmltoimage
+###< knplabs/knp-snappy-bundle ###
+
+###> filename log ###
+LOG_FILE_NAME=test
+###< filename log ###
+
+### Internal requests (@see doc/internal_requests.md)
+INTERNAL_FILES_DOWNLOAD_URI=https://api.test4.opentalent.fr/_internal/secure/files
+###

+ 62 - 0
env/.env.test5

@@ -0,0 +1,62 @@
+###> symfony/framework-bundle ###
+APP_ENV=test
+APP_DEBUG=1
+###< symfony/framework-bundle ###
+
+###> doctrine/doctrine-bundle ###
+DATABASE_URL=mysql://root:mysql2iopenservice369566@localhost:3306/opentalent?serverVersion=5.7
+###< doctrine/doctrine-bundle ###
+
+###> nelmio/cors-bundle ###
+CORS_ALLOW_ORIGIN=^https?://(localhost|127\.0\.0\.1)(:[0-9]+)$
+###< nelmio/cors-bundle ###
+
+###> api v1 ###
+API_LEG_BASE_URL=https://api.test5.opentalent.fr/api
+###< files management ###
+
+###> typo3 client ###
+TYPO3_BASE_URI=http://test5.opentalent.fr/ohcluses
+###< typo3 client ###
+
+###> BlackFire configuration ###
+BLACKFIRE_CLIENT_ID=988fcba8-552d-48df-a9c2-035c76535b69
+BLACKFIRE_CLIENT_TOKEN=8cfbeb263d044da9678dc2612531504da3790c308da7448e35724a5da91c136f
+BLACKFIRE_SERVER_ID=1171e53b-459b-41da-a292-80ff68cee8c2
+BLACKFIRE_SERVER_TOKEN=dbd1cfbea015fe83cccfc189a36ca3c16f3a1b43b94f50032a15e41e53548e8b
+###< BlackFire configuration ###
+
+###> AdminAssos configuration ###
+DATABASE_ADMINASSOS_URL=mysql://root:mysql2iopenservice369566@test:3306/adminassos?serverVersion=5.7
+###< AdminAssos configuration ###
+
+###> Audit configuration ###
+DATABASE_AUDIT_URL=mysql://root:mysql2iopenservice369566@test:3306/audit?serverVersion=5.7
+###< Audit configuration ###
+
+###> symfony/mercure-bundle ###
+# See https://symfony.com/doc/current/mercure.html#configuration
+# The URL of the Mercure hub, used by the app to publish updates (can be a local URL)
+MERCURE_URL=https://mercure.test.opentalent.fr/.well-known/mercure
+# The public URL of the Mercure hub, used by the browser to connect
+MERCURE_PUBLIC_URL=https://mercure.test.opentalent.fr/.well-known/mercure
+# The secret used to sign the JWTs
+MERCURE_JWT_SECRET=NQEupdREijrfYvCmF2mnvZQFL9zLKDH9RCYter6tUWzjemPqzicffhc2fSf0yEmM
+###< symfony/mercure-bundle ###
+
+###> bindfile populate buffer file
+BIND_FILE_BUFFER_FILE=var/subdomain.txt
+###< bindfile populate buffer file
+
+###> knplabs/knp-snappy-bundle ###
+WKHTMLTOPDF_PATH=/usr/bin/wkhtmltopdf
+WKHTMLTOIMAGE_PATH=/usr/bin/wkhtmltoimage
+###< knplabs/knp-snappy-bundle ###
+
+###> filename log ###
+LOG_FILE_NAME=test
+###< filename log ###
+
+### Internal requests (@see doc/internal_requests.md)
+INTERNAL_FILES_DOWNLOAD_URI=https://api.test5.opentalent.fr/_internal/secure/files
+###

+ 3 - 0
env/readme.md

@@ -0,0 +1,3 @@
+# Env files
+
+> @see doc/env.md

+ 82 - 0
src/Commands/SetupEnvCommand.php

@@ -0,0 +1,82 @@
+<?php
+
+namespace App\Commands;
+
+use App\Service\Utils\Path;
+use Symfony\Component\Console\Attribute\AsCommand;
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Contracts\Service\Attribute\Required;
+
+/**
+ * Post install script: create or replace the symlink .env.local pointing
+ * to the .env file matching the current environment.
+ *
+ * @see doc/env.md
+ */
+#[AsCommand(
+    name: 'ot:setup:env',
+    description: 'Setup the env file according to the current host'
+)]
+class SetupEnvCommand extends Command
+{
+    const ENVIRONMENTS_FILES = [
+        'ap2i' => '.env.docker',
+        'prod-2' => '.env.prod',
+        'test-2' => '.env.test',
+        'test1' => '.env.test1',
+        'test2' => '.env.test2',
+        'test3' => '.env.test3',
+        'test4' => '.env.test4',
+        'test5' => '.env.test5',
+        'ci' => '.env.ci',
+    ];
+
+    private string $projectDir;
+
+    /**
+     * @param string $projectDir
+     * @return void
+     */
+    #[Required]
+    public function setProjectDir(string $projectDir): void {
+        $this->projectDir = $projectDir;
+    }
+
+    /**
+     * Configures the command
+     */
+    protected function configure(): void
+    {
+        $this->addOption(
+            'host',
+            null,
+            InputOption::VALUE_REQUIRED,
+            "Use this hostname instead of the current's host name."
+        );
+    }
+
+    protected function execute(InputInterface $input, OutputInterface $output): int
+    {
+        $hostname = $input->getOption('host') ?? gethostname();
+
+        if (!array_key_exists($hostname, self::ENVIRONMENTS_FILES)) {
+            throw new \RuntimeException("Critical : unknown environment [" . $hostname . "]");
+        }
+
+        $envFile = Path::join($this->projectDir, 'env', self::ENVIRONMENTS_FILES[$hostname]);
+        $symlinkLocation = Path::join($this->projectDir, '.env.local');
+
+        if (file_exists($symlinkLocation)) {
+            unlink($symlinkLocation);
+            $output->writeln($symlinkLocation . " was deleted");
+        }
+
+        symlink($envFile, $symlinkLocation);
+        $output->writeln("Symlink created : " . $symlinkLocation . " => " . $envFile);
+
+        return Command::SUCCESS;
+    }
+}