2022-09-08 13:04:17 +00:00
|
|
|
(ns dda.c4k-website.website
|
|
|
|
(:require
|
2022-10-28 11:48:11 +00:00
|
|
|
[clojure.spec.alpha :as s]
|
2022-09-08 13:04:17 +00:00
|
|
|
#?(:cljs [shadow.resource :as rc])
|
|
|
|
#?(:clj [orchestra.core :refer [defn-spec]]
|
|
|
|
:cljs [orchestra.core :refer-macros [defn-spec]])
|
|
|
|
#?(:clj [clojure.edn :as edn]
|
|
|
|
:cljs [cljs.reader :as edn])
|
|
|
|
[dda.c4k-common.yaml :as yaml]
|
2022-09-27 11:53:04 +00:00
|
|
|
[dda.c4k-common.common :as cm]
|
2022-09-27 13:45:13 +00:00
|
|
|
[dda.c4k-common.base64 :as b64]
|
2022-09-30 15:54:26 +00:00
|
|
|
[dda.c4k-common.predicate :as pred]
|
2022-11-18 09:02:12 +00:00
|
|
|
[dda.c4k-common.ingress-cert :as ing]
|
|
|
|
[clojure.string :as str]))
|
2022-09-30 15:54:26 +00:00
|
|
|
|
2022-10-13 14:39:55 +00:00
|
|
|
(defn fqdn-list?
|
2022-09-30 15:54:26 +00:00
|
|
|
[input]
|
2022-10-13 14:39:55 +00:00
|
|
|
(every? true? (map pred/fqdn-string? input)))
|
2022-09-30 15:54:26 +00:00
|
|
|
|
2022-10-18 09:31:47 +00:00
|
|
|
(s/def ::unique-name string?)
|
2022-11-11 12:08:55 +00:00
|
|
|
(s/def ::sha256sum-output string?)
|
2022-10-13 14:39:55 +00:00
|
|
|
(s/def ::issuer pred/letsencrypt-issuer?)
|
2022-10-28 11:48:11 +00:00
|
|
|
(s/def ::volume-size pred/integer-string?)
|
2022-10-13 14:39:55 +00:00
|
|
|
(s/def ::authtoken pred/bash-env-string?)
|
2022-10-18 11:09:13 +00:00
|
|
|
(s/def ::fqdns (s/coll-of pred/fqdn-string?))
|
2022-10-13 14:39:55 +00:00
|
|
|
(s/def ::gitea-host pred/fqdn-string?)
|
|
|
|
(s/def ::gitea-repo string?)
|
|
|
|
(s/def ::branchname string?)
|
|
|
|
(s/def ::username string?)
|
2022-09-08 13:04:17 +00:00
|
|
|
|
2022-10-14 11:41:21 +00:00
|
|
|
(def websitedata? (s/keys :req-un [::unique-name ::fqdns ::gitea-host ::gitea-repo ::branchname]
|
2022-11-11 12:08:55 +00:00
|
|
|
:opt-un [::issuer ::volume-size ::sha256sum-output]))
|
2022-10-13 14:39:55 +00:00
|
|
|
|
2022-10-14 11:41:21 +00:00
|
|
|
(def websiteauth? (s/keys :req-un [::unique-name ::username ::authtoken]))
|
2022-10-13 14:39:55 +00:00
|
|
|
|
2022-11-01 12:23:00 +00:00
|
|
|
(def flattened-and-reduced-config? (s/and websitedata? websiteauth?))
|
|
|
|
|
2022-10-14 11:41:21 +00:00
|
|
|
(s/def ::auth (s/coll-of websiteauth?))
|
2022-10-12 11:42:44 +00:00
|
|
|
|
2022-10-14 11:41:21 +00:00
|
|
|
(s/def ::websites (s/coll-of websitedata?))
|
2022-10-12 11:42:44 +00:00
|
|
|
|
2022-10-13 14:39:55 +00:00
|
|
|
(def auth? (s/keys :req-un [::auth]))
|
|
|
|
|
2022-10-12 11:42:44 +00:00
|
|
|
(def config? (s/keys :req-un [::websites]
|
2022-10-28 11:48:11 +00:00
|
|
|
:opt-un [::issuer ::volume-size]))
|
2022-09-08 13:04:17 +00:00
|
|
|
|
2022-11-11 12:08:55 +00:00
|
|
|
(defn-spec get-hash-from-sha256sum-output string?
|
|
|
|
[sha256sum-output string?]
|
2022-11-11 14:10:47 +00:00
|
|
|
(if (nil? sha256sum-output)
|
|
|
|
nil
|
2022-11-18 09:02:12 +00:00
|
|
|
(first (str/split sha256sum-output #"\ +"))))
|
2022-11-11 12:08:55 +00:00
|
|
|
|
|
|
|
(defn-spec get-file-name-from-sha256sum-output string?
|
|
|
|
[sha256sum-output string?]
|
2022-11-11 14:10:47 +00:00
|
|
|
(if (nil? sha256sum-output)
|
|
|
|
nil
|
2022-11-18 09:02:12 +00:00
|
|
|
(second (str/split (str/trim sha256sum-output) #"\ +"))))
|
2022-11-11 12:08:55 +00:00
|
|
|
|
2022-10-28 09:28:37 +00:00
|
|
|
(defn-spec replace-dots-by-minus string?
|
|
|
|
[fqdn pred/fqdn-string?]
|
2022-10-12 08:56:12 +00:00
|
|
|
(str/replace fqdn #"\." "-"))
|
2022-09-27 11:53:04 +00:00
|
|
|
|
2022-11-01 14:05:59 +00:00
|
|
|
(defn-spec generate-app-name string?
|
|
|
|
[unique-name pred/fqdn-string?]
|
|
|
|
(str (replace-dots-by-minus unique-name) "-website"))
|
|
|
|
|
2022-10-28 09:28:37 +00:00
|
|
|
(defn-spec generate-service-name string?
|
|
|
|
[unique-name pred/fqdn-string?]
|
2022-10-18 08:30:51 +00:00
|
|
|
(str (replace-dots-by-minus unique-name) "-service"))
|
2022-10-11 16:07:01 +00:00
|
|
|
|
2022-10-28 09:28:37 +00:00
|
|
|
(defn-spec generate-cert-name string?
|
|
|
|
[unique-name pred/fqdn-string?]
|
2022-10-18 08:30:51 +00:00
|
|
|
(str (replace-dots-by-minus unique-name) "-cert"))
|
2022-10-11 16:07:01 +00:00
|
|
|
|
2022-11-18 09:02:12 +00:00
|
|
|
(defn-spec generate-ingress-name string?
|
2022-10-28 09:28:37 +00:00
|
|
|
[unique-name pred/fqdn-string?]
|
2022-11-18 09:02:12 +00:00
|
|
|
(str (replace-dots-by-minus unique-name) "-ingress"))
|
2022-10-13 14:39:55 +00:00
|
|
|
|
2022-11-23 10:43:19 +00:00
|
|
|
; https://your.gitea.host/api/v1/repos/<owner>/<repo>/archive/<branch>.zip
|
2022-11-23 10:14:46 +00:00
|
|
|
(defn-spec generate-gitrepourl string?
|
2022-10-28 09:28:37 +00:00
|
|
|
[host pred/fqdn-string?
|
|
|
|
repo string?
|
|
|
|
user string?
|
|
|
|
branch string?]
|
2022-10-12 11:42:44 +00:00
|
|
|
(str "https://" host "/api/v1/repos/" user "/" repo "/archive/" branch ".zip"))
|
2022-10-12 08:56:12 +00:00
|
|
|
|
2022-11-01 12:23:00 +00:00
|
|
|
(defn-spec replace-all-matching-subvalues-in-string-start pred/map-or-seq?
|
|
|
|
[col pred/map-or-seq?
|
|
|
|
value-to-partly-match string?
|
|
|
|
value-to-inplace string?]
|
2022-09-27 11:53:04 +00:00
|
|
|
(clojure.walk/postwalk #(if (and (= (type value-to-partly-match) (type %))
|
|
|
|
(re-matches (re-pattern (str value-to-partly-match ".*")) %))
|
2022-10-12 08:56:12 +00:00
|
|
|
(str/replace % value-to-partly-match value-to-inplace) %)
|
2022-09-27 11:53:04 +00:00
|
|
|
col))
|
|
|
|
|
2022-11-23 10:14:46 +00:00
|
|
|
(defn-spec replace-common-data pred/map-or-seq?
|
|
|
|
[resource-file string?
|
|
|
|
config flattened-and-reduced-config?]
|
2022-11-23 10:43:19 +00:00
|
|
|
(let [{:keys [unique-name]} config]
|
2022-11-23 10:14:46 +00:00
|
|
|
(->
|
|
|
|
(yaml/load-as-edn resource-file)
|
|
|
|
(assoc-in [:metadata :labels :app.kubernetes.part-of] (generate-app-name unique-name))
|
|
|
|
(replace-all-matching-subvalues-in-string-start "NAME" (replace-dots-by-minus unique-name)))))
|
|
|
|
|
|
|
|
(defn-spec replace-build-data pred/map-or-seq?
|
|
|
|
[resource-file string?
|
|
|
|
config flattened-and-reduced-config?]
|
|
|
|
(let [{:keys [sha256sum-output]} config]
|
|
|
|
(->
|
|
|
|
(replace-common-data resource-file config)
|
|
|
|
(cm/replace-all-matching-values-by-new-value "CHECK_SUM" (get-hash-from-sha256sum-output sha256sum-output))
|
|
|
|
(cm/replace-all-matching-values-by-new-value "SCRIPT_FILE" (get-file-name-from-sha256sum-output sha256sum-output)))))
|
|
|
|
|
2022-09-08 13:04:17 +00:00
|
|
|
#?(:cljs
|
|
|
|
(defmethod yaml/load-resource :website [resource-name]
|
2022-10-26 06:56:57 +00:00
|
|
|
(case resource-name
|
2022-10-12 11:42:44 +00:00
|
|
|
"website/nginx-configmap.yaml" (rc/inline "website/nginx-configmap.yaml")
|
2022-09-21 12:40:34 +00:00
|
|
|
"website/nginx-deployment.yaml" (rc/inline "website/nginx-deployment.yaml")
|
2022-09-30 15:54:26 +00:00
|
|
|
"website/nginx-service.yaml" (rc/inline "website/nginx-service.yaml")
|
2022-09-27 11:53:04 +00:00
|
|
|
"website/website-build-cron.yaml" (rc/inline "website/website-build-cron.yaml")
|
|
|
|
"website/website-build-secret.yaml" (rc/inline "website/website-build-secret.yaml")
|
2022-10-26 06:56:57 +00:00
|
|
|
"website/website-content-volume.yaml" (rc/inline "website/website-content-volume.yaml")
|
2022-09-08 13:04:17 +00:00
|
|
|
(throw (js/Error. "Undefined Resource!")))))
|
|
|
|
|
2022-11-18 09:02:12 +00:00
|
|
|
(defn-spec generate-website-ingress pred/map-or-seq?
|
2022-11-01 12:23:00 +00:00
|
|
|
[config flattened-and-reduced-config?]
|
2022-10-18 08:30:51 +00:00
|
|
|
(let [{:keys [unique-name fqdns]} config]
|
2022-11-18 09:02:12 +00:00
|
|
|
(ing/generate-ingress {:fqdns fqdns
|
|
|
|
:app-name (generate-app-name unique-name)
|
|
|
|
:ingress-name (generate-ingress-name unique-name)
|
|
|
|
:service-name (generate-service-name unique-name)
|
|
|
|
:service-port 80})))
|
2022-10-11 16:07:01 +00:00
|
|
|
|
2022-10-12 11:42:44 +00:00
|
|
|
(defn-spec generate-website-certificate pred/map-or-seq?
|
2022-11-01 12:23:00 +00:00
|
|
|
[config flattened-and-reduced-config?]
|
2022-10-18 08:30:51 +00:00
|
|
|
(let [{:keys [unique-name issuer fqdns]
|
|
|
|
:or {issuer "staging"}} config]
|
2022-10-18 09:31:47 +00:00
|
|
|
(ing/generate-certificate {:fqdns fqdns
|
2022-11-01 14:05:59 +00:00
|
|
|
:app-name (generate-app-name unique-name)
|
2022-10-18 09:31:47 +00:00
|
|
|
:cert-name (generate-cert-name unique-name)
|
|
|
|
:issuer issuer})))
|
2022-10-11 16:07:01 +00:00
|
|
|
|
2022-10-12 08:56:12 +00:00
|
|
|
(defn-spec generate-nginx-configmap pred/map-or-seq?
|
2022-11-01 12:23:00 +00:00
|
|
|
[config flattened-and-reduced-config?]
|
2022-11-23 10:14:46 +00:00
|
|
|
(let [{:keys [fqdns]} config]
|
2022-09-27 11:53:04 +00:00
|
|
|
(->
|
2022-11-23 10:14:46 +00:00
|
|
|
(replace-common-data "website/nginx-configmap.yaml" config)
|
2022-10-12 08:56:12 +00:00
|
|
|
(#(assoc-in %
|
|
|
|
[:data :website.conf]
|
|
|
|
(str/replace
|
|
|
|
(-> % :data :website.conf) #"FQDN" (str (str/join " " fqdns) ";")))))))
|
2022-09-08 13:04:17 +00:00
|
|
|
|
2022-09-21 12:40:34 +00:00
|
|
|
(defn-spec generate-nginx-deployment pred/map-or-seq?
|
2022-11-23 10:14:46 +00:00
|
|
|
[config flattened-and-reduced-config?]
|
|
|
|
(replace-build-data "website/nginx-deployment.yaml" config))
|
2022-09-08 13:04:17 +00:00
|
|
|
|
2022-09-21 12:40:34 +00:00
|
|
|
(defn-spec generate-nginx-service pred/map-or-seq?
|
2022-11-01 12:23:00 +00:00
|
|
|
[config flattened-and-reduced-config?]
|
2022-11-23 10:14:46 +00:00
|
|
|
(replace-common-data "website/nginx-service.yaml" config))
|
2022-09-08 13:04:17 +00:00
|
|
|
|
2022-09-21 12:40:34 +00:00
|
|
|
(defn-spec generate-website-content-volume pred/map-or-seq?
|
2022-11-01 12:23:00 +00:00
|
|
|
[config flattened-and-reduced-config?]
|
2022-11-23 10:14:46 +00:00
|
|
|
(let [{:keys [volume-size]
|
2022-11-01 11:25:31 +00:00
|
|
|
:or {volume-size "3"}} config]
|
2022-09-21 12:40:34 +00:00
|
|
|
(->
|
2022-11-23 10:14:46 +00:00
|
|
|
(replace-common-data "website/website-content-volume.yaml" config)
|
2022-10-28 11:48:11 +00:00
|
|
|
(cm/replace-all-matching-values-by-new-value "WEBSITESTORAGESIZE" (str volume-size "Gi")))))
|
2022-09-27 11:53:04 +00:00
|
|
|
|
2022-11-11 12:08:55 +00:00
|
|
|
(defn-spec generate-website-build-cron pred/map-or-seq?
|
2022-11-08 15:23:10 +00:00
|
|
|
[config flattened-and-reduced-config?]
|
2022-11-11 12:08:55 +00:00
|
|
|
(replace-build-data "website/website-build-cron.yaml" config))
|
|
|
|
|
2022-09-27 11:53:04 +00:00
|
|
|
(defn-spec generate-website-build-secret pred/map-or-seq?
|
2022-11-01 12:23:00 +00:00
|
|
|
[auth flattened-and-reduced-config?]
|
2022-11-23 10:14:46 +00:00
|
|
|
(let [{:keys [authtoken
|
2022-10-12 11:42:44 +00:00
|
|
|
gitea-host
|
|
|
|
gitea-repo
|
|
|
|
username
|
|
|
|
branchname]} auth]
|
2022-09-27 11:53:04 +00:00
|
|
|
(->
|
2022-11-23 10:14:46 +00:00
|
|
|
(replace-common-data "website/website-build-secret.yaml" auth)
|
2022-09-28 08:26:50 +00:00
|
|
|
(cm/replace-all-matching-values-by-new-value "TOKEN" (b64/encode authtoken))
|
2022-10-12 11:42:44 +00:00
|
|
|
(cm/replace-all-matching-values-by-new-value "URL" (b64/encode
|
2022-11-23 10:14:46 +00:00
|
|
|
(generate-gitrepourl
|
2022-10-12 11:42:44 +00:00
|
|
|
gitea-host
|
|
|
|
gitea-repo
|
|
|
|
username
|
|
|
|
branchname))))))
|