]> git.immae.eu Git - perso/Immae/Config/Nix.git/blobdiff - modules/private/websites/ludivine/integration.nix
Rework integration websites
[perso/Immae/Config/Nix.git] / modules / private / websites / ludivine / integration.nix
index 49aa11350625f6023dde7bde40d8fa3aa4f8a530..a0d08b3e966821c7823e80892f29138dd2c4352b 100644 (file)
@@ -2,28 +2,29 @@
 let
   secrets = config.myEnv.websites.ludivine.integration;
   cfg = config.myServices.websites.ludivine.integration;
-  pcfg = config.services.phpApplication;
-  webRoot = "/var/lib/ftp/immae/ludivine/web";
+  ftpRoot = "/var/lib/ludivine_integration";
+  phpRoot = "${ftpRoot}/php";
+  webRoot = "${phpRoot}/web";
+  varDir = "${ftpRoot}/var";
+  sessionDir = "${ftpRoot}/sessions";
+  packagePath = "/var/lib/ftp/release.immae.eu/buildbot/Ludivine";
+  branch = "test";
 in {
   options.myServices.websites.ludivine.integration.enable = lib.mkEnableOption "enable Ludivine's website in integration";
 
   config = lib.mkIf cfg.enable {
-    services.phpApplication.apps.ludivine_integration = {
-      websiteEnv = "integration";
-      httpdUser = config.services.httpd.Inte.user;
-      httpdGroup = config.services.httpd.Inte.group;
-      inherit webRoot;
-      varDir = "/var/lib/ftp/immae/ludivine_var";
-      app = "/var/lib/ftp/immae/ludivine";
-      varDirPaths = {
-        "tmp" = "0700";
-      };
-      serviceDeps = [ "mysql.service" ];
-      preStartActions = [
-        "./bin/console --env=dev cache:clear --no-warmup"
-      ];
-      phpOpenbasedir = [ "/tmp" ];
-      phpPool = {
+    services.phpfpm.pools.ludivine_integration = {
+      user = config.services.httpd.Inte.user;
+      group = config.services.httpd.Inte.group;
+      settings = {
+        "listen.owner" = config.services.httpd.Inte.user;
+        "listen.group" = config.services.httpd.Inte.group;
+        "php_admin_value[open_basedir]" = builtins.concatStringsSep ":" [
+          ftpRoot
+          config.secrets.fullPaths."websites/ludivine/integration"
+          "/tmp"
+        ];
+        "php_admin_value[session.save_path]" = sessionDir;
         "php_admin_value[upload_max_filesize]" = "20M";
         "php_admin_value[post_max_size]" = "20M";
         #"php_admin_flag[log_errors]" = "on";
@@ -32,18 +33,61 @@ in {
         "pm.process_idle_timeout" = "60";
       };
       phpEnv = {
-        PATH = lib.makeBinPath [
-          # below ones don't need to be in the PATH but they’re used in
-          # secrets
-          pkgs.imagemagick pkgs.sass pkgs.ruby
-        ];
         SYMFONY_DEBUG_MODE = "\"yes\"";
       };
-      phpWatchFiles = [
-        config.secrets.fullPaths."websites/ludivine/integration"
-      ];
       phpPackage = pkgs.php72;
     };
+    systemd.services."phpfpm-ludivine_integration" = {
+      after = lib.mkAfter ["mysql.service"];
+      wants = ["mysql.service"];
+      path = lib.mkAfter [ pkgs.gnutar pkgs.gzip pkgs.php72 ];
+      preStart = let
+        script = pkgs.writeScriptBin "ludivine-integration-pre" ''
+          #! ${pkgs.stdenv.shell}
+
+          [ -f ${packagePath}/${branch}.tar.gz ] || exit 1
+
+          cd ${ftpRoot}
+          if ! [ -f .tarball_sum ] || ! sha256sum -c .tarball_sum; then
+            tar -xf ${packagePath}/${branch}.tar.gz --one-top-level=php_new
+            if [ -d php ]; then
+              mv php php_old
+            fi
+            mv php_new php
+          fi
+          cd php
+          rm -rf var/{logs,cache,data,miniatures,tmp}
+          ln -sf ${varDir}/{logs,cache,data,miniatures,tmp} var/
+          ln -sf ${config.secrets.fullPaths."websites/ludivine/integration"} app/config/parameters.yml
+          SYMFONY_ENV=dev ./bin/console --env=dev cache:clear --no-warmup
+          sha256sum ${packagePath}/${branch}.tar.gz > ${ftpRoot}/.tarball_sum
+        '';
+      in
+        "/run/wrappers/bin/sudo -u ${config.services.httpd.Inte.user} ${script}/bin/ludivine-integration-pre";
+      postStart = let
+        script = pkgs.writeScriptBin "ludivine-integration-post" ''
+          #! ${pkgs.stdenv.shell}
+
+          cd ${ftpRoot}
+          if [ -d php_old ]; then
+            rm -rf php_old
+          fi
+        '';
+      in
+        "/run/wrappers/bin/sudo -u ${config.services.httpd.Inte.user} ${script}/bin/ludivine-integration-post";
+      serviceConfig.TimeoutStartSec="infinity";
+    };
+    services.filesWatcher.phpfpm-ludivine_integration = {
+      restart = true;
+      paths = [ "${packagePath}/${branch}.tar.gz" ];
+    };
+
+    system.activationScripts.ludivine_integration = {
+      deps = [];
+      text = ''
+        install -m 0700 -o ${config.services.httpd.Inte.user} -g ${config.services.httpd.Inte.group} -d ${ftpRoot} ${sessionDir}
+      '';
+    };
 
     secrets.keys."websites/ludivine/integration" = {
       user = config.services.httpd.Inte.user;
@@ -89,7 +133,7 @@ in {
       extraConfig = [
         ''
         <FilesMatch "\.php$">
-          SetHandler "proxy:unix:${pcfg.phpListenPaths.ludivine_integration}|fcgi://localhost"
+          SetHandler "proxy:unix:${config.services.phpfpm.pools.ludivine_integration.socket}|fcgi://localhost"
         </FilesMatch>
 
         <Location />