diff options
author | Jannik Schönartz | 2021-11-29 00:58:18 +0100 |
---|---|---|
committer | Jannik Schönartz | 2021-11-29 00:58:18 +0100 |
commit | 9097da596d8f58a72c115a7873794106ef34b5e0 (patch) | |
tree | dc8265bddb2b233d7b847d7b67082b51bd14155c /scripts/scp-snapshot | |
parent | Add script for downloading/merging the edited image (diff) | |
download | tmlite-bwlp-remote-edit-vm.tar.gz tmlite-bwlp-remote-edit-vm.tar.xz tmlite-bwlp-remote-edit-vm.zip |
Rework snapshot task to work with edit id, add file deletion on the client (because runvirt doesn't have enough permission) and add lecture mysql update, to use the new merged versionremote-edit-vm
Diffstat (limited to 'scripts/scp-snapshot')
-rwxr-xr-x | scripts/scp-snapshot | 45 |
1 files changed, 25 insertions, 20 deletions
diff --git a/scripts/scp-snapshot b/scripts/scp-snapshot index 48afb9e..0012d58 100755 --- a/scripts/scp-snapshot +++ b/scripts/scp-snapshot @@ -1,24 +1,28 @@ #!/bin/bash -# Working directory has to be owned by taskmanager +# Those variables need to be manually set +WEBSUITE_BACKEND="<WEBSUITE_URL>" +db_user="<DB_USER>" +db_password="<DB_PASSWORD>" + +# Working directory if [ ! -d /tmp/taskmanager ];then - mkdir /tmp/taskmanger + mkdir /tmp/taskmanager fi workdir=/tmp/taskmanager # For the service, the ssh keys has to be in the /home/taskmanager/.ssh directory -# identityFile=/root/.ssh/ssh_poolclients identityFile=~/.ssh/ssh_poolclients -# Info for loggin into the client -# Always login as root for now but $TM_USERNAME can be set via the apicall in slx-admin &username=XXX -user=root -UUID=$TM_UUID - -# Clientip is set during the slx-admin api call clientip=XXX +# Info for loggin into the client (always login as root for now) +# Clientip is set during the slx-admin api based on the given editid client=$TM_CLIENT_IP +user=root +EDIT_ID=$TM_EDIT_ID +LECTURE_ID=$TM_LECTURE_ID -filePath=/tmp/upload/$UUID.qcow2 +# Path is hardcoded for now +filePath=/tmp/upload/$EDIT_ID.qcow2 fileName=$(basename $filePath) # Write ssh fingerprint in known_hosts file @@ -26,15 +30,14 @@ ssh-keyscan -H $client >> ~/.ssh/known_hosts # Get filesize via scp fileSize=$(ssh -q -i $identityFile $user@$client "stat --printf='%s' $filePath") -fileDestination=$workdir/snapshot_$UUID.qcow2 +fileDestination=$workdir/snapshot_$EDIT_ID.qcow2 script -q -f -c "scp -i $identityFile $user@$client:$filePath $fileDestination" $workdir/typescript > $workdir/progress & -WEBSUITE_BACKEND="<WEBSUITE_BACKEND_URL>" lastProgress="" while true do - # Get the last line written by the scp process + # Get the last line written by the scp process (
: ctrl + v -> m) progress=$(tail -n 1 $workdir/progress | tr "
" "$" | rev | cut -d "$" -f2 | rev) # Ignore when the progress didn't change @@ -49,7 +52,9 @@ do eta=$(echo $progress | cut -d " " -f5) # Upload the progress to the bwLehrpool webSuite backend - curl -X POST https://$WEBSUITE_BACKEND/images/$UUID/versions/upload/progress -H "Content-Type: application/json" -d '{ "fileName": '$fileName', "progress": '$percent', "size": '$size', "speed": '$speed', "eta": '$eta' }' + json_template='{ "state": "%s", "fileName": "%s", "percent": "%s", "size": "%s", "speed": "%s", "eta": "%s" }' + json_string=$(printf "$json_template" "TRANSFER" "$fileName" "$percent" "$size" "$speed" "$eta") + curl -X POST $WEBSUITE_BACKEND/edits/$EDIT_ID/progress -H "Content-Type: application/json" -d "$json_string" if [ "$percent" == "100%" ]; then break @@ -59,16 +64,14 @@ do lastProgress=$progress done -# Upload is finished so delete snapshot on client +# Upload is finished so delete snapshot and edit.desktop on client delete=$(ssh -q -i $identityFile $user@$client "rm $filePath") - -# DB variables -db_user="<DB_USER>" -db_password="<DB_PASSWORD>" +delete=$(ssh -q -i $identityFile $user@$client "rm /var/lib/lightdm/edit.desktop") +delete=$(ssh -q -i $identityFile $user@$client "rm /var/lib/lightdm/editsession.id") # Merge snapshot with image # 1. Make a copy of the baseimage with rsync (to have progress) -image_version_uuid=$(mysql -u $db_user --password=$db_password sat -se "SELECT imageversionid from lecture WHERE lectureid='$UUID'") +image_version_uuid=$(mysql -u $db_user --password=$db_password sat -se "SELECT imageversionid from lecture WHERE lectureid='$LECTURE_ID'") image_path=$(mysql -u $db_user --password=$db_password sat -se "SELECT filepath from imageversion WHERE imageversionid='$image_version_uuid'") # TODO: Currently only nfs? bwlp_store=/srv/openslx/nfs @@ -91,9 +94,11 @@ image_virtualizerconfig=$(mysql -u $db_user --password=$db_password sat -se "SEL insert_query="INSERT INTO imageversion (imageversionid, imagebaseid, createtime, expiretime, filesize, filepath, uploaderid, virtualizerconfig, isrestricted, isvalid, isprocessed) VALUES ('$image_uuid','$image_base_uuid',$(date +%s),$image_expire,$image_filesize,'bwlehrpool_store/$image_base_copy','af73720a8be1aace58debedc21d7cb30','$image_virtualizerconfig',0,1,0);" update_query="UPDATE imagebase SET latestversionid = '$image_uuid' WHERE imagebaseid = '$image_base_uuid';" +update_lecture_query="UPDATE lecture SET imageversionid = '$image_uuid' WHERE lectureid = '$LECTURE_ID'" mysql -u $db_user --password=$db_password sat -e "$insert_query" mysql -u $db_user --password=$db_password sat -e "$update_query" +mysql -u $db_user --password=$db_password sat -e "$update_lecture_query" # Delete downloaded /tmp/taskmanager stuff rm -rf /tmp/taskmanager/* |