Skip to content

Instantly share code, notes, and snippets.

@delyada

delyada/bb.edn Secret

Created May 6, 2021 18:28
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save delyada/9f50fa7466358e55f27e4e6b4314242f to your computer and use it in GitHub Desktop.
Save delyada/9f50fa7466358e55f27e4e6b4314242f to your computer and use it in GitHub Desktop.
Our bb.edn
{:paths [#_"bin" "src/clj" "resources" "env/ion/resources"]
:deps {}
:tasks
{:requires ([clojure.string :as str]
[yada.util.date :as date])
:init (do
(defn eshell [& args]
(let [s-args (str/join " " (map str/trimr args))]
(println s-args (str " <" (date/now->dh) ">"))
(apply shell args))))
-config {:extra-deps {cprop/cprop {:mvn/version "0.1.17"}}
:requires ([yada.util.env :as env])}
-config-app {:depends [-config -env-key]
:task (:yada (env/get-config {} -env-key))}
;; Sometimes depended-upon command line arguments
-args-str
(str/join " " *command-line-args*)
-args-rest-str
(str/join " " (rest *command-line-args*))
-arg1-env
{:depends [-envs-names]
:requires ([clojure.edn :as edn])
:task (let [env (some-> *command-line-args* first edn/read-string)]
(assert env (str "First argument must set environment among: " -envs-names))
env)}
-arg2-search-rg
{:web "https://github.com/BurntSushi/ripgrep/blob/master/GUIDE.md"
:task (let [sp (some-> *command-line-args* second)]
(assert sp (str "Second argument must be ripgrep's search pattern"))
sp)}
;; Constants
-envs-names {:depends [-config] :task (env/get-envs-names)} ; ":local, :sone, :demo"
-env-key {:depends [-arg1-env] :task -arg1-env} ; :local
-env-name {:depends [-env-key] :task (name -env-key)} ; "local"
-deps-alias {:depends [-config-app] :task (-> -config-app :bin :deps-alias)}
-deps-main {:depends [-config-app] :task (-> -config-app :bin :deps-main)}
-system {:depends [-config-app] :task (-> -config-app :ion :system)}
-app {:depends [-config-app] :task (-> -config-app :ion :app)}
-compute-group {:depends [-config-app] :task (-> -config-app :ion :compute-group-primary)}
;; Tasks
d
{:doc "DEPLOY"}
d-backend
{:doc "Deploy the backend in the selected environment"
:web "https://console.aws.amazon.com/amplify/home?region=us-east-1#/"
:depends [-config-app -env-key -deps-main -env -env-name -args-rest-str -compute-group]
:task (case -env-key
:local (eshell (str "clojure " -deps-main " -e \"(start/start!)\""))
#_else (eshell (str "clojure -M:only/bin -m d-backend "
" --group " -compute-group
" --creds-profile " -env-name
" --preheat-url " (-> -config-app :ops :url-ops-version)
" " -args-rest-str " ")))}
d-frontend
{:doc "Deploy the frontend in the selected environment"
:web "https://console.aws.amazon.com/amplify/home?region=us-east-1#/"
:depends [-env-key -config-app]
:task (case -env-key
:local (eshell "bin/d_frontend.clj")
#_else (eshell (str "curl -X POST -d {} \""
"https://webhooks.amplify.us-east-1.amazonaws.com/prod/webhooks?id="
(-> -config-app :aws :amplify-webhook-id)
"&token="
(-> -config-app :aws :amplify-webhook-token)
"&operation=startbuild"
"\" -H \"Content-Type:application/json\"")))}
runecho
{:doc "Runs and echoes (prints) the result of running another task"
:depends [-args-rest-str]
:task (eshell "bb run --prn " (first *command-line-args*) -args-rest-str)}
s
{:doc "SETUP"}
s-bootstrap
{:doc "Setup fundamental dev tools (also run when rebuilding container)"
:task (eshell "bin/s-bootstrap.sh")}
s-check
{:doc "Setup: test if fundamental dev tools are properly setup"
:task (eshell "bin/s-check.sh")}
s-deps
{:doc "Setup: download deps in advance"
:task (eshell "bin/s-deps.clj")}
s-more
{:doc "Setup: show more options"
:task (shell "echo" "See https://media4.giphy.com/media/2bLAdm3jWPDsQ/giphy.gif")}
t
{:doc "TUNNEL"}
t-db
{:doc "Tunnel from local port 8182 to compute instance (Socks)"
:web "https://docs.datomic.com/cloud/getting-started/get-connected.html"
:depends [-system -env-name]
:task (eshell "datomic" "client" "access" -system "--profile" -env-name)}
t-db-check
{:doc "Tunnel: test if t-db is currently open"
:web "https://docs.datomic.com/cloud/getting-started/get-connected.html"
:depends [-system -config-app]
:task (eshell "curl -x"
"socks5h://localhost:8182"
(-> -config-app :db :client :endpoint))}
t-repl
{:doc "Tunnel from local port 7000 to compute instance (nREPL)"
:web "https://console.aws.amazon.com/ec2/v2/home?region=us-east-1#Instances:instanceState=running"
:depends [-config-app]
:task (let [kf (-> -config-app :tunnel :ssh-key-path)
ci (-> -config-app :tunnel :ec2-compute-private-ipv4-address)
bi (-> -config-app :tunnel :ec2-bastion-public-ipv4-dns)]
(shell "chmod" "600" kf)
(eshell "ssh"
(str "-i" kf)
(str "-L 7000:" ci ":7000")
(str "ec2-user@" bi)))}
;; Get ahold of `datomic-cloud-yada-*.pem` by asking Daniel or Samuel to give them to you!
;; The above ec2 ipv4 values will be out of date when we stand up newer stacks or instances...
;; When the compute instance changes, we will also need to set it up again for SSH access on port 7000.
;; For compute instance, see https://github.com/markbastian/replion#system-modifications
;; For bastion instance, see https://github.com/markbastian/replion#set-up-ssh-tunnel-through-your-bastion
u
{:doc "UTILITY"}
u-deps-graph
{:doc "Utility to graph the dependencies to a PNG image (for depl. envs; broken in dev)"
:web "https://github.com/practicalli/clojure-deps-edn#project-dependencies"
:depends [-deps-alias]
:task (let [filename "project-dependencies-graph.png"]
(clojure -deps-alias "-X:project/graph-deps")
(eshell "code" filename))}
u-deps-outdated
{:doc "Utility to list outdated dependencies across all envs"
:task (clojure "-M:project/outdated --exclude=com.datomic/dev-local")}
u-deps-search
{:doc "Utility to search through deps across all envs (w/ query or fuzzy)"
:web "https://github.com/BurntSushi/ripgrep/blob/master/GUIDE.md"
:task (if-let [q (first *command-line-args*)]
(eshell "bin/u-deps-search-rg-col.sh" q)
(eshell "bin/u-deps-search-fz.sh"))}
u-deps-tree-search
{:doc "Utility to search through deps tree across all envs (w/ query or fuzzy)"
:web "https://github.com/BurntSushi/ripgrep/blob/master/GUIDE.md"
:task (if-let [q (first *command-line-args*)]
(eshell "bin/u-deps-tree-search-rg.sh" q)
(eshell "bin/u-deps-tree-search-fz.sh"))}}}
(comment
;; May be useful later
;; -------------------
#_(let [res (sh "yarn" :dir "frontend")]
(if (= 0 (:exit res))
(println (str "" \newline (:out res)))
(println (str "" \newline (:err res)))))
-clojure-files (fs/glob "clj" "**.clj")
-classes-dir (let [dir "target/classes"]
(fs/create-dirs dir)
dir)
z-req {:requires ([srcfile])
:task (srcfile/some-fn 1 2 3)}
;; Maintenance tasks
z-clean {:doc "Clean"
:requires ([babashka.fs :as fs])
:task (do (fs/delete-tree "target")
(fs/delete-tree "public/otherthng")
nil)}
z-classes {:depends [-classes-dir -clojure-files]
:task (when-let [modified (fs/modified-since -classes-dir -java-files)]
(prn :modified (map str modified))
(println "Compiling java")
(apply shell "javac" "-d" -classes-dir modified))}
z-classes2 {:depends [-classes-dir -clojure-files]
:task (let [jar-file "target/foo.jar"]
(when-let [modified (seq (concat (fs/modified-since -classes-dir -java-files)
(fs/modified-since -classes-dir -java-files)))]
(println "Packaging")
(if (fs/exists? jar-file)
(apply shell "jar uf" jar-file modified)
(shell "jar cf" jar-file -classes-dir))))}
;; Development tasks
z-onething {:doc "z-onething"
:task (do
(shell "ls")
(shell "ls -l")
(run 'z-req))}
z-otherthng {:depends [z-onething]
:doc "Other thing"
:task (clojure "-M:frontend watch ontolog")}
-z-parathing {:depends [z-onething z-otherthng]}
z-parathing {:doc "Run both things in parallel"
:task (run '-z-parathing {:parallel true})}
;; Deployment tasks
z-both:blah (do (run 'first)
(run 'second))
;; File watching tasks
z-generate {:doc "Re-generate index.html"
:task (load-file "generate.clj")}
-z-fswatcher {:requires ([babashka.pods :as pods])
:task (do (pods/load-pod 'org.babashka/fswatcher "0.0.1")
(require '[pod.babashka.fswatcher :as fw]))}
z-watch {:doc "Watch changes in generate.clj and re-generate index.html on each change"
:depends [-z-fswatcher]
:task
(do (fw/watch "generate.clj" (fn [_]
(run 'generate)))
;; don't exit task
(deref (promise)))})
❯ bb tasks
The following tasks are available:
d DEPLOY
d-backend Deploy the backend in the selected environment
d-frontend Deploy the frontend in the selected environment
runecho Runs and echoes (prints) the result of running another task
s SETUP
s-bootstrap Setup fundamental dev tools (also run when rebuilding container)
s-check Setup: test if fundamental dev tools are properly setup
s-deps Setup: download deps in advance
s-more Setup: show more options
t TUNNEL
t-db Tunnel from local port 8182 to compute instance (Socks)
t-db-check Tunnel: test if t-db is currently open
t-repl Tunnel from local port 7000 to compute instance (nREPL)
u UTILITY
u-deps-graph Utility to graph the dependencies to a PNG image (for depl. envs; broken in dev)
u-deps-outdated Utility to list outdated dependencies across all envs
u-deps-search Utility to search through deps across all envs (w/ query or fuzzy)
u-deps-tree-search Utility to search through deps tree across all envs (w/ query or fuzzy)
@borkdude
Copy link

borkdude commented May 6, 2021

Thanks for clarifying!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment