support nomad jobs from ./jobs/**/*.nomad.hcl files

This commit is contained in:
Kiara Grouwstra 2024-01-24 15:51:43 +01:00
parent 2bfdb8af80
commit 1177ad0bfa
5 changed files with 49 additions and 31 deletions

View File

@ -50,7 +50,9 @@ in rec {
resource = (inNamespace "hcloud" {
ssh_key = setNames (lib.mapAttrs (_: v: { public_key = v; }) my-lib.ssh-keys);
ssh_key = setNames
(lib.mapAttrs (_: v: { public_key = v; })
(my-lib.dirContents ".pub" ./ssh-keys));
});

0
jobs/.gitkeep Normal file
View File

View File

@ -1,5 +1,36 @@
{ lib, ... }:
let
# ".ext" -> ./subdir -> { "foo" = "<CONTENTS OF a/b/foo.ext>"; "bar" = "<CONTENTS OF a/b/bar.ext>"; }
dirContents = let
# ".ext" -> "a/b.ext" -> "b"
fileAttrName = suffix: path: let
ext = lib.last (lib.splitString "." path);
in lib.removeSuffix suffix (builtins.baseNameOf path);
# maps a file to a path
# ".ext" -> "a/b" -> "c/d.ext" -> { name = "d"; value = "a/b/c/d.ext"; }
fileAttrInPath = suffix: path: name: {
name = fileAttrName suffix name;
value = path + "/${name}";
};
# get an object of files in a directory with a given suffix
# ".ext" -> "a/b" -> { "foo" = "a/b/foo.ext"; "bar" = "a/b/bar.ext"; }
dirAttrs = suffix: path: lib.mapAttrs'
(name: _: fileAttrInPath suffix path name)
(lib.filterAttrs
(name: type: lib.hasSuffix suffix name && type == "regular")
(builtins.readDir path));
in suffix: path: lib.mapAttrs (_: lib.readFile) (dirAttrs suffix path);
in
{
ssh-keys = import ./ssh-keys.nix { inherit lib; };
inherit dirContents;
}

View File

@ -1,25 +0,0 @@
{ lib, ... }:
let
# "a/b.ext" -> "b"
fileAttrName = path: let
ext = lib.last (lib.splitString "." path);
in lib.removeSuffix ".${ext}" (builtins.baseNameOf path);
# maps a file to a path
# "a/b" -> "c/d.ext" -> { name = "d"; value = "a/b/c/d.ext"; }
fileAttrInPath = path: name: {
name = fileAttrName name;
value = path + "/${name}";
};
# get an object of files in a directory with a given suffix
# "a/b" -> { "foo" = "a/b/foo.ext"; "bar" = "a/b/bar.ext"; }
dirAttrs = suffix: path: lib.mapAttrs'
(name: _: fileAttrInPath path name)
(lib.filterAttrs
(name: type: lib.hasSuffix suffix name && type == "regular")
(builtins.readDir path));
in lib.mapAttrs (_: lib.readFile) (dirAttrs ".pub" ../ssh-keys)

View File

@ -4,11 +4,15 @@ let
var = options.variable;
my-lib = import ./lib/default.nix { inherit lib; };
in
{
terraform.required_providers.nomad.source = "registry.terraform.io/hashicorp/nomad";
# https://developer.hashicorp.com/nomad/docs/job-specification/hcl2/variables
# https://developer.hashicorp.com/nomad/docs/runtime/interpolation
variable = {
nomad_host = {
@ -40,10 +44,16 @@ in
# https://registry.terraform.io/providers/hashicorp/nomad/latest/docs/
resource = {
nomad_job = lib.mapAttrs (k: v: {
json = true;
jobspec = lib.strings.toJSON v;
}) config.nomad.build.apiJob;
nomad_job =
# nix jobs
lib.mapAttrs (_: v: {
json = true;
jobspec = lib.strings.toJSON v;
}) config.nomad.build.apiJob
# hcl jobs
// lib.mapAttrs (_: v: {
jobspec = v;
}) (my-lib.dirContents ".nomad.hcl" ./jobs);
};