2022-11-11 21:34:08 +00:00
|
|
|
job "${name}" {
|
|
|
|
region = "global"
|
|
|
|
datacenters = ["dc1"]
|
|
|
|
|
|
|
|
type = "service"
|
|
|
|
|
|
|
|
group "${name}" {
|
|
|
|
network {
|
|
|
|
mode = "bridge"
|
|
|
|
%{ if service_port != null ~}
|
|
|
|
port "main" {
|
2023-03-02 18:42:33 +00:00
|
|
|
%{ if ingress }
|
2023-03-24 18:24:36 +00:00
|
|
|
host_network = "wgoverlay"
|
2023-03-02 18:42:33 +00:00
|
|
|
%{~ endif }
|
2022-11-11 21:34:08 +00:00
|
|
|
to = ${service_port}
|
|
|
|
}
|
2023-03-02 18:42:33 +00:00
|
|
|
%{ endif }
|
2022-11-11 21:34:08 +00:00
|
|
|
}
|
|
|
|
|
2023-03-02 18:42:33 +00:00
|
|
|
%{ if length(group_meta) > 0 }
|
2023-01-11 23:40:42 +00:00
|
|
|
meta = {
|
2023-03-02 18:42:33 +00:00
|
|
|
%{ for k, v in group_meta }
|
2023-01-11 23:40:42 +00:00
|
|
|
${k} = ${jsonencode(v)}
|
2023-03-02 18:42:33 +00:00
|
|
|
%{ endfor }
|
2023-01-11 23:40:42 +00:00
|
|
|
}
|
2023-03-02 18:42:33 +00:00
|
|
|
%{~ endif }
|
|
|
|
%{ if sticky_disk }
|
2022-11-11 21:34:08 +00:00
|
|
|
ephemeral_disk {
|
|
|
|
migrate = true
|
|
|
|
sticky = true
|
|
|
|
}
|
2023-03-02 18:42:33 +00:00
|
|
|
%{~ endif }
|
|
|
|
%{ for host_volume in host_volumes }
|
2022-11-11 21:34:08 +00:00
|
|
|
volume "${host_volume.name}" {
|
|
|
|
type = "host"
|
|
|
|
read_only = ${host_volume.read_only}
|
|
|
|
source = "${host_volume.name}"
|
|
|
|
}
|
2023-03-02 18:42:33 +00:00
|
|
|
%{ endfor }
|
2022-11-11 21:34:08 +00:00
|
|
|
%{ if service_port != null ~}
|
|
|
|
service {
|
|
|
|
name = "${replace(name, "_", "-")}"
|
2023-03-24 18:24:36 +00:00
|
|
|
provider = "nomad"
|
2022-11-11 21:34:08 +00:00
|
|
|
port = "main"
|
|
|
|
|
2023-03-24 18:24:36 +00:00
|
|
|
tags = [
|
|
|
|
# TODO: Rename metrics_port_name to something like "prometheus_scrape"
|
2023-03-02 19:00:30 +00:00
|
|
|
%{ if metrics_port_name != null }
|
2023-03-24 18:24:36 +00:00
|
|
|
"prometheus.scrape",
|
2023-03-02 19:00:30 +00:00
|
|
|
%{ endif }
|
2023-03-02 18:42:33 +00:00
|
|
|
%{ if ingress }
|
2022-11-11 21:34:08 +00:00
|
|
|
"traefik.enable=true",
|
|
|
|
"traefik.http.routers.${name}.entryPoints=websecure",
|
|
|
|
%{ if try(ingress_rule, null) != null ~}
|
|
|
|
"traefik.http.routers.${name}.rule=${ingress_rule}",
|
|
|
|
%{ endif ~}
|
|
|
|
%{ for middleware in ingress_middlewares ~}
|
|
|
|
"traefik.http.routers.${name}.middlewares=${middleware}",
|
2023-03-02 18:42:33 +00:00
|
|
|
%{~ endfor }
|
|
|
|
%{~ endif }
|
2022-11-11 21:34:08 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
%{ endif ~}
|
|
|
|
task "${name}" {
|
|
|
|
driver = "docker"
|
|
|
|
|
2023-01-11 23:40:42 +00:00
|
|
|
%{ if length(meta) > 0 ~}
|
|
|
|
meta = {
|
|
|
|
%{ for k, v in meta ~}
|
|
|
|
${k} = ${jsonencode(v)}
|
|
|
|
%{ endfor ~}
|
|
|
|
}
|
|
|
|
|
|
|
|
%{ endif ~}
|
2022-11-11 21:34:08 +00:00
|
|
|
config {
|
|
|
|
image = "${image}"
|
2023-02-27 19:48:24 +00:00
|
|
|
%{if image_pull_timeout != null ~}
|
|
|
|
image_pull_timeout = "${image_pull_timeout}"
|
|
|
|
%{ endif ~}
|
2022-11-11 21:34:08 +00:00
|
|
|
%{ if service_port != null ~}
|
|
|
|
ports = ["main"]
|
|
|
|
%{ endif ~}
|
|
|
|
%{ if length(try(args, [])) > 0 ~}
|
|
|
|
args = ${jsonencode(args)}
|
|
|
|
%{ endif ~}
|
|
|
|
%{ for template in templates ~}
|
|
|
|
%{ if template.mount && !template.env ~}
|
|
|
|
|
|
|
|
mount {
|
|
|
|
type = "bind"
|
|
|
|
target = "${template.dest}"
|
|
|
|
source = "${template.dest_prefix}/${template.dest}"
|
|
|
|
}
|
|
|
|
%{ endif ~}
|
|
|
|
%{ endfor ~}
|
|
|
|
}
|
|
|
|
%{ if length(env) > 0 ~}
|
|
|
|
|
|
|
|
env = {
|
|
|
|
%{ for k, v in env ~}
|
|
|
|
"${k}" = "${v}"
|
|
|
|
%{ endfor }
|
|
|
|
}
|
|
|
|
%{ endif ~}
|
|
|
|
%{ for volume in host_volumes ~}
|
|
|
|
|
|
|
|
volume_mount {
|
|
|
|
volume = "${volume.name}"
|
|
|
|
destination = "${volume.dest}"
|
|
|
|
read_only = ${volume.read_only}
|
|
|
|
}
|
|
|
|
%{ endfor ~}
|
|
|
|
%{ for template in templates ~}
|
|
|
|
|
|
|
|
template {
|
|
|
|
data = <<EOF
|
|
|
|
${template.data}
|
|
|
|
EOF
|
|
|
|
destination = "${coalesce(template.dest_prefix, "$${NOMAD_TASK_DIR}")}/${template.dest}"
|
|
|
|
%{ if template.left_delimiter != null }left_delimiter = "${template.left_delimiter}"%{ endif }
|
|
|
|
%{ if template.right_delimiter != null }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 ~}
|
|
|
|
%{ if resources != null }
|
|
|
|
|
|
|
|
resources {
|
|
|
|
cpu = ${resources.cpu}
|
|
|
|
memory = ${resources.memory}
|
|
|
|
%{ if resources.memory_max != null }memory_max = ${resources.memory_max}%{ endif }
|
|
|
|
}
|
|
|
|
%{~ endif }
|
|
|
|
}
|
|
|
|
%{ if mysql_bootstrap != null }
|
|
|
|
task "${name}-bootstrap" {
|
|
|
|
driver = "docker"
|
|
|
|
|
|
|
|
lifecycle {
|
|
|
|
hook = "prestart"
|
|
|
|
sidecar = false
|
|
|
|
}
|
|
|
|
|
|
|
|
config {
|
|
|
|
image = "mariadb:10"
|
|
|
|
args = [
|
|
|
|
"/bin/bash",
|
|
|
|
"-c",
|
|
|
|
"/usr/bin/mysql --defaults-extra-file=$${NOMAD_SECRETS_DIR}/my.cnf < $${NOMAD_SECRETS_DIR}/bootstrap.sql",
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
vault {
|
|
|
|
policies = [
|
|
|
|
"access-tables",
|
|
|
|
"nomad-task",
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
template {
|
|
|
|
data = <<EOF
|
|
|
|
[client]
|
2023-03-24 18:24:36 +00:00
|
|
|
{{ range nomadService 1 (env "NOMAD_ALLOC_ID") "mysql-server" -}}
|
|
|
|
host={{ .Address }}
|
|
|
|
port={{ .Port }}
|
|
|
|
{{ end -}}
|
2022-11-11 21:34:08 +00:00
|
|
|
user=root
|
2023-03-24 18:24:36 +00:00
|
|
|
# TODO: Use via lesser scoped access
|
|
|
|
{{ with nomadVar "nomad/jobs" -}}
|
|
|
|
password={{ .mysql_root_password }}
|
2022-11-11 21:34:08 +00:00
|
|
|
{{ end -}}
|
|
|
|
EOF
|
|
|
|
destination = "$${NOMAD_SECRETS_DIR}/my.cnf"
|
|
|
|
}
|
|
|
|
|
|
|
|
template {
|
|
|
|
data = <<EOF
|
2023-03-24 18:24:36 +00:00
|
|
|
{{ with nomadVar "${mysql_bootstrap.vault_key}" -}}
|
|
|
|
CREATE DATABASE IF NOT EXISTS `{{ .${mysql_bootstrap.db_name_key} }}`
|
2022-11-11 21:34:08 +00:00
|
|
|
CHARACTER SET = 'utf8mb4'
|
|
|
|
COLLATE = 'utf8mb4_unicode_ci';
|
2023-03-24 18:24:36 +00:00
|
|
|
CREATE USER IF NOT EXISTS '{{ .${mysql_bootstrap.db_user_key} }}'@'%'
|
|
|
|
IDENTIFIED BY '{{ .${mysql_bootstrap.db_pass_key} }}';
|
|
|
|
GRANT ALL ON `{{ .${mysql_bootstrap.db_name_key} }}`.*
|
|
|
|
TO '{{ .${mysql_bootstrap.db_user_key} }}'@'%';
|
|
|
|
# TODO: Optionally grant ro access to ro user
|
2022-11-11 21:34:08 +00:00
|
|
|
{{ else -}}
|
|
|
|
SELECT 'NOOP';
|
|
|
|
{{ end -}}
|
|
|
|
EOF
|
|
|
|
destination = "$${NOMAD_SECRETS_DIR}/bootstrap.sql"
|
|
|
|
}
|
|
|
|
|
|
|
|
resources {
|
|
|
|
cpu = 50
|
|
|
|
memory = 50
|
|
|
|
}
|
|
|
|
}
|
|
|
|
%{ endif }
|
|
|
|
}
|
|
|
|
}
|