Last active
February 26, 2018 14:43
-
-
Save rhopp/d107bf55e495f8d935754d3457ebab90 to your computer and use it in GitHub Desktop.
start_workspace_and_wait
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Usage: start_workspace_and_wait.sh <nubmer_of tries> | |
# Script calls local che-starter to start workspace/get workspaces. | |
# It expects variable ACTIVE_TOKEN to be set. | |
# When asking whether workspace is already running, it wait 6 seconds between every try. | |
# Script ends after this count or when workspace is succesfully started or when workspace switches from STARTING to STOPPED state. | |
if [ -z ${ACTIVE_TOKEN} ]; then | |
echo "ACTIVE_TOKEN not set" | |
exit 1 | |
fi | |
if [ -z "$1" ]; then | |
echo "Parameter for how many times we should wait for workspace to start is missing" | |
exit 1 | |
fi | |
if [ -z "$2" ]; then | |
echo "Parameter for how many workspaces should be started is missing" | |
exit 1 | |
fi | |
function start_workspace { | |
CREATE_WORKSPACE_JSON="{\"branch\": \"master\",\"description\": \"mycustomdescription\",\"repo\": \"https://github.com/openshiftio-vertx-boosters/vertx-http-booster\",\"stackId\": \"vert.x\"}" | |
echo "Starting workspace using json: $CREATE_WORKSPACE_JSON" | |
OUTPUT=$(curl -s -X POST -H "Content-Type: application/json" -H "Authorization: Bearer ${ACTIVE_TOKEN}" -d "${CREATE_WORKSPACE_JSON}" "http://localhost:10000/workspace?masterUrl=https://console.starter-us-east-2.openshift.com&namespace=rhopp-osiotest1-che") | |
export WORKSPACE_ID=$(echo $OUTPUT | jq -r ".id") | |
echo "Started workspace ${WORKSPACE_ID}" | |
} | |
function wait_for_workspace_to_start { | |
TRIES=1 | |
STATUS="" | |
while [[ "$STATUS" != "RUNNING" && $TRIES -le $TRIES_COUNT ]] | |
do | |
echo "Try #${TRIES}" | |
OUTPUT=$(curl -s -X GET -H "Content-Type: application/json" -H "Authorization: Bearer ${ACTIVE_TOKEN}" "http://localhost:10000/workspace?masterUrl=https://console.starter-us-east-2.openshift.com&namespace=rhopp-osiotest1-che") | |
STATUS=$(echo $OUTPUT | jq -r ".[] | select(.id==\"$WORKSPACE_ID\").status") | |
echo "Waiting for workspace id ${WORKSPACE_ID} to be in status RUNNING. Current status: ${STATUS}" | |
if [[ "$STATUS" == "STARTING" ]]; then | |
#Already starting | |
ALREADY_STARTING=true | |
fi | |
if [[ $ALREADY_STARTING = true && "$STATUS" == "STOPPED" ]]; then | |
echo "Something went wrong! Check che logs on openshift." | |
return 1 | |
fi | |
TRIES=$((TRIES+1)) | |
sleep 6 | |
done | |
return 0 | |
} | |
TRIES_COUNT=$1 | |
COUNT_OF_WORKSPACES=$2 | |
WORKSPACES_STARTED=0 | |
while true | |
do | |
if [ $WORKSPACES_STARTED -ge $COUNT_OF_WORKSPACES ]; then | |
exit 0 | |
fi | |
start_workspace | |
export ALREADY_STARTING=false | |
if wait_for_workspace_to_start; then | |
WORKSPACES_STARTED=$((WORKSPACES_STARTED+1)) | |
else | |
echo "Something went wrong" | |
exit 1 | |
fi | |
done | |
echo "Fajront" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment