|
3 | 3 | pkgs, |
4 | 4 | lib, |
5 | 5 | options, |
| 6 | + utils, |
6 | 7 | ... |
7 | 8 | }: |
8 | | - |
9 | 9 | let |
10 | 10 | cfg = config.services.firefox-syncserver; |
11 | 11 | opt = options.services.firefox-syncserver; |
12 | 12 | defaultDatabase = "firefox_syncserver"; |
13 | 13 | defaultUser = "firefox-syncserver"; |
14 | 14 |
|
15 | | - dbIsLocal = cfg.database.host == "localhost"; |
16 | | - dbURL = "mysql://${cfg.database.user}@${cfg.database.host}/${cfg.database.name}${lib.optionalString dbIsLocal "?socket=/run/mysqld/mysqld.sock"}"; |
| 15 | + dbIsMySQL = cfg.database.type == "mysql"; |
| 16 | + dbIsPostgreSQL = cfg.database.type == "postgresql"; |
| 17 | + |
| 18 | + dbIsLocal = |
| 19 | + cfg.database.host == (if dbIsMySQL then "/run/mysqld/mysqld.sock" else "/run/postgresql"); |
| 20 | + |
| 21 | + dbURL = |
| 22 | + if dbIsMySQL then |
| 23 | + "mysql://${cfg.database.user}@${cfg.database.host}/${cfg.database.name}${lib.optionalString dbIsLocal "?socket=/run/mysqld/mysqld.sock"}" |
| 24 | + else if dbIsLocal then |
| 25 | + # Use Unix socket connection with peer authentication by default. |
| 26 | + "postgres:///${cfg.database.name}?host=/run/postgresql&user=${cfg.database.user}" |
| 27 | + else |
| 28 | + "postgres://${cfg.database.user}@${cfg.database.host}/${cfg.database.name}"; |
| 29 | + |
| 30 | + # postgresql.target waits for postgresql-setup.service (which runs |
| 31 | + # ensureDatabases / ensureUsers) to complete, avoiding race conditions |
| 32 | + # where the syncserver starts before its database and role exist. |
| 33 | + dbService = if dbIsMySQL then "mysql.service" else "postgresql.target"; |
| 34 | + |
| 35 | + syncserver = cfg.package.override { dbBackend = cfg.database.type; }; |
17 | 36 |
|
18 | 37 | format = pkgs.formats.toml { }; |
19 | 38 | settings = { |
|
22 | 41 | database_url = dbURL; |
23 | 42 | }; |
24 | 43 | tokenserver = { |
25 | | - node_type = "mysql"; |
| 44 | + node_type = if dbIsMySQL then "mysql" else "postgres"; |
26 | 45 | database_url = dbURL; |
27 | 46 | fxa_email_domain = "api.accounts.firefox.com"; |
28 | 47 | fxa_oauth_server_url = "https://oauth.accounts.firefox.com/v1"; |
|
41 | 60 | }; |
42 | 61 | }; |
43 | 62 | configFile = format.generate "syncstorage.toml" (lib.recursiveUpdate settings cfg.settings); |
44 | | - setupScript = pkgs.writeShellScript "firefox-syncserver-setup" '' |
45 | | - set -euo pipefail |
46 | | - shopt -s inherit_errexit |
47 | | -
|
48 | | - schema_configured() { |
49 | | - mysql ${cfg.database.name} -Ne 'SHOW TABLES' | grep -q services |
50 | | - } |
51 | | -
|
52 | | - update_config() { |
53 | | - mysql ${cfg.database.name} <<"EOF" |
54 | | - BEGIN; |
55 | | -
|
56 | | - INSERT INTO `services` (`id`, `service`, `pattern`) |
57 | | - VALUES (1, 'sync-1.5', '{node}/1.5/{uid}') |
58 | | - ON DUPLICATE KEY UPDATE service='sync-1.5', pattern='{node}/1.5/{uid}'; |
59 | | - INSERT INTO `nodes` (`id`, `service`, `node`, `available`, `current_load`, |
60 | | - `capacity`, `downed`, `backoff`) |
61 | | - VALUES (1, 1, '${cfg.singleNode.url}', ${toString cfg.singleNode.capacity}, |
62 | | - 0, ${toString cfg.singleNode.capacity}, 0, 0) |
63 | | - ON DUPLICATE KEY UPDATE node = '${cfg.singleNode.url}', capacity=${toString cfg.singleNode.capacity}; |
64 | | -
|
65 | | - COMMIT; |
66 | | - EOF |
67 | | - } |
68 | | -
|
69 | 63 |
|
70 | | - for (( try = 0; try < 60; try++ )); do |
71 | | - if ! schema_configured; then |
72 | | - sleep 2 |
73 | | - else |
74 | | - update_config |
75 | | - exit 0 |
76 | | - fi |
77 | | - done |
78 | | -
|
79 | | - echo "Single-node setup failed" |
80 | | - exit 1 |
81 | | - ''; |
| 64 | + setupScript = |
| 65 | + let |
| 66 | + dbSpecific = |
| 67 | + if dbIsMySQL then |
| 68 | + { |
| 69 | + listTables = "mysql ${cfg.database.name} -Ne 'SHOW TABLES'"; |
| 70 | + execSql = "mysql ${cfg.database.name}"; |
| 71 | + upsertSql = '' |
| 72 | + BEGIN; |
| 73 | +
|
| 74 | + INSERT INTO `services` (`id`, `service`, `pattern`) |
| 75 | + VALUES (1, 'sync-1.5', '{node}/1.5/{uid}') |
| 76 | + ON DUPLICATE KEY UPDATE service='sync-1.5', pattern='{node}/1.5/{uid}'; |
| 77 | + INSERT INTO `nodes` (`id`, `service`, `node`, `available`, `current_load`, |
| 78 | + `capacity`, `downed`, `backoff`) |
| 79 | + VALUES (1, 1, '${cfg.singleNode.url}', ${toString cfg.singleNode.capacity}, |
| 80 | + 0, ${toString cfg.singleNode.capacity}, 0, 0) |
| 81 | + ON DUPLICATE KEY UPDATE node = '${cfg.singleNode.url}', capacity=${toString cfg.singleNode.capacity}; |
| 82 | +
|
| 83 | + COMMIT; |
| 84 | + ''; |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + listTables = "psql -d ${cfg.database.name} -tAc \"SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'\""; |
| 89 | + execSql = "psql -d ${cfg.database.name}"; |
| 90 | + upsertSql = '' |
| 91 | + BEGIN; |
| 92 | +
|
| 93 | + INSERT INTO services (id, service, pattern) |
| 94 | + VALUES (1, 'sync-1.5', '{node}/1.5/{uid}') |
| 95 | + ON CONFLICT (id) DO UPDATE SET service = 'sync-1.5', pattern = '{node}/1.5/{uid}'; |
| 96 | + INSERT INTO nodes (id, service, node, available, current_load, |
| 97 | + capacity, downed, backoff) |
| 98 | + VALUES (1, 1, '${cfg.singleNode.url}', ${toString cfg.singleNode.capacity}, |
| 99 | + 0, ${toString cfg.singleNode.capacity}, 0, 0) |
| 100 | + ON CONFLICT (id) DO UPDATE SET node = '${cfg.singleNode.url}', capacity = ${toString cfg.singleNode.capacity}; |
| 101 | +
|
| 102 | + COMMIT; |
| 103 | + ''; |
| 104 | + }; |
| 105 | + in |
| 106 | + pkgs.writeShellScript "firefox-syncserver-setup" '' |
| 107 | + set -euo pipefail |
| 108 | + shopt -s inherit_errexit |
| 109 | +
|
| 110 | + schema_configured() { |
| 111 | + ${dbSpecific.listTables} | grep -q services |
| 112 | + } |
| 113 | +
|
| 114 | + update_config() { |
| 115 | + ${dbSpecific.execSql} <<'EOF' |
| 116 | + ${dbSpecific.upsertSql} |
| 117 | + EOF |
| 118 | + } |
| 119 | +
|
| 120 | + for (( try = 0; try < 60; try++ )); do |
| 121 | + if ! schema_configured; then |
| 122 | + sleep 2 |
| 123 | + else |
| 124 | + update_config |
| 125 | + exit 0 |
| 126 | + fi |
| 127 | + done |
| 128 | +
|
| 129 | + echo "Single-node setup failed" |
| 130 | + exit 1 |
| 131 | + ''; |
82 | 132 | in |
83 | 133 |
|
84 | 134 | { |
|
88 | 138 | the Firefox Sync storage service. |
89 | 139 |
|
90 | 140 | Out of the box this will not be very useful unless you also configure at least |
91 | | - one service and one nodes by inserting them into the mysql database manually, e.g. |
| 141 | + one service and one nodes by inserting them into the database manually, e.g. |
92 | 142 | by running |
93 | 143 |
|
94 | 144 | ``` |
95 | | - INSERT INTO `services` (`id`, `service`, `pattern`) VALUES ('1', 'sync-1.5', '{node}/1.5/{uid}'); |
96 | | - INSERT INTO `nodes` (`id`, `service`, `node`, `available`, `current_load`, |
97 | | - `capacity`, `downed`, `backoff`) |
98 | | - VALUES ('1', '1', 'https://mydomain.tld', '1', '0', '10', '0', '0'); |
| 145 | + INSERT INTO services (id, service, pattern) VALUES (1, 'sync-1.5', '{node}/1.5/{uid}'); |
| 146 | + INSERT INTO nodes (id, service, node, available, current_load, |
| 147 | + capacity, downed, backoff) |
| 148 | + VALUES (1, 1, 'https://mydomain.tld', 1, 0, 10, 0, 0); |
99 | 149 | ``` |
100 | 150 |
|
101 | 151 | {option}`${opt.singleNode.enable}` does this automatically when enabled |
102 | 152 | ''; |
103 | 153 |
|
104 | 154 | package = lib.mkPackageOption pkgs "syncstorage-rs" { }; |
105 | 155 |
|
| 156 | + database.type = lib.mkOption { |
| 157 | + type = lib.types.enum [ |
| 158 | + "mysql" |
| 159 | + "postgresql" |
| 160 | + ]; |
| 161 | + description = '' |
| 162 | + Which database backend to use for storage. |
| 163 | + ::: {.note} |
| 164 | + `"postgresql"` is recommended for new deployments. `"mysql"` is the |
| 165 | + backend used by legacy installations. |
| 166 | + ::: |
| 167 | + ''; |
| 168 | + }; |
| 169 | + |
106 | 170 | database.name = lib.mkOption { |
107 | | - # the mysql module does not allow `-quoting without resorting to shell |
108 | | - # escaping, so we restrict db names for forward compaitiblity should this |
109 | | - # behavior ever change. |
110 | 171 | type = lib.types.strMatching "[a-z_][a-z0-9_]*"; |
111 | 172 | default = defaultDatabase; |
112 | 173 | description = '' |
|
117 | 178 |
|
118 | 179 | database.user = lib.mkOption { |
119 | 180 | type = lib.types.str; |
120 | | - default = defaultUser; |
| 181 | + default = if dbIsPostgreSQL then defaultDatabase else defaultUser; |
| 182 | + defaultText = lib.literalExpression '' |
| 183 | + if database.type == "postgresql" then "${defaultDatabase}" else "${defaultUser}" |
| 184 | + ''; |
121 | 185 | description = '' |
122 | | - Username for database connections. |
| 186 | + Username for database connections. When using PostgreSQL with |
| 187 | + `createLocally`, this defaults to the database name so that |
| 188 | + `ensureDBOwnership` works (it requires user and database names |
| 189 | + to match). |
123 | 190 | ''; |
124 | 191 | }; |
125 | 192 |
|
|
137 | 204 | default = true; |
138 | 205 | description = '' |
139 | 206 | Whether to create database and user on the local machine if they do not exist. |
140 | | - This includes enabling unix domain socket authentication for the configured user. |
| 207 | + This includes enabling the configured database service and setting up |
| 208 | + authentication for the configured user. |
141 | 209 | ''; |
142 | 210 | }; |
143 | 211 |
|
|
237 | 305 | }; |
238 | 306 |
|
239 | 307 | config = lib.mkIf cfg.enable { |
240 | | - services.mysql = lib.mkIf cfg.database.createLocally { |
| 308 | + services.mysql = lib.mkIf (cfg.database.createLocally && dbIsMySQL) { |
241 | 309 | enable = true; |
242 | 310 | ensureDatabases = [ cfg.database.name ]; |
243 | 311 | ensureUsers = [ |
|
250 | 318 | ]; |
251 | 319 | }; |
252 | 320 |
|
| 321 | + services.postgresql = lib.mkIf (cfg.database.createLocally && dbIsPostgreSQL) { |
| 322 | + enable = true; |
| 323 | + ensureDatabases = [ cfg.database.name ]; |
| 324 | + ensureUsers = [ |
| 325 | + { |
| 326 | + name = cfg.database.user; |
| 327 | + ensureDBOwnership = true; |
| 328 | + } |
| 329 | + ]; |
| 330 | + }; |
| 331 | + |
253 | 332 | systemd.services.firefox-syncserver = { |
254 | 333 | wantedBy = [ "multi-user.target" ]; |
255 | | - requires = lib.mkIf dbIsLocal [ "mysql.service" ]; |
256 | | - after = lib.mkIf dbIsLocal [ "mysql.service" ]; |
| 334 | + requires = lib.mkIf dbIsLocal [ dbService ]; |
| 335 | + after = lib.mkIf dbIsLocal [ dbService ]; |
257 | 336 | restartTriggers = lib.optional cfg.singleNode.enable setupScript; |
258 | 337 | environment.RUST_LOG = cfg.logLevel; |
259 | 338 | serviceConfig = { |
260 | | - User = defaultUser; |
261 | | - Group = defaultUser; |
262 | | - ExecStart = "${cfg.package}/bin/syncserver --config ${configFile}"; |
| 339 | + ExecStart = utils.escapeSystemdExecArgs [ |
| 340 | + (lib.getExe syncserver) |
| 341 | + "--config" |
| 342 | + configFile |
| 343 | + ]; |
| 344 | + User = cfg.database.user; |
| 345 | + Group = cfg.database.user; |
263 | 346 | EnvironmentFile = lib.mkIf (cfg.secrets != null) "${cfg.secrets}"; |
264 | 347 |
|
265 | 348 | # hardening |
|
303 | 386 |
|
304 | 387 | systemd.services.firefox-syncserver-setup = lib.mkIf cfg.singleNode.enable { |
305 | 388 | wantedBy = [ "firefox-syncserver.service" ]; |
306 | | - requires = [ "firefox-syncserver.service" ] ++ lib.optional dbIsLocal "mysql.service"; |
307 | | - after = [ "firefox-syncserver.service" ] ++ lib.optional dbIsLocal "mysql.service"; |
308 | | - path = [ config.services.mysql.package ]; |
309 | | - serviceConfig.ExecStart = [ "${setupScript}" ]; |
| 389 | + requires = [ "firefox-syncserver.service" ] ++ lib.optional dbIsLocal dbService; |
| 390 | + after = [ "firefox-syncserver.service" ] ++ lib.optional dbIsLocal dbService; |
| 391 | + path = |
| 392 | + if dbIsMySQL then [ config.services.mysql.package ] else [ config.services.postgresql.package ]; |
| 393 | + serviceConfig = { |
| 394 | + ExecStart = [ "${setupScript}" ]; |
| 395 | + } |
| 396 | + // lib.optionalAttrs dbIsPostgreSQL { |
| 397 | + # Peer authentication maps the system user to the database role of the |
| 398 | + # same name, which owns the database (ensureDBOwnership), so no |
| 399 | + # superuser access is required. |
| 400 | + User = cfg.database.user; |
| 401 | + Group = cfg.database.user; |
| 402 | + DynamicUser = true; |
| 403 | + }; |
310 | 404 | }; |
311 | 405 |
|
312 | 406 | services.nginx.virtualHosts = lib.mkIf cfg.singleNode.enableNginx { |
|
0 commit comments