|
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" '' |
| 58 | + |
| 59 | + mysqlSetupScript = pkgs.writeShellScript "firefox-syncserver-setup" '' |
45 | 60 | set -euo pipefail |
46 | 61 | shopt -s inherit_errexit |
47 | 62 |
|
|
79 | 94 | echo "Single-node setup failed" |
80 | 95 | exit 1 |
81 | 96 | ''; |
| 97 | + |
| 98 | + postgresqlSetupScript = pkgs.writeShellScript "firefox-syncserver-setup" '' |
| 99 | + set -euo pipefail |
| 100 | + shopt -s inherit_errexit |
| 101 | +
|
| 102 | + schema_configured() { |
| 103 | + psql -d ${cfg.database.name} -tAc "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'services')" | grep -q t |
| 104 | + } |
| 105 | +
|
| 106 | + update_config() { |
| 107 | + psql -d ${cfg.database.name} <<'EOF' |
| 108 | + BEGIN; |
| 109 | +
|
| 110 | + INSERT INTO services (id, service, pattern) |
| 111 | + VALUES (1, 'sync-1.5', '{node}/1.5/{uid}') |
| 112 | + ON CONFLICT (id) DO UPDATE SET service = 'sync-1.5', pattern = '{node}/1.5/{uid}'; |
| 113 | + INSERT INTO nodes (id, service, node, available, current_load, |
| 114 | + capacity, downed, backoff) |
| 115 | + VALUES (1, 1, '${cfg.singleNode.url}', ${toString cfg.singleNode.capacity}, |
| 116 | + 0, ${toString cfg.singleNode.capacity}, 0, 0) |
| 117 | + ON CONFLICT (id) DO UPDATE SET node = '${cfg.singleNode.url}', capacity = ${toString cfg.singleNode.capacity}; |
| 118 | +
|
| 119 | + COMMIT; |
| 120 | + EOF |
| 121 | + } |
| 122 | +
|
| 123 | +
|
| 124 | + for (( try = 0; try < 60; try++ )); do |
| 125 | + if ! schema_configured; then |
| 126 | + sleep 2 |
| 127 | + else |
| 128 | + update_config |
| 129 | + exit 0 |
| 130 | + fi |
| 131 | + done |
| 132 | +
|
| 133 | + echo "Single-node setup failed" |
| 134 | + exit 1 |
| 135 | + ''; |
| 136 | + |
| 137 | + setupScript = if dbIsMySQL then mysqlSetupScript else postgresqlSetupScript; |
82 | 138 | in |
83 | 139 |
|
84 | 140 | { |
|
88 | 144 | the Firefox Sync storage service. |
89 | 145 |
|
90 | 146 | 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 | | - ``` |
| 147 | + one service and one nodes by inserting them into the database manually, e.g. |
| 148 | + by running the equivalent SQL for your database backend. |
100 | 149 |
|
101 | 150 | {option}`${opt.singleNode.enable}` does this automatically when enabled |
102 | 151 | ''; |
103 | 152 |
|
104 | 153 | package = lib.mkPackageOption pkgs "syncstorage-rs" { }; |
105 | 154 |
|
| 155 | + database.type = lib.mkOption { |
| 156 | + type = lib.types.enum [ |
| 157 | + "mysql" |
| 158 | + "postgresql" |
| 159 | + ]; |
| 160 | + default = "mysql"; |
| 161 | + description = '' |
| 162 | + Which database backend to use for storage. |
| 163 | + ''; |
| 164 | + }; |
| 165 | + |
106 | 166 | 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 | 167 | type = lib.types.strMatching "[a-z_][a-z0-9_]*"; |
111 | 168 | default = defaultDatabase; |
112 | 169 | description = '' |
|
117 | 174 |
|
118 | 175 | database.user = lib.mkOption { |
119 | 176 | type = lib.types.str; |
120 | | - default = defaultUser; |
| 177 | + default = if dbIsPostgreSQL then defaultDatabase else defaultUser; |
| 178 | + defaultText = lib.literalExpression '' |
| 179 | + if database.type == "postgresql" then "${defaultDatabase}" else "${defaultUser}" |
| 180 | + ''; |
121 | 181 | description = '' |
122 | | - Username for database connections. |
| 182 | + Username for database connections. When using PostgreSQL with |
| 183 | + `createLocally`, this defaults to the database name so that |
| 184 | + `ensureDBOwnership` works (it requires user and database names |
| 185 | + to match). |
123 | 186 | ''; |
124 | 187 | }; |
125 | 188 |
|
|
137 | 200 | default = true; |
138 | 201 | description = '' |
139 | 202 | 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. |
| 203 | + This includes enabling the configured database service and setting up |
| 204 | + authentication for the configured user. |
141 | 205 | ''; |
142 | 206 | }; |
143 | 207 |
|
|
237 | 301 | }; |
238 | 302 |
|
239 | 303 | config = lib.mkIf cfg.enable { |
240 | | - services.mysql = lib.mkIf cfg.database.createLocally { |
| 304 | + services.mysql = lib.mkIf (cfg.database.createLocally && dbIsMySQL) { |
241 | 305 | enable = true; |
242 | 306 | ensureDatabases = [ cfg.database.name ]; |
243 | 307 | ensureUsers = [ |
|
250 | 314 | ]; |
251 | 315 | }; |
252 | 316 |
|
| 317 | + services.postgresql = lib.mkIf (cfg.database.createLocally && dbIsPostgreSQL) { |
| 318 | + enable = true; |
| 319 | + ensureDatabases = [ cfg.database.name ]; |
| 320 | + ensureUsers = [ |
| 321 | + { |
| 322 | + name = cfg.database.user; |
| 323 | + ensureDBOwnership = true; |
| 324 | + } |
| 325 | + ]; |
| 326 | + }; |
| 327 | + |
253 | 328 | systemd.services.firefox-syncserver = { |
254 | 329 | wantedBy = [ "multi-user.target" ]; |
255 | | - requires = lib.mkIf dbIsLocal [ "mysql.service" ]; |
256 | | - after = lib.mkIf dbIsLocal [ "mysql.service" ]; |
| 330 | + requires = lib.mkIf dbIsLocal [ dbService ]; |
| 331 | + after = lib.mkIf dbIsLocal [ dbService ]; |
257 | 332 | restartTriggers = lib.optional cfg.singleNode.enable setupScript; |
258 | 333 | environment.RUST_LOG = cfg.logLevel; |
259 | 334 | serviceConfig = { |
260 | | - User = defaultUser; |
261 | | - Group = defaultUser; |
262 | | - ExecStart = "${cfg.package}/bin/syncserver --config ${configFile}"; |
| 335 | + User = cfg.database.user; |
| 336 | + Group = cfg.database.user; |
| 337 | + ExecStart = "${syncserver}/bin/syncserver --config ${configFile}"; |
263 | 338 | EnvironmentFile = lib.mkIf (cfg.secrets != null) "${cfg.secrets}"; |
264 | 339 |
|
265 | 340 | # hardening |
|
303 | 378 |
|
304 | 379 | systemd.services.firefox-syncserver-setup = lib.mkIf cfg.singleNode.enable { |
305 | 380 | 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}" ]; |
| 381 | + requires = [ "firefox-syncserver.service" ] ++ lib.optional dbIsLocal dbService; |
| 382 | + after = [ "firefox-syncserver.service" ] ++ lib.optional dbIsLocal dbService; |
| 383 | + path = |
| 384 | + if dbIsMySQL then [ config.services.mysql.package ] else [ config.services.postgresql.package ]; |
| 385 | + serviceConfig = { |
| 386 | + ExecStart = [ "${setupScript}" ]; |
| 387 | + } |
| 388 | + // lib.optionalAttrs dbIsPostgreSQL { |
| 389 | + # PostgreSQL peer authentication requires the system user to match the |
| 390 | + # database user. Run as the superuser so we can access all databases. |
| 391 | + User = "postgres"; |
| 392 | + Group = "postgres"; |
| 393 | + }; |
310 | 394 | }; |
311 | 395 |
|
312 | 396 | services.nginx.virtualHosts = lib.mkIf cfg.singleNode.enableNginx { |
|
0 commit comments