Skip to content

Instantly share code, notes, and snippets.

@furkanmustafa furkanmustafa/rancher.sh
Last active Aug 3, 2016

Embed
What would you like to do?
A draft rancher management script using bash and jshon
#!/bin/sh -e
# set -x
RANCHER_URL="rancher.mydomain.com"
PROJECT_ID="1a5" #id of my stack in rancher
CATTLE_ACCESS_KEY=MY_RANCHER_ACCESS_KEY
CATTLE_SECRET_KEY=MY_RANCHER_ACCESS_SECRET
TMP_FILE=$(mktemp)
# GET SERVICES
curl -s -u "${CATTLE_ACCESS_KEY}:${CATTLE_SECRET_KEY}" \
-X GET \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
"https://${RANCHER_URL}/v1/projects/${PROJECT_ID}/services" > $TMP_FILE
SERVICE_COUNT=$(cat $TMP_FILE | jshon -e data -l)
# PARSE IDs AND NAMEs
for i in $(seq 0 $(($SERVICE_COUNT - 1))); do
SERVICE_JSONS[$i]=$(cat $TMP_FILE | jshon -e data -e $i)
SERVICE_NAMES[$i]=$(echo -e ${SERVICE_JSONS[$i]} | jshon -e name -u)
SERVICE_IDS[$i]=$(echo -e ${SERVICE_JSONS[$i]} | jshon -e id -u)
done
rm $TMP_FILE
# LIST SERVICES AND THEIR STATUS
if [ $# -eq 0 ]; then
TMP_FILE=$(mktemp)
for i in $(seq 0 $(($SERVICE_COUNT - 1))); do
STATE=$(echo -e ${SERVICE_JSONS[$i]} | jshon -e state -u)
echo -e "\t${SERVICE_IDS[$i]}\t${SERVICE_NAMES[$i]}\t${STATE}" >> $TMP_FILE
done
cat $TMP_FILE | column -t
rm $TMP_FILE
exit 0
fi
SERVICE_NAME="$1"; shift
SERVICE_ID="~"
SERVICE_IDX=-1
for i in $(seq 0 $(($SERVICE_COUNT - 1))); do
if [ "${SERVICE_NAMES[$i]}" == "${SERVICE_NAME}" ]; then
SERVICE_ID=${SERVICE_IDS[$i]}
SERVICE_IDX=$i
SERVICE_STATE=$(echo -e ${SERVICE_JSONS[$i]} | jshon -e state -u)
fi
done
if [ "$SERVICE_ID" == "~" ]; then
echo "No such service: ${SERVICE_NAME}"
exit 1
fi
# NO ACTION?
if [ $# -eq 0 ]; then
echo "SERVICE: ${SERVICE_NAME} ${SERVICE_ID} ${SERVICE_STATE}"
exit 0;
fi
ACTION="$1"
if [ "$ACTION" == "upgrade" ]; then
echo "Upgrading.."
curl -s -u "${CATTLE_ACCESS_KEY}:${CATTLE_SECRET_KEY}" \
-X POST \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-d '{"inServiceStrategy":{"startFirst":true,"secondaryLaunchConfigs":[]}, "toServiceStrategy":{}}' \
"https://${RANCHER_URL}/v1/projects/${PROJECT_ID}/services/${SERVICE_ID}/?action=upgrade"
echo "done."
exit 0;
fi
if [ "$ACTION" == "finishupgrade" ]; then
echo "Finishing Upgrade.."
curl -s -u "${CATTLE_ACCESS_KEY}:${CATTLE_SECRET_KEY}" \
-X POST \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-d '{"inServiceStrategy":{"startFirst":true,"secondaryLaunchConfigs":[]}, "toServiceStrategy":{}}' \
"https://${RANCHER_URL}/v1/projects/${PROJECT_ID}/services/${SERVICE_ID}/?action=finishupgrade"
echo "done."
exit 0;
fi
echo "Unknown action: $ACTION"
exit 1;
@furkanmustafa

This comment has been minimized.

Copy link
Owner Author

commented Aug 3, 2016

Usage:

# list services in my stack
./rancher.sh

# upgrade frontend service
./rancher.sh frontend upgrade && ./rancher.sh frontend finishupgrade

Currently this doesn't have a state polling to check if upgrade is finished,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.