Use groups for webview

This commit is contained in:
bom 2023-02-03 11:51:22 +01:00
parent fe4c381791
commit 84e70af742
4 changed files with 49 additions and 59 deletions

View file

@ -4,6 +4,7 @@
#?(:clj [orchestra.core :refer [defn-spec]] #?(:clj [orchestra.core :refer [defn-spec]]
:cljs [orchestra.core :refer-macros [defn-spec]]) :cljs [orchestra.core :refer-macros [defn-spec]])
[dda.c4k-common.common :as cm] [dda.c4k-common.common :as cm]
[dda.c4k-common.predicate :as cp]
[dda.c4k-common.yaml :as yaml] [dda.c4k-common.yaml :as yaml]
[dda.c4k-common.postgres :as postgres] [dda.c4k-common.postgres :as postgres]
[dda.c4k-nextcloud.nextcloud :as nextcloud] [dda.c4k-nextcloud.nextcloud :as nextcloud]
@ -13,33 +14,25 @@
(def config-defaults {:issuer "staging"}) (def config-defaults {:issuer "staging"})
(defn-spec k8s-objects any? (defn-spec k8s-objects cp/map-or-seq?
[config (s/merge nextcloud/config? nextcloud/auth?)] [config nextcloud/config?
auth nextcloud/auth?]
(let [nextcloud-default-storage-config {:pvc-storage-class-name default-storage-class (let [nextcloud-default-storage-config {:pvc-storage-class-name default-storage-class
:pv-storage-size-gb 200}] :pv-storage-size-gb 200}]
(map yaml/to-string (map yaml/to-string
[(postgres/generate-config {:postgres-size :8gb}) [(postgres/generate-config {:postgres-size :8gb :db-name "nextcloud"})
(postgres/generate-secret config) (postgres/generate-secret auth)
(postgres/generate-pvc {:pv-storage-size-gb 50 (postgres/generate-pvc {:pv-storage-size-gb 50
:pvc-storage-class-name default-storage-class}) :pvc-storage-class-name default-storage-class})
(postgres/generate-deployment) (postgres/generate-deployment)
(postgres/generate-service) (postgres/generate-service)
(nextcloud/generate-secret config) (nextcloud/generate-secret auth)
(nextcloud/generate-pvc (merge nextcloud-default-storage-config config)) (nextcloud/generate-pvc (merge nextcloud-default-storage-config config))
(nextcloud/generate-deployment config) (nextcloud/generate-deployment config)
(nextcloud/generate-service) (nextcloud/generate-service)]
(nextcloud/generate-certificate config)] (nextcloud/generate-ingress-and-cert config)
(nextcloud/generate-ingress config)
(when (:contains? config :restic-repository) (when (:contains? config :restic-repository)
[(backup/generate-config config) [(backup/generate-config config)
(backup/generate-secret config) (backup/generate-secret auth)
(backup/generate-cron) (backup/generate-cron)
(backup/generate-backup-restore-deployment config)])))) (backup/generate-backup-restore-deployment config)]))))
(defn-spec generate any?
[my-config nextcloud/config?
my-auth nextcloud/auth?]
(cm/concat-vec
(map yaml/to-string
(filter #(not (nil? %))
(merge config-defaults my-config my-auth)))))

View file

