首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Babashka中使用gcloud

如何在Babashka中使用gcloud
EN

Stack Overflow用户
提问于 2021-10-04 15:43:36
回答 2查看 251关注 0票数 1

我正在尝试使用Babashka来替换一些用于在GCP函数上部署函数的Bash脚本。

下面的脚本正在运行,但我想知道是否有更好的方法来执行gcloud shell命令:

代码语言:javascript
复制
#!/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脚本更容易阅读:

代码语言:javascript
复制
#!/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构建命令。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-10-04 16:09:50

如果您想执行shell命令并看到它所显示的直接输出,我建议使用babashka.process/processbabashka.tasks/shell

代码语言:javascript
复制
@(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相同,这意味着:等待进程完成。如果不对此进行预置,则进程将异步运行。

更多信息:

票数 2
EN

Stack Overflow用户

发布于 2021-10-04 16:43:50

这个辅助函数展示了我用来简化对shell的调用的一个技巧

代码语言:javascript
复制
(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
         }

它允许您编写单个命令字符串,而不必手动标记字符串的所有部分。实现非常简单:

代码语言:javascript
复制
 (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发送命令字符串,并允许它正常地解析args。

几年前,我广泛地使用它来通过AWS来控制AWS主机(创建、快照、选择、删除),而且非常容易使用。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69438794

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档