From dd0ba9517046e35d64261f6ad72fd0aa5dd2c5a9 Mon Sep 17 00:00:00 2001
From: David Hoese <david.hoese@ssec.wisc.edu>
Date: Mon, 6 Jul 2020 16:02:30 -0500
Subject: [PATCH] Move rabbit init image building to deploy repository

---
 .gitlab-ci.yml                                | 41 +++++++++++++++++++
 helpers/{build_image.yml => build_image.yaml} |  4 +-
 sidecars/cspp-geo-rabbit-init/Dockerfile      | 12 ++++++
 sidecars/cspp-geo-rabbit-init/README.md       | 31 ++++++++++++++
 .../cspp-geo-rabbit-init/declare_exchange.py  | 24 +++++++++++
 5 files changed, 110 insertions(+), 2 deletions(-)
 rename helpers/{build_image.yml => build_image.yaml} (92%)
 create mode 100644 sidecars/cspp-geo-rabbit-init/Dockerfile
 create mode 100644 sidecars/cspp-geo-rabbit-init/README.md
 create mode 100644 sidecars/cspp-geo-rabbit-init/declare_exchange.py

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 6f334a6..3f3efef 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -15,6 +15,9 @@ variables:
   CICHART_IMAGE: "$CI_REGISTRY_IMAGE/cichart:latest"
 
 
+include:
+  - file: /helpers/build_image.yaml
+
 .docker_based_job:
   image: docker:19.03.1
   tags:
@@ -52,6 +55,20 @@ build ci:
     - if: $BUILD_CI_IMAGE
       when: always
 
+build sidecar rabbit init:
+  stage: .pre
+  extends: .build_image
+  variables:
+    IMAGE_NAME: cspp-geo-rabbit-init
+    IMAGE_DIR: sidecars/cspp-geo-rabbit-init
+  rules:
+    - changes:
+        - sidecars/cspp-geo-rabbit-init/Dockerfile
+        - sidecars/cspp-geo-rabbit-init/declare_exchange.py
+      when: always
+    - if: $BUILD_SIDECARS
+      when: always
+
 
 .get_chart_tmpl:
   extends: .helm_based_job
@@ -93,6 +110,30 @@ get_chart_grb:
     SUBCOMP_REPOS: "geosphere-grb"
     SUBCOMP_CHART_DIR: "cspp-geo-grb"
 
+
+deploy_prod_rabbit:
+  environment:
+    name: production
+    url: http://geosphere.ssec.wisc.edu
+  extends: .helm_based_job
+  stage: deploy rabbit
+  script:
+    - if [ -n "$CI_COMMIT_TAG" ]; then
+        ns="geosphere";
+      else
+        ns="geosphere-test";
+      fi
+    # copy secret kubeconfig to the mounted (pwd) directory
+    - cp $kubekorner_k3s_config .
+    - kubeconfig=$(basename $kubekorner_k3s_config)
+    # install third-party rabbitmq server
+    - helm repo add bitnami https://charts.bitnami.com/bitnami
+    - helm install --namespace $ns geosphere-rabbit bitnami/rabbitmq
+  rules:
+    - if: '$kubekorner_k3s_config == ""'
+      when: never
+    - when: on_success
+
 deploy_g16_grb:
   environment:
     name: production
diff --git a/helpers/build_image.yml b/helpers/build_image.yaml
similarity index 92%
rename from helpers/build_image.yml
rename to helpers/build_image.yaml
index 814a73d..94c031d 100644
--- a/helpers/build_image.yml
+++ b/helpers/build_image.yaml
@@ -24,9 +24,9 @@
   script:
     - image_url="${CI_REGISTRY_IMAGE}/${IMAGE_NAME}"
     - if [ -z "$CI_COMMIT_TAG" ]; then
-      docker_tag=$CI_COMMIT_SHORT_SHA;
+        docker_tag=$CI_COMMIT_SHORT_SHA;
       else
-      docker_tag=$CI_COMMIT_TAG;
+        docker_tag=$CI_COMMIT_TAG;
       fi;
     - echo $docker_tag
     - docker pull ${image_url}:latest || true
diff --git a/sidecars/cspp-geo-rabbit-init/Dockerfile b/sidecars/cspp-geo-rabbit-init/Dockerfile
new file mode 100644
index 0000000..78c8145
--- /dev/null
+++ b/sidecars/cspp-geo-rabbit-init/Dockerfile
@@ -0,0 +1,12 @@
+FROM python:3-alpine as base
+
+FROM base as builder
+
+RUN pip install --prefix=/install pika
+
+FROM base
+
+COPY --from=builder /install /usr/local
+COPY declare_exchange.py .
+
+CMD ["python", "declare_exchange.py"]
diff --git a/sidecars/cspp-geo-rabbit-init/README.md b/sidecars/cspp-geo-rabbit-init/README.md
new file mode 100644
index 0000000..cbe19d1
--- /dev/null
+++ b/sidecars/cspp-geo-rabbit-init/README.md
@@ -0,0 +1,31 @@
+# RabbitMQ Initialization
+
+This container is meant to declare a RabbitMQ exchange on a separate
+RabbitMQ container. This is meant to be used as a Kubernetes pre-install
+hook for the various helm charts that will access the RabbitMQ server.
+
+## Build
+
+```bash
+docker build -t gitlab.ssec.wisc.edu:5555/cspp_geo/geosphere/geosphere-deploy/cspp-geo-rabbit-init:latest cspp_geo_rabbit_init/
+```
+
+## Usage
+
+This container should run and after sending a couple messages to the RabbitMQ
+server it should return.
+
+```bash
+docker run --rm gitlab.ssec.wisc.edu:5555/cspp_geo/geosphere/geosphere-deploy/cspp-geo-rabbit-init:latest
+```
+
+### Environment Variables
+
+* **RABBITMQ_HOST**: Hostname for the remote RabbitMQ server. Required.
+* **RABBITMQ_EXCHANGE_NAME**: Name of the exchange. Required.
+* **RABBITMQ_USER**: Username to connect to the RabbitMQ server.
+  Default: "guest"
+* **RABBITMQ_PASSWORD**: Password to connect to the RabbitMQ server.
+  Default: "guest"
+* **RABBITMQ_EXCHANGE_TYPE**: Type of exchange to declare.
+  Default: "topic"
\ No newline at end of file
diff --git a/sidecars/cspp-geo-rabbit-init/declare_exchange.py b/sidecars/cspp-geo-rabbit-init/declare_exchange.py
new file mode 100644
index 0000000..1c13c9a
--- /dev/null
+++ b/sidecars/cspp-geo-rabbit-init/declare_exchange.py
@@ -0,0 +1,24 @@
+#!/usr/bin/env python3
+
+import os
+import pika
+
+if __name__ == "__main__":
+    user = os.getenv("RABBITMQ_USERNAME", "guest")
+    password = os.getenv("RABBITMQ_PASSWORD", "guest")
+    rabbitmq_host = os.getenv("RABBITMQ_HOST")
+    exchange = os.getenv("RABBITMQ_EXCHANGE_NAME")
+    exchange_type = os.getenv("RABBITMQ_EXCHANGE_TYPE", "topic")
+
+    credentials = pika.PlainCredentials(user, password)
+    conn_params = pika.ConnectionParameters(host=rabbitmq_host,
+                                            credentials=credentials)
+    conn = pika.BlockingConnection(conn_params)
+    ch = conn.channel()
+    ch.exchange_declare(
+        exchange=exchange,
+        exchange_type=exchange_type,
+        durable=True,
+        auto_delete=False,
+        internal=False,
+    )
-- 
GitLab