|
13 | 13 | defaultUser = "firefox-syncserver"; |
14 | 14 |
|
15 | 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"}"; |
| 16 | + dbIsMySQL = cfg.database.type == "mysql"; |
| 17 | + dbIsPostgreSQL = cfg.database.type == "postgresql"; |
| 18 | + |
| 19 | + dbURL = |
| 20 | + if dbIsMySQL then |
| 21 | + "mysql://${cfg.database.user}@${cfg.database.host}/${cfg.database.name}${lib.optionalString dbIsLocal "?socket=/run/mysqld/mysqld.sock"}" |
| 22 | + else |
| 23 | + "postgres://${cfg.database.user}@${cfg.database.host}/${cfg.database.name}${lib.optionalString dbIsLocal "?host=/run/postgresql"}"; |
| 24 | + |
| 25 | + # postgresql.target waits for postgresql-setup.service (which runs |
| 26 | + # ensureDatabases / ensureUsers) to complete, avoiding race conditions |
| 27 | + # where the syncserver starts before its database and role exist. |
| 28 | + dbService = if dbIsMySQL then "mysql.service" else "postgresql.target"; |
| 29 | + |
| 30 | + syncserver = cfg.package.override { dbBackend = cfg.database.type; }; |
17 | 31 |
|
18 | 32 | format = pkgs.formats.toml { }; |
19 | 33 | settings = { |
|
22 | 36 | database_url = dbURL; |
23 | 37 | }; |
24 | 38 | tokenserver = { |
25 | | - node_type = "mysql"; |
| 39 | + node_type = if dbIsMySQL then "mysql" else "postgres"; |
26 | 40 | database_url = dbURL; |
27 | 41 | fxa_email_domain = "api.accounts.firefox.com"; |
28 | 42 | fxa_oauth_server_url = "https://oauth.accounts.firefox.com/v1"; |
|
41 | 55 | }; |
42 | 56 | }; |
43 | 57 | 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 | 58 |
|
69 | | -
|
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 | | - ''; |
| 59 | + setupScript = |
| 60 | + let |
| 61 | + dbSpecific = |
| 62 | + if dbIsMySQL then |
| 63 | + { |
| 64 | + listTables = "mysql ${cfg.database.name} -Ne 'SHOW TABLES'"; |
| 65 | + execSql = "mysql ${cfg.database.name}"; |
| 66 | + upsertSql = '' |
| 67 | + BEGIN; |
| 68 | +
|
| 69 | + INSERT INTO `services` (`id`, `service`, `pattern`) |
| 70 | + VALUES (1, 'sync-1.5', '{node}/1.5/{uid}') |
| 71 | + ON DUPLICATE KEY UPDATE service='sync-1.5', pattern='{node}/1.5/{uid}'; |
| 72 | + INSERT INTO `nodes` (`id`, `service`, `node`, `available`, `current_load`, |
| 73 | + `capacity`, `downed`, `backoff`) |
| 74 | + VALUES (1, 1, '${cfg.singleNode.url}', ${toString cfg.singleNode.capacity}, |
| 75 | + 0, ${toString cfg.singleNode.capacity}, 0, 0) |
| 76 | + ON DUPLICATE KEY UPDATE node = '${cfg.singleNode.url}', capacity=${toString cfg.singleNode.capacity}; |
| 77 | +
|
| 78 | + COMMIT; |
| 79 | + ''; |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + listTables = "psql -d ${cfg.database.name} -tAc \"SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'services')\""; |
| 84 | + execSql = "psql -d ${cfg.database.name}"; |
| 85 | + upsertSql = '' |
| 86 | + BEGIN; |
| 87 | +
|
| 88 | + INSERT INTO services (id, service, pattern) |
| 89 | + VALUES (1, 'sync-1.5', '{node}/1.5/{uid}') |
| 90 | + ON CONFLICT (id) DO UPDATE SET service = 'sync-1.5', pattern = '{node}/1.5/{uid}'; |
| 91 | + INSERT INTO nodes (id, service, node, available, current_load, |
| 92 | + capacity, downed, backoff) |
| 93 | + VALUES (1, 1, '${cfg.singleNode.url}', ${toString cfg.singleNode.capacity}, |
| 94 | + 0, ${toString cfg.singleNode.capacity}, 0, 0) |
| 95 | + ON CONFLICT (id) DO UPDATE SET node = '${cfg.singleNode.url}', capacity = ${toString cfg.singleNode.capacity}; |
| 96 | +
|
| 97 | + COMMIT; |
| 98 | + ''; |
| 99 | + }; |
| 100 | + in |
| 101 | + pkgs.writeShellScript "firefox-syncserver-setup" '' |
| 102 | + set -euo pipefail |
| 103 | + shopt -s inherit_errexit |
| 104 | +
|
| 105 | + schema_configured() { |
| 106 | + ${dbSpecific.listTables} | grep -q services |
| 107 | + } |
| 108 | +
|
| 109 | + update_config() { |
| 110 | + ${dbSpecific.execSql} <<'EOF' |
| 111 | + ${dbSpecific.upsertSql} |
| 112 | + EOF |
| 113 | + } |
| 114 | +
|
| 115 | + for (( try = 0; try < 60; try++ )); do |
| 116 | + if ! schema_configured; then |
| 117 | + sleep 2 |
| 118 | + else |
| 119 | + update_config |
| 120 | + exit 0 |
| 121 | + fi |
| 122 | + done |
| 123 | +
|
| 124 | + echo "Single-node setup failed" |
| 125 | + exit 1 |
| 126 | + ''; |
82 | 127 | in |
83 | 128 |
|
84 | 129 | { |
|
88 | 133 | the Firefox Sync storage service. |
89 | 134 |
|
90 | 135 | 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. |
92 | | - by running |
93 | | -
|
94 | | - ``` |
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'); |
99 | | - ``` |
| 136 | + one service and one nodes by inserting them into the database manually, e.g. |
| 137 | + by running the equivalent SQL for your database backend. |
100 | 138 |
|
101 | 139 | {option}`${opt.singleNode.enable}` does this automatically when enabled |
102 | 140 | ''; |
103 | 141 |
|
104 | 142 | package = lib.mkPackageOption pkgs "syncstorage-rs" { }; |
105 | 143 |
|
| 144 | + database.type = lib.mkOption { |
| 145 | + type = lib.types.enum [ |
| 146 | + "mysql" |
| 147 | + "postgresql" |
| 148 | + ]; |
| 149 | + default = "mysql"; |
| 150 | + description = '' |
| 151 | + Which database backend to use for storage. |
| 152 | + ''; |
| 153 | + }; |
| 154 | + |
106 | 155 | 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 | 156 | type = lib.types.strMatching "[a-z_][a-z0-9_]*"; |
111 | 157 | default = defaultDatabase; |
112 | 158 | description = '' |
|
117 | 163 |
|
118 | 164 | database.user = lib.mkOption { |
119 | 165 | type = lib.types.str; |
120 | | - default = defaultUser; |
| 166 | + default = if dbIsPostgreSQL then defaultDatabase else defaultUser; |
| 167 | + defaultText = lib.literalExpression '' |
| 168 | + if database.type == "postgresql" then "${defaultDatabase}" else "${defaultUser}" |
| 169 | + ''; |
121 | 170 | description = '' |
122 | | - Username for database connections. |
| 171 | + Username for database connections. When using PostgreSQL with |
| 172 | + `createLocally`, this defaults to the database name so that |
| 173 | + `ensureDBOwnership` works (it requires user and database names |
| 174 | + to match). |
123 | 175 | ''; |
124 | 176 | }; |
125 | 177 |
|
|
137 | 189 | default = true; |
138 | 190 | description = '' |
139 | 191 | 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. |
| 192 | + This includes enabling the configured database service and setting up |
| 193 | + authentication for the configured user. |
141 | 194 | ''; |
142 | 195 | }; |
143 | 196 |
|
|
237 | 290 | }; |
238 | 291 |
|
239 | 292 | config = lib.mkIf cfg.enable { |
240 | | - services.mysql = lib.mkIf cfg.database.createLocally { |
| 293 | + services.mysql = lib.mkIf (cfg.database.createLocally && dbIsMySQL) { |
241 | 294 | enable = true; |
242 | 295 | ensureDatabases = [ cfg.database.name ]; |
243 | 296 | ensureUsers = [ |
|
250 | 303 | ]; |
251 | 304 | }; |
252 | 305 |
|
| 306 | + services.postgresql = lib.mkIf (cfg.database.createLocally && dbIsPostgreSQL) { |
| 307 | + enable = true; |
| 308 | + ensureDatabases = [ cfg.database.name ]; |
| 309 | + ensureUsers = [ |
| 310 | + { |
| 311 | + name = cfg.database.user; |
| 312 | + ensureDBOwnership = true; |
| 313 | + } |
| 314 | + ]; |
| 315 | + }; |
| 316 | + |
253 | 317 | systemd.services.firefox-syncserver = { |
254 | 318 | wantedBy = [ "multi-user.target" ]; |
255 | | - requires = lib.mkIf dbIsLocal [ "mysql.service" ]; |
256 | | - after = lib.mkIf dbIsLocal [ "mysql.service" ]; |
| 319 | + requires = lib.mkIf dbIsLocal [ dbService ]; |
| 320 | + after = lib.mkIf dbIsLocal [ dbService ]; |
257 | 321 | restartTriggers = lib.optional cfg.singleNode.enable setupScript; |
258 | 322 | environment.RUST_LOG = cfg.logLevel; |
259 | 323 | serviceConfig = { |
260 | | - User = defaultUser; |
261 | | - Group = defaultUser; |
262 | | - ExecStart = "${cfg.package}/bin/syncserver --config ${configFile}"; |
| 324 | + User = cfg.database.user; |
| 325 | + Group = cfg.database.user; |
| 326 | + ExecStart = "${syncserver}/bin/syncserver --config ${configFile}"; |
263 | 327 | EnvironmentFile = lib.mkIf (cfg.secrets != null) "${cfg.secrets}"; |
264 | 328 |
|
265 | 329 | # hardening |
|
303 | 367 |
|
304 | 368 | systemd.services.firefox-syncserver-setup = lib.mkIf cfg.singleNode.enable { |
305 | 369 | 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}" ]; |
| 370 | + requires = [ "firefox-syncserver.service" ] ++ lib.optional dbIsLocal dbService; |
| 371 | + after = [ "firefox-syncserver.service" ] ++ lib.optional dbIsLocal dbService; |
| 372 | + path = |
| 373 | + if dbIsMySQL then [ config.services.mysql.package ] else [ config.services.postgresql.package ]; |
| 374 | + serviceConfig = { |
| 375 | + ExecStart = [ "${setupScript}" ]; |
| 376 | + } |
| 377 | + // lib.optionalAttrs dbIsPostgreSQL { |
| 378 | + # PostgreSQL peer authentication requires the system user to match the |
| 379 | + # database user. Run as the superuser so we can access all databases. |
| 380 | + User = "postgres"; |
| 381 | + Group = "postgres"; |
| 382 | + }; |
310 | 383 | }; |
311 | 384 |
|
312 | 385 | services.nginx.virtualHosts = lib.mkIf cfg.singleNode.enableNginx { |
|
0 commit comments