/
zfs.clj
55 lines (44 loc) · 1.79 KB
/
zfs.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
(ns re-mote.repl.zfs
"A bunch of function for ZFS automation"
(:require
[clj-time.format :as f]
[clj-time.local :refer [local-now]]
[clojure.core.strint :refer (<<)]
[pallet.stevedore :refer (script)]
[re-mote.repl.output :refer (refer-out)]
[re-mote.repl.base :refer (refer-base)]))
(refer-base)
(refer-out)
(defn scrub [hs pool]
(run (exec hs (<< "sudo /sbin/zpool scrub ~{pool}")) | (pretty "scrub")))
(def errors "'(DEGRADED|FAULTED|OFFLINE|UNAVAIL|REMOVED|FAIL|DESTROYED|corrupt|cannot|unrecover)'")
(defn healty [pool errors]
(script
(pipe ("/sbin/zpool" "status" ~pool) ("egrep" "-v" "-i" ~errors))))
(defn cap-with-range [maximum]
(script
(set! used @("/sbin/zpool" "list" "-H" "-o" "capacity" | "cut" "-d'%'" "-f1"))
(if (>= @used ~maximum)
(chain-and (println "used capacity is too high" @used "maximum allowed is" ~maximum) ("exit" 1))
("exit" 0))))
(defn purging
"purge script"
[pool dataset n]
(let [n+ (str "+" n) from (str pool "/" dataset)]
(script
(pipe
(pipe ("zfs" "list" "-H" "-t" "snapshot" "-o" "name" "-S" "creation" "-d1" ~from) ("tail" "-n" ~n+))
("xargs" "-r" "-n" "1" "zfs" "destroy" "-r")))))
(defn health [hs pool]
(run> (exec hs (healty pool errors)) | (pretty "health")))
(defn capacity [hs maximum]
(run> (exec hs (cap-with-range maximum)) | (pretty "capacity")))
(defn snapshot [hs pool dataset]
(let [date (f/unparse (f/formatter "dd-MM-YYYY_hh:mm:ss_SS") (local-now))]
(run> (exec hs (<< "/sbin/zfs snapshot ~{pool}/~{dataset}@~{date}")) | (pretty "snapshot"))))
(defn purge
"clear last n snapshots of a dataset"
[hs pool dataset n]
(run> (exec hs (purging pool dataset n)) | (pretty "purge")))
(defn refer-zfs []
(require '[re-mote.repl.zfs :as zfs :refer (health snapshot scrub capacity purge)]))