aboutsummaryrefslogtreecommitdiff
path: root/nixops/modules/websites/tellesflorian/tellesflorian.nix
blob: afd723c0936f8b0c388b1f72f634c2a760bd791c (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
{ lib, writeText, fetchedGitPrivate, fetchurl, stdenv, composerEnv }:
let
  tellesflorian = { config }: rec {
    environment = config.environment;
    varDir = "/var/lib/tellesflorian_${environment}";
    keys = [
      {
        dest = "webapps/${environment}-tellesflorian-passwords";
        user = apache.user;
        group = apache.group;
        permissions = "0400";
        text = ''
          invite:${config.invite_passwords}
        '';
      }
      {
        dest = "webapps/${environment}-tellesflorian";
        user = apache.user;
        group = apache.group;
        permissions = "0400";
        text = ''
          # This file is auto-generated during the composer install
          parameters:
            database_host: ${config.mysql.host}
            database_port: ${config.mysql.port}
            database_name: ${config.mysql.name}
            database_user: ${config.mysql.user}
            database_password: ${config.mysql.password}
            mailer_transport: smtp
            mailer_host: 127.0.0.1
            mailer_user: null
            mailer_password: null
            secret: ${config.secret}
        '';
      }
    ];
    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 ./bin/console --env=${environment} cache:clear --no-warmup
          popd > /dev/null
          echo -n "${webappDir}" > ${varDir}/currentWebappDir
          sha512sum /var/secrets/webapps/${environment}-tellesflorian > ${varDir}/currentKey
        fi
        '';
      serviceDeps = [ "mysql.service" ];
      socket = "/var/run/phpfpm/floriantelles-${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] = "/var/secrets/webapps/${environment}-tellesflorian:${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
        ''}'';
    };
    apache = rec {
      user = "wwwrun";
      group = "wwwrun";
      modules = [ "proxy_fcgi" ];
      webappName = "florian_${environment}";
      root = "/run/current-system/webapps/${webappName}";
      vhostConf = ''
      <FilesMatch "\.php$">
        SetHandler "proxy:unix:${phpFpm.socket}|fcgi://localhost"
      </FilesMatch>

      ${if environment == "dev" then ''
      <Location />
        AuthBasicProvider file ldap
        Use LDAPConnect
        Require ldap-group   cn=app.tellesflorian.com,cn=httpd,ou=services,dc=immae,dc=eu

        AuthUserFile "/var/secrets/webapps/${environment}-tellesflorian-passwords"
        Require user "invite"

        ErrorDocument 401 "<html><meta http-equiv=\"refresh\" content=\"0;url=https://tellesflorian.com\"></html>"
      </Location>

      <Directory ${root}>
        Options Indexes FollowSymLinks MultiViews Includes
        AllowOverride None
        Require all granted

        DirectoryIndex app_dev.php

        <IfModule mod_negotiation.c>
        Options -MultiViews
        </IfModule>

        <IfModule mod_rewrite.c>
          RewriteEngine On

          RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
          RewriteRule ^(.*) - [E=BASE:%1]

          # Maintenance script
          RewriteCond %{DOCUMENT_ROOT}/maintenance.php -f
          RewriteCond %{SCRIPT_FILENAME} !maintenance.php
          RewriteRule ^.*$ %{ENV:BASE}/maintenance.php [R=503,L]
          ErrorDocument 503 /maintenance.php

          # Sets the HTTP_AUTHORIZATION header removed by Apache
          RewriteCond %{HTTP:Authorization} .
          RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

          RewriteCond %{ENV:REDIRECT_STATUS} ^$
          RewriteRule ^app_dev\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]

          # If the requested filename exists, simply serve it.
          # We only want to let Apache serve files and not directories.
          RewriteCond %{REQUEST_FILENAME} -f
          RewriteRule ^ - [L]

          # Rewrite all other queries to the front controller.
          RewriteRule ^ %{ENV:BASE}/app_dev.php [L]
        </IfModule>

      </Directory>
      '' else ''
      <Directory ${root}>
        Options Indexes FollowSymLinks MultiViews Includes
        AllowOverride All
        Require all granted
      </Directory>
      ''}
      '';
    };
    activationScript = {
      deps = [ "wrappers" ];
      text = ''
      install -m 0755 -o ${apache.user} -g ${apache.group} -d ${varDir} \
        ${varDir}/var
      install -m 0750 -o ${apache.user} -g ${apache.group} -d ${varDir}/phpSessions
      '';
    };
    webappDir = composerEnv.buildPackage (
      import ./php-packages.nix { inherit composerEnv fetchurl; } //
      fetchedGitPrivate ./tellesflorian.json //
      rec {
        noDev = (environment == "prod");
        preInstall = ''
          export SYMFONY_ENV="${environment}"
          '';
        postInstall = ''
          cd $out
          rm app/config/parameters.yml
          ln -sf /var/secrets/webapps/${environment}-tellesflorian app/config/parameters.yml
          rm -rf var/{logs,cache}
          ln -sf ${varDir}/var/{logs,cache,sessions} var/
          '';
      });
    webRoot = "${webappDir}/web";
  };
in
  tellesflorian