/
base.clj
192 lines (152 loc) · 4.88 KB
/
base.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
(ns re-mote.repl.base
(:require
[clojure.java.shell :refer [sh]]
[clojure.core.strint :refer (<<)]
[clojure.edn :as edn]
[clojure.tools.trace :as t]
[clojure.java.io :refer (reader file)]
[taoensso.timbre :refer (refer-timbre)]
[hara.data.map :refer (dissoc-in)]
[re-mote.ssh.pipeline :refer (run-hosts upload-hosts)]
[pallet.stevedore.bash]
[pallet.stevedore :refer (script)]))
(refer-timbre)
(defn sh-hosts
"Run a local commands against hosts"
[{:keys [auth hosts]} sh-fn]
(let [results (map (fn [host] (assoc (sh-fn host) :host host)) hosts)
grouped (group-by :code results)]
{:hosts hosts :success (grouped 0) :failure (dissoc grouped 0)}))
(.bindRoot #'pallet.stevedore/*script-language* :pallet.stevedore.bash/bash)
(defmacro | [source fun & funs]
(let [f (first fun) args (rest fun)]
`(let [[this# res#] ~source]
(~f this# res# ~@args))))
(defmacro run>
"Run and return last function pipe output"
[f p s & fns]
(if-not (empty? fns)
`(run> (~p ~f ~s) ~(first fns) ~(second fns) ~@(rest (rest fns)))
`(~p ~f ~s)))
(defmacro run
"Run and nullify output"
[f p s & fns]
`(let [[this# m#] (run> ~f ~p ~s ~@fns)]
[this# {}]))
(defn safe-output [{:keys [out err exit]}]
(when (seq out)
(debug out))
(when-not (zero? exit)
(error err exit))
{:code exit :out out :error err})
(def safe (comp safe-output sh))
(defprotocol Shell
(exec [this script])
(nohup [this script])
(null [this m])
(rm
[this target flags]
[this m target flags])
(ls [this target flags])
(grep [this expr flags])
(mkdir [this folder flags])
(cp [this src dest flags]))
(defprotocol Tracing
(ping [this target]))
(defprotocol Copy
(scp
[this src dest]
[this m src dest])
(sync-2
[this src dest]
[this m src dest])
(sync-
[this src dest]
[this m src dest]))
(defprotocol Tar
(extract [this m archive target]))
(defprotocol Performance
(measure [this m]))
(defprotocol Select
(initialize [this])
(downgrade
[this f]
[this m f]
[this m f args])
(pick
[this f]
[this m f]))
(defn rsync [src target host {:keys [user ssh-key]}]
(let [opts (if ssh-key (<< "-ae 'ssh -i ~{ssh-key}'") "-a")
dest (<< "~{user}@~{host}:~{target}")]
(script ("rsync" "--delete" ~opts ~src ~dest))))
(defn- merge-results [[_ {:keys [success failure] :as res}] m]
(-> m
(dissoc-in [:failure -1])
(clojure.core/update :success (partial into success))
(clojure.core/update :failure (partial merge-with conj failure))))
(defrecord Hosts [auth hosts]
Select
(initialize [this]
[this hosts])
(pick [this f]
(Hosts. auth (filter f hosts)))
(pick [this {:keys [failure success] :as m} f]
(let [hs (f success failure hosts)]
(if (empty? hs)
(throw (ex-info "no succesful hosts found" m))
[(Hosts. auth hs) {}])))
(downgrade [this f]
[this {}])
(downgrade [this {:keys [failure] :as m} f]
(downgrade this m f nil))
(downgrade [this {:keys [failure] :as m} f args]
(let [failed (map :host (get failure -1))]
(if-not (empty? failed)
[this (merge-results (apply f (Hosts. auth failed) args) m)]
[this m])))
Shell
(ls [this target flags]
[this (run-hosts this (script ("ls" ~flags ~target)))])
(mkdir [this folder flags]
[this (run-hosts this (script ("mkdir" ~flags ~folder)))])
(rm [this target flags]
[this (run-hosts this (script ("rm" ~flags ~target)))])
(rm [this _ target flags]
(rm this target flags))
(exec [this script]
[this (run-hosts this script)])
(nohup [this cmd]
(exec this (<< "nohup sh -c '~{cmd} &' &>/dev/null")))
(null [this m]
[this {}])
Tar
(extract [this _ archive target]
[this (run-hosts this (script ("tar" "-xzf" ~archive "-C" ~target)))])
Copy
(scp [this _ src target]
(scp this src target))
(scp [this src target]
[this (upload-hosts this src target)])
(sync- [this _ src target]
(sync- this src target))
(sync- [{:keys [auth hosts] :as this} src target]
[this (sh-hosts this (fn [host] (safe "bash" "-c" (rsync src target host auth))))])
(sync-2 [this _ src target]
(sync-2 this src target))
(sync-2 [{:keys [auth hosts] :as this} src target]
[this (run-hosts this (script ("rsync" "--delete" "-a" ~src ~target)))]) Tracing
(ping [this target]
[this (run-hosts this (script ("ping" "-c" 1 ~target)))]))
(defn successful
"Used for picking successful"
[success _ hs]
(filter (set (map :host success)) hs))
(defn into-hosts
"builds hosts from an edn file"
[f]
(let [{:keys [auth hosts]} (edn/read-string (slurp (file f)))]
(Hosts. auth hosts)))
(defn refer-base []
(require '[re-mote.repl.base :as base :refer (run> run | initialize pick successful ping ls into-hosts
exec scp extract rm nohup mkdir sync- sync-2 downgrade null)]))