Skip to content

Commit

Permalink
Merge pull request #18 from uc-cdis/fix/if-cond
Browse files Browse the repository at this point in the history
Fix/if cond
  • Loading branch information
mfshao authored Aug 14, 2024
2 parents 398062f + b488f52 commit d3f13c5
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions sidecar.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,40 +23,36 @@ populate_notebook() {
filesize=$(echo $j | jq -r .file_size)

# Need to add a literal newline character that's why the quote is ending on next line
drs_pull="!gen3 drs-pull object $obj
"
drs_pull="!gen3 drs-pull object $obj"
# Need to add a literal newline character that's why the quote is ending on next line
jq --arg cmd "# File name: $filename - File size: $filesize
" '.cells[5].source += [$cmd]' "$FOLDER/data.ipynb" > "$FOLDER/data.tmp"
jq --arg cmd "# File name: $filename - File size: $filesize" '.cells[5].source += [$cmd]' "$FOLDER/data.ipynb" > "$FOLDER/data.tmp"
mv "$FOLDER/data.tmp" "$FOLDER/data.ipynb"

jq --arg cmd "$drs_pull" '.cells[5].source += [$cmd]' "$FOLDER/data.ipynb" > "$FOLDER/data.tmp"
mv "$FOLDER/data.tmp" "$FOLDER/data.ipynb"


done
log "Done populating notebook"
}

function populate() {
log "querying manifest service at $GEN3_ENDPOINT/manifests"
MANIFEST=$(curl -s -H "Authorization: Bearer ${ACCESS_TOKEN}" "https://$GEN3_ENDPOINT/manifests")
MANIFESTS=$(curl -s -H "Authorization: Bearer ${ACCESS_TOKEN}" "https://$GEN3_ENDPOINT/manifests/")
log "querying manifest service at $GEN3_ENDPOINT/metadata"
METADATA=$(curl -s -H "Authorization: Bearer ${ACCESS_TOKEN}" "https://$GEN3_ENDPOINT/manifests/metadata")

while [ -z "$MANIFEST" ] && [ -z "$METADATA_FILE" ]; do
if [ -z "$MANIFEST" ]; then
log "Unable to get manifests from '$GEN3_ENDPOINT/manifests'"
log $MANIFEST
while [ -z "$MANIFESTS" ] && [ -z "$METADATA" ]; do
if [ -z "$MANIFESTS" ]; then
log "Unable to get manifests from '$GEN3_ENDPOINT/manifests/'"
log $MANIFESTS
fi
if [ -z "$METADATA_FILE" ]; then
if [ -z "$METADATA" ]; then
log "Unable to get metadata from '$GEN3_ENDPOINT/manifests/metadata'"
log $METADATA
fi
log "sleeping for 15 seconds before trying again.."
sleep 15
MANIFEST=$(curl -s -H "Authorization: Bearer ${ACCESS_TOKEN}" "https://$GEN3_ENDPOINT/manifests")
MANIFESTS=$(curl -s -H "Authorization: Bearer ${ACCESS_TOKEN}" "https://$GEN3_ENDPOINT/manifests/")
METADATA=$(curl -s -H "Authorization: Bearer ${ACCESS_TOKEN}" "https://$GEN3_ENDPOINT/manifests/metadata")
done
log "successfully retrieved manifests and metadata for user"
Expand All @@ -77,24 +73,25 @@ function populate() {
# make sure folder can be written to by notebook
chown -R 1000:100 $FOLDER

if [["$base_dir" == "manifests"]];then
if [[ "$base_dir" == "manifests" ]]; then
MANIFEST_FILE=$(curl -s -H "Authorization: Bearer ${ACCESS_TOKEN}" "https://$GEN3_ENDPOINT/manifests/file/$FILENAME")
echo "${MANIFEST_FILE}" > $FOLDER/manifest.json
log "Creating notebook for $FILENAME"
cp ./template_manifest.json $FOLDER/data.ipynb
populate_notebook "$MANIFEST_FILE" "$FOLDER"
elif [["$base_dir" == "metadata"]];then
elif [[ "$base_dir" == "metadata" ]]; then
METADATA_FILE=$(curl -s -H "Authorization: Bearer ${ACCESS_TOKEN}" "https://$GEN3_ENDPOINT/manifests/metadata/$FILENAME")
echo "${METADATA_FILE}" > $FOLDER/metadata.json
fi
fi
done
}
if [ -n "$MANIFEST_FILE" ]; then
process_files "manifests" "$(echo $MANIFEST_FILE | jq -c '.manifests')"

if [ -n "$MANIFESTS" ]; then
process_files "manifests" "$(echo $MANIFESTS | jq -c '.manifests')"
fi
if [ -n "$METADATA_FILE" ]; then
process_files "metadata" "$(echo $METADATA_FILE | jq -c '.external_file_metadata')"
if [ -n "$METADATA" ]; then
process_files "metadata" "$(echo $METADATA | jq -c '.external_file_metadata')"
fi

# Make sure notebook user has write access to the folders
Expand Down Expand Up @@ -175,11 +172,11 @@ function main() {
mkdir "/data/${GEN3_ENDPOINT}/"
fi

log "Trying to populate data from MDS..."
log "Trying to populate data from Manifest Service..."
while true; do
populate
# If the access token expires, fetch a new access token and try again
if [[ $(echo "$MANIFEST" | jq -r '.error') = "Please log in." ]]; then
if [[ $(echo "$MANIFESTS" | jq -r '.error') = "Please log in." ]]; then
echo "Session Expired. Trying again with new access token"
get_access_token
else
Expand Down

0 comments on commit d3f13c5

Please sign in to comment.