From 476968d4b167b67a575bcbf4db518134a892b6e6 Mon Sep 17 00:00:00 2001 From: Duck Nebuchadnezzar Date: Sat, 17 Dec 2022 19:46:53 -0500 Subject: [PATCH] Add docker support --- .circleci/config.yml | 13 ++++ .envrc-example | 13 ++++ .gitignore | 6 +- Earthfile | 96 ++++++++++++++++++++++++++ Makefile | 8 +-- bootstrap.sh | 7 ++ devspace.yaml | 86 +++++++++++++++++++++++ runbook.org | 158 +++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 382 insertions(+), 5 deletions(-) create mode 100644 .circleci/config.yml create mode 100644 .envrc-example create mode 100644 Earthfile create mode 100755 bootstrap.sh create mode 100644 devspace.yaml create mode 100644 runbook.org diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..4be6871 --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,13 @@ +# .circleci/config.yml + +version: 2.1 +jobs: + build: + machine: + image: ubuntu-2004:202111-02 + steps: + - checkout + - run: docker login --username "$DOCKER_USER" --password "$DOCKER_PASS" + - run: "sudo /bin/sh -c 'wget https://github.com/earthly/earthly/releases/latest/download/earthly-linux-amd64 -O /usr/local/bin/earthly && chmod +x /usr/local/bin/earthly'" + - run: earthly --version + - run: earthly -P --remote-cache=duck1123/me.untethr.nostr-relay:cache --push +ci --RELAY_VERSION=0.2.4-SNAPSHOT diff --git a/.envrc-example b/.envrc-example new file mode 100644 index 0000000..cc98a0d --- /dev/null +++ b/.envrc-example @@ -0,0 +1,13 @@ +# You shouldn't need to change these +# export REPOSITORY_HOST=k3d-myregistry.localhost:12345 +# export REPO=duck1123 +# export PROJECT=me.untethr.nostr-relay + +# TODO: Read this from the official source +export RELAY_VERSION=0.2.3-SNAPSHOT + +# Set these to your values +export RELAY_HOST=relay.localhost +export RELAY_CONTACT=someuser@example.com +export RELAY_ADMIN_USERNAME=someuser +export RELAY_ADMIN_PUBKEY= diff --git a/.gitignore b/.gitignore index dbf12cf..8bc1918 100644 --- a/.gitignore +++ b/.gitignore @@ -40,5 +40,9 @@ n.db* n-test.db* .cpcache +.devspace dump -scratch \ No newline at end of file +scratch + +# User-specific configuration +.envrc diff --git a/Earthfile b/Earthfile new file mode 100644 index 0000000..cbbd512 --- /dev/null +++ b/Earthfile @@ -0,0 +1,96 @@ +# Earthfile +VERSION 0.6 +ARG REPO=duck1123 +ARG PROJECT=me.untethr.nostr-relay +ARG TAG=latest +ARG RELAY_VERSION=0.2.3-SNAPSHOT + +ARG BUILD_IMAGE=circleci/clojure:tools-deps +ARG BUILD_IMAGE_USER=circleci +ARG USER_HOME=/home/${BUILD_IMAGE_USER} +ARG uid=3434 +ARG gid=3434 + +IMPORT_JAR_DEPS: + COMMAND + # Store the files under the user + COPY --dir \ + --chown=${BUILD_IMAGE_USER} \ + +jar-deps/.clojure \ + +jar-deps/.deps.clj \ + +jar-deps/.gitlibs \ + +jar-deps/.m2 \ + ${USER_HOME} + # Store the files under root + # COPY --dir \ + # --chown=root \ + # +jar-deps/.clojure \ + # +jar-deps/.gitlibs \ + # +jar-deps/.m2 \ + # /root + COPY --dir --chown=${BUILD_IMAGE_USER} +jar-deps/.cpcache . + +# A base image others can be built off of +builder-base: + FROM ${BUILD_IMAGE} + WORKDIR /app + COPY deps.edn Makefile . + +jar-deps: + FROM +builder-base + RUN whoami + USER root + RUN rm -rf ${USER_HOME}/.m2 + RUN --mount=type=cache,target=/root/.clojure \ + --mount=type=cache,target=/root/.deps.clj \ + --mount=type=cache,target=/root/.gitlibs \ + --mount=type=cache,target=/root/.m2 \ + ( \ + clojure -Stree \ + && clojure -A:uberdeps -Stree \ + && clojure -A:test -Stree \ + && clojure -X:test \ + ) \ + && cp -r /root/.clojure ${USER_HOME}/ \ + && cp -r /root/.deps.clj ${USER_HOME}/ \ + && cp -r /root/.gitlibs ${USER_HOME}/ \ + && cp -r /root/.m2 ${USER_HOME}/ + USER ${uid} + SAVE ARTIFACT ${USER_HOME}/.clojure + SAVE ARTIFACT ${USER_HOME}/.deps.clj + SAVE ARTIFACT ${USER_HOME}/.gitlibs + SAVE ARTIFACT ${USER_HOME}/.m2 + SAVE ARTIFACT .cpcache + +build-src: + FROM +builder-base + DO +IMPORT_JAR_DEPS + COPY --dir conf src . + +jar: + FROM +build-src + ARG RELAY_VERSION=${RELAY_VERSION} + RUN echo "(ns me.untethr.nostr.version) (def version \"${RELAY_VERSION}\")" > src/me/untethr/nostr/version.clj + RUN make uberjar + SAVE ARTIFACT target/app.jar me.untethr.nostr-relay.jar + +image: + FROM amazoncorretto:19-alpine + WORKDIR /app + ARG EXPECTED_REF=${REPO}/${PROJECT}:${TAG} + COPY \ + ( +jar/me.untethr.nostr-relay.jar --RELAY_VERSION ${RELAY_VERSION} ) \ + . + COPY bootstrap.sh . + COPY conf/logback.xml . + CMD ["./bootstrap.sh"] + SAVE IMAGE --push ${EXPECTED_REF} + +test: + FROM +build-src + COPY --dir test . + RUN make test + +ci: + BUILD +test + BUILD +image diff --git a/Makefile b/Makefile index 47ca577..8ba43ae 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ clean: .PHONY: test test: - clj -X:test + clojure -X:test .PHONY: dependency-sources dependency-sources: @@ -13,14 +13,14 @@ dependency-sources: .PHONY: run run: - clj \ + clojure \ -J-Dlogback.configurationFile=conf/logback.xml \ -J-Xms1g -J-Xmx1g \ - -M -m me.untethr.nostr.app + -M -m me.untethr.nostr.app .PHONY: uberjar uberjar: - clj -M:uberdeps + clojure -M:uberdeps .PHONY: run-uberjar run-uberjar: diff --git a/bootstrap.sh b/bootstrap.sh new file mode 100755 index 0000000..0dfa237 --- /dev/null +++ b/bootstrap.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env sh + +java \ + -Dlogback.configurationFile=logback.xml \ + -cp me.untethr.nostr-relay.jar \ + clojure.main \ + -m me.untethr.nostr.app diff --git a/devspace.yaml b/devspace.yaml new file mode 100644 index 0000000..d5ed46d --- /dev/null +++ b/devspace.yaml @@ -0,0 +1,86 @@ +version: v2beta1 +name: me-untether-nostr-relay + +vars: + REPO: + default: "duck1123" + PROJECT: + default: "dinsro" + REPOSITORY_HOST: + default: "k3d-myregistry.localhost:12345" + RELAY_ADMIN_USERNAME: + default: "" + RELAY_CONTACT: + default: "" + RELAY_ADMIN_PUBKEY: + default: "" + RELAY_NAME: + default: "" + RELAY_HOST: + default: "relay.localhost" + RELAY_VERSION: + default: "0.2.3-SNAPSHOT" + +pipelines: + build: + build_images --all + build-app: + build_images app + dev: |- + echo "Running dev pipeline" + deploy: |- + echo "Deploying" + run_dependencies --all + build_images --all -t $(git describe --always) + create_deployments --all + +images: + app: + image: ${REPOSITORY_HOST}/${REPO}/${PROJECT} + custom: + command: |- + set -ex + earthly +image \ + --REPO ${REPO} \ + --PROJECT ${PROJECT} \ + --TAG ${runtime.images.app.tag} \ + --RELAY_VERSION ${RELAY_VERSION} + docker tag ${REPO}/${PROJECT}:${runtime.images.app.tag} ${runtime.images.app.image}:${runtime.images.app.tag} + docker push ${runtime.images.app.image}:${runtime.images.app.tag} + docker image rm ${REPO}/${PROJECT}:${runtime.images.app.tag} + docker image rm ${runtime.images.app.image}:${runtime.images.app.tag} + +deployments: + app: + helm: + chart: + name: me-untethr-nostr-relay + repo: https://chart.kronkltd.net/ + version: 0.1.3 + # name: ../chart.kronkltd.net/stable/me-untethr-nostr-relay + # valuesFiles: + # - ./values.yaml + values: + image: + registry: ${REPOSITORY_HOST} + repository: ${REPO}/${PROJECT} + tag: "${runtime.images.app.tag}" + +dev: + app: + labelSelector: + app.kubernetes.io/name: relay + app.kubernetes.io/instance: app + proxyCommands: + - command: devspace + - command: kubectl + - command: helm + - command: git + +commands: + deploy: + command: devspace deploy + test: + command: make test + render-chart: + command: helm template untethr helm diff --git a/runbook.org b/runbook.org new file mode 100644 index 0000000..446d1eb --- /dev/null +++ b/runbook.org @@ -0,0 +1,158 @@ +* Setup + +These are the perquisites before running + +** Install Dependencies + +*** Docker + +#+BEGIN_SRC shell + sudo apt install docker-ce +#+END_SRC + +*** k3d + +https://k3d.io/v5.4.6/#installation + +#+BEGIN_SRC shell + wget -q -O - https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh | bash +#+END_SRC + +*** Earthly + +https://earthly.dev/get-earthly + +#+BEGIN_SRC shell + sudo /bin/sh -c 'wget https://github.com/earthly/earthly/releases/latest/download/earthly-linux-amd64 -O /usr/local/bin/earthly \ + && chmod +x /usr/local/bin/earthly \ + && /usr/local/bin/earthly bootstrap --with-autocomplete' +#+END_SRC + +*** Devspace + +https://www.devspace.sh/docs/getting-started/installation + +#+BEGIN_SRC shell + curl -L -o devspace "https://github.com/loft-sh/devspace/releases/latest/download/devspace-linux-amd64" \ + && sudo install -c -m 0755 devspace /usr/local/bin +#+END_SRC + +*** Clojure + +https://clojure.org/guides/install_clojure#_linux_instructions + +#+BEGIN_SRC shell + CLOJURE_VERSION=1.11.1.1208 \ + curl -O https://download.clojure.org/install/linux-install-${CLOJURE_VERSION}.sh \ + && chmod +x linux-install-${CLOJURE_VERSION}.sh \ + && sudo ./linux-install-${CLOJURE_VERSION}.sh +#+END_SRC + +** Create Cluster Environment + +*** Create Registry + +This will create a registry to share created images with the cluster + +#+BEGIN_SRC shell + k3d registry create myregistry.localhost --port 12345 +#+END_SRC + +*** Create Cluster + +This will create a single node kubernetes cluster in a docker container + +#+BEGIN_SRC shell + k3d cluster create \ + --api-port 6550 \ + -p "80:80@loadbalancer" \ + -p "443:443@loadbalancer" \ + --servers 1 \ + --registry-use k3d-myregistry.localhost:12345 \ + --kubeconfig-update-default +#+END_SRC + +*** install cert-manager + +https://cert-manager.io/docs/installation/kubectl/ + +**** install cert manager manifests + +#+BEGIN_SRC shell + kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.10.1/cert-manager.yaml +#+END_SRC + +**** Create letsencrypt provider + +#+BEGIN_SRC shell +EMAIL=someuser@example.com cat <