#!/bin/bash # Those variables need to be manually set WEBSUITE_BACKEND="" db_user="" db_password="" # Working directory if [ ! -d /tmp/taskmanager ];then mkdir /tmp/taskmanager fi workdir=/tmp/taskmanager # For the service, the ssh keys has to be in the /home/taskmanager/.ssh directory identityFile=~/.ssh/ssh_poolclients # 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 # Path is hardcoded for now filePath=/tmp/upload/$EDIT_ID.qcow2 fileName=$(basename $filePath) # Write ssh fingerprint in known_hosts file 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_$EDIT_ID.qcow2 script -q -f -c "scp -i $identityFile $user@$client:$filePath $fileDestination" $workdir/typescript > $workdir/progress & lastProgress="" while true do # 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 if [ "$lastProgress" == "$progress" ]; then continue fi # If status did change, cut the % from the scp progress and see if it's 100% (finished) percent=$(echo $progress | cut -d " " -f2) size=$(echo $progress | cut -d " " -f3) speed=$(echo $progress | cut -d " " -f4) eta=$(echo $progress | cut -d " " -f5) # Upload the progress to the bwLehrpool webSuite backend 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 fi # Set the lastProgess to the current progress and goto the next iteration of the loop lastProgress=$progress done # Upload is finished so delete snapshot and edit.desktop on client delete=$(ssh -q -i $identityFile $user@$client "rm $filePath") 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='$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 image_base_copy="$(date +'%y-%m')/$(date +'%d_%H-%M-%S_merged-image.qcow2')" script -q -f -c "rsync --mkpath --info=progress2 $bwlp_store/$image_path $bwlp_store/bwlehrpool_store/$image_base_copy" $workdir/typescript_copy > $workdir/progress_copy #& # TODO while true read progress until 100 look above! # 2. Rebase the snapshot -- use -u (unsafe) so it works LUL qemu-img rebase -u -b "$bwlp_store/bwlehrpool_store/$image_base_copy" $fileDestination # 3. Commit (merge) the snapshot with the baseimage qemu-img commit $fileDestination # 4. Update database entries, to set the new image to the newest version image_expire=$(mysql -u $db_user --password=$db_password sat -se "SELECT expiretime from imageversion WHERE imageversionid='$image_version_uuid'") image_base_uuid=$(mysql -u $db_user --password=$db_password sat -se "SELECT imagebaseid from imageversion WHERE imageversionid='$image_version_uuid'") image_filesize=$(mysql -u $db_user --password=$db_password sat -se "SELECT filesize from imageversion WHERE imageversionid='$image_version_uuid'") image_uuid=$(cat /proc/sys/kernel/random/uuid) image_virtualizerconfig=$(mysql -u $db_user --password=$db_password sat -se "SELECT virtualizerconfig from imageversion WHERE imageversionid='$image_version_uuid'") 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/* exit 0