-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.clj
60 lines (51 loc) · 1.52 KB
/
node.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
(ns re-share.es.node
"Elasticsearch node management"
(:require
[clojure.core.strint :refer (<<)]
[qbits.spandex :as s]
[taoensso.timbre :refer (refer-timbre)]
[safely.core :refer [safely]])
(:import
[org.apache.http.ssl SSLContextBuilder]
[org.apache.http.conn.ssl TrustSelfSignedStrategy]))
(refer-timbre)
(def c (atom nil))
(defn connection []
(if @c
@c
(throw (ex-info "no connection is set for Elasticsearch" {}))))
(defn health
"get cluster health"
[]
(:body (s/request (connection) {:url ["_cluster" "health"] :method :get})))
(defn check
"check the connection is working and cluster is healthy"
[]
(try
(let [h (health)]
(if (= "red" (h :status))
(throw (ex-info "Elasticsearch is read" h))))
(catch java.net.ConnectException e
(throw (ex-info "Elasticsearch is down" {})))))
(defn self-signed-context
"trusting self signed ssl certs"
[]
(.build (.loadTrustMaterial (SSLContextBuilder/create) (TrustSelfSignedStrategy.))))
(defn add-context [m]
(assoc-in m [:http-client :ssl-context] (self-signed-context)))
(defn connect
"Connecting to Elasticsearch"
[{:keys [host auth self?]}]
(when-not @c
(info (<< "Connecting to elasticsearch ~{host}"))
(let [m {:hosts [host] :http-client {:auth-caching? true :basic-auth auth}}]
(reset! c
(s/client (if self? (add-context m) m)))
(check))))
(defn stop
"Reset connection atom"
[]
(info "Closing elasticsearch connection")
(when @c
(s/close! @c)
(reset! c nil)))