aboutsummaryrefslogtreecommitdiff
path: root/nixops/modules/websites/aten/aten.nix
blob: c35af6f098ee3dc1ea936ee397c7f8549486a1ee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
{ lib, writeText, fetchedGitPrivate, stdenv, runCommand, composerEnv, fetchurl, fetchgit, jq, python, nodejs, libsass, yarn2nixPackage }:
let
  aten = { config }: rec {
    environment = config.environment;
    varDir = "/var/lib/aten_${environment}";
    phpFpm = rec {
      preStart = ''
        if [ ! -f "${varDir}/currentWebappDir" -o \
            ! -f "${varDir}/currentKey" -o \
            "${webappDir}" != "$(cat ${varDir}/currentWebappDir 2>/dev/null)" ] \
            || ! sha512sum -c --status ${varDir}/currentKey; then
          pushd ${webappDir} > /dev/null
          /run/wrappers/bin/sudo -u wwwrun APP_ENV=${environment} ./bin/console --env=${environment} cache:clear --no-warmup
          popd > /dev/null
          echo -n "${webappDir}" > ${varDir}/currentWebappDir
          sha512sum /var/secrets/webapps/${environment}-aten > ${varDir}/currentKey
        fi
        '';
      serviceDeps = [ "postgresql.service" ];
      socket = "/var/run/phpfpm/aten-${environment}.sock";
      pool = ''
        listen = ${socket}
        user = ${apache.user}
        group = ${apache.group}
        listen.owner = ${apache.user}
        listen.group = ${apache.group}
        php_admin_value[upload_max_filesize] = 20M
        php_admin_value[post_max_size] = 20M
        ;php_admin_flag[log_errors] = on
        php_admin_value[open_basedir] = "${webappDir}:${varDir}:/tmp"
        php_admin_value[session.save_path] = "${varDir}/phpSessions"
        ${if environment == "dev" then ''
        pm = ondemand
        pm.max_children = 5
        pm.process_idle_timeout = 60
        env[SYMFONY_DEBUG_MODE] = "yes"
        '' else ''
        pm = dynamic
        pm.max_children = 20
        pm.start_servers = 2
        pm.min_spare_servers = 1
        pm.max_spare_servers = 3
        ''}'';
    };
    keys = [{
      dest = "webapps/${environment}-aten";
      user = apache.user;
      group = apache.group;
      permissions = "0400";
      text = ''
        SetEnv APP_ENV      "${environment}"
        SetEnv APP_SECRET   "${config.secret}"
        SetEnv DATABASE_URL "${config.psql_url}"
        '';
    }];
    apache = rec {
      user = "wwwrun";
      group = "wwwrun";
      modules = [ "proxy_fcgi" ];
      webappName = "aten_${environment}";
      root = "/run/current-system/webapps/${webappName}";
      vhostConf = ''
      <FilesMatch "\.php$">
        SetHandler "proxy:unix:${phpFpm.socket}|fcgi://localhost"
      </FilesMatch>

      Include /var/secrets/webapps/${environment}-aten

      ${if environment == "dev" then ''
      <Location />
        Use LDAPConnect
        Require ldap-group   cn=dev.aten.pro,cn=httpd,ou=services,dc=immae,dc=eu
        ErrorDocument 401 "<html><meta http-equiv=\"refresh\" content=\"0;url=https://aten.pro\"></html>"
      </Location>

      <Location /backend>
        Use LDAPConnect
        Require ldap-group   cn=dev.aten.pro,cn=httpd,ou=services,dc=immae,dc=eu
        ErrorDocument 401 "<html><meta http-equiv=\"refresh\" content=\"0;url=https://aten.pro\"></html>"
      </Location>
      '' else ''
      Use Stats aten.pro

      <Location /backend>
        Use LDAPConnect
        Require ldap-group   cn=aten.pro,cn=httpd,ou=services,dc=immae,dc=eu
        ErrorDocument 401 "<html><meta http-equiv=\"refresh\" content=\"0;url=https://aten.pro\"></html>"
      </Location>
      ''}

      <Directory ${root}>
        Options Indexes FollowSymLinks MultiViews Includes
        AllowOverride All
        Require all granted
        DirectoryIndex index.php
        FallbackResource /index.php
      </Directory>
      '';
    };
    activationScript = {
      deps = [ "wrappers" ];
      text = ''
      install -m 0755 -o ${apache.user} -g ${apache.group} -d ${varDir}
      install -m 0750 -o ${apache.user} -g ${apache.group} -d ${varDir}/phpSessions
      '';
    };
    yarnModules = let
        info = fetchedGitPrivate ./aten.json;
        packagejson = runCommand "package.json" { buildInputs = [ jq ]; } ''
          cat ${info.src}/package.json | jq -r '.version = "v1.0.0"|.name="aten"' > $out
          '';
      in
        yarn2nixPackage.mkYarnModules rec {
          name = "aten-yarn";
          pname = name;
          version = "v1.0.0";
          packageJSON = packagejson;
          yarnLock = "${info.src}/yarn.lock";
          pkgConfig = {
            all = {
              buildInputs = [ yarn2nixPackage.src ];
            };
            node-sass = {
              buildInputs = [ libsass python ];
              postInstall = let
                nodeHeaders = fetchurl {
                  url = "https://nodejs.org/download/release/v${nodejs.version}/node-v${nodejs.version}-headers.tar.gz";
                  sha256 = "16f20ya3ys6w5w6y6l4536f7jrgk4gz46bf71w1r1xxb26a54m32";
                };
              in
                ''
                  node scripts/build.js --tarball=${nodeHeaders}
                '';
            };
          };
        };
    webappDir = composerEnv.buildPackage (
      import ./php-packages.nix { inherit composerEnv fetchurl fetchgit; } //
      fetchedGitPrivate ./aten.json //
      rec {
        noDev = (environment == "prod");
        preInstall = ''
          export SYMFONY_ENV="${environment}"
          export APP_ENV="${environment}"
          '';
        postInstall = ''
          ln -sf ${yarnModules}/node_modules .
          yarn run --offline encore production
          rm -rf var/{log,cache}
          ln -sf ${varDir}/{log,cache} var/
          '';
        buildInputs = [ yarnModules yarn2nixPackage.yarn ];
      });
    webRoot = "${webappDir}/public";
  };
in
  aten