Fix race condition
This commit is contained in:
@@ -41,17 +41,23 @@ in {
|
|||||||
description = "Set DB passwords and ownership after Postgres is ready";
|
description = "Set DB passwords and ownership after Postgres is ready";
|
||||||
after = [ "postgresql.service" ];
|
after = [ "postgresql.service" ];
|
||||||
requires = [ "postgresql.service" ];
|
requires = [ "postgresql.service" ];
|
||||||
|
partOf = [ "postgresql.service" ];
|
||||||
wantedBy = [ "multi-user.target" ];
|
wantedBy = [ "multi-user.target" ];
|
||||||
|
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
Type = "oneshot";
|
Type = "oneshot";
|
||||||
User = "postgres"; # Runs as postgres user directly
|
User = "postgres"; # Runs as postgres user directly
|
||||||
RemainAfterExit = true;
|
RemainAfterExit = true;
|
||||||
|
ExecStartPre = "${pkgs.coreutils}/bin/sleep 2";
|
||||||
};
|
};
|
||||||
|
|
||||||
script = ''
|
script = ''
|
||||||
PSQL="${pkgs.postgresql}/bin/psql"
|
PSQL="${pkgs.postgresql}/bin/psql"
|
||||||
${lib.concatMapStringsSep "\n" (name: ''
|
${lib.concatMapStringsSep "\n" (name: ''
|
||||||
|
until $PSQL -tAc "SELECT 1 FROM pg_roles WHERE rolname = '${name}_user'" | grep -q 1; do
|
||||||
|
echo "Waiting for user ${name}_user..."
|
||||||
|
sleep 1
|
||||||
|
done
|
||||||
$PSQL -tAc "ALTER DATABASE ${name}_db OWNER TO ${name}_user;"
|
$PSQL -tAc "ALTER DATABASE ${name}_db OWNER TO ${name}_user;"
|
||||||
|
|
||||||
if [ -f "${config.sops.secrets."${name}_pass".path}" ]; then
|
if [ -f "${config.sops.secrets."${name}_pass".path}" ]; then
|
||||||
|
|||||||
Reference in New Issue
Block a user