bootstrap-install 10.6 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
# ------------------------------------------------------------------------------
# This function checks the integrity of the tar package
26
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 45
# 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 " \
46
           "a equipe ficará ciente do problema para resolvê-lo.\n\n" >&3
47 48

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

    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"

61 62 63 64 65 66 67 68
    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"
69 70 71 72 73 74
    elif test "${DISTRO}" = "Mandriva"; then
      head -n 1 /etc/issue
    elif test "${DISTRO}" = "Welcome"; then
      cut -sd "\\" -f1 /etc/issue | cut -d ' ' -f3-
    else
      cut -sd "\\" -f1 /etc/issue
75 76 77 78 79
    fi
    printf "Kernel\n"
    cut -d " " -f3 /proc/version
    printf "\n\n----------------------------------\n\n"

80 81
    printf "    Commands\n%b" \
           "    ========\n\n"
82 83
    printf "seed_mec user crontab\n"
    crontab -u seed_mec -l
84 85 86 87 88 89 90 91 92 93 94 95 96
    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"

97 98 99 100 101
    printf "    seed_mec user files\n%b" \
           "    ===================\n\n"
    ls -lR "${SEED_MEC_HOME}"
    printf "\n\n----------------------------------\n\n"

102 103 104 105 106 107 108 109 110 111
    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"

112
    printf "    update.sh\n%b" \
113
           "    ========\n\n"
114 115 116 117 118 119 120
    cat ${AGENT_DIR}/update.sh
    
     printf "    inventory.sh\n%b" \
           "    ========\n\n"
    printf "\n\n----------------------------------\n\n"
    cat  ${AGENT_DIR}/inventory.sh
    
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    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" \
141
           "proinfodata.c3sl.ufpr.br\n" >&3
142 143
}

144
# ------------------------------------------------------------------------------
145 146 147 148 149
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"

150
# These variables will be replaced when generating the installation package
151 152 153
MD5_CHECK=
BOOTSTRAP_LINES=

154
# Set variables
155
FILE="SEED2.tgz"
156 157 158 159 160 161 162 163
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
164
AGENT_DIR="${SEED_MEC_HOME}/SEED2"
165 166
LASTEXEC="${AGENT_DIR}/tmp/last-exec.tmp"
NETLASTEXEC="${AGENT_DIR}/tmp/net-last-exec.tmp"
167 168 169
LOGFILE=/tmp/collected-information.log
# Save stdout to file descriptor 3 and redirect all output to $LOGFILE
exec 3>&1 &> ${LOGFILE}
170

171
# Check if last-exec.tmp exist then save its content to prevent that
172 173 174 175
# agent send a inventory more than once per day
if test -f ${LASTEXEC}; then 
	LASTEXECBACKUP=$(cat ${LASTEXEC})
fi
176 177 178 179 180

if test -f ${NETLASTEXEC}; then 
	NETLASTEXECBACKUP=$(cat ${NETLASTEXEC})
fi

181 182 183 184 185
# Remove any previous installation of the agent
rm -rf "${AGENT_DIR}"
/usr/sbin/userdel -r seed_mec
/usr/sbin/groupdel seed_mec

186 187
# Create seed_mec group and exit on error (except by the error of previous
# existence of seed_mec group)
188
if ! /usr/sbin/groupadd -f seed_mec; then
189
    printf "ERRO: Falha ao criar o grupo seed_mec.\n" >&3
190
    exit 1
191
fi
192

193 194 195 196
# 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})"

197
# This is a temporary skel directory to avoid installing in the default skel
198
TMPSKEL=$(mktemp -d)
199 200 201

# Create seed_mec user with seed_mec as group, BASH as default shell,
# $SEED_MEC_HOME as home and $TMPSKEL as temporary skel
202
/usr/sbin/useradd -r -m -g seed_mec -s /bin/bash -d "${SEED_MEC_HOME}" \
203 204 205 206
        -k "${TMPSKEL}" seed_mec
RETVAL=$?

