Browse Source

First commit

Stephen Starkey 3 months ago
commit
a89ba263be
5 changed files with 115 additions and 0 deletions
  1. 5
    0
      .gitignore
  2. 40
    0
      README.md
  3. 10
    0
      ensure-mounted
  4. 9
    0
      project.clj
  5. 51
    0
      src/ensure_mounted/core.clj

+ 5
- 0
.gitignore View File

@@ -0,0 +1,5 @@
1
+target
2
+.nrepl-port
3
+
4
+.idea
5
+ensure-mounted.iml

+ 40
- 0
README.md View File

@@ -0,0 +1,40 @@
1
+# ensure-mounted
2
+
3
+A simple program for making sure folders are mounted. Eventually.
4
+Meant to be run periodically as a cron job.
5
+
6
+## Requirements
7
+
8
+* [Java](https://adoptopenjdk.net/)
9
+* [Leiningen](https://leiningen.org/)
10
+
11
+## Deploy to Clojars
12
+
13
+```
14
+lein deploy clojars
15
+```
16
+
17
+## Prepare your /etc/fstab
18
+Add the following:
19
+
20
+above the first mount point you want to ensure is mounted: 
21
+
22
+```
23
+#== ensure-mounted
24
+```
25
+
26
+below the last mount point you want to ensure is mounted:
27
+
28
+```
29
+#== ensure-mounted-end
30
+```
31
+
32
+## Install & Run
33
+1. Install [inlein](https://github.com/hyPiRion/inlein/wiki/Getting-Started)
34
+2. Run the following: `
35
+curl https://git.calmabiding.me/scstarkey/ensure-mounted/raw/branch/master/ensure-mounted 
36
+-o ~/bin/ensure-mounted && chmod +x ~/bin/ensure-mounted `
37
+
38
+```
39
+./ensure-mounted
40
+```

+ 10
- 0
ensure-mounted View File

@@ -0,0 +1,10 @@
1
+#!/usr/bin/env inlein
2
+
3
+;; See: https://github.com/hyPiRion/inlein/wiki/Getting-Started
4
+
5
+'{:dependencies [[org.clojure/clojure "1.10.1"]
6
+                 [ensure-mounted "LATEST"]]}
7
+
8
+(require '[ensure-mounted.core :as em])
9
+
10
+(apply em/-main *command-line-args*)

+ 9
- 0
project.clj View File

@@ -0,0 +1,9 @@
1
+(defproject ensure-mounted "0.1.0"
2
+  :description "Ensure that directories are mounted"
3
+  :url "https://git.calmabiding.me/scstarkey/ensure-mounted"
4
+  :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
5
+            :url  "https://www.eclipse.org/legal/epl-2.0/"}
6
+  :dependencies [[org.clojure/clojure "1.10.1"]]
7
+  :main ensure-mounted.core
8
+  :uberjar-name "ensure-mounted.standalone.jar"
9
+  :profiles {:uberjar {:aot :all}})

+ 51
- 0
src/ensure_mounted/core.clj View File

@@ -0,0 +1,51 @@
1
+(ns ensure-mounted.core
2
+  (:require [clojure.java.shell :as sh]
3
+            [clojure.set :as set]
4
+            [clojure.string :as str])
5
+  (:import (java.io BufferedReader FileInputStream InputStreamReader))
6
+  (:gen-class))
7
+
8
+(def start-ensure? (partial = "#== ensure-mounted"))
9
+(def end-ensure? (partial = "#== ensure-mounted-end"))
10
+(defn mount-point [line] (-> line (str/split #" ") second))
11
+(def mount-points (comp (partial apply hash-set) (partial map mount-point)))
12
+
13
+(defn lines [filename]
14
+  (with-open [f (FileInputStream. filename)
15
+              ir (InputStreamReader. f)
16
+              r (BufferedReader. ir)]
17
+    (doall (line-seq r))))
18
+
19
+(defn assess-line [{:keys [block-active?] :as result} line]
20
+  (cond
21
+    (start-ensure? line) (assoc result :block-active? true)
22
+    (end-ensure? line) (assoc result :block-active? false)
23
+    block-active? (update result :lines conj line)
24
+    :else result))
25
+
26
+(defn desired-block [lines]
27
+  (->> lines (reduce assess-line {:block-active? false, :lines []}) :lines))
28
+
29
+(defn mount! [path] (sh/sh "mount" path))
30
+
31
+(defn assess-result [results {:keys [exit err]}]
32
+  (if (= 0 exit)
33
+    results
34
+    (-> results (assoc :failed? true) (update :errs conj err))))
35
+
36
+(defn mount-missing! []
37
+  (let [existing (-> "/proc/mounts" lines mount-points)
38
+        desired (-> "/etc/fstab" lines desired-block mount-points)
39
+        necessary (set/difference desired existing)
40
+        results (doall (map mount! necessary))]
41
+    (if (seq desired)
42
+      (reduce assess-result {:failed? false :errs []} results)
43
+      {:failed? true
44
+       :errs ["Unable to find any mount points. Is your fstab annotated?"]})))
45
+
46
+(defn -main [& _]
47
+  (let [{:keys [failed? errs]} (mount-missing!)]
48
+    (when failed?
49
+      (binding [*out* *err*] (-> errs str/join println))
50
+      (System/exit 1)))
51
+  (System/exit 0))