From 84e70af7426d333a44b8f1c0823aee9ac579fe34 Mon Sep 17 00:00:00 2001 From: bom Date: Fri, 3 Feb 2023 11:51:22 +0100 Subject: [PATCH] Use groups for webview --- src/main/cljc/dda/c4k_nextcloud/core.cljc | 27 +++----- .../cljc/dda/c4k_nextcloud/nextcloud.cljc | 18 +----- src/main/cljs/dda/c4k_nextcloud/browser.cljs | 61 +++++++++++-------- .../dda/c4k_nextcloud/nextcloud_test.cljc | 2 +- 4 files changed, 49 insertions(+), 59 deletions(-) diff --git a/src/main/cljc/dda/c4k_nextcloud/core.cljc b/src/main/cljc/dda/c4k_nextcloud/core.cljc index 6353a10..78453bb 100644 --- a/src/main/cljc/dda/c4k_nextcloud/core.cljc +++ b/src/main/cljc/dda/c4k_nextcloud/core.cljc @@ -4,6 +4,7 @@ #?(:clj [orchestra.core :refer [defn-spec]] :cljs [orchestra.core :refer-macros [defn-spec]]) [dda.c4k-common.common :as cm] + [dda.c4k-common.predicate :as cp] [dda.c4k-common.yaml :as yaml] [dda.c4k-common.postgres :as postgres] [dda.c4k-nextcloud.nextcloud :as nextcloud] @@ -13,33 +14,25 @@ (def config-defaults {:issuer "staging"}) -(defn-spec k8s-objects any? - [config (s/merge nextcloud/config? nextcloud/auth?)] +(defn-spec k8s-objects cp/map-or-seq? + [config nextcloud/config? + auth nextcloud/auth?] (let [nextcloud-default-storage-config {:pvc-storage-class-name default-storage-class :pv-storage-size-gb 200}] (map yaml/to-string - [(postgres/generate-config {:postgres-size :8gb}) - (postgres/generate-secret config) + [(postgres/generate-config {:postgres-size :8gb :db-name "nextcloud"}) + (postgres/generate-secret auth) (postgres/generate-pvc {:pv-storage-size-gb 50 :pvc-storage-class-name default-storage-class}) (postgres/generate-deployment) (postgres/generate-service) - (nextcloud/generate-secret config) + (nextcloud/generate-secret auth) (nextcloud/generate-pvc (merge nextcloud-default-storage-config config)) (nextcloud/generate-deployment config) - (nextcloud/generate-service) - (nextcloud/generate-certificate config)] - (nextcloud/generate-ingress config) + (nextcloud/generate-service)] + (nextcloud/generate-ingress-and-cert config) (when (:contains? config :restic-repository) [(backup/generate-config config) - (backup/generate-secret config) + (backup/generate-secret auth) (backup/generate-cron) (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))))) diff --git a/src/main/cljc/dda/c4k_nextcloud/nextcloud.cljc b/src/main/cljc/dda/c4k_nextcloud/nextcloud.cljc index 75a3951..cf08a4a 100644 --- a/src/main/cljc/dda/c4k_nextcloud/nextcloud.cljc +++ b/src/main/cljc/dda/c4k_nextcloud/nextcloud.cljc @@ -37,31 +37,19 @@ #?(:cljs (defmethod yaml/load-resource :nextcloud [resource-name] (case resource-name - "nextcloud/certificate.yaml" (rc/inline "nextcloud/certificate.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/service.yaml" (rc/inline "nextcloud/service.yaml") "nextcloud/secret.yaml" (rc/inline "nextcloud/secret.yaml") (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? [config config?] (let [{:keys [fqdn]} config] (-> (yaml/load-as-edn "nextcloud/deployment.yaml") (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?] (ing/generate-ingress-and-cert (merge @@ -82,8 +70,8 @@ (yaml/load-as-edn "nextcloud/service.yaml")) (defn-spec generate-secret cp/map-or-seq? - [config config?] - (let [{:keys [nextcloud-admin-user nextcloud-admin-password]} config] + [auth auth?] + (let [{:keys [nextcloud-admin-user nextcloud-admin-password]} auth] (-> (yaml/load-as-edn "nextcloud/secret.yaml") (cm/replace-key-value :nextcloud-admin-user (b64/encode nextcloud-admin-user)) diff --git a/src/main/cljs/dda/c4k_nextcloud/browser.cljs b/src/main/cljs/dda/c4k_nextcloud/browser.cljs index eee519c..411bd88 100644 --- a/src/main/cljs/dda/c4k_nextcloud/browser.cljs +++ b/src/main/cljs/dda/c4k_nextcloud/browser.cljs @@ -1,31 +1,36 @@ (ns dda.c4k-nextcloud.browser (:require [clojure.tools.reader.edn :as edn] + [dda.c4k-common.common :as cm] [dda.c4k-nextcloud.core :as core] [dda.c4k-nextcloud.nextcloud :as nextcloud] [dda.c4k-common.browser :as br] [dda.c4k-common.postgres :as pgc])) -(defn generate-content - [] - (into [] (concat [(assoc (br/generate-needs-validation) :content - (into [] (concat (br/generate-input-field "fqdn" "Your fqdn:" "nextcloud-neu.prod.meissa-gmbh.de") - (br/generate-input-field "nextcloud-data-volume-path" "(Optional) Your nextcloud-data-volume-path:" "/var/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-br)] - (br/generate-text-area "auth" "Your auth.edn:" "{:postgres-db-user \"nextcloud\" +(defn generate-content [] + (cm/concat-vec + [(assoc + (br/generate-needs-validation) :content + (cm/concat-vec + (br/generate-group "domain" + (cm/concat-vec (br/generate-input-field "fqdn" "Your fqdn:" "nextcloud-neu.prod.meissa-gmbh.de") + (br/generate-input-field "pv-storage-size-gb" "(Optional) Your nextcloud storage size in GB" "8") + (br/generate-input-field "pvc-storage-class-name" "(Optional) Your storage class type (manual / local-path):" "local-path") + (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\" :nextcloud-admin-password \"nextcloud-admin-password\" :nextcloud-admin-user \"nextcloud-admin-user\" :aws-access-key-id \"aws-id\" :aws-secret-access-key \"aws-secret\" :restic-password \"restic-password\"}" - "5") - [(br/generate-br)] - (br/generate-button "generate-button" "Generate c4k yaml"))))] - (br/generate-output "c4k-nextcloud-output" "Your c4k deployment.yaml:" "25")))) + "5")) + [(br/generate-br)] + (br/generate-button "generate-button" "Generate c4k yaml")))] + (br/generate-output "c4k-nextcloud-output" "Your c4k deployment.yaml:" "25"))) (defn generate-content-div [] @@ -35,29 +40,30 @@ (generate-content)}) (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) restic-repository (br/get-content-from-element "restic-repository" :optional true) issuer (br/get-content-from-element "issuer" :optional true :deserializer keyword)] (merge {:fqdn (br/get-content-from-element "fqdn")} - (when (some? nextcloud-data-volume-path) - {:nextcloud-data-volume-path nextcloud-data-volume-path}) + (when (and (some? pv-storage-size-gb) (some? pvc-storage-class-name)) + {:pv-storage-size-gb pv-storage-size-gb :pvc-storage-class-name pvc-storage-class-name}) (when (some? postgres-data-volume-path) {:postgres-data-volume-path postgres-data-volume-path}) (when (some? restic-repository) {:restic-repository restic-repository}) (when (some? issuer) - {:issuer issuer}) - ))) + {:issuer issuer})))) (defn validate-all! [] (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! "restic-repository" ::nextcloud/restic-repository :optional true) - (br/validate! "issuer" ::nextcloud/issuer :optional true :deserializer keyword) - (br/validate! "auth" core/auth? :deserializer edn/read-string) + (br/validate! "issuer" ::nextcloud/issuer :optional true) + (br/validate! "auth" nextcloud/auth? :deserializer edn/read-string) (br/set-validated!)) (defn add-validate-listener [name] @@ -70,12 +76,15 @@ (.getElementById "generate-button") (.addEventListener "click" #(do (validate-all!) - (-> (core/generate - (config-from-document) - (br/get-content-from-element "auth" :deserializer edn/read-string)) + (-> (cm/generate-common + (config-from-document) + (br/get-content-from-element "auth" :deserializer edn/read-string) + {} + core/k8s-objects) (br/set-output!))))) (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 "restic-repository") (add-validate-listener "issuer") diff --git a/src/test/cljc/dda/c4k_nextcloud/nextcloud_test.cljc b/src/test/cljc/dda/c4k_nextcloud/nextcloud_test.cljc index 83ea56d..9d28d7c 100644 --- a/src/test/cljc/dda/c4k_nextcloud/nextcloud_test.cljc +++ b/src/test/cljc/dda/c4k_nextcloud/nextcloud_test.cljc @@ -62,7 +62,7 @@ :rules [{:host "somefqdn.de", :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 (is (= {:apiVersion "v1"