Skip to content

Commit

Permalink
update: video record standalone with GraphQL endpoint needs basic auth
Browse files Browse the repository at this point in the history
Signed-off-by: Viet Nguyen Duc <nguyenducviet4496@gmail.com>
  • Loading branch information
VietND96 committed Sep 21, 2024
1 parent 89a9729 commit a3b4b62
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Video/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ RUN --mount=type=secret,id=SEL_PASSWD \
# Add Supervisor configuration files
#======================================
COPY supervisord.conf /etc
COPY --chown="${SEL_UID}:${SEL_GID}" entry_point.sh video.sh video_ready.py video_graphQLQuery.sh video_gridUrl.sh /opt/bin/
COPY --chown="${SEL_UID}:${SEL_GID}" entry_point.sh validate_endpoint.sh video.sh video_ready.py video_graphQLQuery.sh video_gridUrl.sh /opt/bin/

#======================================
# Add RCLONE for uploading videos
Expand Down
26 changes: 26 additions & 0 deletions Video/validate_endpoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash

endpoint=$1
graphql_endpoint=${2:-false}
max_time=1
process_name="endpoint.checks"

BASIC_AUTH="$(echo -n "${SE_ROUTER_USERNAME}:${SE_ROUTER_PASSWORD}" | base64)"

if [ "${graphql_endpoint}" = "true" ]; then
endpoint_checks=$(curl --noproxy "*" -m ${max_time} -k -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Basic ${BASIC_AUTH}" \
--data '{"query":"{ grid { sessionCount } }"}' \
-s "${endpoint}" -o /dev/null -w "%{http_code}")
else
endpoint_checks=$(curl --noproxy "*" -H "Authorization: Basic ${BASIC_AUTH}" -m ${max_time} -s -k -o /dev/null -w "%{http_code}" "${endpoint}")
fi

if [[ "$endpoint_checks" = "404" ]]; then
echo "$(date +%FT%T%Z) [${process_name}] - Endpoint ${endpoint} is not found - status code: ${endpoint_checks}"
elif [[ "$endpoint_checks" = "401" ]]; then
echo "$(date +%FT%T%Z) [${process_name}] - Endpoint ${endpoint} requires authentication - status code: ${endpoint_checks}. Please provide valid credentials via SE_ROUTER_USERNAME and SE_ROUTER_PASSWORD environment variables."
elif [[ "$endpoint_checks" != "200" ]]; then
echo "$(date +%FT%T%Z) [${process_name}] - Endpoint ${endpoint} is not available - status code: ${endpoint_checks}"
fi
16 changes: 7 additions & 9 deletions Video/video.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ else
NODE_STATUS_ENDPOINT="${SE_SERVER_PROTOCOL}://${DISPLAY_CONTAINER_NAME}:${SE_NODE_PORT}/status"
fi

/opt/bin/validate_endpoint.sh "${NODE_STATUS_ENDPOINT}"
BASIC_AUTH="$(echo -n "${SE_ROUTER_USERNAME}:${SE_ROUTER_PASSWORD}" | base64)"

if [ -d "${VIDEO_FOLDER}" ]; then
echo "$(date +%FT%T%Z) [${process_name}] - Video folder exists: ${VIDEO_FOLDER}"
else
Expand Down Expand Up @@ -70,7 +73,7 @@ function wait_for_display() {
}

function check_if_api_respond() {
endpoint_checks=$(curl --noproxy "*" -sk -o /dev/null -w "%{http_code}" "${NODE_STATUS_ENDPOINT}")
endpoint_checks=$(curl --noproxy "*" -H "Authorization: Basic ${BASIC_AUTH}" -sk -o /dev/null -w "%{http_code}" "${NODE_STATUS_ENDPOINT}")
if [[ "${endpoint_checks}" != "200" ]]; then
return 1
fi
Expand Down Expand Up @@ -198,20 +201,15 @@ else
recorded_count=0

wait_for_api_respond
while curl --noproxy "*" -sk --request GET ${NODE_STATUS_ENDPOINT} >/tmp/status.json; do
while curl --noproxy "*" -H "Authorization: Basic ${BASIC_AUTH}" -sk --request GET ${NODE_STATUS_ENDPOINT} >/tmp/status.json; do
session_id=$(jq -r "${JQ_SESSION_ID_QUERY}" /tmp/status.json)
if [[ "$session_id" != "null" && "$session_id" != "" && "$session_id" != "reserved" && "$recording_started" = "false" ]]; then
echo "$(date +%FT%T%Z) [${process_name}] - Session: $session_id is created"
return_list=($(bash ${VIDEO_CONFIG_DIRECTORY}/video_graphQLQuery.sh "$session_id"))
caps_se_video_record="${return_list[0]}"
video_file_name="${return_list[1]}.mp4"
endpoint_status="${return_list[2]}"
endpoint_url="${return_list[3]}"
if [[ "${endpoint_status}" = "401" ]]; then
echo "$(date +%FT%T%Z) [${process_name}] - GraphQL endpoint requires authentication, please set env variables SE_ROUTER_USERNAME and SE_ROUTER_PASSWORD"
elif [[ "${endpoint_status}" = "404" ]]; then
echo "$(date +%FT%T%Z) [${process_name}] - GraphQL endpoint could not be found, please check the endpoint ${endpoint_url}"
fi
endpoint_url="${return_list[2]}"
/opt/bin/validate_endpoint.sh "${endpoint_url}" "true"
echo "$(date +%FT%T%Z) [${process_name}] - Start recording: $caps_se_video_record, video file name: $video_file_name"
log_node_response
fi
Expand Down
6 changes: 2 additions & 4 deletions Video/video_graphQLQuery.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ if [ -n "${GRAPHQL_ENDPOINT}" ]; then
-s "${GRAPHQL_ENDPOINT}" -o "/tmp/graphQL_${SESSION_ID}.json" -w "%{http_code}")
current_check=$((current_check + 1))
# Check if the response contains "capabilities"
if [[ "$endpoint_checks" = "404" ]] || [[ $current_check -eq $retry_time ]]; then
break
elif [[ "$endpoint_checks" = "401" ]] || [[ $current_check -eq $retry_time ]]; then
if [[ $current_check -eq $retry_time ]]; then
break
elif [[ "$endpoint_checks" = "200" ]] && [[ $(jq -e '.data.session.capabilities | fromjson | ."'se:vncEnabled'"' /tmp/graphQL_${SESSION_ID}.json >/dev/null) -eq 0 ]]; then
break
Expand Down Expand Up @@ -78,7 +76,7 @@ fi
# Normalize the video file name
TEST_NAME="$(echo "${TEST_NAME}" | tr ' ' '_' | tr -dc "${VIDEO_FILE_NAME_TRIM}" | cut -c 1-251)"

return_array=("${RECORD_VIDEO}" "${TEST_NAME}" "${endpoint_checks}" "${GRAPHQL_ENDPOINT}")
return_array=("${RECORD_VIDEO}" "${TEST_NAME}" "${GRAPHQL_ENDPOINT}")

# stdout the values for other scripts consuming
echo "${return_array[@]}"

0 comments on commit a3b4b62

Please sign in to comment.