Skip to content

Add option to specify endpoint by name #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ Usage:

* Set the following environmental variables or edit file and set the authentication details
```bash
P_USER="root"
P_PASS="password"
P_URL="http://example.com:9000"
P_USER="root"
P_PASS="password"
P_URL="http://example.com:9000"
P_PRUNE="false"
P_ENDPOINT="My Endpoint"
```

* run with
```bash
export P_USER="root"
export P_PASS="password"
export P_URL="http://example.com:9000"
export P_USER="root"
export P_PASS="password"
export P_URL="http://example.com:9000"
export P_PRUNE="false"
./stack-update.sh mqtt mqtt/docker-compose.yml
```
76 changes: 48 additions & 28 deletions stack-update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ P_USER=${P_USER:-"root"}
P_PASS=${P_PASS:-"rootroot"}
P_URL=${P_URL:-"http://10.11.9.200:9000"}
P_PRUNE=${P_PRUNE:-"false"}
P_ENDPOINT=${P_ENDPOINT:-""}

if [ -z ${1+x} ]; then
echo "Parameter #1 missing: stack name "
Expand All @@ -29,7 +30,30 @@ fi
T=$(echo $P_TOKEN | awk -F '"' '{print $4}')
echo "Token: $T"

INFO=$(curl -s -H "Authorization: Bearer $T" "$P_URL/api/endpoints/1/docker/info")
if [[ $P_ENDPOINT != "" ]]; then
echo "Getting endpoint..."
P_ENDPOINT_ENC=$(printf %s "$P_ENDPOINT" | jq -sRr @uri)
ENDPOINTS=$(curl -s -H "Authorization: Bearer $T" "$P_URL/api/endpoints?type=1&search=$P_ENDPOINT_ENC")
if [[ $ENDPOINTS = "[]" ]]; then
ENDPOINTS=$(curl -s -H "Authorization: Bearer $T" "$P_URL/api/endpoints?type=2&search=$P_ENDPOINT_ENC")
fi
if [[ $ENDPOINTS = "[]" ]]; then
echo "Result: Endpoint not found."
exit 1
fi
endpoint=$(echo "$ENDPOINTS"|jq --arg TARGET "$P_ENDPOINT" -jc '.[] | select(.Name == $TARGET)')
if [[ "$endpoint" = "" ]]; then
echo "Result: Endpoint not found."
exit 1
fi
eid="$(echo "$endpoint" |jq -j ".Id")"
fi

eid=${eid:-"1"}

echo "Using Endpoint ID: $eid"

INFO=$(curl -s -H "Authorization: Bearer $T" "$P_URL/api/endpoints/$eid/docker/info")
CID=$(echo "$INFO" | awk -F '"Cluster":{"ID":"' '{print $2}' | awk -F '"' '{print $1}')
echo "Cluster ID: $CID"

Expand All @@ -38,20 +62,28 @@ STACKS=$(curl -s -H "Authorization: Bearer $T" "$P_URL/api/stacks")

#echo "/---" && echo $STACKS && echo "\\---"

found=0
stack=$(echo "$STACKS"|jq --arg TARGET "$TARGET" -jc '.[]| select(.Name == $TARGET)')

if [ -z "$stack" ];then
echo "Result: Stack not found."
exit 1
if [ ! -z "$stack" ]; then
# Updating existing
sid="$(echo "$stack" |jq -j ".Id")"
name=$(echo "$stack" |jq -j ".Name")
echo "Identified stack: $sid / $name"

existing_env_json="$(echo -n "$stack"|jq ".Env" -jc)"
data_prefix="{\"Id\":\"$sid\",\"StackFileContent\":\""
method="PUT"
add_url="/$sid?endpointId=$eid"
echo "Updating stack..."
else
# Creating new
sid=""
existing_env_json="[]"
data_prefix="{\"Name\":\"$TARGET\",\"SwarmID\":\"$CID\",\"StackFileContent\":\""
method="POST"
add_url="?endpointId=$eid&method=string&type=1"
echo "Creating stack..."
fi
sid="$(echo "$stack" |jq -j ".Id")"
name=$(echo "$stack" |jq -j ".Name")

found=1
echo "Identified stack: $sid / $name"

existing_env_json="$(echo -n "$stack"|jq ".Env" -jc)"

dcompose=$(cat "$TARGET_YML")
dcompose="${dcompose//$'\r'/''}"
Expand All @@ -61,18 +93,16 @@ echo "/-----READ_YML--------"
echo "$dcompose"
echo "\---------------------"
dcompose="${dcompose//$'\n'/'\n'}"
data_prefix="{\"Id\":\"$sid\",\"StackFileContent\":\""
data_suffix="\",\"Env\":"$existing_env_json",\"Prune\":$P_PRUNE}"
sep="'"
echo "/~~~~CONVERTED_JSON~~~~~~"
echo "$data_prefix$dcompose$data_suffix"
echo "\~~~~~~~~~~~~~~~~~~~~~~~~"
echo "$data_prefix$dcompose$data_suffix" > json.tmp

echo "Updating stack..."
UPDATE=$(curl -s \
"$P_URL/api/stacks/$sid?endpointId=1" \
-X PUT \
"$P_URL/api/stacks$add_url" \
-X $method \
-H "Authorization: Bearer $T" \
-H "Content-Type: application/json;charset=UTF-8" \
-H 'Cache-Control: no-cache' \
Expand All @@ -81,19 +111,9 @@ UPDATE=$(curl -s \
rm json.tmp
echo "Got response: $UPDATE"
if [ -z ${UPDATE+x} ]; then
echo "Result: failure to update"
echo "Result: failure to create/update"
exit 1
else
echo "Result: successfully updated"
echo "Result: successfully created/updated"
exit 0
fi


if [ "$found" == "1" ]; then
echo "Result: found stack but failed to process"
exit 1
else
echo "Result: fail"
exit 1
fi