You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
c4k-common/src/main/cljc/dda/c4k_common/postgres.cljc

87 lines
3.3 KiB
Plaintext

3 years ago
(ns dda.c4k-common.postgres
(:require
[clojure.spec.alpha :as s]
#?(:cljs [shadow.resource :as rc])
3 years ago
#?(:clj [orchestra.core :refer [defn-spec]]
:cljs [orchestra.core :refer-macros [defn-spec]])
3 years ago
[dda.c4k-common.yaml :as yaml]
[dda.c4k-common.base64 :as b64]
3 years ago
[dda.c4k-common.predicate :as cp]
[dda.c4k-common.common :as cm]))
3 years ago
(defn postgres-size?
[input]
(contains? #{:2gb :4gb :8gb :16gb} input))
3 years ago
(defn postgres-image?
[input]
(contains? #{"postgres:13" "postgres:14"} input))
(s/def ::postgres-db-user cp/bash-env-string?)
(s/def ::postgres-db-password cp/bash-env-string?)
3 years ago
(s/def ::postgres-data-volume-path string?)
(s/def ::postgres-size postgres-size?)
(s/def ::db-name cp/bash-env-string?)
3 years ago
(defn pg-config? [input]
3 years ago
(s/keys :opt-un [::postgres-size ::db-name ::postgres-data-volume-path]))
3 years ago
(defn pg-auth? [input]
3 years ago
(s/keys :opt-un [::postgres-db-user ::postgres-db-password]))
3 years ago
3 years ago
(def postgres-function (s/keys :opt-un [::deserializer ::optional]))
#?(:cljs
(defmethod yaml/load-resource :postgres [resource-name]
(case resource-name
"postgres/config-2gb.yaml" (rc/inline "postgres/config-2gb.yaml")
"postgres/config-4gb.yaml" (rc/inline "postgres/config-4gb.yaml")
"postgres/config-8gb.yaml" (rc/inline "postgres/config-8gb.yaml")
"postgres/config-16gb.yaml" (rc/inline "postgres/config-16gb.yaml")
"postgres/deployment.yaml" (rc/inline "postgres/deployment.yaml")
"postgres/persistent-volume.yaml" (rc/inline "postgres/persistent-volume.yaml")
"postgres/pvc.yaml" (rc/inline "postgres/pvc.yaml")
"postgres/secret.yaml" (rc/inline "postgres/secret.yaml")
"postgres/service.yaml" (rc/inline "postgres/service.yaml")
(throw (js/Error. "Undefined Resource!")))))
3 years ago
(defn-spec generate-config cp/map-or-seq?
3 years ago
[& config (s/? pg-config?)]
(let [{:keys [postgres-size db-name]
:or {postgres-size :2gb
3 years ago
db-name "postgres"}} (first config)]
(->
3 years ago
(yaml/from-string (yaml/load-resource
(str "postgres/config-" (name postgres-size) ".yaml")))
(assoc-in [:data :postgres-db] db-name))))
3 years ago
3 years ago
3 years ago
(defn-spec generate-deployment cp/map-or-seq?
3 years ago
[& config (s/? pg-config?)]
(let [{:keys [postgres-image]
3 years ago
:or {postgres-image "postgres:13"}} (first config)]
(->
(yaml/from-string (yaml/load-resource "postgres/deployment.yaml"))
(assoc-in [:spec :template :spec :containers 0 :image] postgres-image))))
3 years ago
3 years ago
(defn-spec generate-persistent-volume cp/map-or-seq?
3 years ago
[config pg-config?]
3 years ago
(let [{:keys [postgres-data-volume-path]} config]
(->
(yaml/from-string (yaml/load-resource "postgres/persistent-volume.yaml"))
(assoc-in [:spec :hostPath :path] postgres-data-volume-path))))
3 years ago
(defn-spec generate-pvc cp/map-or-seq?
[]
3 years ago
(yaml/from-string (yaml/load-resource "postgres/pvc.yaml")))
3 years ago
(defn-spec generate-secret cp/map-or-seq?
3 years ago
[my-auth any?]
3 years ago
(let [{:keys [postgres-db-user postgres-db-password]} my-auth]
(->
(yaml/from-string (yaml/load-resource "postgres/secret.yaml"))
(cm/replace-key-value :postgres-user (b64/encode postgres-db-user))
(cm/replace-key-value :postgres-password (b64/encode postgres-db-password)))))
3 years ago
(defn-spec generate-service cp/map-or-seq?
[]
3 years ago
(yaml/from-string (yaml/load-resource "postgres/service.yaml")))