From 0311d683712160eebad24838b36f5fa66333a32d Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Wed, 6 Mar 2024 17:57:11 +0100 Subject: [PATCH] first results for dbuild --- .gitignore | 2 ++ deps.edn | 6 +++- infrastructure/backup/bb.edn | 8 +++++ src/main/clj/dda/devops_build/devops.clj | 21 +++++++---- .../clj/dda/devops_build/devops/domain.clj | 35 ++++++++++++++++++ src/main/clj/dda/devops_build/image.clj | 36 +++++++------------ .../clj/dda/devops_build/image/domain.clj | 17 +++++++++ .../dda/devops_build/devops/domain_test.clj | 32 +++++++++++++++++ .../dda/devops_build/image/domain_test.clj | 25 +++++++++++++ 9 files changed, 151 insertions(+), 31 deletions(-) create mode 100644 infrastructure/backup/bb.edn create mode 100644 src/main/clj/dda/devops_build/devops/domain.clj create mode 100644 src/main/clj/dda/devops_build/image/domain.clj create mode 100644 src/test/clj/dda/devops_build/devops/domain_test.clj create mode 100644 src/test/clj/dda/devops_build/image/domain_test.clj diff --git a/.gitignore b/.gitignore index 4fa8f10..ee9ffac 100644 --- a/.gitignore +++ b/.gitignore @@ -109,3 +109,5 @@ venv.bak/ .clj-kondo/ .lsp/ +.calva +.cpcache diff --git a/deps.edn b/deps.edn index d076d5a..95baef9 100644 --- a/deps.edn +++ b/deps.edn @@ -1,3 +1,7 @@ {:paths ["src/main/clj"] :deps {org.clojure/spec.alpha {:mvn/version "0.4.233"} - orchestra {:mvn/version "2021.01.01-1"}}} \ No newline at end of file + orchestra {:mvn/version "2021.01.01-1"}} + :aliases + {:test/env + {:extra-paths ["src/test/clj"] + :extra-deps {}}}} \ No newline at end of file diff --git a/infrastructure/backup/bb.edn b/infrastructure/backup/bb.edn new file mode 100644 index 0000000..4d578f7 --- /dev/null +++ b/infrastructure/backup/bb.edn @@ -0,0 +1,8 @@ +{:deps {dda/dda-devops-build {:local/root "../../."}} + :tasks + {:requires ([dda.devops-build.image :as image]) + image-build (image/dbuild {:name "dda-backup" + :project-root-path "../.." + :build-dir-name "target" + :version "4.11.8-dev"})}} + diff --git a/src/main/clj/dda/devops_build/devops.clj b/src/main/clj/dda/devops_build/devops.clj index 9b5f63c..baf0b3a 100644 --- a/src/main/clj/dda/devops_build/devops.clj +++ b/src/main/clj/dda/devops_build/devops.clj @@ -1,12 +1,19 @@ (ns dda.devops-build.devops - (:require [clojure.spec.alpha :as s])) + (:require + [clojure.spec.alpha :as s] + [dda.devops-build.devops.domain :as domain])) -(s/def ::name string?) -(s/def ::module string?) -(s/def ::stage string?) -(s/def ::project-root-path string?) -(s/def ::build-dir-name string?) +(s/def ::name ::domain/name) +(s/def ::module ::domain/module) +(s/def ::stage ::domain/stage) +(s/def ::project-root-path ::domain/project-root-path) +(s/def ::build-dir-name ::domain/build-dir-name) (s/def ::devops (s/keys :req-un [::name] - :opt-un [::module ::stage ::project-root-path ::build-dir-name])) \ No newline at end of file + :opt-un [::module ::stage ::project-root-path ::build-dir-name])) + +(def default {:name "dda-backup" + :project-root-path "." + :build-dir-name "target" + :stage "dev"}) diff --git a/src/main/clj/dda/devops_build/devops/domain.clj b/src/main/clj/dda/devops_build/devops/domain.clj new file mode 100644 index 0000000..75c734e --- /dev/null +++ b/src/main/clj/dda/devops_build/devops/domain.clj @@ -0,0 +1,35 @@ +(ns dda.devops-build.devops.domain + (:require + [clojure.string :as str] + [clojure.spec.alpha :as s] + [orchestra.core :refer [defn-spec]])) + +(s/def ::name string?) +(s/def ::module string?) +(s/def ::stage string?) +(s/def ::project-root-path string?) +(s/def ::build-dir-name string?) + +(s/def ::devops + (s/keys :req-un [::name ::stage ::project-root-path ::build-dir-name] + :opt-un [::module])) + +(defn-spec build-path string? + [devops ::devops] + (let + [{:keys [project-root-path build-dir-name name module]} devops] + (str/join + "/" + (filter + some? + [project-root-path build-dir-name name module])))) + +(defn-spec clean-build-dir-command seq? + [devops ::devops] + (let [{:keys [name]} devops] + ["rm" "-rf" (build-path devops)])) + +(defn-spec create-build-dir-command seq? + [devops ::devops] + (let [{:keys [name]} devops] + ["mkdir" "-p" (build-path devops)])) diff --git a/src/main/clj/dda/devops_build/image.clj b/src/main/clj/dda/devops_build/image.clj index 720bc18..6aecd00 100644 --- a/src/main/clj/dda/devops_build/image.clj +++ b/src/main/clj/dda/devops_build/image.clj @@ -1,28 +1,18 @@ (ns dda.devops-build.image - (:require [clojure.spec.alpha :as s] - [babashka.process :as p] + (:require [orchestra.core :refer [defn-spec]] [babashka.tasks :as t] - [orchestra.core :refer [defn-spec]] - [dda.devops-build.devops :as d])) + [clojure.string :as str] + [dda.devops-build.devops :as d] + [dda.devops-build.devops.domain :as dd] + [dda.devops-build.image.domain :as domain])) -(s/def ::options (s/* #{"-h"})) -(s/def ::cmd-args (s/cat :options ::options - :args any?)) +(def default + (merge d/default {})) -(defn invalid-args-msg - [spec args] - (s/explain spec args) - (println (str "Bad commandline arguments\n"))) - -(defn-spec - dbuild any? +(defn-spec dbuild nil? [devops ::d/devops] - (let [{:keys [name]} devops] - (t/shell "echo 'docker" "build" "-t" name "--file" "{path}/image/Dockerfile" "{path}/image"))) - -(defn drun [& cmd-args] - (let [parsed-args-cmd (s/conform ::cmd-args cmd-args)] - (if (= ::s/invalid parsed-args-cmd) - (invalid-args-msg ::cmd-args cmd-args) - (-> (p/process "docker run" "-it" "{name}" "/bin/bash") - (p/check))))) \ No newline at end of file + (let [final (merge default devops)] + (apply t/shell (dd/clean-build-dir-command final)) + (apply t/shell (dd/create-build-dir-command final)) + (apply t/shell (domain/copy-image-command final)) + (apply t/shell (domain/dbuild-command final)))) diff --git a/src/main/clj/dda/devops_build/image/domain.clj b/src/main/clj/dda/devops_build/image/domain.clj new file mode 100644 index 0000000..ca164d2 --- /dev/null +++ b/src/main/clj/dda/devops_build/image/domain.clj @@ -0,0 +1,17 @@ +(ns dda.devops-build.image.domain + (:require [clojure.spec.alpha :as s] + [orchestra.core :refer [defn-spec]] + [dda.devops-build.devops.domain :as d])) + +(defn-spec copy-image-command seq? + [devops ::d/devops] + (let [{:keys [name]} devops] + ["cp" "-r" "image" (d/build-path devops)])) + + +(defn-spec dbuild-command seq? + [devops ::d/devops] + (let [{:keys [name]} devops] + ["docker" "build" "-t" name "--file" + (str (d/build-path devops) "/image/Dockerfile") + (str (d/build-path devops) "/image")])) diff --git a/src/test/clj/dda/devops_build/devops/domain_test.clj b/src/test/clj/dda/devops_build/devops/domain_test.clj new file mode 100644 index 0000000..e090e6a --- /dev/null +++ b/src/test/clj/dda/devops_build/devops/domain_test.clj @@ -0,0 +1,32 @@ +(ns dda.devops-build.devops.domain-test + (:require + [clojure.test :refer [deftest is are testing run-tests]] + [dda.devops-build.devops.domain :as cut])) + +(deftest should-calculate-build-path + (is (= "../../target/dda-backup" + (cut/build-path {:name "dda-backup" + :project-root-path "../.." + :build-dir-name "target" + :version "4.11.8-dev"}))) + (is (= "../../target/dda/backup" + (cut/build-path {:name "dda" + :module "backup" + :project-root-path "../.." + :build-dir-name "target" + :version "4.11.8-dev"})))) + +(deftest should-calculate-clean-build-dir-command + (is (= ["rm" "-rf" "../../target/dda-backup"] + (cut/clean-build-dir-command {:name "dda-backup" + :project-root-path "../.." + :build-dir-name "target" + :version "4.11.8-dev"})))) + +(deftest should-calculate-create-build-dir-command + (is (= ["mkdir" "-p" "../../target/dda-backup"] + (cut/create-build-dir-command {:name "dda-backup" + :project-root-path "../.." + :build-dir-name "target" + :version "4.11.8-dev"})))) + diff --git a/src/test/clj/dda/devops_build/image/domain_test.clj b/src/test/clj/dda/devops_build/image/domain_test.clj new file mode 100644 index 0000000..1331cbc --- /dev/null +++ b/src/test/clj/dda/devops_build/image/domain_test.clj @@ -0,0 +1,25 @@ +(ns dda.devops-build.image.domain-test + (:require + [clojure.test :refer [deftest is are testing run-tests]] + [dda.devops-build.image.domain :as cut])) + +(deftest should-calculate-copy-image-command + (is (= ["cp" "-r" "image" "../../target/dda-backup"] + (cut/copy-image-command {:name "dda-backup" + :project-root-path "../.." + :build-dir-name "target" + :version "4.11.8-dev"})))) + +(deftest should-calculate-dbuild-command + (is (= ["docker" + "build" + "-t" + "dda-backup" + "--file" + "../../target/dda-backup/image/Dockerfile" + "../../target/dda-backup/image"] + (cut/dbuild-command {:name "dda-backup" + :project-root-path "../.." + :build-dir-name "target" + :version "4.11.8-dev"})))) +