bootstrap-update.sh 6.03 KB
Newer Older
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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
#!/bin/bash
# Copyright (C) 2009-2012 Centro de Computacao Cientifica e Software Livre
# Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
#
# This file is part of buildpackage
#
# buildpackage is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.

# This is the update bootstrap

# ------------------------------------------------------------------------------
# This function verifies if the package MD5 is correct
verify_md5(){
    printf "[SEED2-update] Checking MD5... "
    if [[ ${MD5_CHECK} == $(md5sum "${SEED_MEC_HOME}/update/tmp/${FILE}" | \
                            awk '{print $1}') ]]; then
        printf "OK!\n"
    else
        printf "ERROR: This package is currupted.\n"
        rm -rf ${FILE} &> /dev/null
        exit 1
    fi
}

# ------------------------------------------------------------------------------
# Re-create the inep and proxy's MD5
restore_inep_proxy_md5sum(){
    for FILE in inep proxy; do
        md5sum "${SEED_MEC_HOME}/SEED2/client/conf/${FILE}" |cut -d' ' -f1 > \
               "${SEED_MEC_HOME}/${UPDATE_DIR}/.md5sum/client/conf/${FILE}"

        # The backup md5 is the same
        cp -a "${SEED_MEC_HOME}/${UPDATE_DIR}/.md5sum/client/conf/${FILE}" \
               "${SEED_MEC_HOME}/${UPDATE_DIR}/.md5sum/.backup/client/conf/"
    done
}

# ------------------------------------------------------------------------------
# Re-create the inep and proxy files' backup and copy the MD5 files
restore_inep_proxy_backup(){
    for FILE in inep proxy; do
        # The backup itself
        cp -a "${SEED_MEC_HOME}/SEED2/client/conf/${FILE}" \
              "${SEED_MEC_HOME}/${UPDATE_DIR}/.backup/client/conf/"
    done
}

# ------------------------------------------------------------------------------
# These variables will be replaced while generating the package
FILE=
MD5_CHECK=
BOOTSTRAP_LINES=

if test -n "${HOME}"; then
    SEED_MEC_HOME=${HOME}
else
    SEED_MEC_HOME=/opt/seed_mec
fi
UPDATE_DIR="update/tmp/SEED2-run"

# ------------------------------------------------------------------------------
# Create auxiliar directories
mkdir -p "${SEED_MEC_HOME}/update/tmp"
mkdir -p "${SEED_MEC_HOME}/update/old"

# Extract the tar package
tail -n +${BOOTSTRAP_LINES} "$0" > "${SEED_MEC_HOME}/update/tmp/${FILE}"
verify_md5
cd "${SEED_MEC_HOME}/update/tmp"
tar xzf "${FILE}" &> /dev/null
cd - &> /dev/null

# ------------------------------------------------------------------------------
# Make sure the directories exist in the update tree to backup the inep and
# proxy files, along with their counterparts (backup and MD5)
mkdir -p "${SEED_MEC_HOME}/update/tmp/SEED2-run/client/conf"
mkdir -p "${SEED_MEC_HOME}/update/tmp/SEED2-run/.md5sum/client/conf"
mkdir -p "${SEED_MEC_HOME}/update/tmp/SEED2-run/.backup/client/conf"
mkdir -p "${SEED_MEC_HOME}/update/tmp/SEED2-run/.md5sum/.backup/client/conf"

# Check if the inep and proxy files exist
ERROR=0
INEP_PROXY="${SEED_MEC_HOME}/SEED2/client/conf/inep
            ${SEED_MEC_HOME}/SEED2/client/conf/proxy"
for FILE in ${INEP_PROXY}; do
    test -f "${FILE}" || ERROR=1
done

# Copy the inep and proxy files. Also, copy their backups and MD5 counterparts,
# if they exist. Otherwise, create them
if test "${ERROR}" -eq 0; then

    # Copy inep and proxy to the new tree
    cp -a "${SEED_MEC_HOME}/SEED2/client/conf/"{inep,proxy} \
          "${SEED_MEC_HOME}/update/tmp/SEED2-run/client/conf/"

    # Copy or create the md5 files from inep and proxy
    (test -d "${SEED_MEC_HOME}/SEED2/.md5sum" &&
        cp -a "${SEED_MEC_HOME}/SEED2/.md5sum/client/conf/"{inep,proxy} \
              "${SEED_MEC_HOME}/update/tmp/SEED2-run/.md5sum/client/conf/" &&
        cp -a "${SEED_MEC_HOME}/SEED2/.md5sum/.backup/client/conf/"{inep,proxy} \
              "${SEED_MEC_HOME}/update/tmp/SEED2-run/.md5sum/.backup/client/conf/") ||
    restore_inep_proxy_md5sum

    # Copy or create the backup files from inep and proxy
    (test -d "${SEED_MEC_HOME}/SEED2/.backup" &&
        cp -a "${SEED_MEC_HOME}/SEED2/.backup/client/conf/"{inep,proxy} \
              "${SEED_MEC_HOME}/update/tmp/SEED2-run/.backup/client/conf/") ||
    restore_inep_proxy_backup

fi

# ------------------------------------------------------------------------------
# TODO: do some security backup???
#cp -ra "${SEED_MEC_HOME}/SEED2/" "${SEED_MEC_HOME}/old/"

# If something interrupts the script here, the tree will not be entirely
# correct, so a file is created to mark that something happened. During the next
# next execution the package will try to recover itself
##### CRITICAL #####
touch "${SEED_MEC_HOME}/SEED2/updating"

# md5sum and backup folders
rm -rf "${SEED_MEC_HOME}/SEED2/.md5sum" \
       "${SEED_MEC_HOME}/SEED2/.backup" &> /dev/null
mv -f "${SEED_MEC_HOME}/update/tmp/SEED2-run/"{.md5*,.back*}* \
      "${SEED_MEC_HOME}/SEED2/"

# Copy the files themself
143
rm -rf "${SEED_MEC_HOME}/SEED2/"{*.sh,bin,lib,net,client,extra,network-scripts,scripts} &> /dev/null
144 145 146 147 148 149 150 151 152 153
mv -f "${SEED_MEC_HOME}/update/tmp/SEED2-run/"* \
      "${SEED_MEC_HOME}/SEED2/"

rm -f "${SEED_MEC_HOME}/SEED2/updating" &> /dev/null
### END CRITICAL ###

# ------------------------------------------------------------------------------
# Remove auxiliar directories
rm -rf "${SEED_MEC_HOME}/update" &> /dev/null

154 155 156 157 158 159
#Fix the le-autoupgrade on LE 5
if test -f /etc/le_version && \
    dpkg --compare-versions $(cat /etc/le_version ) gt 4; then
    sudo rm -f '/etc/apt/sources.list.d/quantal-repos.list'
fi

160 161 162 163
# Call the agent
"${SEED_MEC_HOME}/SEED2/update.sh"

exit 0