From c57b4b2c72e288a5e6af54c8913c5a54f1d87e60 Mon Sep 17 00:00:00 2001
From: Nemo <me@captnemo.in>
Date: Sat, 13 Oct 2018 20:55:20 +0530
Subject: [PATCH] WIP: Image refactor

- docker_container cannot take upload as a parameter, leaving
  no choice but to use docker_container like a savage.
- This will take some backward refactors.
---
 lychee.tf                 |  7 +++----
 rss-bridge.tf             | 10 +++++-----
 media/airsonic.tf         | 17 +++++++++--------
 radicale/main.tf          | 35 +++++++++++++++++++++++++++--------
 modules/container/main.tf | 10 ++--------
 modules/container/vars.tf |  5 -----
 modules/image/main.tf     | 16 ++++++++++++++++
 7 files changed, 54 insertions(+), 46 deletions(-)

diff --git a/lychee.tf b/lychee.tf
index 46d6fe6..b97c530 100644
--- a/lychee.tf
+++ a/lychee.tf
@@ -13,10 +13,9 @@
     },
   ]
 
-  uploads = [{
-    content = "${file("${path.module}/docker/conf/lychee.php.ini")}"
-    file    = "/config/lychee/user.ini"
-  }]
+  files = "/config/lychee/user.ini"
+
+  contents = ["${file("${path.module}/docker/conf/lychee.php.ini")}"]
 
   web {
     expose = true
diff --git a/rss-bridge.tf b/rss-bridge.tf
index b70bbad..5563383 100644
--- a/rss-bridge.tf
+++ a/rss-bridge.tf
@@ -9,9 +9,10 @@
   }
 
   networks = "${list(module.docker.traefik-network-id)}"
+  files    = ["/app/public/whitelist.txt"]
 
-  uploads = [{
-    content = <<EOF
+  contents = [
+    <<EOF
 AmazonBridge
 BandcampBridge
 ContainerLinuxReleasesBridge
@@ -29,7 +30,6 @@
 StripeAPIChangeLogBridge
 AmazonPriceTrackerBridge
 EOF
-
-    file = "/app/public/whitelist.txt"
-  }]
+    ,
+  ]
 }
diff --git a/media/airsonic.tf b/media/airsonic.tf
index 5fadf62..ae49f76 100644
--- a/media/airsonic.tf
+++ a/media/airsonic.tf
@@ -22,15 +22,14 @@
     "JAVA_OPTS=-Xmx512m -Dserver.use-forward-headers=true -Dserver.context-path=/",
   ]
 
-  uploads = [
-    {
-      file    = "/usr/lib/jvm/java-1.8-openjdk/jre/lib/airsonic.properties"
-      content = "${data.template_file.airsonic-properties-file.rendered}"
-    },
-    {
-      file    = "/usr/lib/jvm/java-1.8-openjdk/jre/lib/sound.properties"
-      content = "${file("${path.module}/conf/airsonic.sound.properties")}"
-    },
+  files = [
+    "/usr/lib/jvm/java-1.8-openjdk/jre/lib/airsonic.properties",
+    "/usr/lib/jvm/java-1.8-openjdk/jre/lib/sound.properties",
+  ]
+
+  contents = [
+    "${data.template_file.airsonic-properties-file.rendered}",
+    "${file("${path.module}/conf/airsonic.sound.properties")}",
   ]
 
   volumes = [
diff --git a/radicale/main.tf b/radicale/main.tf
index 65f5466..cd54c1b 100644
--- a/radicale/main.tf
+++ a/radicale/main.tf
@@ -1,7 +1,12 @@
+module "image" {
+  source = "../modules/image"
+  image  = "tomsquest/docker-radicale:latest"
+}
+
 module "container" {
   name   = "radicale"
   source = "../modules/container"
-  image  = "tomsquest/docker-radicale:latest"
+  image  = "${module.image.image}"
 
   web {
     expose = true
@@ -20,18 +25,18 @@
     },
   ]
 
-  uploads = [
-    {
-      content = "${file("${path.module}/config")}"
-      file    = "/config/config"
-    },
-    {
-      content = "${file("${path.module}/logging.conf")}"
-      file    = "/config/logging"
-    },
-    {
-      content = "${file("${path.module}/users")}"
-      file    = "/config/users"
-    },
-  ]
+  # uploads = [
+  #   {
+  #     content = "${file("${path.module}/config")}"
+  #     file    = "/config/config"
+  #   },
+  #   {
+  #     content = "${file("${path.module}/logging.conf")}"
+  #     file    = "/config/logging"
+  #   },
+  #   {
+  #     content = "${file("${path.module}/users")}"
+  #     file    = "/config/users"
+  #   },
+  # ]
 }
diff --git a/modules/container/main.tf b/modules/container/main.tf
index dcb2320..ca92672 100644
--- a/modules/container/main.tf
+++ a/modules/container/main.tf
@@ -23,17 +23,11 @@
 
   // Only attach the traefik network if
   // service is exposed to the web
-  networks = ["${concat(var.networks, split(",",
-    lookup(var.web, "expose", "false") == "false" ?
-    "" :
-    "${data.docker_network.traefik.id}"
-  ))}"]
+  networks = ["${concat(var.networks,split(",",lookup(var.web, "expose", "false") == "false" ? "" :"${data.docker_network.traefik.id}"))}"]
 
   memory = "${local.resource["memory"]}"
 
-  volumes = "${var.volumes}"
-
-  upload = "${var.uploads}"
+  volumes = ["${var.volumes}"]
 
   # Look at this monstrosity
   # And then https://github.com/hashicorp/terraform/issues/12453#issuecomment-365569618
diff --git a/modules/container/vars.tf b/modules/container/vars.tf
index d9e5857..0540ab0 100644
--- a/modules/container/vars.tf
+++ a/modules/container/vars.tf
@@ -88,8 +88,3 @@
   type        = "list"
   default     = []
 }
-
-variable "uploads" {
-  description = "uploads"
-  default     = []
-}
diff --git a/modules/image/main.tf b/modules/image/main.tf
new file mode 100644
index 0000000..68eb353 100644
--- /dev/null
+++ a/modules/image/main.tf
@@ -1,0 +1,16 @@
+variable "image" {
+  description = "image to use"
+}
+
+data "docker_registry_image" "image" {
+  name = "${var.image}"
+}
+
+resource "docker_image" "image" {
+  name          = "${data.docker_registry_image.image.name}"
+  pull_triggers = ["${data.docker_registry_image.image.sha256_digest}"]
+}
+
+output "image" {
+  value = "${docker_image.image.latest}"
+}
--
rgit 0.1.5