簡體   English   中英

如何將 gcloud 與 Babashka 一起使用

[英]How to use gcloud with Babashka

我正在嘗試使用 Babashka 替換我用來在 GCP Cloud Functions 上部署函數的一些 Bash 腳本。

下面的腳本正在運行,但我想知道是否有更好的方法來執行gcloud shell 命令:

#!/usr/bin/env bb
(require '[cheshire.core :as json]
         '[clojure.java.shell :refer [sh]])

(let [package-json (json/parse-string (slurp "package.json") true)
      name (:name package-json)
      entry-point "entryPoint"
      region "europe-west3"
      memory "128MB"
      runtime "nodejs14"
      source "dist"
      service-account "sa-function-invoker@prj-kitchen-sink.iam.gserviceaccount.com"
      timeout "10s"]
  (println "deploy function" name "with entry point" entry-point "to GCP Cloud Functions." "Attach service account" service-account)
  (let [output (sh "gcloud" "functions" "deploy" name "--region" region "--entry-point" entry-point "--memory" memory "--runtime" runtime "--service-account" service-account "--source" source "--trigger-http" "--timeout" timeout)]
    (if (= "" (:err output))
      (println (:out output))
      (println (:err output)))))

作為比較,我使用的 Bash 腳本更易於閱讀:

#!/bin/bash
set -euo pipefail

FUNCTION_NAME=$(cat package.json | jq '{name}' | jq '.name' | sed 's/"//g')
FUNCTION_ENTRY_POINT=entryPoint
ATTACHED_SA=sa-function-invoker@prj-kitchen-sink.iam.gserviceaccount.com
MEMORY=128MB

echo "deploy function `${FUNCTION_NAME}` with entry point `${FUNCTION_ENTRY_POINT}` to GCP Cloud Functions. Attach service account `${ATTACHED_SA}`"

gcloud functions deploy ${FUNCTION_NAME} \
  --project ${GCP_PROJECT_ID} \
  --region ${GCP_REGION} \
  --memory ${MEMORY} \
  --runtime nodejs14 \
  --service-account ${ATTACHED_SA} \
  --source dist \
  --entry-point ${FUNCTION_ENTRY_POINT} \
  --timeout 10s

我想我的問題不是特定於 Babashka 或 gcloud,但它是關於如何使用 clojure.java.shell 構造命令一般...

如果你想執行 shell 命令並看到直接的 output,我建議使用babashka.process/processbabashka.tasks/shell


@(babashka.process/process ["ls" "-la"] {:out :inherit :err :inherit})

@(babashka.process/process ["ls" "-la"] {:inherit true})

(babashka.tasks/shell "ls -la")

上面的調用幾乎相同,但shell還應用babashka.process/check如果退出代碼不為零則拋出。 調用前的@符號與調用deref相同,意思是:等待進程完成。 如果您不預先添加它,那么該過程將異步運行。

更多信息:

這個助手 function顯示了我用來簡化對 shell 的調用的一個技巧:

(shell-cmd cmd-str)
Runs a command string in the default OS shell (/bin/bash); returns result in a Clojure map. Example:

 (shell-cmd "ls -ldF *")
   ;=>   {:exit    0     ; unix exit status (0 -> normal)
          :err    ''     ; text from any errors
          :out    '...'  ; text output as would printed to console
         }

它允許您編寫單個命令字符串,而不必手動標記字符串的所有部分。 實現非常簡單:

 (def ^:dynamic *os-shell* "/bin/bash") ; could also use /bin/zsh, etc

 (defn shell-cmd
   [cmd-str]
   (let [result (shell/sh *os-shell* "-c" cmd-str)]
     (if (= 0 (grab :exit result))
       result
       (throw (ex-info "shell-cmd: clojure.java.shell/sh failed, cmd-str:" (vals->map cmd-str result))))))

因此,它允許您將命令字符串直接發送到/bin/bash並允許它正常解析參數。

幾年前,我廣泛使用它通過 AWS CLI 控制 AWS RDS 主機(創建、快照、選擇、刪除),它非常易於使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM