Add DigitalOcean util example [skip ci] (#673)
This commit is contained in:
parent
661019e494
commit
83f16421c0
2 changed files with 42 additions and 0 deletions
|
|
@ -381,3 +381,15 @@ Example usage:
|
||||||
``` shell
|
``` shell
|
||||||
$ cat src/babashka/main.clj | bb examples/rofi.clj
|
$ cat src/babashka/main.clj | bb examples/rofi.clj
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### [digitalocean-ping.clj](digitalocean-ping.clj)
|
||||||
|
|
||||||
|
The script allows to define which DigitalOcean cloud datacenter (region) has best network performance (ping latency).
|
||||||
|
|
||||||
|
See [digitalocean-ping.clj](digitalocean-ping.clj)
|
||||||
|
|
||||||
|
Example usage:
|
||||||
|
|
||||||
|
``` shell
|
||||||
|
$ bb digitalocean-ping.clj
|
||||||
|
```
|
||||||
|
|
|
||||||
30
examples/digitalocean-ping.clj
Normal file
30
examples/digitalocean-ping.clj
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
#!/usr/bin/env bb
|
||||||
|
|
||||||
|
(require '[babashka.curl :as curl]
|
||||||
|
'[clojure.java.shell :as shell]
|
||||||
|
'[clojure.string :as str])
|
||||||
|
|
||||||
|
(def url "http://speedtest-ams2.digitalocean.com/")
|
||||||
|
|
||||||
|
(def get-endpoints
|
||||||
|
(let [{:keys [body]} (curl/get url)]
|
||||||
|
(re-seq #"speedtest\-.+.digitalocean.com" body)))
|
||||||
|
|
||||||
|
(defn get-average [result]
|
||||||
|
(-> result
|
||||||
|
str/split-lines
|
||||||
|
last
|
||||||
|
(str/split #"/")
|
||||||
|
(get 4)))
|
||||||
|
|
||||||
|
(def mac? (str/starts-with? (System/getProperty "os.name") "Mac"));; TODO: test on Windows
|
||||||
|
|
||||||
|
(def timeout-arg (if mac? "-t3" "-w3"))
|
||||||
|
|
||||||
|
(defn ping-result [endpoint]
|
||||||
|
(let [{:keys [out]} (shell/sh "ping" "-c5" timeout-arg endpoint)
|
||||||
|
msg (str endpoint " => " (get-average out) "ms")]
|
||||||
|
(println msg)))
|
||||||
|
|
||||||
|
(doseq [endpoint get-endpoints]
|
||||||
|
(ping-result endpoint))
|
||||||
Loading…
Reference in a new issue