Skip to content

Commit

Permalink
refactoring of code
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Buchleitner <[email protected]>
  • Loading branch information
mabunixda committed Oct 23, 2024
1 parent 88010e8 commit 15f5574
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 18 deletions.
4 changes: 3 additions & 1 deletion nomad/app_dynamic.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ job "dynamic-app" {
Port = {{ .Port }}
{{end}}
Database = my_app
{{ with secret "dynamic-app/kv/database" }}
Database = {{ .Data.database }}
{{ end }}
{{ with secret "dynamic-app/db/creds/app" }}
User = {{ .Data.username }}
Password = {{ .Data.password }}
Expand Down
6 changes: 3 additions & 3 deletions nomad/app_hardcoded.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ job "dynamic-app" {
Port = {{ .Port }}
{{end}}
Database = my_app
User = root
Password = super-duper-password
Database = app
User = app
Password = my-app-super-password
EOF
}
resources {
Expand Down
2 changes: 1 addition & 1 deletion nomad/app_static.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ job "dynamic-app" {
Port = {{ .Port }}
{{end}}
Database = my_app
{{ with secret "dynamic-app/kv/database" }}
Database = {{ .Data.database }}
User = {{ .Data.username }}
Password = {{ .Data.password }}
{{ end }}
Expand Down
4 changes: 3 additions & 1 deletion nomad/app_transit_connect.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ job "dynamic-app" {
Address = 127.0.0.1
Port = 3306
Database = my_app
{{ with secret "dynamic-app/kv/database" }}
Database = {{ .Data.database }}
{{ end }}
{{ with secret "dynamic-app/db/creds/app" }}
User = {{ .Data.username }}
Password = {{ .Data.password }}
Expand Down
11 changes: 5 additions & 6 deletions nomad/app_transit_connect_traefik.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ job "dynamic-app" {

network {
mode = "bridge"
// port "web" {
// to = 8080
// }
}

vault {
Expand All @@ -32,8 +29,8 @@ job "dynamic-app" {
port = "8080"
tags = ["traefik.enable=true",
"traefik.http.routers.dynamic-app.rule=Host(`dynamic-app.127.0.0.1.nip.io`)",
"traefik.http.routers.dynamic-app.entrypoints=http",
"traefik.http.routers.dynamic-app.tls=false",
"traefik.http.routers.dynamic-app.entrypoints=https",
"traefik.http.routers.dynamic-app.tls=true",
"traefik.connsulcatalog.connect=true"
]
connect {
Expand Down Expand Up @@ -78,7 +75,9 @@ job "dynamic-app" {
Address = 127.0.0.1
Port = 3306
Database = my_app
{{ with secret "dynamic-app/kv/database" }}
Database = {{ .Data.database }}
{{ end }}
{{ with secret "dynamic-app/db/creds/app" }}
User = {{ .Data.username }}
Password = {{ .Data.password }}
Expand Down
6 changes: 6 additions & 0 deletions nomad/mysql.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ job "mysql-server" {

env = {
"MYSQL_ROOT_PASSWORD" = "super-duper-password"
"MYSQL_DATABASE" = "app"
"MYSQL_USER" = "app"
"MYSQL_PASSWORD" = "my-app-super-password"
}

config {
image = "mysql:9"
ports = ["db"]
volumes = [
"/srv/mysql/:/var/lib/mysql"
]
}

resources {
Expand Down
31 changes: 26 additions & 5 deletions nomad/mysql_connect.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ job "mysql-server" {
}
}

vault {
policies = ["nomad-dynamic-app", "nomad-mysql"]
change_mode = "signal"
change_signal = "SIGINT"
}

restart {
attempts = 10
interval = "5m"
Expand All @@ -23,16 +29,31 @@ job "mysql-server" {
task "mysql-server" {
driver = "docker"

env = {
"MYSQL_ROOT_PASSWORD" = "super-duper-password"
}

config {
image = "mysql:9"

ports = ["db"]
volumes = [
"/srv/mysql/:/var/lib/mysql"
]
}

template {
env = true
destination = "secrets/.envs"
change_mode = "noop"
env = true
data = <<EOF
{{ with secret "dynamic-app/kv/database" }}
MYSQL_DATABASE = "{{ .Data.database }}"
MYSQL_USER = "{{ .Data.username }}"
MYSQL_PASSWORD = "{{ .Data.password }}"
{{ end }}
{{ with secret "dynamic-app/kv/database_root" }}
MYSQL_ROOT_PASSWORD = "{{ .Data.password }}"
{{ end }}
EOF
}
resources {
cpu = 500
memory = 500
Expand Down
66 changes: 66 additions & 0 deletions nomad/mysql_static.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
job "mysql-server" {
datacenters = ["dc1"]
type = "service"

group "mysql-server" {
count = 1

vault {
policies = ["nomad-dynamic-app", "nomad-mysql"]
change_mode = "signal"
change_signal = "SIGINT"
}

service {
name = "mysql-server"
port = "db"
}

restart {
attempts = 10
interval = "5m"
delay = "25s"
mode = "delay"
}

task "mysql-server" {
driver = "docker"

config {
image = "mysql:9"
ports = ["db"]
volumes = [
"/srv/mysql/:/var/lib/mysql"
]
}
template {
env = true
destination = "secrets/.envs"
change_mode = "noop"
env = true
data = <<EOF
{{ with secret "dynamic-app/kv/database" }}
MYSQL_DATABASE = "{{ .Data.database }}"
MYSQL_USER = "{{ .Data.username }}"
MYSQL_PASSWORD = "{{ .Data.password }}"
{{ end }}
{{ with secret "dynamic-app/kv/database_root" }}
MYSQL_ROOT_PASSWORD = "{{ .Data.password }}"
{{ end }}
EOF
}
resources {
cpu = 500
memory = 500
}
}
network {
mode = "bridge"
port "db" {
static = 3306
to = 3306
}
}
}
}
10 changes: 9 additions & 1 deletion setup/vault_kv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ fi
if [ "$(vault secrets list | jq ' . | keys' | grep "$VAULT_MOUNT" | wc -l | tr -d ' ')" -eq 0 ]; then
vault secrets enable -path=$VAULT_MOUNT kv
fi
vault kv put $VAULT_MOUNT/database username="root" password="super-duper-password"
vault kv put $VAULT_MOUNT/database username="app" password="my-app-super-password" database="app"
vault kv put $VAULT_MOUNT/database_root username="root" password="super-duper-password"

if [ -n "$(vault policy list | grep nomad-dynamic-app)" ]; then
exit 0
Expand All @@ -32,3 +33,10 @@ path \"$VAULT_MOUNT/database\" {
capabilities = [ \"read\" ]
}
" | vault policy write nomad-dynamic-app -


echo "
path \"$VAULT_MOUNT/database_root\" {
capabilities = [ \"read\" ]
}
" | vault policy write nomad-mysql -

0 comments on commit 15f5574

Please sign in to comment.