From c9675b7d95abb8f5a18f7544fb39c4e326a8eec6 Mon Sep 17 00:00:00 2001 From: lens0021 Date: Tue, 1 Oct 2024 11:55:46 +0900 Subject: [PATCH] Use local.main instead of !var.test --- jobs/fastcgi.nomad | 92 ++++++++++++++++++++++---------------------- jobs/http.nomad | 23 ++++++----- jobs/memcached.nomad | 6 ++- jobs/mysql.nomad | 10 +++-- 4 files changed, 71 insertions(+), 60 deletions(-) diff --git a/jobs/fastcgi.nomad b/jobs/fastcgi.nomad index 3d267989..86f044f8 100644 --- a/jobs/fastcgi.nomad +++ b/jobs/fastcgi.nomad @@ -14,6 +14,10 @@ variable "mysql_password_mediawiki" { default = "" } +locals { + main = !var.test +} + job "fastcgi" { datacenters = ["dc1"] @@ -33,8 +37,8 @@ job "fastcgi" { "-c", format( "while ! ncat --send-only %s %s < /dev/null; do sleep 1; done", - var.test ? NOMAD_UPSTREAM_IP_mysql : "127.0.0.1", - var.test ? NOMAD_UPSTREAM_PORT_mysql : "3306" + local.main ? "127.0.0.1" : NOMAD_UPSTREAM_IP_mysql, + local.main ? "3306" : NOMAD_UPSTREAM_PORT_mysql ), ] } @@ -52,8 +56,8 @@ job "fastcgi" { args = [ "-c", format("while ! ncat --send-only %s %s < /dev/null; do sleep 1; done", - var.test ? NOMAD_UPSTREAM_IP_memcached : "127.0.0.1", - var.test ? NOMAD_UPSTREAM_PORT_memcached : "11211" + local.main ? "127.0.0.1" : NOMAD_UPSTREAM_IP_memcached, + local.main ? "11211" : NOMAD_UPSTREAM_PORT_memcached ), ] } @@ -105,7 +109,7 @@ job "fastcgi" { } template { - data = var.test ? var.hotfix_test : var.hotfix + data = local.main ? var.hotfix : var.hotfix_test destination = "local/Hotfix.php" change_mode = "noop" } @@ -154,7 +158,7 @@ job "fastcgi" { ] cpu_hard_limit = true - network_mode = var.test ? "bridge" : "host" + network_mode = local.main ? "host" : "bridge" } resources { @@ -164,71 +168,67 @@ job "fastcgi" { } dynamic "env" { - for_each = !var.test ? [] : [{}] - content { - MEDIAWIKI_SKIP_INSTALL = var.test ? "0" : "1" - MEDIAWIKI_SKIP_IMPORT_SITES = "1" - MEDIAWIKI_SKIP_UPDATE = var.test ? "0" : "1" - } - } - - dynamic "env" { - for_each = var.test ? [] : [{}] + for_each = local.main ? [{}] : [] content { NOMAD_UPSTREAM_ADDR_http = "127.0.0.1:80" NOMAD_UPSTREAM_ADDR_mysql = "127.0.0.1:3306" NOMAD_UPSTREAM_ADDR_memcached = "127.0.0.1:11211" - MEDIAWIKI_SKIP_INSTALL = var.test ? "0" : "1" + MEDIAWIKI_SKIP_INSTALL = local.main ? "1" : "0" MEDIAWIKI_SKIP_IMPORT_SITES = "1" - MEDIAWIKI_SKIP_UPDATE = var.test ? "0" : "1" + MEDIAWIKI_SKIP_UPDATE = local.main ? "1" : "0" } } - } - dynamic "network" { - for_each = var.test ? [{}] : [] - content { - mode = "bridge" + dynamic "env" { + for_each = var.test ? [{}] : [] + content { + MEDIAWIKI_SKIP_INSTALL = local.main ? "1" : "0" + MEDIAWIKI_SKIP_IMPORT_SITES = "1" + MEDIAWIKI_SKIP_UPDATE = local.main ? "1" : "0" + } } } dynamic "service" { - for_each = !var.test ? [] : [{}] + for_each = var.test ? [{}] : [] content { name = "fastcgi" port = "9000" - dynamic "connect" { - for_each = !var.test ? [] : [{}] - - content { - sidecar_service { - proxy { - upstreams { - destination_name = "mysql" - local_bind_port = 3306 - } - - upstreams { - destination_name = "memcached" - local_bind_port = 11211 - } + connect { + sidecar_service { + proxy { + upstreams { + destination_name = "mysql" + local_bind_port = 3306 } - } - sidecar_task { - config { - memory_hard_limit = 300 - } - resources { - memory = 20 + upstreams { + destination_name = "memcached" + local_bind_port = 11211 } } } + + sidecar_task { + config { + memory_hard_limit = 300 + } + resources { + memory = 20 + } + } } } } + + dynamic "network" { + for_each = var.test ? [{}] : [] + content { + mode = "bridge" + } + } } reschedule { diff --git a/jobs/http.nomad b/jobs/http.nomad index de8c58c9..aa92e70b 100644 --- a/jobs/http.nomad +++ b/jobs/http.nomad @@ -4,6 +4,10 @@ variable "test" { default = false } +locals { + main = !var.test +} + job "http" { datacenters = ["dc1"] @@ -24,17 +28,8 @@ job "http" { destination = "/etc/caddycerts" read_only = false } - - dynamic "template" { - for_each = !var.test ? [] : [{}] - - content { - data = var.caddyfile_for_test - destination = "local.Caddyfile" - } - } dynamic "artifact" { - for_each = var.test ? [] : [{}] + for_each = local.main ? [{}] : [] content { source = "https://github.com/femiwiki/nomad/raw/main/caddy/Caddyfile" @@ -44,6 +39,14 @@ job "http" { options { checksum = "md5:ee0300e384afa6aca74f09a44323ee6e" } } } + dynamic "template" { + for_each = var.test ? [{}] : [] + + content { + data = var.caddyfile_for_test + destination = "local.Caddyfile" + } + } artifact { source = "https://github.com/femiwiki/nomad/raw/main/res/robots.txt" diff --git a/jobs/memcached.nomad b/jobs/memcached.nomad index 162d7a4b..4a2d5f72 100644 --- a/jobs/memcached.nomad +++ b/jobs/memcached.nomad @@ -4,6 +4,10 @@ variable "test" { default = false } +locals { + main = !var.test +} + job "memcached" { datacenters = ["dc1"] @@ -33,7 +37,7 @@ job "memcached" { } dynamic "service" { - for_each = var.test ? [] : [{}] + for_each = local.main ? [{}] : [] content { provider = "nomad" name = "memcached" diff --git a/jobs/mysql.nomad b/jobs/mysql.nomad index 829c21b0..63cb2f5d 100644 --- a/jobs/mysql.nomad +++ b/jobs/mysql.nomad @@ -4,12 +4,16 @@ variable "test" { default = false } +locals { + main = !var.test +} + job "mysql" { datacenters = ["dc1"] group "mysql" { dynamic "volume" { - for_each = var.test ? [] : [{}] + for_each = local.main ? [{}] : [] labels = ["mysql"] content { @@ -25,7 +29,7 @@ job "mysql" { driver = "docker" dynamic "volume_mount" { - for_each = var.test ? [] : [{}] + for_each = local.main ? [{}] : [] content { volume = "mysql" @@ -60,7 +64,7 @@ job "mysql" { network { mode = "bridge" dynamic "port" { - for_each = var.test ? [] : [{}] + for_each = local.main ? [{}] : [] labels = ["network"] content {