简体   繁体   English

如何将 gcloud 与 Babashka 一起使用

[英]How to use gcloud with Babashka

I'm trying to use Babashka to replace a few Bash scripts I use to deploy functions on GCP Cloud Functions.我正在尝试使用 Babashka 替换我用来在 GCP Cloud Functions 上部署函数的一些 Bash 脚本。

The script below is working, but I wonder if there is a better way to execute the gcloud shell command:下面的脚本正在运行,但我想知道是否有更好的方法来执行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)))))

As a comparison, the Bash script I was using is easier to read:作为比较,我使用的 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

I guess my question is not very specific to Babashka or gcloud, but it's about how to construct commands with clojure.java.shell in general...我想我的问题不是特定于 Babashka 或 gcloud,但它是关于如何使用 clojure.java.shell 构造命令一般...

If you want to execute the shell command and see the direct output as it appears, I recommend using babashka.process/process or babashka.tasks/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")

The above invocations do pretty much the same, but shell also applied babashka.process/check which throws if the exit code was non-zero.上面的调用几乎相同,但shell还应用babashka.process/check如果退出代码不为零则抛出。 The @ sign before the invocation is the same as calling deref which means: wait for the process to finish.调用前的@符号与调用deref相同,意思是:等待进程完成。 If you don't prepend that, than the process is going to run asynchronously.如果您不预先添加它,那么该过程将异步运行。

More info:更多信息:

One trick I use to simplify calling out to the shell is shown by this helper function : 这个助手 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
         }

It allows you to write a single command string, instead of having to manually tokenize all the parts of the string.它允许您编写单个命令字符串,而不必手动标记字符串的所有部分。 The implementation is quite simple:实现非常简单:

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

So it allows you to send a command string directly to /bin/bash and allow it to parse the args as normal.因此,它允许您将命令字符串直接发送到/bin/bash并允许它正常解析参数。

I used this extensively a few years back to control AWS RDS hosts (creating, snapshots, selecting, deleting) via the AWS CLI and it was very easy to use.几年前,我广泛使用它通过 AWS CLI 控制 AWS RDS 主机(创建、快照、选择、删除),它非常易于使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM