summaryrefslogtreecommitdiffstats
path: root/scripts/scp-snapshot
blob: 0012d58a785b0252fbf9089ea124f786af7da058 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/bash

# 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/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