Blame view

software/slaprunner/template/runner-import.sh.jinja2 7.19 KB
Cédric Le Ninivin committed
1
#!{{ shell_binary }}
Marco Mariani committed
2 3
LC_ALL=C
export LC_ALL
Cédric Le Ninivin committed
4
umask 077
Nicolas Wavrant committed
5

Vincent Pelletier committed
6 7 8 9
# Exit on any error, to prevent inconsistent backup
# Error on unset variable expansion
set -eu

Nicolas Wavrant committed
10
# Redirect output to log
Vincent Pelletier committed
11
exec > >(tee -ai '{{ output_log_file }}')
Nicolas Wavrant committed
12 13
exec 2>&1

Vincent Pelletier committed
14 15 16
echo -e "\n\n$0 run at : $(date)"

srv_directory='{{ directory["srv"] }}'
Alain Takoudjou committed
17
backup_directory='{{ directory["backup"] }}'
Vincent Pelletier committed
18 19 20 21
etc_directory='{{ directory["etc"] }}'

RESTORE_EXIT_CODE_FILE='{{ restore_exit_code_file }}'
RESTORE_ERROR_MESSAGE_FILE='{{ restore_error_message_file }}'
Nicolas Wavrant committed
22
ERROR_MESSAGE=""
Nicolas Wavrant committed
23 24 25

fail_with_exit_code () {
  echo 1 > $RESTORE_EXIT_CODE_FILE
Nicolas Wavrant committed
26
  echo -e "Failure during step : $ERROR_MESSAGE" > $RESTORE_ERROR_MESSAGE_FILE
Nicolas Wavrant committed
27
  exit 1
Nicolas Wavrant committed
28 29
}
trap fail_with_exit_code ERR
Nicolas Wavrant committed
30

Nicolas Wavrant committed
31 32 33 34 35
log_message () {
    ERROR_MESSAGE=$1
    echo -e $1
}
# Delete the error message file, to not keep it even after a successful build
Vincent Pelletier committed
36 37 38 39 40 41 42 43
rm "$RESTORE_ERROR_MESSAGE_FILE" || true

rsync () {
  set -x
  '{{ rsync_binary }}' -rlptgov --stats --safe-links --delete "$@"
  set +x
}

Nicolas Wavrant committed
44
log_message "Restoring WebRunner content..."
Vincent Pelletier committed
45
(
Julien Muchembled committed
46
  # XXX: code duplication with runner-export.sh.jinja2
Vincent Pelletier committed
47 48 49 50 51 52 53 54
  path=$srv_directory/runner
  backup_path=$backup_directory/runner/
  cd "$backup_path"

  if [ -d instance ]; then
    # Concatenate the exclude file of each partition of webrunner
    # to create a global exclude file.
    # Also, ignore all buildout-managed files.
Julien Muchembled committed
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
    exclude=$({{ sys.executable }} - "$path" <<EOF
if 1:
        import glob, errno, os, sys
        sys.path[:0] = {{ repr(easy_install.buildout_and_setuptools_path) }}
        from zc.buildout.configparser import parse
        path = sys.argv[1]

        def print_relative(path_list):
            for p in path_list:
                p = p.strip()
                if p:
                    print(os.path.relpath(p, path))
        print("*.sock")
        print("*.socket")
        print("*.pid")
        print(".installed*.cfg")
        for partition in glob.glob(path + "/instance/slappart*"):
            os.chdir(partition)
            try:
                with open("srv/exporter.exclude") as f:
                    exclude = f.readlines()
            except IOError as e:
                if e.errno != errno.ENOENT:
                    raise
            else:
                print_relative(exclude)
            for installed in glob.glob(".installed*.cfg"):
                try:
                    with open(installed) as f:
                        installed = parse(f, installed)
                except IOError as e:
                    if e.errno != errno.ENOENT:
                        raise
                else:
                    for section in installed.itervalues():
                        print_relative(section.get(
                            '__buildout_installed__', '').splitlines())
EOF
)
    echo "$exclude" |rsync --exclude-from=- instance "$path"
Vincent Pelletier committed
95
  fi
Nicolas Wavrant committed
96

Vincent Pelletier committed
97
  test -d project  && rsync project "$path"
Nicolas Wavrant committed
98
  test -d public  && rsync public "$path"
Vincent Pelletier committed
99 100
  test -f proxy.db && rsync proxy.db "$path"
)
Rafael Monnerat committed
101

Vincent Pelletier committed
102 103 104 105 106 107 108
log_message "Restoring WebRunner config (etc directory)..."
(
  cd "$backup_directory"/etc/
  rsync config.json "$etc_directory"
  # Hidden files are related to the webrunner's internals
  cp -r .??* "$etc_directory"
)
Rafael Monnerat committed
109

110
# Invoke arbitrary script to perform specific restoration
Rafael Monnerat committed
111
# procedure.
112
runner_import_restore=$srv_directory/runner-import-restore
Vincent Pelletier committed
113 114 115
if [ -x "$runner_import_restore" ]; then
  log_message "Running $runner_import_restore..."
  "$srv_directory/runner-import-restore"
116
fi
117

Nicolas Wavrant committed
118
# If no "etc/.project" neither "srv/runner/proxy.db", we can safely assume
Vincent Pelletier committed
119 120
# that there is no instance deployed on runner0
if [ ! -f "$etc_directory/.project" -a ! -f "$srv_directory/runner/proxy.db" ]; then
Nicolas Wavrant committed
121
  log_message "No Software Requested... Writing status file... End"
Nicolas Wavrant committed
122 123 124 125
  echo 0 > $RESTORE_EXIT_CODE_FILE
  exit 0
