Fix snapshots

This commit is contained in:
Maniack Crudelis 2017-05-15 18:28:16 +02:00
parent f2e1a388be
commit 3e6f16c430
2 changed files with 24 additions and 12 deletions

View file

@ -8,6 +8,7 @@ echo -e "Loads functions from launcher.sh"
arg_ssh="-tt" arg_ssh="-tt"
snapshot_path="/var/lib/lxcsnaps/$lxc_name" snapshot_path="/var/lib/lxcsnaps/$lxc_name"
current_snapshot=snap0
#================================================= #=================================================
@ -27,8 +28,10 @@ create_temp_backup () {
# Create the snapshot. # Create the snapshot.
sudo lxc-snapshot --name $lxc_name >> "$test_result" 2>&1 sudo lxc-snapshot --name $lxc_name >> "$test_result" 2>&1
# Get the last created snapshot and return it # Get the last created snapshot, and set it as the new current snapshot.
sudo lxc-snapshot --name $lxc_name --list | sort | tail --lines=1 | cut --delimiter=' ' --fields=1 current_snapshot=$(sudo lxc-snapshot --name $lxc_name --list | sort | tail --lines=1 | cut --delimiter=' ' --fields=1)
# And return it
echo "$current_snapshot"
# Restart the container, after the snapshot # Restart the container, after the snapshot
LXC_START "true" >&2 LXC_START "true" >&2
@ -37,13 +40,13 @@ create_temp_backup () {
use_temp_snapshot () { use_temp_snapshot () {
# Use a temporary snapshot, if it already exists # Use a temporary snapshot, if it already exists
# $1 = Name of the snapshot to use # $1 = Name of the snapshot to use
local snapshot_name=$1 current_snapshot=$1
# Fix the missing hostname in the hosts file... # Fix the missing hostname in the hosts file...
echo "127.0.0.1 $lxc_name" | sudo tee --append "$snapshot_path/$snapshot_name/rootfs/etc/hosts" > /dev/null echo "127.0.0.1 $lxc_name" | sudo tee --append "$snapshot_path/$current_snapshot/rootfs/etc/hosts" > /dev/null
# Restore this snapshot. # Restore this snapshot.
sudo rsync --acls --archive --delete --executability --itemize-changes --xattrs "$snapshot_path/$snapshot_name/rootfs/" "/var/lib/lxc/$lxc_name/rootfs/" > /dev/null 2>> "$test_result" sudo rsync --acls --archive --delete --executability --itemize-changes --xattrs "$snapshot_path/$current_snapshot/rootfs/" "/var/lib/lxc/$lxc_name/rootfs/" > /dev/null 2>> "$test_result"
# Fake the yunohost_result return code of the installation # Fake the yunohost_result return code of the installation
yunohost_result=0 yunohost_result=0
@ -67,6 +70,7 @@ destroy_temporary_snapshot () {
# Clear the variables which contains the snapshot names # Clear the variables which contains the snapshot names
unset root_snapshot unset root_snapshot
unset subpath_snapshot unset subpath_snapshot
current_snapshot=snap0
} }
LXC_INIT () { LXC_INIT () {
@ -182,22 +186,22 @@ LXC_STOP () {
# Fix the missing hostname in the hosts file # Fix the missing hostname in the hosts file
# If the hostname is missing in /etc/hosts inside the snapshot # If the hostname is missing in /etc/hosts inside the snapshot
if ! sudo grep --quiet "$lxc_name" "$snapshot_path/snap0/rootfs/etc/hosts" if ! sudo grep --quiet "$lxc_name" "$snapshot_path/$current_snapshot/rootfs/etc/hosts"
then then
# If the hostname was replaced by snap0, fix it # If the hostname was replaced by name of the snapshot, fix it
if sudo grep --quiet "snap0" "$snapshot_path/snap0/rootfs/etc/hosts" if sudo grep --quiet "$current_snapshot" "$snapshot_path/$current_snapshot/rootfs/etc/hosts"
then then
# Replace snap0 by the real hostname # Replace snapX by the real hostname
sudo sed --in-place "s/snap0/$lxc_name/" "$snapshot_path/snap0/rootfs/etc/hosts" sudo sed --in-place "s/$current_snapshot/$lxc_name/" "$snapshot_path/$current_snapshot/rootfs/etc/hosts"
else else
# Otherwise, simply add the hostname # Otherwise, simply add the hostname
echo "127.0.0.1 $lxc_name" | sudo tee --append "$snapshot_path/snap0/rootfs/etc/hosts" > /dev/null echo "127.0.0.1 $lxc_name" | sudo tee --append "$snapshot_path/$current_snapshot/rootfs/etc/hosts" > /dev/null
fi fi
fi fi
# Restore the snapshot. # Restore the snapshot.
echo "Restore the previous snapshot." | tee --append "$test_result" echo "Restore the previous snapshot." | tee --append "$test_result"
sudo rsync --acls --archive --delete --executability --itemize-changes --xattrs "$snapshot_path/snap0/rootfs/" "/var/lib/lxc/$lxc_name/rootfs/" > /dev/null 2>> "$test_result" sudo rsync --acls --archive --delete --executability --itemize-changes --xattrs "$snapshot_path/$current_snapshot/rootfs/" "/var/lib/lxc/$lxc_name/rootfs/" > /dev/null 2>> "$test_result"
} }
LXC_TURNOFF () { LXC_TURNOFF () {

View file

@ -28,6 +28,9 @@ break_before_continue () {
SETUP_APP () { SETUP_APP () {
# Install an application in a LXC container # Install an application in a LXC container
# Uses the default snapshot
current_snapshot=snap0
# Install the application in a LXC container # Install the application in a LXC container
LXC_START "sudo yunohost --debug app install \"$package_dir\" -a \"$manifest_args_mod\"" LXC_START "sudo yunohost --debug app install \"$package_dir\" -a \"$manifest_args_mod\""
@ -1015,6 +1018,8 @@ CHECK_BACKUP_RESTORE () {
# Second, restore the whole container to remove completely the application # Second, restore the whole container to remove completely the application
elif [ $j -eq 1 ] elif [ $j -eq 1 ]
then then
# Uses the default snapshot
current_snapshot=snap0
# Stop and restore the LXC container # Stop and restore the LXC container
LXC_STOP LXC_STOP
@ -1253,6 +1258,9 @@ TEST_LAUNCHER () {
# Execute the test # Execute the test
$1 $2 $1 $2
# Uses the default snapshot
current_snapshot=snap0
# Stop and restore the LXC container # Stop and restore the LXC container
LXC_STOP LXC_STOP
} }