Add infra for using c4k

This commit is contained in:
bom 2024-09-27 15:04:05 +02:00
parent 2f3ad2e6a6
commit d384ad7f7b
3 changed files with 139 additions and 1 deletions

View file

@ -7,7 +7,9 @@
:deps
{org.clojure/clojure {:mvn/version "1.11.4"}
org.clojure/spec.alpha {:mvn/version "0.5.238"}
orchestra/orchestra {:mvn/version "2021.01.01-1"}}
orchestra/orchestra {:mvn/version "2021.01.01-1"}
org.domaindrivenarchitecture/c4k-common-clj {:mvn/version "8.0.1-SNAPSHOT"}
cheshire/cheshire {:mvn/version "5.13.0"}}
:aliases
{

65
src/dda/build/c4k.clj Normal file
View file

@ -0,0 +1,65 @@
(ns dda.build.c4k
(:require [orchestra.core :refer [defn-spec]]
[clojure.spec.test.alpha :as st]
[cheshire.core :refer [parse-string generate-string]]
[dda.build.devops :as d]
[dda.build.c4k.domain :as domain]
[dda.build.infrastructure :as i]))
(st/instrument `clean-build-dir!)
(def default
(merge d/default {:autoapply false
:c4k-output-filename "c4k-app.yaml"
:c4k-config-filename "c4k-config.yaml"
:c4k-auth-filename "c4k-auth.yaml"}))
(defn-spec clean-build-dir! nil?
[devops ::d/devops]
(let [config (merge default devops)]
(i/execute! (domain/clean-build-dir-command config) config)))
(defn-spec run-c4k-jar! nil?
[devops ::d/devops]
(let [config (merge default devops)]
(doseq [c (domain/c4k-uberjar-command config)]
(i/execute! c config))))
(defn-spec run-c4k-executable! nil?
[devops ::d/devops]
(let [config (merge default devops)]
(doseq [c (domain/c4k-graalvm-command config)]
(i/execute! c config))))
; TODO: Generate functions assume that files have already been copied,
; which will happen if this is run after terragrunt
; but it is not guaranteed
(defn-spec generate-jar! nil?
[devops ::d/devops]
(let [config (merge default devops)]
(run-c4k-jar! config)))
(defn-spec generate! nil?
[devops ::d/devops]
(let [config (merge default devops)]
(run-c4k-executable! config)))
(defn-spec insert-tf-out! nil?
[devops ::d/devops
tf-out ::domain/tf-out]
(let [config (merge default devops)
default-c4k-config (parse-string (slurp (domain/config-path config))
(fn [k] (keyword (.toLowerCase k))))
tf-out-c4k-config (domain/create-c4k-config config tf-out)]
(->> default-c4k-config
(merge tf-out-c4k-config)
(generate-string)
(spit (domain/config-path config)))))
(st/instrument `clean-build-dir!)
(st/instrument `run-c4k-jar!)
(st/instrument `run-c4k-executable!)
(st/instrument `generate-jar!)
(st/instrument `generate!)
(st/instrument `insert-tf-out!)

View file

@ -0,0 +1,71 @@
(ns dda.build.c4k.domain
(:require [clojure.spec.alpha :as s]
[orchestra.core :refer [defn-spec]]
[dda.build.devops.domain :as d]
[dda.c4k-common.predicate :as pred]))
(s/def ::c4k-output-filenname string?)
(s/def ::autoapply boolean?)
(s/def ::sensitive boolean?)
(s/def ::type vector?)
(s/def ::fqdn pred/fqdn-string?)
(s/def ::ipv4 pred/ipv4-string?)
(s/def ::ipv6 pred/ipv6-string?)
(s/def ::value
(s/keys :req-un [::fqdn ::ipv4 ::ipv6]))
(s/def ::out
(s/keys :req-un [::sensitive ::type ::value]))
(s/def ::tf-out
(s/keys :req-un [::out]))
(s/def ::c4k-config-filename string?)
(s/def ::c4k-auth-filename string?)
(s/def ::config
(s/keys :req-un [::d/name ::d/stage ::d/project-root-path ::d/build-dir-name ::d/debug
::d/dry-run ::d/module ::c4k-output-filenname ::autoapply ::c4k-config-filename ::c4k-auth-filename]
:opt-un []))
(defn-spec config-path string?
[config ::config]
(let [{:keys [c4k-config-filename]} config]
(str (d/build-path config) "/" c4k-config-filename)))
(defn-spec auth-path string?
[config ::config]
(let [{:keys [c4k-auth-filename]} config]
(str (d/build-path config) "/" c4k-auth-filename)))
(defn-spec output-path string?
[config ::config]
(let [{:keys [c4k-output-filename]} config]
(str (d/build-path config) "/" c4k-output-filename)))
(defn-spec clean-build-dir-command seq?
[config ::config]
["rm" "-rf" (d/build-path (dissoc config :module))])
(defn-spec c4k-uberjar-command seq?
[config ::config]
(let [{:keys [module]} config
executable-name (str "c4k-" module "-standalone.jar")]
[["bash" "-c" (str executable-name " " (config-path config) " " (auth-path config) " > " (output-path config))]]))
(defn-spec c4k-graalvm-command seq?
[config ::config]
(let [{:keys [module]} config
executable-name (str "c4k-" module)]
[["bash" "-c" (str executable-name " " (config-path config) " " (auth-path config) " > " (output-path config))]]))
(defn-spec create-c4k-config
[config ::config
tf-out ::tf-out]
(let [{:keys [stage]} config
issuer (if (= stage "prod") "prod" "staging")
fqdn (:fqdn (:value (:out tf-out)))]
{:issuer issuer :fqdn fqdn}))