fi

Nicolas Wavrant committed
126
log_message "Updating slapproxy database..."
Vincent Pelletier committed
127
HOME='{{ directory["home"] }}'
128 129 130
# XXX Hardcoded
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
export MAKEFLAGS=-j4
Vincent Pelletier committed
131
SLAPOS='{{ directory["bin"] }}'/slapos
132 133 134 135
# 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
136
# XXX hardcoded
137
PARTITION=$(basename $HOME)
Vincent Pelletier committed
138
OLD_SOFTWARE_RELEASE=$("$SQLITE3" "$DATABASE" "select software_release from partition11 where reference='slappart0';")
Rafael Monnerat committed
139
SOFTWARE_RELEASE=$(echo $OLD_SOFTWARE_RELEASE | sed -e 's/\(.*\)\(slappart\|test0-\)[0-9][0-9][0-9]\?/\1'"$PARTITION"'/')
Vincent Pelletier committed
140 141
"$SQLITE3" "$DATABASE" "update partition11 set software_release='$SOFTWARE_RELEASE' where software_release NOT NULL;"
"$SQLITE3" "$DATABASE" "update software11 set url='$SOFTWARE_RELEASE' where url='$OLD_SOFTWARE_RELEASE';" || "$SQLITE3" "$DATABASE" "delete from software11 where url='$OLD_SOFTWARE_RELEASE';"
142
# Change slapproxy database to have all instances stopped
Vincent Pelletier committed
143
"$SQLITE3" "$DATABASE" "update partition11 set requested_state='stopped';"
Nicolas Wavrant committed
144
# Change slapproxy database to get correct IPs
Vincent Pelletier committed
145 146 147 148
IPV4='{{ ipv4 }}'
IPV6='{{ ipv6 }}'
"$SQLITE3" "$DATABASE" "update partition_network11 set address='$IPV4' where netmask='255.255.255.255';"
"$SQLITE3" "$DATABASE" "update partition_network11 set address='$IPV6' where netmask='ffff:ffff:ffff::';"
149

Vincent Pelletier committed
150
MASTERURL='http://{{ ipv4 }}:{{ proxy_port }}'
151

Nicolas Wavrant committed
152 153
log_message "Removing old supervisord service description files..."
# XXX: Path hardcoded in slapos.core
Vincent Pelletier committed
154 155 156 157 158
rm '{{ instance_folder }}'/etc/supervisord.conf.d/* || true

SLAPOSCFG='{{ supervisord["slapos-cfg"] }}'
SLAPGRIDSRLOG='{{ supervisord["slapgrid-sr-log"] }}'
SLAPGRIDCPLOG='{{ supervisord["slapgrid-cp-log"] }}'
Nicolas Wavrant committed
159

Nicolas Wavrant committed
160
log_message "Building newest Software Release..."
Jérome Perrin committed
161 162 163
"$SLAPOS" node software --cfg "$SLAPOSCFG" --all --master-url="$MASTERURL" --logfile "$SLAPGRIDSRLOG" >/dev/null 2>&1 ||
"$SLAPOS" node software --cfg "$SLAPOSCFG" --all --master-url="$MASTERURL" --logfile "$SLAPGRIDSRLOG" >/dev/null 2>&1 ||
"$SLAPOS" node software --cfg "$SLAPOSCFG" --all --master-url="$MASTERURL" --logfile "$SLAPGRIDSRLOG" >/dev/null 2>&1 ||
Vincent Pelletier committed
164
(tail -n 200 "$SLAPGRIDSRLOG" && false)
165
# Remove defined scripts to force buildout to recreate them to have updated paths
Vincent Pelletier committed
166
rm "$srv_directory"/runner/instance/slappart*/srv/runner-import-restore || true
Nicolas Wavrant committed
167
log_message "Fixing Instances as needed after import..."
168
# XXX hardcoded
Jérome Perrin committed
169 170 171
"$SLAPOS" node instance --cfg "$SLAPOSCFG" --master-url=$MASTERURL --logfile "$SLAPGRIDCPLOG" >/dev/null 2>&1 ||
"$SLAPOS" node instance --cfg "$SLAPOSCFG" --master-url=$MASTERURL --logfile "$SLAPGRIDCPLOG" >/dev/null 2>&1 ||
"$SLAPOS" node instance --cfg "$SLAPOSCFG" --master-url=$MASTERURL --logfile "$SLAPGRIDCPLOG" >/dev/null 2>&1 ||
Vincent Pelletier committed
172
(tail -n 200 "$SLAPGRIDCPLOG" && false)
173

174
# Invoke defined scripts for each partition inside of slaprunner
Nicolas Wavrant committed
175
log_message "Invoke custom import scripts defined by each instances..."
Vincent Pelletier committed
176
for partition in "$srv_directory"/runner/instance/slappart*/
177 178
do
  script=$partition/srv/runner-import-restore
Vincent Pelletier committed
179
  if [ -x "$script" ]; then
Nicolas Wavrant committed
180
    log_message "Running custom instance script : $script..."
Vincent Pelletier committed
181
    "$script"
182 183
  fi
done
184

185
# Change back slapproxy database to have all instances started
Nicolas Wavrant committed
186
log_message "Set instances as to start after takeover..."
Vincent Pelletier committed
187
"$SQLITE3" "$DATABASE" "update partition11 set requested_state='started';"
188 189

# Write exit code to an arbitrary file that will be checked by promise/monitor
Nicolas Wavrant committed
190
log_message "Writing status file... End"
Nicolas Wavrant committed
191 192
echo 0 > $RESTORE_EXIT_CODE_FILE
exit 0