first results for dbuild

This commit is contained in:
Michael Jerger 2024-03-06 17:57:11 +01:00
parent 198713c229
commit 0311d68371
9 changed files with 151 additions and 31 deletions

2
.gitignore vendored
View file

@ -109,3 +109,5 @@ venv.bak/
.clj-kondo/
.lsp/
.calva
.cpcache

View file

@ -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"}}}
orchestra {:mvn/version "2021.01.01-1"}}
:aliases
{:test/env
{:extra-paths ["src/test/clj"]
:extra-deps {}}}}

View file

@ -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"})}}

View file

@ -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]))
:opt-un [::module ::stage ::project-root-path ::build-dir-name]))
(def default {:name "dda-backup"
:project-root-path "."
:build-dir-name "target"
:stage "dev"})

View file

@ -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)]))

View file

@ -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)))))
(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))))

View file

@ -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")]))

View file

@ -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"}))))

View file

@ -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"}))))