Created
February 3, 2021 16:03
-
-
Save ABeltramo/ff17455e63a37a8b42c219904df4bed4 to your computer and use it in GitHub Desktop.
Babashka GCloud automl API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bb | |
; Install gcloud from https://cloud.google.com/sdk/docs/install | |
; first time configure using gcloud init | |
; See API documentation at https://cloud.google.com/vision/automl/docs/ | |
(require '[babashka.curl :as curl] | |
'[clojure.java.shell :refer [sh]] | |
'[cheshire.core :as json] | |
'[clojure.string :as str] | |
'[clojure.tools.cli :refer [parse-opts]]) | |
(defn get-bearer [] | |
(-> (sh "gcloud" "auth" "application-default" "print-access-token") | |
:out | |
(str/trim-newline))) | |
(defn default-opts [bearer] | |
{:headers {"Authorization" (str "Bearer " bearer) | |
"Accept" "application/json" | |
"Content-Type" "application/json"}}) | |
(defn get-models [bearer {:keys [project-id location model]}] | |
(let [models (-> (curl/get (str "https://automl.googleapis.com/v1/projects/" project-id "/locations/" location "/models") | |
(default-opts bearer)) | |
:body | |
(json/parse-string true) | |
:model)] | |
(if model | |
(filter | |
(fn [{:keys [displayName]}] | |
(str/includes? displayName model)) | |
models) | |
models))) | |
(defn undeploy-models [opts] | |
(let [bearer (get-bearer) | |
models (get-models bearer opts)] | |
(println "Found models:\n" | |
models) | |
(->> models | |
(map (fn [{:keys [deploymentState name displayName]}] | |
(if (= deploymentState "DEPLOYED") | |
(do | |
(println | |
(str "⏳ " displayName " is deployed, undeploying...")) | |
(-> (curl/post (str "https://automl.googleapis.com/v1/" name ":undeploy") | |
(default-opts bearer)) | |
:body | |
(json/parse-string true))) | |
(println | |
(str "✅ " displayName " is undeployed, skipping")))))))) | |
(defn deploy-models [opts] | |
(let [bearer (get-bearer) | |
models (get-models bearer opts)] | |
(println "Found models:\n" | |
models) | |
(->> models | |
(map (fn [{:keys [deploymentState name displayName]}] | |
(if (= deploymentState "DEPLOYED") | |
(println | |
(str "✅ " displayName " is deployed, skipping")) | |
(do | |
(println | |
(str "⏳ " displayName " is undeployed, deploying...")) | |
(-> (curl/post (str "https://automl.googleapis.com/v1/" name ":deploy") | |
(default-opts bearer)) | |
:body | |
(json/parse-string true))))))))) | |
;;;; CLI MAIN | |
(let [cli-options [["-p" "--project-id PROJECT" "Project ID - REQUIRED"] | |
["-l" "--location LOCATION" "Location" | |
:default "us-central1"] | |
["-m" "--model MODEL" "Model Name, if not passed all registered models will be considered"] | |
["-h" "--help"]] | |
{:keys [options arguments summary]} (parse-opts *command-line-args* cli-options) | |
help-message (str "GCloud automl deploy/undeploy models\n" | |
"To run you have to install and configure gcloud cli toolkit, see https://cloud.google.com/sdk/docs/install\n\n" | |
"./gcloud-automl.clj [list|deploy|undeploy] [--project-id a-project-id [--location us-central1 --model model-name]]" | |
"Example: ./gcloud-automl.clj undeploy --project-id a-project-id --location us-central1\n\n" | |
summary)] | |
(when (:help options) | |
(do (println help-message) | |
(System/exit 0))) | |
(when-not (:project-id options) | |
(do (println "❌ ERROR: MISSING project-id options\n" help-message) | |
(System/exit 0))) | |
(case (first arguments) | |
"deploy" (deploy-models options) | |
"undeploy" (undeploy-models options) | |
"list" (-> (get-bearer) | |
(get-models options)) | |
(println | |
(str "❌ ERROR: Unrecognised argument: '" (first arguments) "'\n" | |
help-message)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment