bootstrap-install 8.84 KB
Newer Older
1
#!/bin/bash
2
# Copyright (C) 2009-2012 Centro de Computacao Cientifica e Software Livre
3 4 5 6
# Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
#
# This file is part of buildpackage
#
7
# buildpackage is free software; you can redistribute it and/or
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 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 installation bootstrap

24

25 26
# This function checks the integrity of tar package
verify_md5(){
27
    printf "Verificando MD5... " >&3
28
    if [[ ${MD5_CHECK} == $(md5sum ${FILE} | awk '{print $1}') ]] ; then
29
        printf "OK!\n" >&3
30
    else
31
        printf "ERRO: Este pacote está corrompido. %b" \
32
               "Por favor, faça um novo download.\n" >&3
33
        rm -rf "${FILE}" &> /dev/null
34
        exit 4
35 36 37
    fi
}

38 39 40 41 42 43 44
# This function collects the agent log, data and runs some tests, in case some
# error ocurred during the installation
collect_information(){
    printf "\nAlgumas informações referentes ao agente PROINFODATA %b%b%b%b" \
           "recém instalado serão coletadas. O resultado da coleta " \
           "será armazenado no arquivo temporário \"${LOGFILE}\". Favor " \
           "enviar esse arquivo para \"proinfodata@c3sl.ufpr.br\". Assim " \
45
           "a equipe ficará ciente do problema para resolvê-lo.\n\n" >&3
46 47

    printf "Iniciando coleta das informações (isso pode demorar alguns %b" \
48
           "minutos)... " >&3
49 50 51 52 53 54 55 56 57 58 59

    printf "    Configuration files\n%b" \
           "    ===================\n\n"
    printf "version\n"
    cat ${AGENT_DIR}/client/conf/version
    printf "proxy\n"
    cat ${AGENT_DIR}/client/conf/proxy
    printf "inep\n"
    cat ${AGENT_DIR}/client/conf/inep
    printf "\n\n----------------------------------\n\n"

60 61 62 63 64 65 66 67 68 69 70 71 72
    printf "    OS information\n%b" \
           "    ==============\n\n"
    printf "Distro\n"
    DISTRO=$(cut -sd ' ' -f1 /etc/issue | head -n 1)
    if test -f "/etc/le_version"; then
      printf "Linux Educacional $(cat /etc/le_version)\n"
    elif test "${DISTRO}" = "Ubuntu" -a -f "/usr/lib/edu/fbedu/tutorial.htm"; then
      printf "Linux Educacional\n"
    fi
    printf "Kernel\n"
    cut -d " " -f3 /proc/version
    printf "\n\n----------------------------------\n\n"

73 74
    printf "    Commands\n%b" \
           "    ========\n\n"
75 76
    printf "seed_mec user crontab\n"
    crontab -u seed_mec -l
77 78 79 80 81 82 83 84 85 86 87 88 89
    printf "ifconfig\n"
    /sbin/ifconfig
    printf "route\n"
    /sbin/route
    printf "Routes test\n"
    for interface in $(/sbin/route | grep default | awk '{print $NF}'); do
        printf "Tested interface: ${interface}\n"
        # Send ICMP ECHO_REQUEST to root server I or J
        ping -c 1 -I ${interface} 192.36.148.17
        ping -c 1 -I ${interface} 192.58.128.30
    done
    printf "\n\n----------------------------------\n\n"

90 91 92 93 94
    printf "    seed_mec user files\n%b" \
           "    ===================\n\n"
    ls -lR "${SEED_MEC_HOME}"
    printf "\n\n----------------------------------\n\n"

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
    printf "    Inventory XML\n%b" \
           "    =============\n\n"
    cat ${AGENT_DIR}/data/collect-data.xml
    printf "\n\n----------------------------------\n\n"

    printf "    run.sh\n%b" \
           "    ========\n\n"
    cat ${AGENT_DIR}/client/run.sh
    printf "\n\n----------------------------------\n\n"

    printf "    agent.sh\n%b" \
           "    ========\n\n"
    cat ${AGENT_DIR}/agent.sh
    printf "\n\n----------------------------------\n\n"

    printf "    Collect agent log\n%b" \
           "    =================\n\n"
    cat ${AGENT_DIR}/log/collect.log
    printf "\n\n----------------------------------\n\n"

    printf "    Web service client log\n%b" \
           "    ======================\n\n"
    cat ${AGENT_DIR}/client/run.log
    printf "\n\n----------------------------------\n\n"

    printf "    TincUCA client log\n%b" \
           "    ==================\n\n"
    cat ${AGENT_DIR}/log/tincucaclient-install.log
    printf "\n\n----------------------------------\n\n"

    printf "feito!\n\nFavor enviar o arquivo temporário \"${LOGFILE}\"%b%b%b" \
           " para \"proinfodata@c3sl.ufpr.br\".\n\n" \
           "Em caso de dúvidas, acesse o portal do PROINFODATA:\n" \
128
           "seed.c3sl.ufpr.br\n" >&3
129 130
}

131 132 133 134 135
printf "Iniciando instalação do agente PROINFODATA. Tenha certeza de %b%b%b" \
       "estar conectado à Internet e executando este arquivo como super-" \
       "usuário (root). Além disso, para o agente funcionar é necessário " \
       "ter instalado no sistema os pacotes \"cron\" e \"sudo\".\n"

136
# These variables will be replaced when generating the installation package
137 138 139
MD5_CHECK=
BOOTSTRAP_LINES=

