propagate http backend config (#538)

Reviewed-on: fediversity/fediversity#538
This commit is contained in:
Kiara Grouwstra 2025-10-06 13:50:25 +02:00
parent fd49868cda
commit 2a7135fd64
7 changed files with 40 additions and 10 deletions

View file

@ -21,5 +21,9 @@ in
default = [ ]; default = [ ];
example = "ConnectTimeout=60"; example = "ConnectTimeout=60";
}; };
httpBackend = mkOption {
description = "environment variables to configure the TF HTTP back-end, see <https://developer.hashicorp.com/terraform/language/backend/http#configuration-variables>";
type = types.attrsOf (types.either types.str types.int);
};
}; };
} }

View file

@ -23,6 +23,7 @@ let
pathToRoot pathToRoot
targetSystem targetSystem
sshOpts sshOpts
httpBackend
; ;
inherit (lib) mkOption types; inherit (lib) mkOption types;
eval = eval =
@ -201,7 +202,7 @@ let
inherit sshOpts; inherit sshOpts;
}; };
module = self; module = self;
inherit args deployment-name; inherit args deployment-name httpBackend;
root-path = pathToRoot; root-path = pathToRoot;
}; };
}; };

View file

@ -8,10 +8,18 @@ let
inherit (pkgs) system; inherit (pkgs) system;
inherit (import ./constants.nix) pathToRoot; inherit (import ./constants.nix) pathToRoot;
nodeName = "target"; nodeName = "target";
backendPort = builtins.toString 8080;
deployment-config = { deployment-config = {
inherit nodeName pathToRoot; inherit nodeName pathToRoot;
targetSystem = system; targetSystem = system;
sshOpts = [ ]; sshOpts = [ ];
httpBackend = rec {
TF_HTTP_USERNAME = "basic";
TF_HTTP_PASSWORD = "fake-secret";
TF_HTTP_ADDRESS = "http://localhost:${backendPort}/state/project1/example";
TF_HTTP_LOCK_ADDRESS = TF_HTTP_ADDRESS;
TF_HTTP_UNLOCK_ADDRESS = TF_HTTP_ADDRESS;
};
}; };
deploy = deploy =
(import ../common/data-model.nix { (import ../common/data-model.nix {
@ -54,7 +62,7 @@ in
services.terraform-backend = { services.terraform-backend = {
enable = true; enable = true;
settings = { settings = {
LISTEN_ADDR = ":8080"; LISTEN_ADDR = ":${backendPort}";
KMS_KEY = "l99yC7MhbuuraACQ8bjaU1rMrT6L4PXEYupX6BzhJvY="; KMS_KEY = "l99yC7MhbuuraACQ8bjaU1rMrT6L4PXEYupX6BzhJvY=";
}; };
}; };

View file

@ -222,6 +222,10 @@ let
description = "The path to the root of the repository."; description = "The path to the root of the repository.";
type = types.path; type = types.path;
}; };
httpBackend = mkOption {
description = "environment variables to configure the TF HTTP back-end, see <https://developer.hashicorp.com/terraform/language/backend/http#configuration-variables>";
type = types.attrsOf (types.either types.str types.int);
};
run = mkOption { run = mkOption {
type = types.package; type = types.package;
# error: The option `tf-deployment.tf-host.run' is read-only, but it's set multiple times. # error: The option `tf-deployment.tf-host.run' is read-only, but it's set multiple times.
@ -235,6 +239,7 @@ let
args args
deployment-name deployment-name
root-path root-path
httpBackend
; ;
inherit (ssh) inherit (ssh)
host host
@ -260,7 +265,7 @@ let
deployment-type = "tf-host"; deployment-type = "tf-host";
}; };
}; };
tf-env = pkgs.callPackage ./run/tf-single-host/tf-env.nix { }; tf-env = pkgs.callPackage ./run/tf-single-host/tf-env.nix { inherit httpBackend; };
in in
pkgs.writers.writeBashBin "deploy-tf.sh" pkgs.writers.writeBashBin "deploy-tf.sh"
(withPackages [ (withPackages [
@ -269,6 +274,7 @@ let
]) ])
'' ''
env ${toString (lib.mapAttrsToList (k: v: "TF_VAR_${k}=\"${toBash v}\"") environment)} \ env ${toString (lib.mapAttrsToList (k: v: "TF_VAR_${k}=\"${toBash v}\"") environment)} \
${toString (lib.mapAttrsToList (k: v: "${k}=\"${toBash v}\"") httpBackend)} \
tf_env=${tf-env} bash ./deployment/run/tf-single-host/run.sh tf_env=${tf-env} bash ./deployment/run/tf-single-host/run.sh
''; '';
}; };

View file

@ -2,7 +2,21 @@
pkgs, pkgs,
lib, lib,
sources, sources,
httpBackend,
}: }:
let
# FIXME factor out
toBash =
v:
lib.replaceStrings [ "\"" ] [ "\\\"" ] (
if lib.isPath v || builtins.isNull v then
toString v
else if lib.isString v then
v
else
lib.strings.toJSON v
);
in
pkgs.writeScriptBin "setup" '' pkgs.writeScriptBin "setup" ''
set -e set -e
# calculated pins # calculated pins
@ -12,5 +26,7 @@ pkgs.writeScriptBin "setup" ''
rm -f .terraform.lock.hcl rm -f .terraform.lock.hcl
# suppress warning on architecture-specific generated lock file: # suppress warning on architecture-specific generated lock file:
# `Warning: Incomplete lock file information for providers`. # `Warning: Incomplete lock file information for providers`.
env TF_HTTP_RETRY_MAX=1 TF_HTTP_RETRY_WAIT_MIN=0 \
${toString (lib.mapAttrsToList (k: v: "${k}=\"${toBash v}\"") httpBackend)} \
tofu init -input=false 1>/dev/null tofu init -input=false 1>/dev/null
'' ''

View file

@ -1,11 +1,5 @@
terraform { terraform {
# TODO un-hardcode
backend "http" { backend "http" {
username = "basic"
password = "fake-secret"
address = "http://localhost:8080/state/project1/example"
lock_address = "http://localhost:8080/state/project1/example"
unlock_address = "http://localhost:8080/state/project1/example"
} }
} }

View file

@ -1,6 +1,7 @@
{ {
lib, lib,
pkgs, pkgs,
httpBackend,
sources ? import ../../../npins, sources ? import ../../../npins,
}: }:
pkgs.stdenv.mkDerivation { pkgs.stdenv.mkDerivation {
@ -14,7 +15,7 @@ pkgs.stdenv.mkDerivation {
}; };
buildInputs = [ buildInputs = [
(pkgs.callPackage ./tf.nix { inherit sources; }) (pkgs.callPackage ./tf.nix { inherit sources; })
(pkgs.callPackage ../tf-setup.nix { inherit sources; }) (pkgs.callPackage ../tf-setup.nix { inherit sources httpBackend; })
]; ];
buildPhase = '' buildPhase = ''
runHook preBuild runHook preBuild