Clean up finally rendered templates for services

This commit is contained in:
IamTheFij 2023-08-24 15:37:42 -07:00
parent 013dd8248b
commit 7aa5b800ba

View File

@ -10,84 +10,82 @@ job "${name}" {
network { network {
mode = "bridge" mode = "bridge"
%{ if service_port != null ~} %{~ if service_port != null }
port "main" { port "main" {
%{~ if use_wesher ~} %{~ if use_wesher ~}
host_network = "wesher" host_network = "wesher"
%{~ endif ~} %{~ endif ~}
%{ if service_port_static ~} %{~ if service_port_static ~}
static = ${service_port} static = ${service_port}
%{ else ~} %{~ else ~}
to = ${service_port} to = ${service_port}
%{~ endif } %{~ endif ~}
} }
%{ endif ~} %{~ endif ~}
%{ for port in ports ~} %{~ for port in ports }
port "${port.name}" { port "${port.name}" {
%{ if port.host_network != null }host_network = "${port.host_network}"%{ endif ~} %{ if port.host_network != null }host_network = "${port.host_network}"%{ endif ~}
%{ if port.from != null }to = ${port.from}%{ endif ~} %{ if port.from != null }to = ${port.from}%{ endif ~}
%{ if port.to != null }to = ${port.to}%{ endif ~} %{ if port.to != null }to = ${port.to}%{ endif ~}
%{ if port.static != null }static = ${port.static}%{ endif ~} %{ if port.static != null }static = ${port.static}%{ endif ~}
} }
%{ endfor } %{~ endfor ~}
} }
%{~ for constraint in constraints }
%{ for constraint in constraints ~}
constraint { constraint {
attribute = "${constraint.attribute}" attribute = "${constraint.attribute}"
operator = "${constraint.operator}" operator = "${constraint.operator}"
value = "${constraint.value}" value = "${constraint.value}"
} }
%{~ endfor ~}
%{ endfor ~} %{~ if length(group_meta) > 0 }
%{ if length(group_meta) > 0 }
meta = { meta = {
%{ for k, v in group_meta } %{~ for k, v in group_meta ~}
${k} = ${jsonencode(v)} ${k} = ${jsonencode(v)}
%{ endfor } %{~ endfor ~}
} }
%{~ endif } %{~ endif ~}
%{ if sticky_disk } %{~ if sticky_disk }
ephemeral_disk { ephemeral_disk {
migrate = true migrate = true
sticky = true sticky = true
} }
%{~ endif } %{~ endif ~}
%{ for host_volume in host_volumes } %{~ for host_volume in host_volumes }
volume "${host_volume.name}" { volume "${host_volume.name}" {
type = "host" type = "host"
read_only = ${host_volume.read_only} read_only = ${host_volume.read_only}
source = "${host_volume.name}" source = "${host_volume.name}"
} }
%{ endfor } %{~ endfor ~}
%{ if service_port != null ~} %{~ if service_port != null }
service { service {
name = "${replace(name, "_", "-")}" name = "${replace(name, "_", "-")}"
provider = "nomad" provider = "nomad"
port = "main" port = "main"
tags = [ tags = [
%{ if prometheus == true } %{~ if prometheus == true ~}
"prometheus.scrape", "prometheus.scrape",
%{ endif } %{~ endif ~}
%{ if ingress } %{~ if ingress ~}
"traefik.enable=true", "traefik.enable=true",
"traefik.http.routers.${name}.entryPoints=websecure", "traefik.http.routers.${name}.entryPoints=websecure",
%{ if try(ingress_rule, null) != null ~} %{~ if try(ingress_rule, null) != null ~}
"traefik.http.routers.${name}.rule=${ingress_rule}", "traefik.http.routers.${name}.rule=${ingress_rule}",
%{ endif ~} %{~ endif ~}
%{ for middleware in ingress_middlewares ~} %{~ for middleware in ingress_middlewares ~}
"traefik.http.routers.${name}.middlewares=${middleware}", "traefik.http.routers.${name}.middlewares=${middleware}",
%{ endfor ~} %{~ endfor ~}
%{ endif ~} %{~ endif ~}
%{ for tag in service_tags ~} %{~ for tag in service_tags ~}
"${tag}", "${tag}",
%{ endfor ~} %{~ endfor ~}
] ]
} }
%{~ endif ~}
%{ endif ~} %{~ for custom_service in custom_services ~}
%{ for custom_service in custom_services }
service { service {
name = "${custom_service.name}" name = "${custom_service.name}"
provider = "nomad" provider = "nomad"
@ -96,91 +94,96 @@ job "${name}" {
tags = ${jsonencode(custom_service.tags)} tags = ${jsonencode(custom_service.tags)}
} }
%{ endfor } %{~ endfor ~}
task "${name}" { task "${name}" {
driver = "docker" driver = "docker"
%{~ if length(meta) > 0 }
%{ if length(meta) > 0 ~}
meta = { meta = {
%{ for k, v in meta ~} %{ for k, v in meta ~}
${k} = ${jsonencode(v)} ${k} = ${jsonencode(v)}
%{ endfor ~} %{ endfor ~}
} }
%{~ endif ~}
%{ endif ~}
config { config {
image = "${image}" image = "${image}"
%{if image_pull_timeout != null ~} %{~if image_pull_timeout != null ~}
image_pull_timeout = "${image_pull_timeout}" image_pull_timeout = "${image_pull_timeout}"
%{ endif ~} %{~ endif ~}
%{ if service_port != null ~} %{~ if service_port != null ~}
ports = ["main"] ports = ["main"]
%{ endif ~} %{~ endif ~}
%{ if length(try(args, [])) > 0 ~} %{~ if length(try(args, [])) > 0 ~}
args = ${jsonencode(args)} args = ${jsonencode(args)}
%{ endif ~} %{~ endif ~}
%{ if length(docker_devices) > 0 ~} %{~ if length(docker_devices) > 0 ~}
devices = [ devices = [
%{ for dev in docker_devices } %{~ for dev in docker_devices ~}
{ {
host_path = "${dev.host_path}" host_path = "${dev.host_path}"
container_path = "${dev.container_path}" container_path = "${dev.container_path}"
}, },
%{ endfor } %{~ endfor ~}
] ]
%{ endif ~} %{~ endif ~}
%{ for template in templates ~} %{~ for template in templates ~}
%{ if template.mount && !template.env ~} %{~ if template.mount && !template.env }
mount { mount {
type = "bind" type = "bind"
target = "${template.dest}" target = "${template.dest}"
source = "${template.dest_prefix}/${template.dest}" source = "${template.dest_prefix}/${template.dest}"
} }
%{ endif ~} %{~ endif ~}
%{ endfor ~} %{~ endfor ~}
} }
%{ if length(env) > 0 ~} %{~ if length(env) > 0 }
env = { env = {
%{ for k, v in env ~} %{~ for k, v in env ~}
"${k}" = "${v}" "${k}" = ${jsonencode(v)}
%{ endfor } %{~ endfor ~}
} }
%{ endif ~} %{~ endif ~}
%{ for volume in host_volumes ~} %{~ for volume in host_volumes }
volume_mount { volume_mount {
volume = "${volume.name}" volume = "${volume.name}"
destination = "${volume.dest}" destination = "${volume.dest}"
read_only = ${volume.read_only} read_only = ${volume.read_only}
} }
%{ endfor ~} %{~ endfor ~}
%{ for template in templates ~} %{~ for template in templates }
template { template {
data = <<EOF data = <<EOF
${template.data} ${template.data}
EOF EOF
destination = "${coalesce(template.dest_prefix, "$${NOMAD_TASK_DIR}")}/${template.dest}" destination = "${coalesce(template.dest_prefix, "$${NOMAD_TASK_DIR}")}/${template.dest}"
%{ if template.left_delimiter != null }left_delimiter = "${template.left_delimiter}"%{ endif } %{~ if template.left_delimiter != null }
%{ if template.right_delimiter != null }right_delimiter = "${template.right_delimiter}"%{ endif } left_delimiter = "${template.left_delimiter}"
%{ if template.change_mode != null }change_mode = "${template.change_mode}"%{ endif } %{~ endif ~}
%{ if template.change_signal != null }change_signal = "${template.change_signal}"%{ endif } %{~ if template.right_delimiter != null }
%{ if template.env != null }env = ${template.env}%{ endif } right_delimiter = "${template.right_delimiter}"
%{~ endif ~}
%{~ if template.change_mode != null }
change_mode = "${template.change_mode}"
%{~ endif ~}
%{~ if template.change_signal != null }
change_signal = "${template.change_signal}"
%{~ endif ~}
%{~ if template.env != null }
env = ${template.env}
%{~ endif ~}
} }
%{ endfor ~} %{~ endfor ~}
%{ if resources != null } %{~ if resources != null }
resources { resources {
cpu = ${resources.cpu} cpu = ${resources.cpu}
memory = ${resources.memory} memory = ${resources.memory}
%{ if resources.memory_max != null }memory_max = ${resources.memory_max}%{ endif } %{~ if resources.memory_max != null ~}
memory_max = ${resources.memory_max}
%{~ endif ~}
} }
%{~ endif } %{~ endif ~}
} }
%{~ if mysql_bootstrap != null }
%{ if mysql_bootstrap != null ~}
task "mysql-bootstrap" { task "mysql-bootstrap" {
driver = "docker" driver = "docker"
@ -243,9 +246,8 @@ SELECT 'NOOP';
memory = 50 memory = 50
} }
} }
%{ endif } %{~ endif ~}
%{~ if postgres_bootstrap != null }
%{ if postgres_bootstrap != null ~}
task "postgres-bootstrap" { task "postgres-bootstrap" {
driver = "docker" driver = "docker"
@ -267,13 +269,13 @@ SELECT 'NOOP';
template { template {
data = <<EOF data = <<EOF
%{ if length(postgres_bootstrap.databases) > 0 } %{ if length(postgres_bootstrap.databases) > 0 ~}
%{ for db_name in postgres_bootstrap.databases ~} %{ for db_name in postgres_bootstrap.databases }
/usr/bin/createdb ${db_name} /usr/bin/createdb ${db_name}
%{ endfor } %{ endfor ~}
%{ else } %{ else ~}
{{ with nomadVar "nomad/jobs/${name}" }}/usr/bin/createdb {{ .${postgres_bootstrap.db_name_key} }}{{ end }} {{ with nomadVar "nomad/jobs/${name}" }}/usr/bin/createdb {{ .${postgres_bootstrap.db_name_key} }}{{ end }}
%{ endif } %{ endif ~}
/usr/bin/psql -X -f $${NOMAD_SECRETS_DIR}/bootstrap.sql /usr/bin/psql -X -f $${NOMAD_SECRETS_DIR}/bootstrap.sql
EOF EOF
destination = "$${NOMAD_TASK_DIR}/bootstrap.sh" destination = "$${NOMAD_TASK_DIR}/bootstrap.sh"
@ -299,13 +301,13 @@ PGPASSWORD={{ .superuser_pass }}
DO $$ DO $$
BEGIN BEGIN
CREATE ROLE {{ .${postgres_bootstrap.db_user_key} }} LOGIN PASSWORD '{{ .${postgres_bootstrap.db_pass_key} }}'; CREATE ROLE {{ .${postgres_bootstrap.db_user_key} }} LOGIN PASSWORD '{{ .${postgres_bootstrap.db_pass_key} }}';
%{ if length(postgres_bootstrap.databases) > 0 } %{ if length(postgres_bootstrap.databases) > 0 ~}
%{ for db_name in postgres_bootstrap.databases } %{ for db_name in postgres_bootstrap.databases }
GRANT ALL ON DATABASE "${db_name}" TO {{ .${postgres_bootstrap.db_user_key} }}; GRANT ALL ON DATABASE "${db_name}" TO {{ .${postgres_bootstrap.db_user_key} }};
%{ endfor } %{ endfor ~}
%{ else } %{ else ~}
GRANT ALL ON DATABASE "{{ .${postgres_bootstrap.db_name_key} }}" TO {{ .${postgres_bootstrap.db_user_key} }}; GRANT ALL ON DATABASE "{{ .${postgres_bootstrap.db_name_key} }}" TO {{ .${postgres_bootstrap.db_user_key} }};
%{ endif } %{ endif ~}
EXCEPTION WHEN duplicate_object THEN RAISE NOTICE '%, skipping', SQLERRM USING ERRCODE = SQLSTATE; EXCEPTION WHEN duplicate_object THEN RAISE NOTICE '%, skipping', SQLERRM USING ERRCODE = SQLSTATE;
END END
$$; $$;
@ -319,9 +321,8 @@ $$;
memory = 50 memory = 50
} }
} }
%{ endif } %{~ endif ~}
%{~ if use_mysql || use_redis || use_ldap || use_postgres }
%{ if use_mysql || use_redis || use_ldap || use_postgres ~}
task "stunnel" { task "stunnel" {
driver = "docker" driver = "docker"
@ -338,7 +339,9 @@ $$;
resources { resources {
cpu = ${stunnel_resources.cpu} cpu = ${stunnel_resources.cpu}
memory = ${stunnel_resources.memory} memory = ${stunnel_resources.memory}
%{ if stunnel_resources.memory_max != null }memory_max = ${stunnel_resources.memory_max}%{ endif } %{~ if stunnel_resources.memory_max != null ~}
memory_max = ${stunnel_resources.memory_max}
%{~ endif ~}
} }
template { template {
@ -355,50 +358,45 @@ exec stunnel {{ env "NOMAD_TASK_DIR" }}/stunnel.conf
syslog = no syslog = no
foreground = yes foreground = yes
delay = yes delay = yes
%{ if use_mysql }
%{~ if use_mysql }
[mysql_client] [mysql_client]
client = yes client = yes
accept = 127.0.0.1:3306 accept = 127.0.0.1:3306
{{ range nomadService 1 (env "NOMAD_ALLOC_ID") "mysql-tls" -}} {{ range nomadService 1 (env "NOMAD_ALLOC_ID") "mysql-tls" }}
connect = {{ .Address }}:{{ .Port }} connect = {{ .Address }}:{{ .Port }}
{{- end }} {{ end }}
PSKsecrets = {{ env "NOMAD_SECRETS_DIR" }}/mysql_stunnel_psk.txt PSKsecrets = {{ env "NOMAD_SECRETS_DIR" }}/mysql_stunnel_psk.txt
%{~ endif } %{ endif ~}
%{ if use_redis }
%{~ if use_redis }
[redis_client] [redis_client]
client = yes client = yes
accept = 127.0.0.1:6379 accept = 127.0.0.1:6379
{{ range nomadService 1 (env "NOMAD_ALLOC_ID") "redis-${name}" -}} {{ range nomadService 1 (env "NOMAD_ALLOC_ID") "redis-${name}" }}
connect = {{ .Address }}:{{ .Port }} connect = {{ .Address }}:{{ .Port }}
{{- end }} {{ end }}
PSKsecrets = {{ env "NOMAD_SECRETS_DIR" }}/redis_stunnel_psk.txt PSKsecrets = {{ env "NOMAD_SECRETS_DIR" }}/redis_stunnel_psk.txt
%{~ endif } %{ endif }
%{ if use_ldap }
%{~ if use_ldap }
[ldap_client] [ldap_client]
client = yes client = yes
accept = 127.0.0.1:389 accept = 127.0.0.1:389
{{ range nomadService 1 (env "NOMAD_ALLOC_ID") "lldap-tls" -}} {{ range nomadService 1 (env "NOMAD_ALLOC_ID") "lldap-tls" }}
connect = {{ .Address }}:{{ .Port }} connect = {{ .Address }}:{{ .Port }}
{{- end }} {{ end }}
PSKsecrets = {{ env "NOMAD_SECRETS_DIR" }}/ldap_stunnel_psk.txt PSKsecrets = {{ env "NOMAD_SECRETS_DIR" }}/ldap_stunnel_psk.txt
%{~ endif } %{ endif ~}
%{ if use_postgres ~}
%{~ if use_postgres }
[postgres_client] [postgres_client]
client = yes client = yes
accept = 127.0.0.1:5432 accept = 127.0.0.1:5432
{{ range nomadService 1 (env "NOMAD_ALLOC_ID") "postgres-tls" -}} {{ range nomadService 1 (env "NOMAD_ALLOC_ID") "postgres-tls" }}
connect = {{ .Address }}:{{ .Port }} connect = {{ .Address }}:{{ .Port }}
{{- end }} {{ end }}
PSKsecrets = {{ env "NOMAD_SECRETS_DIR" }}/postgres_stunnel_psk.txt PSKsecrets = {{ env "NOMAD_SECRETS_DIR" }}/postgres_stunnel_psk.txt
%{~ endif } %{ endif ~}
EOF EOF
destination = "$${NOMAD_TASK_DIR}/stunnel.conf" destination = "$${NOMAD_TASK_DIR}/stunnel.conf"
} }
%{~ if use_mysql } %{~ if use_mysql }
template { template {
data = <<EOF data = <<EOF
@ -406,7 +404,7 @@ PSKsecrets = {{ env "NOMAD_SECRETS_DIR" }}/postgres_stunnel_psk.txt
EOF EOF
destination = "$${NOMAD_SECRETS_DIR}/mysql_stunnel_psk.txt" destination = "$${NOMAD_SECRETS_DIR}/mysql_stunnel_psk.txt"
} }
%{ endif } %{~ endif ~}
%{~ if use_redis } %{~ if use_redis }
template { template {
data = <<EOF data = <<EOF
@ -414,7 +412,7 @@ EOF
EOF EOF
destination = "$${NOMAD_SECRETS_DIR}/redis_stunnel_psk.txt" destination = "$${NOMAD_SECRETS_DIR}/redis_stunnel_psk.txt"
} }
%{~ endif } %{~ endif ~}
%{~ if use_ldap } %{~ if use_ldap }
template { template {
data = <<EOF data = <<EOF
@ -422,7 +420,7 @@ EOF
EOF EOF
destination = "$${NOMAD_SECRETS_DIR}/ldap_stunnel_psk.txt" destination = "$${NOMAD_SECRETS_DIR}/ldap_stunnel_psk.txt"
} }
%{~ endif } %{~ endif ~}
%{~ if use_postgres } %{~ if use_postgres }
template { template {
data = <<EOF data = <<EOF
@ -430,8 +428,8 @@ EOF
EOF EOF
destination = "$${NOMAD_SECRETS_DIR}/postgres_stunnel_psk.txt" destination = "$${NOMAD_SECRETS_DIR}/postgres_stunnel_psk.txt"
} }
%{~ endif } %{~ endif ~}
} }
%{~ endif } %{~ endif ~}
} }
} }