@ -37,31 +37,19 @@
#?(:cljs #?(:cljs
(defmethod yaml/load-resource :nextcloud [resource-name] (defmethod yaml/load-resource :nextcloud [resource-name]
(case resource-name (case resource-name
"nextcloud/certificate.yaml" (rc/inline "nextcloud/certificate.yaml")
"nextcloud/deployment.yaml" (rc/inline "nextcloud/deployment.yaml") "nextcloud/deployment.yaml" (rc/inline "nextcloud/deployment.yaml")
"nextcloud/ingress.yaml" (rc/inline "nextcloud/ingress.yaml")
"nextcloud/pvc.yaml" (rc/inline "nextcloud/pvc.yaml") "nextcloud/pvc.yaml" (rc/inline "nextcloud/pvc.yaml")
"nextcloud/service.yaml" (rc/inline "nextcloud/service.yaml") "nextcloud/service.yaml" (rc/inline "nextcloud/service.yaml")
"nextcloud/secret.yaml" (rc/inline "nextcloud/secret.yaml") "nextcloud/secret.yaml" (rc/inline "nextcloud/secret.yaml")
(throw (js/Error. "Undefined Resource!"))))) (throw (js/Error. "Undefined Resource!")))))
(defn-spec generate-certificate cp/map-or-seq?
[config config?]
(let [{:keys [fqdn issuer]} config
letsencrypt-issuer issuer]
(->
(yaml/load-as-edn "nextcloud/certificate.yaml")
(assoc-in [:spec :commonName] fqdn)
(assoc-in [:spec :dnsNames] [fqdn])
(assoc-in [:spec :issuerRef :name] letsencrypt-issuer))))
(defn-spec generate-deployment cp/map-or-seq? (defn-spec generate-deployment cp/map-or-seq?
[config config?] [config config?]
(let [{:keys [fqdn]} config] (let [{:keys [fqdn]} config]
(-> (yaml/load-as-edn "nextcloud/deployment.yaml") (-> (yaml/load-as-edn "nextcloud/deployment.yaml")
(cm/replace-all-matching-values-by-new-value "fqdn" fqdn)))) (cm/replace-all-matching-values-by-new-value "fqdn" fqdn))))
(defn-spec generate-ingress cp/map-or-seq? (defn-spec generate-ingress-and-cert cp/map-or-seq?
[config config?] [config config?]
(ing/generate-ingress-and-cert (ing/generate-ingress-and-cert
(merge (merge
@ -82,8 +70,8 @@
(yaml/load-as-edn "nextcloud/service.yaml")) (yaml/load-as-edn "nextcloud/service.yaml"))
(defn-spec generate-secret cp/map-or-seq? (defn-spec generate-secret cp/map-or-seq?
[config config?] [auth auth?]
(let [{:keys [nextcloud-admin-user nextcloud-admin-password]} config] (let [{:keys [nextcloud-admin-user nextcloud-admin-password]} auth]
(-> (->
(yaml/load-as-edn "nextcloud/secret.yaml") (yaml/load-as-edn "nextcloud/secret.yaml")
(cm/replace-key-value :nextcloud-admin-user (b64/encode nextcloud-admin-user)) (cm/replace-key-value :nextcloud-admin-user (b64/encode nextcloud-admin-user))

View file

@ -1,31 +1,36 @@
(ns dda.c4k-nextcloud.browser (ns dda.c4k-nextcloud.browser
(:require (:require
[clojure.tools.reader.edn :as edn] [clojure.tools.reader.edn :as edn]
[dda.c4k-common.common :as cm]
[dda.c4k-nextcloud.core :as core] [dda.c4k-nextcloud.core :as core]
[dda.c4k-nextcloud.nextcloud :as nextcloud] [dda.c4k-nextcloud.nextcloud :as nextcloud]
[dda.c4k-common.browser :as br] [dda.c4k-common.browser :as br]
[dda.c4k-common.postgres :as pgc])) [dda.c4k-common.postgres :as pgc]))
(defn generate-content (defn generate-content []
[] (cm/concat-vec
(into [] (concat [(assoc (br/generate-needs-validation) :content [(assoc
(into [] (concat (br/generate-input-field "fqdn" "Your fqdn:" "nextcloud-neu.prod.meissa-gmbh.de") (br/generate-needs-validation) :content
(br/generate-input-field "nextcloud-data-volume-path" "(Optional) Your nextcloud-data-volume-path:" "/var/nextcloud") (cm/concat-vec
(br/generate-input-field "postgres-data-volume-path" "(Optional) Your postgres-data-volume-path:" "/var/postgres") (br/generate-group "domain"
(br/generate-input-field "restic-repository" "(Optional) Your restic-repository:" "restic-repository") (cm/concat-vec (br/generate-input-field "fqdn" "Your fqdn:" "nextcloud-neu.prod.meissa-gmbh.de")
(br/generate-input-field "issuer" "(Optional) Your issuer prod/staging:" "") (br/generate-input-field "pv-storage-size-gb" "(Optional) Your nextcloud storage size in GB" "8")
[(br/generate-br)] (br/generate-input-field "pvc-storage-class-name" "(Optional) Your storage class type (manual / local-path):" "local-path")
(br/generate-text-area "auth" "Your auth.edn:" "{:postgres-db-user \"nextcloud\" (br/generate-input-field "postgres-data-volume-path" "(Optional) Your postgres-data-volume-path:" "/var/postgres")
(br/generate-input-field "restic-repository" "(Optional) Your restic-repository:" "restic-repository")
(br/generate-input-field "issuer" "(Optional) Your issuer prod/staging:" "")))
(br/generate-group "credentials"
(br/generate-text-area "auth" "Your auth.edn:" "{:postgres-db-user \"nextcloud\"
:postgres-db-password \"nextcloud-db-password\" :postgres-db-password \"nextcloud-db-password\"
:nextcloud-admin-password \"nextcloud-admin-password\" :nextcloud-admin-password \"nextcloud-admin-password\"
:nextcloud-admin-user \"nextcloud-admin-user\" :nextcloud-admin-user \"nextcloud-admin-user\"
:aws-access-key-id \"aws-id\" :aws-access-key-id \"aws-id\"
:aws-secret-access-key \"aws-secret\" :aws-secret-access-key \"aws-secret\"
:restic-password \"restic-password\"}" :restic-password \"restic-password\"}"
"5") "5"))
[(br/generate-br)] [(br/generate-br)]
(br/generate-button "generate-button" "Generate c4k yaml"))))] (br/generate-button "generate-button" "Generate c4k yaml")))]
(br/generate-output "c4k-nextcloud-output" "Your c4k deployment.yaml:" "25")))) (br/generate-output "c4k-nextcloud-output" "Your c4k deployment.yaml:" "25")))
(defn generate-content-div (defn generate-content-div
[] []
@ -35,29 +40,30 @@
(generate-content)}) (generate-content)})
(defn config-from-document [] (defn config-from-document []
(let [nextcloud-data-volume-path (br/get-content-from-element "nextcloud-data-volume-path" :optional true) (let [pv-storage-size-gb (br/get-content-from-element "pv-storage-size-gb" :optional true)
pvc-storage-class-name (br/get-content-from-element "pvc-storage-class-name" :optional true)
postgres-data-volume-path (br/get-content-from-element "postgres-data-volume-path" :optional true) postgres-data-volume-path (br/get-content-from-element "postgres-data-volume-path" :optional true)
restic-repository (br/get-content-from-element "restic-repository" :optional true) restic-repository (br/get-content-from-element "restic-repository" :optional true)
issuer (br/get-content-from-element "issuer" :optional true :deserializer keyword)] issuer (br/get-content-from-element "issuer" :optional true :deserializer keyword)]
(merge (merge
{:fqdn (br/get-content-from-element "fqdn")} {:fqdn (br/get-content-from-element "fqdn")}
(when (some? nextcloud-data-volume-path) (when (and (some? pv-storage-size-gb) (some? pvc-storage-class-name))
{:nextcloud-data-volume-path nextcloud-data-volume-path}) {:pv-storage-size-gb pv-storage-size-gb :pvc-storage-class-name pvc-storage-class-name})
(when (some? postgres-data-volume-path) (when (some? postgres-data-volume-path)
{:postgres-data-volume-path postgres-data-volume-path}) {:postgres-data-volume-path postgres-data-volume-path})
(when (some? restic-repository) (when (some? restic-repository)
{:restic-repository restic-repository}) {:restic-repository restic-repository})
(when (some? issuer) (when (some? issuer)
{:issuer issuer}) {:issuer issuer}))))
)))
(defn validate-all! [] (defn validate-all! []
(br/validate! "fqdn" ::nextcloud/fqdn) (br/validate! "fqdn" ::nextcloud/fqdn)
(br/validate! "nextcloud-data-volume-path" ::nextcloud/nextcloud-data-volume-path :optional true) (br/validate! "pv-storage-size-gb" ::nextcloud/pv-storage-size-gb :optional true)
(br/validate! "pvc-storage-class-name" ::nextcloud/pvc-storage-class-name :optional true)
(br/validate! "postgres-data-volume-path" ::pgc/postgres-data-volume-path :optional true) (br/validate! "postgres-data-volume-path" ::pgc/postgres-data-volume-path :optional true)
(br/validate! "restic-repository" ::nextcloud/restic-repository :optional true) (br/validate! "restic-repository" ::nextcloud/restic-repository :optional true)
(br/validate! "issuer" ::nextcloud/issuer :optional true :deserializer keyword) (br/validate! "issuer" ::nextcloud/issuer :optional true)
(br/validate! "auth" core/auth? :deserializer edn/read-string) (br/validate! "auth" nextcloud/auth? :deserializer edn/read-string)
(br/set-validated!)) (br/set-validated!))
(defn add-validate-listener [name] (defn add-validate-listener [name]
@ -70,12 +76,15 @@
(.getElementById "generate-button") (.getElementById "generate-button")
(.addEventListener "click" (.addEventListener "click"
#(do (validate-all!) #(do (validate-all!)
(-> (core/generate (-> (cm/generate-common
(config-from-document) (config-from-document)
(br/get-content-from-element "auth" :deserializer edn/read-string)) (br/get-content-from-element "auth" :deserializer edn/read-string)
{}
core/k8s-objects)
(br/set-output!))))) (br/set-output!)))))
(add-validate-listener "fqdn") (add-validate-listener "fqdn")
(add-validate-listener "nextcloud-data-volume-path") (add-validate-listener "pv-storage-size-gb")
(add-validate-listener "pvc-storage-class-name")
(add-validate-listener "postgres-data-volume-path") (add-validate-listener "postgres-data-volume-path")
(add-validate-listener "restic-repository") (add-validate-listener "restic-repository")
(add-validate-listener "issuer") (add-validate-listener "issuer")

View file

@ -62,7 +62,7 @@
:rules :rules
[{:host "somefqdn.de", [{:host "somefqdn.de",
:http {:paths [{:pathType "Prefix", :path "/", :backend {:service {:name "nextcloud", :port {:number 80}}}}]}}]}}] :http {:paths [{:pathType "Prefix", :path "/", :backend {:service {:name "nextcloud", :port {:number 80}}}}]}}]}}]
(cut/generate-ingress {:fqdn "somefqdn.de"})))) (cut/generate-ingress-and-cert {:fqdn "somefqdn.de"}))))
(deftest should-generate-pvc (deftest should-generate-pvc
(is (= {:apiVersion "v1" (is (= {:apiVersion "v1"