# If an error occurred (return isn't 0), besides the previous existence of
207
# seed_mec user (return 9, which means the agent will be reinstalled), print an
208
# error message and exit
209
if test ${RETVAL} -ne 0 -a ${RETVAL} -ne 9; then
210
    printf "ERRO: Falha ao criar o usuário seed_mec.\n" >&3
211
    exit 2
212
fi
213 214

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

217
# ------------------------------------------------------------------------------
218 219 220
# Ensure seed_mec's home exists
mkdir -p "${SEED_MEC_HOME}"

221
# Remove old crontab of seed_mec, in case there's one, avoinding a specific
222
# error of wrong owner (lack of permissions)
223 224
crontab -u seed_mec -r

225
# Check if this is not an UCA Classmate, UCA Server nor PROINFO Projector, and
226 227 228 229
# 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
230 231 232
    # Remove any possible line containing a rule for seed_mec user before adding
    # root permissions in the sudoers file
    TMPFILE=$(mktemp)
233 234 235 236 237 238 239 240 241 242 243 244 245 246
    sed '/^\(Defaults:\)\?seed_mec/d' /etc/sudoers > "${TMPFILE}"

    # Add root permissions to datasid
    echo -e "seed_mec\tALL=(ALL) NOPASSWD: ALL" >> "${TMPFILE}"

    # Remove on Fedora the requirement of tty for sudo
    if grep -q "Fedora" /etc/issue; then
       echo -e 'Defaults:seed_mec\t!requiretty' >> "${TMPFILE}"
    fi

    # Check if modified sudoers file is valid and apply it
    if ! visudo -cf "${TMPFILE}" || ! mv "${TMPFILE}" /etc/sudoers \
       || ! chmod 440 /etc/sudoers; then
        printf "FALHA!\n" >&3
247
        printf "ERRO: Falha ao adicionar permissões de super-usuário %b" \
248 249
           "ao usuário seed_mec.\n"
        rm -f "${TMPFILE}"
250
        exit 3
251
    fi
252
    rm -f "${TMPFILE}"
253
fi
254

255
# ------------------------------------------------------------------------------
256
# Uncompress tar package and check its integrity
257
tail -n +${BOOTSTRAP_LINES} $0 > "${SEED_MEC_HOME}/${FILE}"
258 259 260
cd "${SEED_MEC_HOME}"
verify_md5

261 262
# Remove old agent and extract the new one
rm -rf "${AGENT_DIR}" &> /dev/null
263
tar xzf "${FILE}" &> /dev/null
264
rm -rf "${FILE}" &> /dev/null
265

266
# Change files' owner
267 268
if ! chown -R seed_mec:seed_mec "${SEED_MEC_HOME}"; then
    rm -rf "${SEED_MEC_HOME}" &> /dev/null
269
    printf "ERRO: Falha ao mudar dono dos arquivos.\n" >&3
270 271
    exit 5
fi
272

273
# ------------------------------------------------------------------------------
274 275 276
# Export a variable to inform the agent that this is its first execution
export FIRST_EXECUTION=1

277
# Restore last-exec.tmp
278 279 280
if ! test -z ${LASTEXECBACKUP};then  
	echo ${LASTEXECBACKUP} > ${LASTEXEC}
fi
281 282 283 284
# Restore net-last-exec.tmp
if ! test -z ${NETLASTEXECBACKUP};then  
	echo ${NETLASTEXECBACKUP} > ${NETLASTEXEC}
fi
285

286 287 288 289 290
# Run the agent and check if its execution went alright, exiting in case of
# error
if ! su -c "bash ${AGENT_DIR}/update.sh" seed_mec ||
    ! su -c "bash ${AGENT_DIR}/inventory.sh" seed_mec; then
    printf "ERRO: Falha na execução do novo agente.\n" >&3
291 292 293
    collect_information
    exit 6
fi
294

295 296 297
# Restore stdout and close fd 3
exec >&3 3>&-

298 299
printf "\nO agente PROINFODATA foi instalado com sucesso. %b%b" \
       "Em caso de dúvidas, acesse o portal do PROINFODATA:\n" \
300
       "proinfodata.c3sl.ufpr.br\n"
301

302
exit 0