runner-import.sh.jinja2 3.57 KB
Newer Older
1
#!{{ shell_binary }}
2
set -e
3 4
LC_ALL=C
export LC_ALL
5
umask 077
6
srv_directory={{ directory['srv'] }}
7 8 9 10 11 12 13 14 15
restore_element () {
  backup_path=$1
  restore_path=$2
  shift 2
  element_list=$*
  for element in $element_list
  do
    cd $backup_path;
    if [ -f $element ] || [ -d $element ]; then
16
       {{ rsync_binary }} -av --delete $backup_path/$element $restore_path;
17 18 19
    fi
  done
}
20 21 22

write_backup_proof () {
  cd {{ directory['backup'] }}
23
  find -type f ! -name backup.signature ! -wholename "./rdiff-backup-data/*" -print0 | xargs -P4 -0 sha256sum  | LC_ALL=C sort -k 66 > $srv_directory/proof.signature
24
  diff -ruw {{ directory['backup'] }} $srv_directory/proof.signature > $srv_directory/backup.diff || true # diff exits with code 1 when files are different
25 26
}

27
# For now we just make the diff before
28 29
write_backup_proof

30
restore_element {{ directory['backup'] }}/runner/ $srv_directory/runner  instance project  proxy.db
31 32
restore_element  {{ directory['backup'] }}/etc/ {{ directory['etc'] }} config.json ssh
cp -r {{ directory['backup'] }}/etc/.??* {{ directory['etc'] }};
33
{{ curl_binary }} --insecure -g6L --max-time 5  {{ backend_url }}/isSRReady;
34 35


36
# Invoke arbitrary script to perform specific restoration
37
# procedure.
38
RESTORE_EXIT_CODE=0
39 40 41 42 43 44
runner_import_restore=$srv_directory/runner-import-restore
if [ ! -e "$runner_import_restore" ]; then
  touch $runner_import_restore
  chmod +x $runner_import_restore
fi
echo "Running $runner_import_restore script..."
45
$srv_directory/runner-import-restore || RESTORE_EXIT_CODE=$?
46

47 48 49
echo "Updating slapproxy database, software release and instances..."
HOME="{{ directory['home'] }}"
SLAPOS="{{ directory['bin'] }}/slapos"
50 51 52 53
# XXX hardcoded
SQLITE3="$HOME/software_release/parts/sqlite3/bin/sqlite3"
DATABASE="$HOME/srv/runner/proxy.db"
# Change slapproxy database to point instances to new software release
54
# XXX hardcoded
55 56
PARTITION=$(basename $HOME)
OLD_SOFTWARE_RELEASE=$($SQLITE3 $DATABASE "select software_release from partition11 where reference='slappart0';")
57
SOFTWARE_RELEASE=$(echo $OLD_SOFTWARE_RELEASE | sed -e 's/\(.*\)\(slappart\|test0-\)[0-9][0-9]\?/\1'"$PARTITION"'/')
58
$SQLITE3 $DATABASE "update partition11 set software_release='$SOFTWARE_RELEASE' where software_release NOT NULL;"
59
$SQLITE3 $DATABASE "update software11 set url='$SOFTWARE_RELEASE' where url='$OLD_SOFTWARE_RELEASE';"
60 61 62
# Change slapproxy database to have all instances stopped
$SQLITE3 $DATABASE "update partition11 set slap_state='stopped';"
# Build newest software
63
$SLAPOS node software --cfg $HOME/etc/slapos.cfg --pidfile $HOME/var/run/slapos-node-software.pid --all >/dev/null 2>&1 || true
64 65
# Remove defined scripts to force buildout to recreate them to have updated paths
rm $srv_directory/runner/instance/slappart*/srv/runner-import-restore
66
# Start supervisord
67
$SLAPOS node supervisord --cfg $HOME/etc/slapos.cfg -n >/dev/null 2>&1 &
68
sleep 5
69 70
# Run slapos node instance
# XXX hardcoded
71
$SLAPOS node instance --cfg $HOME/etc/slapos.cfg --pidfile $HOME/var/run/slapos-node-instance.pid >/dev/null 2>&1 || true
72

73 74 75 76 77 78
# Invoke defined scripts for each partition inside of slaprunner
for partition in $srv_directory/runner/instance/slappart*/
do
  script=$partition/srv/runner-import-restore
  if [ -e "$script" ]; then
    echo "Running $script script..."
79
    $script || RESTORE_EXIT_CODE=$?
80 81
  fi
done
82

83 84
# Change back slapproxy database to have all instances started
$SQLITE3 $DATABASE "update partition11 set slap_state='started';"
85 86 87 88 89

# Write exit code to an arbitrary file that will be checked by promise/monitor
RESTORE_EXIT_CODE_FILE="{{ restore_exit_code_file }}"
echo $RESTORE_EXIT_CODE > $RESTORE_EXIT_CODE_FILE
exit $RESTORE_EXIT_CODE