From d784b830051bb86172b9a3669826774cc20e1f41 Mon Sep 17 00:00:00 2001 From: Philipp Steinpass Date: Sat, 2 Sep 2017 21:23:56 +0200 Subject: nixos/hydra postgresql: Fix #27314 and add test case --- .../continuous-integration/hydra/default.nix | 4 +-- nixos/modules/services/databases/postgresql.nix | 19 +++++++++---- nixos/tests/hydra.nix | 32 ++++++++++++++++++++++ 3 files changed, 47 insertions(+), 8 deletions(-) create mode 100644 nixos/tests/hydra.nix diff --git a/nixos/modules/services/continuous-integration/hydra/default.nix b/nixos/modules/services/continuous-integration/hydra/default.nix index fcc0f58637c4..43fec5ff5bb2 100644 --- a/nixos/modules/services/continuous-integration/hydra/default.nix +++ b/nixos/modules/services/continuous-integration/hydra/default.nix @@ -270,8 +270,8 @@ in ${optionalString haveLocalDB '' if ! [ -e ${baseDir}/.db-created ]; then - ${config.services.postgresql.package}/bin/createuser hydra - ${config.services.postgresql.package}/bin/createdb -O hydra hydra + ${pkgs.sudo}/bin/sudo -u ${config.services.postgresql.superUser} ${config.services.postgresql.package}/bin/createuser hydra + ${pkgs.sudo}/bin/sudo -u ${config.services.postgresql.superUser} ${config.services.postgresql.package}/bin/createdb -O hydra hydra touch ${baseDir}/.db-created fi ''} diff --git a/nixos/modules/services/databases/postgresql.nix b/nixos/modules/services/databases/postgresql.nix index d06e03a52978..1bbab3296005 100644 --- a/nixos/modules/services/databases/postgresql.nix +++ b/nixos/modules/services/databases/postgresql.nix @@ -38,9 +38,6 @@ let pre84 = versionOlder (builtins.parseDrvName postgresql.name).version "8.4"; - # NixOS traditionally used `root` as superuser, most other distros use `postgres`. From 17.09 - # we also try to follow this standard - superuser = (if versionAtLeast config.system.stateVersion "17.09" then "postgres" else "root"); in @@ -151,6 +148,16 @@ in Contents of the recovery.conf file. ''; }; + superUser = mkOption { + type = types.str; + default= if versionAtLeast config.system.stateVersion "17.09" then "postgres" else "root"; + internal = true; + description = '' + NixOS traditionally used `root` as superuser, most other distros use `postgres`. + From 17.09 we also try to follow this standard. Internal since changing this value + would lead to breakage while setting up databases. + ''; + }; }; }; @@ -215,7 +222,7 @@ in '' # Initialise the database. if ! test -e ${cfg.dataDir}/PG_VERSION; then - initdb -U ${superuser} + initdb -U ${cfg.superUser} # See postStart! touch "${cfg.dataDir}/.first_startup" fi @@ -247,14 +254,14 @@ in # Wait for PostgreSQL to be ready to accept connections. postStart = '' - while ! ${pkgs.sudo}/bin/sudo -u ${superuser} psql --port=${toString cfg.port} -d postgres -c "" 2> /dev/null; do + while ! ${pkgs.sudo}/bin/sudo -u ${cfg.superUser} psql --port=${toString cfg.port} -d postgres -c "" 2> /dev/null; do if ! kill -0 "$MAINPID"; then exit 1; fi sleep 0.1 done if test -e "${cfg.dataDir}/.first_startup"; then ${optionalString (cfg.initialScript != null) '' - ${pkgs.sudo}/bin/sudo -u ${superuser} psql -f "${cfg.initialScript}" --port=${toString cfg.port} -d postgres + ${pkgs.sudo}/bin/sudo -u ${cfg.superUser} psql -f "${cfg.initialScript}" --port=${toString cfg.port} -d postgres ''} rm -f "${cfg.dataDir}/.first_startup" fi diff --git a/nixos/tests/hydra.nix b/nixos/tests/hydra.nix new file mode 100644 index 000000000000..6abd7a5ad300 --- /dev/null +++ b/nixos/tests/hydra.nix @@ -0,0 +1,32 @@ +import ./make-test.nix ({ pkgs, ...} : { + name = "hydra-init-localdb"; + meta = with pkgs.stdenv.lib.maintainers; { + maintainers = [ pstn ]; + }; + + machine = + { config, pkgs, ... }: + + { + services.hydra = { + enable = true; + + #Hydra needs those settings to start up, so we add something not harmfull. + hydraURL = "example.com"; + notificationSender = "example@example.com"; + }; + }; + + testScript = + '' + # let the system boot up + $machine->waitForUnit("multi-user.target"); + # test whether the database is running + $machine->succeed("systemctl status postgresql.service"); + # test whether the actual hydra daemons are running + $machine->succeed("systemctl status hydra-queue-runner.service"); + $machine->succeed("systemctl status hydra-init.service"); + $machine->succeed("systemctl status hydra-evaluator.service"); + $machine->succeed("systemctl status hydra-send-stats.service"); + ''; +}) -- cgit 1.4.1