From 587b6258bb70d53bc22426d5cddc7d0c614b3e98 Mon Sep 17 00:00:00 2001
From: Nemo <commits@captnemo.in>
Date: Mon, 26 Dec 2022 12:47:30 +0530
Subject: [PATCH] Mastodon: Initial Configuration

This is missing some secrets, will commit those later
---
 secrets.tf           |  5 +++++
 mastodon/db.tf       | 18 ++++++++++++++++++
 mastodon/main.tf     | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 mastodon/network.tf  |  5 +++++
 mastodon/provider.tf | 10 ++++++++++
 mastodon/vars.tf     |  3 +++
 6 files changed, 135 insertions(+)

diff --git a/secrets.tf b/secrets.tf
index 90d0932..5bcb3ce 100644
--- a/secrets.tf
+++ a/secrets.tf
@@ -176,3 +176,8 @@
   path = "Nebula/navidrome-spotify-secret"
 }
 
+
+
+data "pass_password" "mastodon-db-password" {
+  path = "Nebula/MASTODON_DB_PASSWORD"
+}
diff --git a/mastodon/db.tf b/mastodon/db.tf
new file mode 100644
index 0000000..0b326e7 100644
--- /dev/null
+++ a/mastodon/db.tf
@@ -1,0 +1,18 @@
+module "mastodon-redis" {
+  name   = "mastodon-redis"
+  source = "../modules/container"
+  image  = "redis:alpine"
+  networks   = ["mastodon"]
+  keep_image = true
+
+  resource = {
+    memory      = 256
+    memory_swap = 256
+  }
+}
+
+module "mastodon-db" {
+  source   = "../modules/postgres"
+  name     = "mastodon"
+  password = var.db-password
+}
diff --git a/mastodon/main.tf b/mastodon/main.tf
new file mode 100644
index 0000000..35e2c86 100644
--- /dev/null
+++ a/mastodon/main.tf
@@ -1,0 +1,94 @@
+module "mastodon-web" {
+  name   = "mastodon-web"
+  source = "../modules/container"
+  image  = "tootsuite/mastodon:v4.0"
+
+  networks = ["mastodon", "traefik", "external", "postgres"]
+
+  env = concat(local.env,[
+    "MAX_THREADS=4",
+    "WEB_CONCURRENCY=5"
+  ])
+
+  command = [
+    "bash",
+    "-c",
+    "rm -f /mastodon/tmp/pids/server.pid; bundle exec rake db:migrate; bundle exec rails s -p 3000"
+  ]
+
+  volumes = [{
+    container_path = "/mastodon/public/system"
+    host_path      = "/mnt/xwing/data/mastodon"
+  }]
+
+  web = {
+    expose = "true"
+    host = "tatooine.club"
+    port = 3000
+  }
+
+  resource = {
+    memory      = 1024
+    memory_swap = 1024
+  }
+}
+
+
+module "mastodon-streaming" {
+  name   = "mastodon-streaming"
+  source = "../modules/container"
+  image  = "tootsuite/mastodon:v4.0"
+  # 24 threads for Streaming
+  env = concat(local.env,[
+    "DB_POOL=8",
+    "STREAMING_CLUSTER_NUM=4"
+  ])
+
+  networks = ["postgres", "external", "mastodon"]
+
+  command = [
+    "node",
+    "./streaming"
+  ]
+
+  web = {
+    expose = "false"
+  }
+
+  resource = {
+    memory      = 1024
+    memory_swap = 1024
+  }
+}
+
+
+module "mastodon-sidekiq" {
+  name   = "mastodon-sidekiq"
+  source = "../modules/container"
+  image  = "tootsuite/mastodon:v4.0"
+  env = concat(local.env,[
+    "DB_POOL=50"
+  ])
+
+   web = {
+    expose = "false"
+  }
+
+  networks = ["postgres", "external", "mastodon"]
+
+  command = [
+    "bundle",
+    "exec",
+    "sidekiq"
+  ]
+
+  volumes =  [{
+      container_path = "/mastodon/public/system"
+      host_path      = "/mnt/xwing/data/mastodon"
+    }]
+
+  resource = {
+    memory      = 1024
+    memory_swap = 1024
+  }
+}
diff --git a/mastodon/network.tf b/mastodon/network.tf
new file mode 100644
index 0000000..be7dc01 100644
--- /dev/null
+++ a/mastodon/network.tf
@@ -1,0 +1,5 @@
+resource "docker_network" "mastodon" {
+  name   = "mastodon"
+  driver = "bridge"
+  internal = true
+}diff --git a/mastodon/provider.tf b/mastodon/provider.tf
new file mode 100644
index 0000000..b924f6c 100644
--- /dev/null
+++ a/mastodon/provider.tf
@@ -1,0 +1,10 @@
+terraform {
+  required_providers {
+    postgresql = {
+      source = "cyrilgdn/postgresql"
+    }
+    docker = {
+      source = "kreuzwerker/docker"
+    }
+  }
+}
diff --git a/mastodon/vars.tf b/mastodon/vars.tf
new file mode 100644
index 0000000..f47eb44 100644
--- /dev/null
+++ a/mastodon/vars.tf
@@ -1,0 +1,3 @@
+variable "db-password" {
+	type = string
+}
--
rgit 0.1.5