implement writing a unit file

This commit is contained in:
hiro98 2020-07-05 21:43:46 +02:00
parent 3ff5347d5b
commit 57ed429c6e
6 changed files with 69 additions and 4 deletions

View file

@ -6,6 +6,7 @@
:dependencies [[org.clojure/clojure "1.10.1"]
[com.github.thjomnx/java-systemd "1.1.0"]
[com.taoensso/timbre "4.10.0"]
[cljstache/cljstache "2.0.6"]
[org.clojure/core.async "1.2.603"]]
:main ^:skip-aot stream.core
:target-path "target/%s"

View file

@ -0,0 +1,8 @@
[Unit]
Description={{ description }}
[Service]
ExecStart={{ command }}
[Install]
WantedBy={{ target }}

View file

@ -0,0 +1,11 @@
(ns stream.commander.api)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Globals, Structures and State ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; TODO: specs
(defrecord process
[unit-name monitor-channel ffmpeg-config problems])
(def processes (ref {}))

View file

@ -0,0 +1,33 @@
(ns stream.commander.impl
(:require [cljstache.core :as template]
[clojure.java.io :as io]
[taoensso.timbre :as timbre
:refer [log trace debug info warn error fatal report
logf tracef debugf infof warnf errorf fatalf reportf
spy get-env]]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Unit file Management ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn- render-unit-file
"Renders the unit file from a template.
The template can be found at:
resources/commander/templates/unit.mustache"
[command description target]
(template/render-resource "commander/templates/unit.mustache"
{:description description
:command command
:target target}))
(defn create-unit-file!
"Creates or overwrites a service unit file in the appropriate
directory and returns the file path."
[name command description target]
(let [unit-contents (render-unit-file command description target)
path (str (System/getProperty "user.home") "/.config/systemd/user/" name ".service")
file (io/as-file path)]
(.mkdirs (.getParentFile file))
(spit path unit-contents)
(debug "Wrote a unit file to:" path)
path))

View file

@ -0,0 +1,16 @@
(ns stream.commander-test
(:require [stream.commander.impl :as impl]
[stream.commander.api :as api]
[clojure.test :refer :all]
[clojure.java.io :as io]))
(deftest unit-files
(testing "The rendering."
(is (#'impl/render-unit-file "test" "test" "test")))
(let [name (str (java.util.UUID/randomUUID))]
(testing "Writing a unit file."
(let [file (io/as-file
(impl/create-unit-file! name
"test" "test" "test"))]
(is (.exists file))
(.delete file)))))

View file

@ -1,7 +1,3 @@
(ns stream.core-test
(:require [clojure.test :refer :all]
[stream.core :refer :all]))
(deftest a-test
(testing "FIXME, I fail."
(is (= 0 1))))