diff options
-rw-r--r-- | nixos/modules/services/networking/firefox-syncserver.nix | 90 |
1 files changed, 40 insertions, 50 deletions
diff --git a/nixos/modules/services/networking/firefox-syncserver.nix b/nixos/modules/services/networking/firefox-syncserver.nix index d7d5df59a4eb..c3d9f43f7457 100644 --- a/nixos/modules/services/networking/firefox-syncserver.nix +++ b/nixos/modules/services/networking/firefox-syncserver.nix @@ -32,6 +32,44 @@ let }; }; configFile = format.generate "syncstorage.toml" (lib.recursiveUpdate settings cfg.settings); + setupScript = pkgs.writeShellScript "firefox-syncserver-setup" '' + set -euo pipefail + shopt -s inherit_errexit + + schema_configured() { + mysql ${cfg.database.name} -Ne 'SHOW TABLES' | grep -q services + } + + update_config() { + mysql ${cfg.database.name} <<"EOF" + BEGIN; + + INSERT INTO `services` (`id`, `service`, `pattern`) + VALUES (1, 'sync-1.5', '{node}/1.5/{uid}') + ON DUPLICATE KEY UPDATE service='sync-1.5', pattern='{node}/1.5/{uid}'; + INSERT INTO `nodes` (`id`, `service`, `node`, `available`, `current_load`, + `capacity`, `downed`, `backoff`) + VALUES (1, 1, '${cfg.singleNode.url}', ${toString cfg.singleNode.capacity}, + 0, ${toString cfg.singleNode.capacity}, 0, 0) + ON DUPLICATE KEY UPDATE node = '${cfg.singleNode.url}', capacity=${toString cfg.singleNode.capacity}; + + COMMIT; + EOF + } + + + for (( try = 0; try < 60; try++ )); do + if ! schema_configured; then + sleep 2 + else + update_config + exit 0 + fi + done + + echo "Single-node setup failed" + exit 1 + ''; in { @@ -210,6 +248,7 @@ in wantedBy = [ "multi-user.target" ]; requires = lib.mkIf dbIsLocal [ "mysql.service" ]; after = lib.mkIf dbIsLocal [ "mysql.service" ]; + restartTriggers = lib.optional cfg.singleNode.enable setupScript; environment.RUST_LOG = cfg.logLevel; serviceConfig = { User = defaultUser; @@ -255,56 +294,7 @@ in requires = [ "firefox-syncserver.service" ] ++ lib.optional dbIsLocal "mysql.service"; after = [ "firefox-syncserver.service" ] ++ lib.optional dbIsLocal "mysql.service"; path = [ config.services.mysql.package ]; - script = '' - set -euo pipefail - shopt -s inherit_errexit - - schema_configured() { - mysql ${cfg.database.name} -Ne 'SHOW TABLES' | grep -q services - } - - services_configured() { - [ 1 != $(mysql ${cfg.database.name} -Ne 'SELECT COUNT(*) < 1 FROM `services`') ] - } - - create_services() { - mysql ${cfg.database.name} <<"EOF" - BEGIN; - - INSERT INTO `services` (`id`, `service`, `pattern`) - VALUES (1, 'sync-1.5', '{node}/1.5/{uid}'); - INSERT INTO `nodes` (`id`, `service`, `node`, `available`, `current_load`, - `capacity`, `downed`, `backoff`) - VALUES (1, 1, '${cfg.singleNode.url}', ${toString cfg.singleNode.capacity}, - 0, ${toString cfg.singleNode.capacity}, 0, 0); - - COMMIT; - EOF - } - - update_nodes() { - mysql ${cfg.database.name} <<"EOF" - UPDATE `nodes` - SET `capacity` = ${toString cfg.singleNode.capacity} - WHERE `id` = 1; - EOF - } - - for (( try = 0; try < 60; try++ )); do - if ! schema_configured; then - sleep 2 - elif services_configured; then - update_nodes - exit 0 - else - create_services - exit 0 - fi - done - - echo "Single-node setup failed" - exit 1 - ''; + serviceConfig.ExecStart = [ "${setupScript}" ]; }; services.nginx.virtualHosts = lib.mkIf cfg.singleNode.enableNginx { |