diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 6f334a6fa31408111fec1d96fbcc681045bb1017..3f3efef03e0a2010e3c4fdb34276991deeaf5403 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 814a73deaa0214a628c0793e4513a559b8e553bf..94c031d03188266a120f3d87bf135ed7e68c7460 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 0000000000000000000000000000000000000000..78c8145dd1f681c7123e31464d99a7ea6d5644c7 --- /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 0000000000000000000000000000000000000000..cbe19d17b2931ca20717c51512f61b1be2c7242a --- /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 0000000000000000000000000000000000000000..1c13c9ae11eabdb96c8fa539d9caebcd7cbb4cb4 --- /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, + )