From 92a60cbe3b6e66c9fa94c7ddff5a451e1ceb509b Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Tue, 30 Aug 2022 15:16:08 -0700 Subject: [PATCH] Update services template to support env and host volumes Also adds sonarr as an example --- nomad/service.nomad | 28 +++++++++++++++++++++++++++- nomad/services.tf | 31 ++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/nomad/service.nomad b/nomad/service.nomad index 5e3d07c..43e5820 100644 --- a/nomad/service.nomad +++ b/nomad/service.nomad @@ -6,6 +6,7 @@ # sticky_disk = bool # args = json(list[str]) # resources = dict(cpu = int, mem = int) +# env = json(dict(str: any)) # templates = json(list(dict( # data = str, # dest = str, @@ -14,6 +15,11 @@ # left_delimiter = str, # right_delimiter = str, # ))) +# host_volumes = json(list(dict( +# name = str, +# dest = str, +# read_only = bool, +# ))) # healthcheck = "/" # mysql = bool # redis = bool @@ -45,6 +51,16 @@ job "[[.name]]" { } [[ end ]] + [[ with .host_volumes -]] + [[ range $v := . | parseJSON -]] + volume "[[ $v.name ]]" { + type = "host" + read_only = [[ default true $v.read_only ]] + source = "[[ $v.name ]]" + } + [[ end ]] + [[ end -]] + [[ if not (empty .service_port) ]] service { name = "[[.name | replace "_" "-"]]" @@ -133,12 +149,22 @@ job "[[.name]]" { [[ with .env -]] env = { - [[ range $k, $v := . -]] + [[ range $k, $v := . | parseJSON -]] "[[$k]]" = "[[$v]]" [[ end -]] } [[ end -]] + [[ with .host_volumes -]] + [[ range $v := . | parseJSON -]] + volume_mount { + volume = "[[ $v.name ]]" + destination = "[[ $v.dest ]]" + read_only = [[ default true $v.read_only ]] + } + [[ end ]] + [[ end -]] + [[ with .templates -]] [[ range $t := . | parseJSON -]] template { diff --git a/nomad/services.tf b/nomad/services.tf index d08830b..bdfc090 100644 --- a/nomad/services.tf +++ b/nomad/services.tf @@ -18,9 +18,38 @@ resource "nomad_job" "whoami" { hcl2 { enabled = true vars = { - "count" = "${2 * length(data.consul_service.nomad.service)}", + "count" = 1, + # "count" = "${2 * length(data.consul_service.nomad.service)}", } } jobspec = file("${path.module}/whoami.nomad") } + +module "sonarr" { + source = "./levant" + + template_path = "service.nomad" + variables = { + name = "sonarr" + image = "linuxserver/sonarr" + service_port = 8989 + ingress = true + env = jsonencode({ + PGID = 100 + PUID = 1001 + }) + host_volumes = jsonencode([ + { + name = "media-write" + dest = "/srv/volumes/media-write" + read_only = false + }, + { + name = "download" + dest = "/srv/volumes/download" + read_only = false + }, + ]) + } +}