Skip to content

Commit

Permalink
Rename internal group-name var to module-name
Browse files Browse the repository at this point in the history
  • Loading branch information
julienvincent committed Oct 29, 2023
1 parent af356f1 commit 70faed8
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 97 deletions.
6 changes: 3 additions & 3 deletions src/k16/kl/api/executor.clj
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
(cond-> base
(seq containers) (assoc :services containers))))

(defn- exec-configuration! [{:keys [group-name module direction]}]
(defn- exec-configuration! [{:keys [module-name module direction]}]
(let [compose-data (build-docker-compose module)
compose-file (api.fs/from-submodule-work-dir group-name "docker-compose.yaml")
compose-file (api.fs/from-module-work-dir module-name "docker-compose.yaml")

direction (if (:services compose-data) direction :down)

Expand All @@ -41,7 +41,7 @@
(try
(proc/shell (concat
["docker" "compose"
"--project-name" (str "kl-" group-name)]
"--project-name" (str "kl-" module-name)]

args))
(catch Exception _))))
Expand Down
10 changes: 5 additions & 5 deletions src/k16/kl/api/fs.clj
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
(defn get-lock-file ^java.io.File [group-name]
(from-modules-dir group-name "module.lock.edn"))

(defn from-submodule-work-dir ^java.io.File [group-name & segments]
(from-modules-dir group-name ".kl" (flatten segments)))
(defn from-module-work-dir ^java.io.File [module-name & segments]
(from-modules-dir module-name ".kl" (flatten segments)))

(defn from-submodule-dir ^java.io.File [group-name module-name & segments]
(from-submodule-work-dir group-name ".modules" (name module-name) (flatten segments)))
(defn from-submodule-dir ^java.io.File [module-name submodule-name & segments]
(from-module-work-dir module-name ".modules" (name submodule-name) (flatten segments)))

(defn read-edn [^java.io.File file]
(try
Expand All @@ -39,7 +39,7 @@
(let [contents (with-out-str (pprint/pprint data))]
(spit file contents)))

(defn list-configuration-groups []
(defn list-modules []
(let [dir (from-modules-dir)]
(->> (.listFiles dir)
(filter (fn [^java.io.File file]
Expand Down
14 changes: 7 additions & 7 deletions src/k16/kl/api/module.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

(set! *warn-on-reflection* true)

(defn- merge-modules [group-name root-module sub-modules]
(let [state (api.state/get-state group-name)
(defn- merge-modules [module-name root-module sub-modules]
(let [state (api.state/get-state module-name)
merged
(->> sub-modules
(reduce (fn [acc [module-name]]
(let [module-file (api.fs/from-submodule-dir group-name module-name "module.edn")
(reduce (fn [acc [submodule-name]]
(let [module-file (api.fs/from-submodule-dir module-name submodule-name "module.edn")
module (api.fs/read-edn module-file)]
(metamerge/meta-merge acc module)))
{}))]
Expand All @@ -20,6 +20,6 @@
root-module
(select-keys state [:network :containers]))))

(defn get-resolved-module [group-name modules]
(let [root-module (api.fs/read-edn (api.fs/get-root-module-file group-name))]
(dissoc (merge-modules group-name root-module modules) :modules)))
(defn get-resolved-module [module-name modules]
(let [root-module (api.fs/read-edn (api.fs/get-root-module-file module-name))]
(dissoc (merge-modules module-name root-module modules) :modules)))
8 changes: 4 additions & 4 deletions src/k16/kl/api/proxy.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
[k16.kl.api.fs :as api.fs]
[meta-merge.core :as metamerge]))

(defn- get-proxies-projection-file [group-name]
(api.fs/from-config-dir "proxy/" (str group-name ".yaml")))
(defn- get-proxies-projection-file [module-name]
(api.fs/from-config-dir "proxy/" (str module-name ".yaml")))

(defn- route->traefik-rule [{:keys [host path-prefix]}]
(cond-> (str "Host(`" host "`)")
Expand Down Expand Up @@ -43,7 +43,7 @@

(metamerge/meta-merge services routes)))

(defn write-proxy-config! [{:keys [group-name module]}]
(defn write-proxy-config! [{:keys [module-name module]}]
(let [routes (build-routes module)
file (get-proxies-projection-file group-name)]
file (get-proxies-projection-file module-name)]
(spit file (yaml/generate-string routes))))
28 changes: 14 additions & 14 deletions src/k16/kl/api/resolver.clj
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,28 @@

(defn- resolve-modules [{:keys [module lock force-resolve?]}]
(->> (:modules module)
(map (fn [[module-name module]]
(map (fn [[submodule-name location]]
(p/vthread
(let [lock-entry (get lock module-name)
(let [lock-entry (get lock submodule-name)

should-resolve?
(or (not (:sha lock-entry))

(and (:sha module) (not= (:sha module) (:sha lock-entry)))
(and (:ref module) (not= (:ref module) (:ref lock-entry)))
(and (:subdir module) (not= (:subdir module) (:subdir lock-entry)))
(and (:sha location) (not= (:sha location) (:sha lock-entry)))
(and (:ref location) (not= (:ref location) (:ref lock-entry)))
(and (:subdir location) (not= (:subdir location) (:subdir lock-entry)))

force-resolve?)]
(if should-resolve?
[module-name (resolve-module-sha module)]
[module-name lock-entry])))))
[submodule-name (resolve-module-sha location)]
[submodule-name lock-entry])))))
doall
(map (fn [promise] @promise))
(into {})))

(defn pull! [group-name {:keys [update-lockfile? force?]}]
(let [module (api.fs/read-edn (api.fs/get-root-module-file group-name))
lock (api.fs/read-edn (api.fs/get-lock-file group-name))
(defn pull! [module-name {:keys [update-lockfile? force?]}]
(let [module (api.fs/read-edn (api.fs/get-root-module-file module-name))
lock (api.fs/read-edn (api.fs/get-lock-file module-name))

modules (resolve-modules {:module module
:lock lock
Expand All @@ -65,17 +65,17 @@

downloads (when (or lockfile-updated? force?)
(->> modules
(map (fn [[module-name module]]
(map (fn [[submodule-name module]]
(p/vthread
(resolver.downloader/download-remote-module!
{:group-name group-name
:module-name (name module-name)
{:module-name module-name
:submodule-name (name submodule-name)
:module module}))))

doall))]

(when lockfile-updated?
(api.fs/write-edn (api.fs/get-lock-file group-name) modules))
(api.fs/write-edn (api.fs/get-lock-file module-name) modules))

(when downloads
(doseq [download downloads]
Expand Down
10 changes: 5 additions & 5 deletions src/k16/kl/api/resolver/downloader.clj
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
(str/replace acc (str "{{" (name key) "}}") value))
contents)))

(defn download-remote-module! [{:keys [group-name module-name module]}]
(defn download-remote-module! [{:keys [module-name submodule-name module]}]
(let [{:keys [sha url subdir]
:or {subdir ".kl"}} module
sha-short (subs sha 0 7)

submodule-dir (-> (api.fs/from-submodule-dir group-name module-name)
submodule-dir (-> (api.fs/from-submodule-dir module-name submodule-name)
.toString)

vars {:SHA sha
Expand All @@ -53,9 +53,9 @@
(->> (:include config)
(map (fn [file]
(p/vthread
(log/info (str "Downloading " file " [" module-name "]"))
(log/info (str "Downloading " file " [" submodule-name "]"))
(let [contents (-> (read-repo-file url sha (relative-to subdir file))
(replace-vars vars))]
(spit (api.fs/from-submodule-dir group-name module-name file) contents)))))))
(spit (api.fs/from-submodule-dir module-name submodule-name file) contents)))))))

(api.fs/write-edn (api.fs/from-submodule-dir group-name module-name "module.edn") config))))
(api.fs/write-edn (api.fs/from-submodule-dir module-name submodule-name "module.edn") config))))
12 changes: 6 additions & 6 deletions src/k16/kl/api/state.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
(:require
[k16.kl.api.fs :as api.fs]))

(defn get-state-file [group-name]
(api.fs/from-submodule-work-dir group-name "state.edn"))
(defn get-state-file [module-name]
(api.fs/from-module-work-dir module-name "state.edn"))

(defn get-state [group-name]
(api.fs/read-edn (get-state-file group-name)))
(defn get-state [module-name]
(api.fs/read-edn (get-state-file module-name)))

(defn save-state [group-name state]
(api.fs/write-edn (get-state-file group-name) state))
(defn save-state [module-name state]
(api.fs/write-edn (get-state-file module-name) state))
37 changes: 19 additions & 18 deletions src/k16/kl/commands/containers.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
{:command "list"
:description "Select containers to run"

:opts [{:option "group"
:opts [{:option "module"
:short 0
:type :string}]

:runs (fn [props]
(let [group-name (prompt.config/get-group-name props)
(let [module-name (prompt.config/get-module-name props)

{:keys [modules]} (api.resolver/pull! group-name {})
module (api.module/get-resolved-module group-name modules)
{:keys [modules]} (api.resolver/pull! module-name {})
module (api.module/get-resolved-module module-name modules)

state (api.state/get-state group-name)
state (api.state/get-state module-name)

containers (->> (:containers module)
(map (fn [[container-name container]]
Expand All @@ -41,17 +41,17 @@
{:command "run"
:description "Select containers to run"

:opts [{:option "group"
:opts [{:option "module"
:short 0
:type :string}]

:runs (fn [props]
(let [group-name (prompt.config/get-group-name props)
(let [module-name (prompt.config/get-module-name props)

{:keys [modules]} (api.resolver/pull! group-name {})
module (api.module/get-resolved-module group-name modules)
{:keys [modules]} (api.resolver/pull! module-name {})
module (api.module/get-resolved-module module-name modules)

state (api.state/get-state group-name)
state (api.state/get-state module-name)

options (->> (:containers module)
(map (fn [[container-name]]
Expand All @@ -71,27 +71,28 @@
[container-name {:enabled enabled}])))
(into {})))]

(api.state/save-state group-name updated-state)
(api.state/save-state module-name updated-state)

(let [module (metamerge/meta-merge module updated-state)]
(api.proxy/write-proxy-config! {:group-name group-name
(api.proxy/write-proxy-config! {:module-name module-name
:module module})
(api.executor/start-configuration! {:group-name group-name
(api.executor/start-configuration! {:module-name module-name
:module module}))))})

(def ^:private stop-cmd
{:command "down"
:description "Stop all running containers "

:opts [{:option "group"
:opts [{:option "module"
:short 0
:type :string}]

:runs (fn [props]
(let [group-name (prompt.config/get-group-name props)
services (api.fs/read-edn (api.fs/get-root-module-file group-name))]
(api.executor/stop-configuration! {:group-name group-name
:services services})))})
(let [module-name (prompt.config/get-module-name props)
{:keys [modules]} (api.resolver/pull! module-name {})
module (api.module/get-resolved-module module-name modules)]
(api.executor/stop-configuration! {:module-name module-name
:module module})))})

(def cmd
{:command "containers"
Expand Down
8 changes: 4 additions & 4 deletions src/k16/kl/commands/endpoints.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
[k16.kl.prompt.config :as prompt.config]))

(defn- list-endpoints [props]
(let [group-name (prompt.config/get-group-name props)
(let [module-name (prompt.config/get-module-name props)

{:keys [modules]} (api.resolver/pull! group-name {})
module (api.module/get-resolved-module group-name modules)
{:keys [modules]} (api.resolver/pull! module-name {})
module (api.module/get-resolved-module module-name modules)

selected-service-name (keyword (:service props))

Expand Down Expand Up @@ -37,7 +37,7 @@
:subcommands [{:command "list"
:description "List all endpoints"

:opts [{:option "group"
:opts [{:option "module"
:short 0
:type :string}

Expand Down
16 changes: 8 additions & 8 deletions src/k16/kl/commands/modules.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,32 @@
(set! *warn-on-reflection* true)

(defn- pull! [{:keys [update force] :as props}]
(let [group-name (prompt.config/get-group-name props)
updated? (api.resolver/pull! group-name {:update-lockfile? update
:force? force})]
(let [module-name (prompt.config/get-module-name props)
updated? (api.resolver/pull! module-name {:update-lockfile? update
:force? force})]
(if updated?
(log/info "Services updated")
(log/info "Services are all up to date"))))

(defn- set-default-module! [props]
(let [group-name (prompt.config/get-group-name (assoc props :skip-default? true))]
(api.fs/write-edn (api.fs/get-config-file) {:default-module group-name})))
(let [module-name (prompt.config/get-module-name (assoc props :skip-default? true))]
(api.fs/write-edn (api.fs/get-config-file) {:default-module module-name})))

(def cmd
{:command "module"
:description "Manage module configurations"

:subcommands [{:command "set-default"
:description "Set the default module"
:opts [{:option "group"
:opts [{:option "module"
:short 0
:type :string}]
:runs set-default-module!}

{:command "pull"
:description "Pull down changes to a module"

:opts [{:option "group"
:opts [{:option "module"
:short 0
:type :string}

Expand All @@ -50,7 +50,7 @@
{:command "update"
:description "Resolve the latest sha's of a module. This is the same as `pull --update`"

:opts [{:option "group"
:opts [{:option "module"
:short 0
:type :string}

Expand Down
Loading

0 comments on commit 70faed8

Please sign in to comment.