Add db bootstrap and more template options to services template

This commit is contained in:
IamTheFij 2022-11-10 10:20:53 -08:00
parent 2bde762902
commit 77c2fcc96b

View File

@ -15,6 +15,7 @@
# change_signal = str, # change_signal = str,
# left_delimiter = str, # left_delimiter = str,
# right_delimiter = str, # right_delimiter = str,
# mount = bool
# ))) # )))
# host_volumes = json(list(dict( # host_volumes = json(list(dict(
# name = str, # name = str,
@ -29,6 +30,15 @@
# mysql = bool # mysql = bool
# redis = bool # redis = bool
# vault = bool # vault = bool
# mysql_bootstrap = json(dict(
# vault_key = str
# db_name = str
# db_name_key = str
# db_user = str
# db_user_key = str
# db_pass = str
# db_pass_key = str
# ))
job "[[.name]]" { job "[[.name]]" {
region = "global" region = "global"
datacenters = ["dc1"] datacenters = ["dc1"]
@ -139,20 +149,22 @@ job "[[.name]]" {
config { config {
image = "[[.image]]" image = "[[.image]]"
[[ if not (empty .service_port) -]] [[ with .service_port -]]
ports = ["main"] ports = ["main"]
[[ end -]] [[ end -]]
[[ if not (empty .args) -]] [[ with .args -]]
args = ["[[ .args | parseJSON | join `", "` ]]"] args = [[ . ]]
[[ end -]] [[ end -]]
[[ with .templates -]] [[ with .templates -]]
[[ range $t := . | parseJSON -]] [[ range $t := . | parseJSON -]]
[[ if and (default true $t.mount) (not (default false $t.env)) -]]
mount { mount {
type = "bind" type = "bind"
target = "[[ $t.dest ]]" target = "[[ $t.dest ]]"
source = "local/[[ $t.dest ]]" source = "[[ default "local/" $t.dest_prefix ]][[ $t.dest ]]"
} }
[[ end -]]
[[ end ]] [[ end ]]
[[ end -]] [[ end -]]
} }
@ -190,7 +202,7 @@ job "[[.name]]" {
data = <<EOF data = <<EOF
[[ $t.data ]] [[ $t.data ]]
EOF EOF
destination = "local/[[ $t.dest ]]" destination = "[[ default "local/" $t.dest_prefix ]][[ $t.dest ]]"
[[ with $t.left_delimiter ]]left_delimiter = "[[ . ]]"[[ end -]] [[ with $t.left_delimiter ]]left_delimiter = "[[ . ]]"[[ end -]]
[[ with $t.right_delimiter ]]right_delimiter = "[[ . ]]"[[ end -]] [[ with $t.right_delimiter ]]right_delimiter = "[[ . ]]"[[ end -]]
[[ with $t.change_mode ]]change_mode = "[[ . ]]"[[ end -]] [[ with $t.change_mode ]]change_mode = "[[ . ]]"[[ end -]]
@ -207,5 +219,67 @@ EOF
} }
[[ end -]] [[ end -]]
} }
[[ with .mysql_bootstrap ]][[ with . | parseJSON -]]
task "[[$.name]]-bootstrap" {
driver = "docker"
lifecycle {
hook = "prestart"
sidecar = false
}
config {
image = "mysql:8"
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]
host={{ env "NOMAD_UPSTREAM_IP_mysql_server" }}
port={{ env "NOMAD_UPSTREAM_PORT_mysql_server" }}
user=root
{{ with secret "kv/data/mysql" -}}
password={{ .Data.data.root_password }}
{{ end -}}
EOF
destination = "${NOMAD_SECRETS_DIR}/my.cnf"
}
template {
data = <<EOF
{{ with secret "[[.vault_key]]" -}}
{{ if .Data.data.[[.db_name_key]] -}}
CREATE DATABASE IF NOT EXISTS `{{ .Data.data.[[.db_name_key]] }}`
CHARACTER SET = 'utf8mb4'
COLLATE = 'utf8mb4_unicode_ci';
CREATE USER IF NOT EXISTS '{{ .Data.data.[[.db_user_key]] }}'@'%'
IDENTIFIED BY '{{ .Data.data.[[.db_pass_key]] }}';
GRANT ALL ON `{{ .Data.data.[[.db_name_key]] }}`.* to '{{ .Data.data.[[.db_user_key]] }}'@'%';
{{ else -}}
SELECT 'NOOP';
{{ end -}}
{{ end -}}
EOF
destination = "${NOMAD_SECRETS_DIR}/bootstrap.sql"
}
resources {
cpu = 50
memory = 50
}
}
[[ end -]][[ end -]]
} }
} }