From 1e5bbb94ae8b82de2bb53b2272086ca08f9d14eb Mon Sep 17 00:00:00 2001 From: Matthew Baggett Date: Fri, 28 Jun 2024 15:49:44 +0200 Subject: [PATCH] Non-functional Vigil --- modules/vigil/configuration.tf | 32 +++++++++++++ modules/vigil/inputs.tf | 68 ++++++++++++++++++++++++++ modules/vigil/terraform.tf | 16 +++++++ modules/vigil/vigil.service.toml.tpl | 14 ++++++ modules/vigil/vigil.tf | 55 +++++++++++++++++++++ modules/vigil/vigil.toml.tpl | 71 ++++++++++++++++++++++++++++ printers.tf | 12 ++--- vigil.tf | 25 ++++++++++ 8 files changed, 287 insertions(+), 6 deletions(-) create mode 100644 modules/vigil/configuration.tf create mode 100644 modules/vigil/inputs.tf create mode 100644 modules/vigil/terraform.tf create mode 100644 modules/vigil/vigil.service.toml.tpl create mode 100644 modules/vigil/vigil.tf create mode 100644 modules/vigil/vigil.toml.tpl create mode 100644 vigil.tf diff --git a/modules/vigil/configuration.tf b/modules/vigil/configuration.tf new file mode 100644 index 0000000..0290153 --- /dev/null +++ b/modules/vigil/configuration.tf @@ -0,0 +1,32 @@ +locals { + services_toml = [ + for service_group, services in var.services : templatefile("${path.module}/vigil.service.toml.tpl", { + service_group_id = service_group + service_group_label = service_group + services = services + }) + ] + vigil_toml = templatefile("${path.module}/vigil.toml.tpl", { + manager_token = random_password.token["manager"].result + reporter_token = random_password.token["worker"].result + page_title = var.page_title + page_url = var.page_url + company_name = var.company_name + icon_color = var.icon_color + icon_url = var.icon_url + logo_color = var.logo_color + logo_url = var.logo_url + website_url = var.website_url + support_url = var.support_url + custom_html = var.custom_html + services = local.services_toml + }) + vigil_toml_checksum = md5(local.vigil_toml) +} +resource "scratch_string" "services" { + in = yamlencode(local.services_toml) +} +resource "local_file" "vigil_toml" { + filename = "${path.root}/.debug/vigil.toml" + content = local.vigil_toml +} diff --git a/modules/vigil/inputs.tf b/modules/vigil/inputs.tf new file mode 100644 index 0000000..3e2d27c --- /dev/null +++ b/modules/vigil/inputs.tf @@ -0,0 +1,68 @@ +variable "vigil_version" { + type = string + description = "The version of Vigil to deploy" + default = "v1.26.3" +} +variable "vigil_service_name" { + type = string + description = "The name of the Vigil service" + default = "vigil" +} +variable "services" { + type = map(list(object({ + id = string + label = string + endpoints = list(string) + }))) +} +variable "page_title" { + type = string + description = "The title of the Vigil page" + default = "Vigil" +} +variable "page_url" { + type = string + description = "The URL of the Vigil page" + default = "https://vigil.example.com" +} +variable "company_name" { + type = string + description = "The name of the company" + default = "ExampleCo" +} +variable "icon_color" { + type = string + description = "The color of the icon" + default = "#1972F5" +} +variable "icon_url" { + type = string + description = "The URL of the icon" + default = "https://example.com/icon.png" +} +variable "logo_color" { + type = string + description = "The color of the logo" + default = "#1972F5" +} +variable "logo_url" { + type = string + description = "The URL of the logo" + default = "https://example.com/logo.png" +} +variable "website_url" { + type = string + description = "The URL of the website" + default = "https://example.com" +} +variable "support_url" { + type = string + description = "The URL of the support page" + default = "https://example.com/support" +} +variable "custom_html" { + type = string + description = "Custom HTML to include in the Vigil page" + default = "" +} + diff --git a/modules/vigil/terraform.tf b/modules/vigil/terraform.tf new file mode 100644 index 0000000..2f7817b --- /dev/null +++ b/modules/vigil/terraform.tf @@ -0,0 +1,16 @@ +terraform { + required_providers { + docker = { + source = "kreuzwerker/docker" + version = "~>3.0" + } + random = { + source = "hashicorp/random" + version = "~>3.3" + } + scratch = { + source = "BrendanThompson/scratch" + version = "~> 0.4" + } + } +} diff --git a/modules/vigil/vigil.service.toml.tpl b/modules/vigil/vigil.service.toml.tpl new file mode 100644 index 0000000..1ad08fa --- /dev/null +++ b/modules/vigil/vigil.service.toml.tpl @@ -0,0 +1,14 @@ +[[probe.service]] +id = "${service_group_id}" +label = "${service_group_label}" + +[[probe.service.node]] +id = "${id}" +label = "${label}" +mode = "poll" +reveal_replica_name = true +replicas = [ +%{ for endpoint in endpoints ~} + "${endpoint}", +%{ endfor ~} +] \ No newline at end of file diff --git a/modules/vigil/vigil.tf b/modules/vigil/vigil.tf new file mode 100644 index 0000000..e530acf --- /dev/null +++ b/modules/vigil/vigil.tf @@ -0,0 +1,55 @@ +resource "random_password" "token" { + for_each = toset(["manager", "worker"]) + length = 32 + special = false +} +data "docker_registry_image" "vigil" { + name = "valeriansaliou/vigil:${var.vigil_version}" +} +resource "docker_service" "vigil" { + name = lower(var.vigil_service_name) + task_spec { + container_spec { + image = "${data.docker_registry_image.vigil.name}@${data.docker_registry_image.vigil.sha256_digest}" + healthcheck { + test = ["CMD-SHELL", "wget -q --no-verbose --tries=1 --spider http://localhost:8080/ || exit 1"] + interval = "10s" + timeout = "10s" + retries = 3 + start_period = "1m" + } + configs { + config_id = docker_config.vigil.id + config_name = docker_config.vigil.name + file_name = "/etc/vigil.cfg" + } + } + restart_policy { + condition = "any" + delay = "20s" + window = "0s" + } + } + converge_config { + delay = "5s" + timeout = "2m" + } + update_config { + order = "stop-first" + parallelism = 1 + } +} +resource "random_id" "vigil_iter" { + byte_length = 4 + keepers = { + checksum = local.vigil_toml_checksum + } +} +resource "docker_config" "vigil" { + name = lower(join("-", [var.vigil_service_name, random_id.vigil_iter.hex])) + data = sensitive(base64encode(local.vigil_toml)) // I have marked this as sensitive just so it wont spam the hell out of the terminal with a wall of text. Its not actually sensitive. + lifecycle { + ignore_changes = [name] + create_before_destroy = true + } +} diff --git a/modules/vigil/vigil.toml.tpl b/modules/vigil/vigil.toml.tpl new file mode 100644 index 0000000..0629d4f --- /dev/null +++ b/modules/vigil/vigil.toml.tpl @@ -0,0 +1,71 @@ +# Vigil +# Microservices Status Page +# Configuration file +# Example: https://github.com/valeriansaliou/vigil/blob/master/config.cfg + +[server] +log_level = "debug" +inet = "0.0.0.0:3000" +workers = 4 + +manager_token = "${manager_token}" +reporter_token = "${reporter_token}" + +[assets] +path = "./res/assets/" + +[branding] +page_title = "${page_title}" +page_url = "${page_url}" +company_name = "${company_name}" +icon_color = "${icon_color}" +icon_url = "${icon_url}" +logo_color = "${logo_color}" +logo_url = "${logo_url}" +website_url = "${website_url}" +support_url = "${support_url}" +custom_html = "${custom_html}" + +[metrics] +poll_interval = 15 +poll_retry = 2 +poll_http_status_healthy_above = 200 +poll_http_status_healthy_below = 400 +poll_delay_dead = 10 +poll_delay_sick = 5 +poll_parallelism = 4 +push_delay_dead = 20 +push_system_cpu_sick_above = 0.90 +push_system_ram_sick_above = 0.90 +script_interval = 300 +script_parallelism = 2 +local_delay_dead = 40 + +[plugins] + +[plugins.rabbitmq] +api_url = "http://127.0.0.1:15672" +auth_username = "rabbitmq-administrator" +auth_password = "RABBITMQ_ADMIN_PASSWORD" +virtualhost = "crisp" +queue_ready_healthy_below = 500 +queue_nack_healthy_below = 100 +queue_ready_dead_above = 20000 +queue_nack_dead_above = 5000 +queue_loaded_retry_delay = 500 + +[notify] +startup_notification = false +reminder_interval = 600 +reminder_backoff_function = "linear" +reminder_backoff_limit = 3 + +#[notify.telegram] +#bot_token = "xxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +#chat_id = "xxxxxxxxx" + +[probe] + +%{ for service in services ~} +${service} +%{ endfor ~} \ No newline at end of file diff --git a/printers.tf b/printers.tf index eec8f63..80c423d 100644 --- a/printers.tf +++ b/printers.tf @@ -4,18 +4,18 @@ resource "docker_image" "octoprint" { build { context = "${path.module}/printers" target = "octoprint-mjpg-streamer" - tag = ["ti-octoprint"] + tag = ["ti-octoprint:latest"] } } resource "docker_volume" "ender5plus" { provider = docker.printi - name = "ender5plus_config" + name = "ender5plus_config" } resource "docker_service" "ender5plus" { provider = docker.printi - name = "ender5plus" + name = "ender5plus" task_spec { container_spec { image = docker_image.octoprint.name @@ -34,9 +34,9 @@ resource "docker_service" "ender5plus" { } } restart_policy { - condition = "any" - delay = "20s" - window = "0s" + condition = "any" + delay = "20s" + window = "0s" } } endpoint_spec { diff --git a/vigil.tf b/vigil.tf new file mode 100644 index 0000000..96ea24c --- /dev/null +++ b/vigil.tf @@ -0,0 +1,25 @@ +module "vigil" { + source = "./modules/vigil" + services = { + "printers" = [ + { + id = "prin.ti" + label = "Prin.ti (Print Controller)" + endpoints = [ + "icmp://prin.ti", + "tcp://prin.ti:22", + "http://prin.ti:3000", + ] + }, { + id = "v400" + label = "FLSun V400" + endpoints = [ + "icmp://v400.prin.ti", + "tcp://v400.prin.ti:22", + "http://v400.prin.ti:80", + "http://v400.prin.ti:8080", + ] + } + ] + } +} \ No newline at end of file