#!/bin/bash [ -z "${GLORIA_URL}" -o -z "${GLORIA_TOKEN}" ] && exit_plus 1 "FATAL: Operation as the Gloria pilot requested without URL and/or access token" GLORIA_MAX_FAILED=${GLORIA_MAX_FAILED:-60} GLORIA_NEXT_TRY_WAIT=${GLORIA_NEXT_TRY_WAIT:-60} GLORIA_PROXY_FILE="gloria.proxy" GLORIA_STAGEIN_LIST="gloria.stagein.list" CLOUD_INFILES_LIST="gloria.infiles" curl_put () { local data=$1 curl -s -H "Accept: application/json" -H "Content-Type:application/json" -X PUT --data $data "${GLORIA_URL}" } add_stagein_file () { local stage_url=$1 local stage_file=$2 echo "$stage_url $stage_file" >> $GLORIA_STAGEIN_LIST } add_infiles_list () { local stage_file=$1 echo "$stage_file" >> $CLOUD_INFILES_LIST } hold_on_parse_failed () { echo "Error: There is no $1 in Gloria response. Retrying connection." PILOT_ACTION='hold' PILOT_HOLD_TIME=${GLORIA_NEXT_TRY_WAIT} } export_json_vars () { local json_f=$1 local array_base=$2 local var_prefix=$3 local params_cnt=$( cat $json_f | jq -r "${array_base} | length" ) local params_idx=0 while [ $params_idx -lt $params_cnt ]; do local par_name=$( cat $json_f | jq -r "${array_base} | keys | .[${params_idx}]" ) local par_value=$( cat $json_f | jq "${array_base} | .[\"$par_name\"]" ) eval "export ${var_prefix}${par_name}=${par_value}" params_idx=$((params_idx+1)) done } pilot_get_action () { json_f=$( mktemp gloria.json.XXXXXXX ) data='{ pilot_token : '${GLORIA_TOKEN}', pilot_status : running, request : getjob }' curl_put $data > $json_f # in case of connection to Gloria has failed - return 'hold' and increase failed counter if [ $? -ne 0 ]; then gloria_failed_count=$(( gloria_failed_count + 1 )) # exit pilot after MAX_FAILED tries if [ $gloria_failed_count -gt ${GLORIA_MAX_FAILED} ]; then PILOT_ACTION='exit' PILOT_EXIT_CODE=1 PILOT_EXIT_REASON='Connection to Gloria service failed' else PILOT_ACTION='hold' PILOT_HOLD_TIME=${GLORIA_NEXT_TRY_WAIT} fi return 0 else gloria_failed_count=0 fi # print all JSON output in case of debug [ -n "${RAINBOW_DEBUG}" ] && cat $json_f | jq '.' # get pilot action from JSON PILOT_ACTION=$( cat $json_f | jq -r '.action' ) # parse and define paramenters for 'runvm' action if [ "$PILOT_ACTION" = "runvm" ]; then # main OS VM image local vm_image_url=$( cat $json_f | jq -r '.vminfo.image_url' ) if [ -n "$vm_image_url" ]; then local vm_image_file=$( cat $json_f | jq -r '.vminfo.image_file' ) [ "$vm_image_file" = "null" ] && vm_image_file=${vm_image_url##*/} export CLOUD_VM_SYSIMAGE=$vm_image_file add_stagein_file $vm_image_url $vm_image_file else hold_on_parse_failed 'VM image location' && return fi # OS image params description file local vm_descr_url=$( cat $json_f | jq -r '.vminfo.vm_description_url' ) if [ "$vm_descr_url" != "null" ]; then local vm_descr_file=$( cat $json_f | jq -r '.vminfo.vm_description_file' ) [ "$vm_descr_file" = "null" ] && vm_descr_file=${vm_descr_url##*/} export CLOUD_VM_DESCRIPTION=$vm_descr_file add_stagein_file $vm_descr_url $vm_descr_file fi # OS image params direct export_json_vars $json_f '.vminfo.vm_description' # VM resources export_json_vars $json_f '.vmresources' 'PILOT_' # Rainbow features triggers export_json_vars $json_f '.features' # user data files local data_files_cnt=$( cat $json_f | jq -r '.userdata | length' ) local data_idx=0 while [ $data_idx -lt $data_files_cnt ]; do local file_name=$( cat $json_f | jq -r ".userdata | keys | .[${data_idx}]" ) local file_url=$( cat $json_f | jq -r ".userdata | .[\"$file_name\"]" ) add_stagein_file "$file_url" "$file_name" add_infiles_list "$file_name" data_idx=$((data_idx+1)) done # user proxy local proxy_str=$( cat $json_f | jq -r '.x509_user_proxy' ) if [ "$proxy_str" != "null" ]; then echo "$proxy_str" > $GLORIA_PROXY_FILE set_userproxy $GLORIA_PROXY_FILE fi fi rm -f $json_f } pilot_operation () { exit_pilot=0 while [ $exit_pilot -eq 0 ]; do echo "Starting Gloria pilot processing cycle..." pilot_get_action case $PILOT_ACTION in hold) sleep $PILOT_HOLD_TIME ;; exit) exit_plus $PILOT_EXIT_CODE "Terminating Gloria pilot: ${PILOT_EXIT_REASON}" ;; runvm) echo "Preparing to start VM on request of Gloria" echo "Staging files on WN..." download_files $GLORIA_STAGEIN_LIST echo_action "Done." vm_life_cycle # TODO: ask Gloria what to do next exit_pilot=1 ;; esac done } pilot_operation