summaryrefslogtreecommitdiffstats
path: root/scripts/scp-snapshot
blob: 48afb9eceeb5ec687d7486ce76614adb4858e38f (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
#!/bin/bash

# Working directory has to be owned by taskmanager
if [ ! -d /tmp/taskmanager ];then
	mkdir /tmp/taskmanger
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
client=$TM_CLIENT_IP

filePath=/tmp/upload/$UUID.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_$UUID.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
	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
	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' }'

	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 on client
delete=$(ssh -q -i $identityFile $user@$client "rm $filePath")

# DB variables
db_user="<DB_USER>"
db_password="<DB_PASSWORD>"

# 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_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';"

mysql -u $db_user --password=$db_password sat -e "$insert_query"
mysql -u $db_user --password=$db_password sat -e "$update_query"

# Delete downloaded /tmp/taskmanager stuff
rm -rf /tmp/taskmanager/*

exit 0