Add DigitalOcean util example [skip ci] (#673)

This commit is contained in:
Michael Salihi 2020-12-08 15:54:10 +01:00 committed by GitHub
parent 661019e494
commit 83f16421c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 0 deletions

View file

@ -381,3 +381,15 @@ Example usage:
``` shell
$ 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
```

View 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))