如何使用 bash 使用预定义的键值 运行 CONSUL (agent -dev)

How to run CONSUL (agent -dev) with predefined key value using bash

我需要使用

启动本地 consul (https://www.consul.io/)
consul agent -dev

但是默认情况下,这个本地领事必须有一些 key/value 存在。 我想有一种方法可以使用 REST API...有人可以向我解释一下吗?

此致, 尼古拉斯

这是我基于 HTTP Rest 编写的脚本 API

#!/bin/bash

echo "********* **************** ************"
echo "********* RUN LOCAL CONSUL ************"
echo "********* **************** ************"

# OVERRIDEN VALUES
LOCAL_CONSUL_PATH="." # example: C:\consul_1.4.2_windows_amd64
LOCAL_CONSUL_PORT=8500
LOCAL_ENV="http://localhost:5002" # example: http://localhost:5002

MASTER_TOKEN="no-need-when-for-local-consul-agent"
TOKEN=$MASTER_TOKEN
consulPath="http://localhost:8500"

function run(){
  killConsul
  startConsul
  createKey "sample4unittest" "consul";
  createKey "unittest/sample" "consul";
  createKey "_global/Environment" $LOCAL_ENV;
}

function killConsul(){
  echo "*** killConsul..."
  port=
  if [[ -z "$port" ]]; then port=$LOCAL_CONSUL_PORT; fi
  echo $"   KILL CONSUL port[${port}]"
  PID=`netstat -a -o -n  | grep 127.0.0.1:${port} | grep LISTENING | cut -d ' ' -f37-`
  echo $"   Local consul listening PID[${PID}]"
  if [[ ! -z "$PID" ]]; then taskkill -F -PID $PID; fi
  echo "*** killConsul finished"
}

function startConsul(){
  echo "*** startConsul..."
  cd $LOCAL_CONSUL_PATH
  ./consul agent -dev &
  echo "*** startConsul finished"
}

function delete(){
  keyName=
  curl -k -H $"X-Consul-Token: ${TOKEN}" \
    --request DELETE \
    $"${consulPath}/v1/kv/${keyName}"
}

function createKey(){
  keyName=
  value=
  echo "*** Start CreateKey: key[${keyName}] value[${value}]"
  echo "--- DELETE the potential existing key "
  delete $"${keyName}"

  curl -k -H $"X-Consul-Token: ${TOKEN}" \
     --request PUT \
     --data $"${value}" \
     $"${consulPath}/v1/kv/${keyName}"
  echo "*** done"
}

run

此致, 尼古拉斯