140
# Set variables
141
FILE="SEED2.tgz"
142 143 144 145 146 147 148 149
PROJECTOR=$(grep "Projetor ProInfo" /etc/issue 2> /dev/null)
UCA_SERVER=$(grep "Metasys School Server" /etc/issue 2> /dev/null)
UCA_CLASSMATE=$(grep "Metasys ClassmatePC" /etc/issue 2> /dev/null)
if test -f "/usr/sbin/projetor" -o -n "${PROJECTOR}"; then
    SEED_MEC_HOME="/mnt/conf/seed_mec"
else
    SEED_MEC_HOME="/opt/seed_mec"
fi
150
AGENT_DIR="${SEED_MEC_HOME}/SEED2"
151 152 153 154
LOGFILE=/tmp/collected-information.log

# Save stdout to file descriptor 3 and redirect all output to $LOGFILE
exec 3>&1 &> ${LOGFILE}
155

156 157 158 159 160
# Remove any previous installation of the agent
rm -rf "${AGENT_DIR}"
/usr/sbin/userdel -r seed_mec
/usr/sbin/groupdel seed_mec

161 162
# Create seed_mec group and exit on error (except by the error of previous
# existence of seed_mec group)
163
if ! /usr/sbin/groupadd -f seed_mec; then
164 165
    printf "ERRO: Falha ao criar o grupo seed_mec.\n"
    exit 1
166
fi
167

168 169 170 171
# Create the directory path to make sure useradd(1) will be able to create the
# home for seed_mec user
mkdir -p "$(dirname ${SEED_MEC_HOME})"

172
# This is a temporary skel directory to avoid installing in the default skel
173
TMPSKEL=$(mktemp -d)
174 175 176

# Create seed_mec user with seed_mec as group, BASH as default shell,
# $SEED_MEC_HOME as home and $TMPSKEL as temporary skel
177
/usr/sbin/useradd -m -g seed_mec -s /bin/bash -d "${SEED_MEC_HOME}" \
178 179 180 181
        -k "${TMPSKEL}" seed_mec
RETVAL=$?

# If an error occurred (return isn't 0), besides the previous existence of
182
# seed_mec user (return 9, which means the agent will be reinstalled), print an
183
# error message and exit
184
if test ${RETVAL} -ne 0 -a ${RETVAL} -ne 9; then
185 186
    printf "ERRO: Falha ao criar o usuário seed_mec.\n"
    exit 2
187
fi
188 189

# Remove the temporary skel, as it isn't needed anymore
190 191
rmdir "${TMPSKEL}"

192 193 194
# Ensure seed_mec's home exists
mkdir -p "${SEED_MEC_HOME}"

195 196 197 198
# Remove old crontab of seed_mec, in case there's one, avoinding a specific
# error of wrong owner/lack of permissions
crontab -u seed_mec -r

199 200 201 202 203
# Check if this is not an UCA Classmate, UCA Server nor ProInfo Projector, and
# if the line that gives root permission doesn't exist before adding it
if ! test -n "${PROJECTOR}" -o -f "/usr/sbin/projetor" -o \
          -f "/etc/cmpc-release" -o -n "${UCA_SERVER}" -o \
          -n "${UCA_CLASSMATE}"; then
204 205 206 207 208 209
    # Remove any possible line containing a rule for seed_mec user before adding
    # root permissions in the sudoers file
    TMPFILE=$(mktemp)
    sed '/^seed_mec[\t ]*ALL=(ALL/d' /etc/sudoers > "${TMPFILE}"
    mv "${TMPFILE}" /etc/sudoers
    chmod 440 /etc/sudoers
210
    # Add root permissions to seed_mec user, without the need of password
211 212 213 214 215
    if ! $(echo -e "seed_mec\tALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers)
    then
        printf "ERRO: Falha ao adicionar permissões de super-usuário %b" \
           "ao usuário seed_mec.\n"
        exit 3
216 217
    fi
fi
218 219

# Uncompress tar package and check its integrity
220
tail -n +${BOOTSTRAP_LINES} $0 > "${SEED_MEC_HOME}/${FILE}"
221 222 223
cd "${SEED_MEC_HOME}"
verify_md5

224 225
# Remove old agent and extract the new one
rm -rf "${AGENT_DIR}" &> /dev/null
226
tar xzf "${FILE}" &> /dev/null
227
rm -rf "${FILE}" &> /dev/null
228

229
# Change files owner
230 231 232 233 234
if ! chown -R seed_mec:seed_mec "${SEED_MEC_HOME}"; then
    rm -rf "${SEED_MEC_HOME}" &> /dev/null
    printf "ERRO: Falha ao mudar dono dos arquivos.\n"
    exit 5
fi
235 236 237 238

# Export a variable to inform the agent that this is its first execution
export FIRST_EXECUTION=1

239 240 241 242 243 244
# Run the agent and check if its execution was alright, exiting in case of error
if ! su -c "bash ${AGENT_DIR}/agent.sh" seed_mec; then
    printf "ERRO: Falha na execução do novo agente.\n"
    collect_information
    exit 6
fi
245

246 247 248
# Restore stdout and close fd 3
exec >&3 3>&-

249 250 251 252
printf "\nO agente PROINFODATA foi instalado com sucesso. %b%b" \
       "Em caso de dúvidas, acesse o portal do PROINFODATA:\n" \
       "seed.c3sl.ufpr.br\n"

253
exit 0