source: installer/opengnsys_update.sh @ cc2032b

918-git-images-111dconfigfileconfigure-oglivegit-imageslgromero-new-oglivemainmaint-cronmount-efivarfsmultivmmultivm-ogboot-installerogClonningEngineogboot-installer-jenkinsoglive-ipv6test-python-scriptsticket-301ticket-50ticket-50-oldticket-577ticket-585ticket-611ticket-612ticket-693ticket-700ubu24tplunification2use-local-agent-oglivevarios-instalacionwebconsole3
Last change on this file since cc2032b was cc2032b, checked in by ramon <ramongomez@…>, 15 years ago

Script de creación de ficheros .torrent para el Repositorio de imágenes.

git-svn-id: https://opengnsys.es/svn/trunk@1088 a21b9725-9963-47de-94b9-378ad31fedc9

  • Property mode set to 100755
File size: 13.6 KB
Line 
1#!/bin/bash
2#/**
3#@file    opengnsys_update.sh
4#@brief   Script actualización de OpenGnSys
5#@warning No se actualiza BD, ni ficheros de configuración.
6#@version 0.9 - basado en opengnsys_installer.sh
7#@author  Ramón Gómez - ETSII Univ. Sevilla
8#@date    2010/01/27
9#*/
10
11
12# Sólo ejecutable por usuario root
13if [ "$(whoami)" != 'root' ]
14then
15        echo "ERROR: this program must run under root privileges!!"
16        exit 1
17fi
18
19# Comprobar si se ha descargado el paquete comprimido (USESVN=0) o sólo el instalador (USESVN=1).
20PROGRAMDIR=$(readlink -e $(dirname "$0"))
21DEPS="rsync gcc ctorrent"
22if [ -d "$PROGRAMDIR/../installer" ]; then
23    USESVN=0
24else
25    USESVN=1
26    SVN_URL=svn://www.informatica.us.es:3690/opengnsys/trunk
27    DEPS="$DEPS subversion"
28fi
29
30WORKDIR=/tmp/opengnsys_update
31mkdir -p $WORKDIR
32
33INSTALL_TARGET=/opt/opengnsys
34LOG_FILE=/tmp/opengnsys_update.log
35
36
37
38#####################################################################
39####### Algunas funciones útiles de propósito general:
40#####################################################################
41function getDateTime()
42{
43        echo `date +%Y%m%d-%H%M%S`
44}
45
46# Escribe a fichero y muestra por pantalla
47function echoAndLog()
48{
49        echo $1
50        FECHAHORA=`getDateTime`
51        echo "$FECHAHORA;$SSH_CLIENT;$1" >> $LOG_FILE
52}
53
54function errorAndLog()
55{
56        echo "ERROR: $1"
57        FECHAHORA=`getDateTime`
58        echo "$FECHAHORA;$SSH_CLIENT;ERROR: $1" >> $LOG_FILE
59}
60
61
62#####################################################################
63####### Funciones de copia de seguridad y restauración de ficheros
64#####################################################################
65
66# Hace un backup del fichero pasado por parámetro
67# deja un -last y uno para el día
68function backupFile()
69{
70        if [ $# -ne 1 ]; then
71                errorAndLog "${FUNCNAME}(): invalid number of parameters"
72                exit 1
73        fi
74
75        local fichero=$1
76        local fecha=`date +%Y%m%d`
77
78        if [ ! -f $fichero ]; then
79                errorAndLog "${FUNCNAME}(): file $fichero doesn't exists"
80                return 1
81        fi
82
83    echoAndLog "${FUNCNAME}(): Making $fichero back-up"
84
85        # realiza una copia de la última configuración como last
86        cp -p $fichero "${fichero}-LAST"
87
88        # si para el día no hay backup lo hace, sino no
89        if [ ! -f "${fichero}-${fecha}" ]; then
90                cp -p $fichero "${fichero}-${fecha}"
91        fi
92}
93
94# Restaura un fichero desde su copia de seguridad
95function restoreFile()
96{
97        if [ $# -ne 1 ]; then
98                errorAndLog "${FUNCNAME}(): invalid number of parameters"
99                exit 1
100        fi
101
102        local fichero=$1
103
104    echoAndLog "${FUNCNAME}(): restoring $fichero file"
105        if [ -f "${fichero}-LAST" ]; then
106                cp -p "$fichero-LAST" "$fichero"
107        fi
108}
109
110
111#####################################################################
112####### Funciones de instalación de paquetes
113#####################################################################
114
115# Instalar las deependencias necesarias para el actualizador.
116function installDependencies ()
117{
118        if [ $# = 0 ]; then
119                echoAndLog "${FUNCNAME}(): no deps needed."
120    else
121        while [ $# -gt 0 ]; do
122            dpkg -s $1 &>/dev/null | grep Status | grep -qw install
123            if [ $? -ne 0 ]; then
124                INSTALLDEPS="$INSTALLDEPS $1"
125            fi
126            shift
127        done
128        if [ -n "$INSTALLDEPS" ]; then
129            apt-get update && apt-get install $INSTALLDEPS
130                if [ $? -ne 0 ]; then
131                        errorAndLog "${FUNCNAME}(): cannot install some dependencies: $INSTALLDEPS."
132                return 1
133                fi
134        fi
135    fi
136}
137
138
139#####################################################################
140####### Funciones para el manejo de Subversion
141#####################################################################
142
143function svnExportCode()
144{
145        if [ $# -ne 1 ]; then
146                errorAndLog "${FUNCNAME}(): invalid number of parameters"
147                exit 1
148        fi
149
150        local url=$1
151
152        echoAndLog "${FUNCNAME}(): downloading subversion code..."
153
154        svn checkout "${url}" opengnsys
155        if [ $? -ne 0 ]; then
156                errorAndLog "${FUNCNAME}(): error getting code from ${url}, verify your user and password"
157                return 1
158        fi
159        echoAndLog "${FUNCNAME}(): subversion code downloaded"
160        return 0
161}
162
163
164############################################################
165###  Detectar red
166############################################################
167
168function getNetworkSettings()
169{
170        local MAINDEV
171
172        echoAndLog "getNetworkSettings(): Detecting default network parameters."
173        MAINDEV=$(ip -o link show up | awk '!/loopback/ {d=d$2} END {sub(/:.*/,"",d); print d}')
174        if [ -z "$MAINDEV" ]; then
175                errorAndLog "${FUNCNAME}(): Network device not detected."
176                return 1
177        fi
178
179        # Variables de ejecución de Apache
180        # - APACHE_RUN_USER
181        # - APACHE_RUN_GROUP
182        if [ -f /etc/apache2/envvars ]; then
183                source /etc/apache2/envvars
184        fi
185        APACHE_RUN_USER=${APACHE_RUN_USER:-"www-data"}
186        APACHE_RUN_GROUP=${APACHE_RUN_GROUP:-"www-data"}
187}
188
189
190#####################################################################
191####### Funciones específicas de la instalación de Opengnsys
192#####################################################################
193
194# Copiar ficheros de arranque de los servicios del sistema de OpenGnSys
195
196function updateServicesStart(){
197        echoAndLog "${FUNCNAME}(): Updating /etc/init.d/opengnsys ..."
198        cp -p $WORKDIR/opengnsys/admin/Services/opengnsys.init /etc/init.d/opengnsys
199        if [ $? != 0 ]; then
200                errorAndLog "${FUNCNAME}(): Error updating /etc/init.d/opengnsys"
201                exit 1
202        fi
203        echoAndLog "${FUNCNAME}(): /etc/init.d/opengnsys updated successfully."
204}
205
206# Copiar ficheros del OpenGnSys Web Console.
207function updateWebFiles()
208{
209        local ERRCODE
210        echoAndLog "${FUNCNAME}(): Updating web files..."
211        backupFile $INSTALL_TARGET/www/controlacceso.php
212        mv $INSTALL_TARGET/www $INSTALL_TARGET/WebConsole
213        rsync --exclude .svn -irplt $WORKDIR/opengnsys/admin/WebConsole $INSTALL_TARGET
214        ERRCODE=$?
215        mv $INSTALL_TARGET/WebConsole $INSTALL_TARGET/www
216        if [ $ERRCODE != 0 ]; then
217                errorAndLog "${FUNCNAME}(): Error updating web files."
218                exit 1
219        fi
220        restoreFile $INSTALL_TARGET/www/controlacceso.php
221        # Cambiar permisos para ficheros especiales.
222        chown -R $APACHE_RUN_USER:$APACHE_RUN_GROUP \
223                        $INSTALL_TARGET/www/includes \
224                        $INSTALL_TARGET/www/comandos/gestores/filescripts \
225                        $INSTALL_TARGET/www/images/iconos
226        echoAndLog "${FUNCNAME}(): Web files updated successfully."
227}
228
229
230# Crear documentación Doxygen para la consola web.
231function makeDoxygenFiles()
232{
233        echoAndLog "${FUNCNAME}(): Making Doxygen web files..."
234        $WORKDIR/opengnsys/installer/ogGenerateDoc.sh \
235                        $WORKDIR/opengnsys/client/engine $INSTALL_TARGET/www
236        if [ ! -d "$INSTALL_TARGET/www/html" ]; then
237                errorAndLog "${FUNCNAME}(): unable to create Doxygen web files."
238                return 1
239        fi
240        mv "$INSTALL_TARGET/www/html" "$INSTALL_TARGET/www/api"
241        rm -fr $INSTALL_TARGET/www/{man,perlmod,rtf}
242        chown -R $APACHE_RUN_USER:$APACHE_RUN_GROUP $INSTALL_TARGET/www/api
243        echoAndLog "${FUNCNAME}(): Doxygen web files created successfully."
244}
245
246
247# Crea la estructura base de la instalación de opengnsys
248function createDirs()
249{
250        echoAndLog "${FUNCNAME}(): creating directory paths in ${INSTALL_TARGET}"
251
252        mkdir -p ${INSTALL_TARGET}
253        mkdir -p ${INSTALL_TARGET}/admin/{autoexec,comandos,menus,usuarios}
254        mkdir -p ${INSTALL_TARGET}/bin
255        mkdir -p ${INSTALL_TARGET}/client
256        mkdir -p ${INSTALL_TARGET}/doc
257        mkdir -p ${INSTALL_TARGET}/etc
258        mkdir -p ${INSTALL_TARGET}/lib
259        mkdir -p ${INSTALL_TARGET}/log/clients
260        mkdir -p ${INSTALL_TARGET}/sbin
261        mkdir -p ${INSTALL_TARGET}/www
262        mkdir -p ${INSTALL_TARGET}/images
263        ln -fs /var/lib/tftpboot ${INSTALL_TARGET}
264        ln -fs ${INSTALL_TARGET}/log /var/log/opengnsys
265
266        if [ $? -ne 0 ]; then
267                errorAndLog "${FUNCNAME}(): error while creating dirs. Do you have write permissions?"
268                return 1
269        fi
270
271        echoAndLog "${FUNCNAME}(): directory paths created"
272        return 0
273}
274
275# Copia ficheros de configuración y ejecutables genéricos del servidor.
276function updateServerFiles () {
277
278        local SOURCES=( client/boot/initrd-generator \
279                        client/boot/upgrade-clients-udeb.sh \
280                        client/boot/udeblist.conf  \
281                        client/boot/udeblist-jaunty.conf  \
282                        client/boot/udeblist-karmic.conf \
283                        client/boot/udeblist-lucid.conf \
284                        repoman/bin \
285                        doc )
286        local TARGETS=( bin/initrd-generator \
287                        bin/upgrade-clients-udeb.sh \
288                        etc/udeblist.conf \
289                        etc/udeblist-jaunty.conf  \
290                        etc/udeblist-karmic.conf \
291                        etc/udeblist-lucid.conf \
292                        bin \
293                        doc )
294
295        if [ ${#SOURCES[@]} != ${#TARGETS[@]} ]; then
296                errorAndLog "${FUNCNAME}(): inconsistent number of array items"
297                exit 1
298        fi
299
300        echoAndLog "${FUNCNAME}(): updating files in server directories"
301        pushd $WORKDIR/opengnsys >/dev/null
302        local i
303        for (( i = 0; i < ${#SOURCES[@]}; i++ )); do
304                rsync --exclude .svn -irplt "${SOURCES[$i]}" "${INSTALL_TARGET}/${TARGETS[$i]}"
305        done
306        popd >/dev/null
307        echoAndLog "${FUNCNAME}(): server files updated successfully."
308}
309
310####################################################################
311### Funciones de compilación de código fuente de servicios
312####################################################################
313
314# Recompilar y actualiza el binario del clinete
315function recompileClient ()
316{
317        # Compilar OpenGnSys Client
318        echoAndLog "${FUNCNAME}(): recompiling OpenGnSys Client"
319        pushd $WORKDIR/opengnsys/admin/Services/ogAdmClient
320        make && mv ogAdmClient ../../../client/nfsexport/bin
321        if [ $? -ne 0 ]; then
322                echoAndLog "${FUNCNAME}(): error while compiling OpenGnSys Client"
323                hayErrores=1
324        fi
325        popd
326
327        return $hayErrores
328}
329
330
331####################################################################
332### Funciones instalacion cliente opengnsys
333####################################################################
334
335function updateClient()
336{
337        local OSDISTRIB OSCODENAME
338
339        local hayErrores=0
340
341        echoAndLog "${FUNCNAME}(): Copying OpenGnSys Client files."
342        rsync --exclude .svn -irplt $WORKDIR/opengnsys/client/nfsexport/* $INSTALL_TARGET/client
343        echoAndLog "${FUNCNAME}(): Copying OpenGnSys Cloning Engine files."
344        mkdir -p $INSTALL_TARGET/client/lib/engine/bin
345        rsync -iplt $WORKDIR/opengnsys/client/engine/*.lib $INSTALL_TARGET/client/lib/engine/bin
346        if [ $? -ne 0 ]; then
347                errorAndLog "${FUNCNAME}(): error while copying engine files"
348                hayErrores=1
349        fi
350
351        # Cargar Kernel, Initrd y paquetes udeb para la distribución del servidor (o por defecto).
352        OSDISTRIB=$(lsb_release -i | awk -F: '{sub(/\t/,""); print $2}') 2>/dev/null
353        OSCODENAME=$(lsb_release -c | awk -F: '{sub(/\t/,""); print $2}') 2>/dev/null
354        if [ "$OSDISTRIB" = "Ubuntu" -a -n "$OSCODENAME" ]; then
355                echoAndLog "${FUNCNAME}(): Loading Kernel and Initrd files for $OSDISTRIB $OSCODENAME."
356                $INSTALL_TARGET/bin/initrd-generator -t $INSTALL_TARGET/tftpboot -v $OSCODENAME 2>&1 | tee -a $LOG_FILE
357                if [ $? -ne 0 ]; then
358                        errorAndLog "${FUNCNAME}(): error while generating initrd OpenGnSys Admin Client"
359                        hayErrores=1
360                fi
361                echoAndLog "${FUNCNAME}(): Loading udeb files for $OSDISTRIB $OSCODENAME."
362                $INSTALL_TARGET/bin/upgrade-clients-udeb.sh $OSCODENAME 2>&1 | tee -a $LOG_FILE
363                if [ $? -ne 0 ]; then
364                        errorAndLog "${FUNCNAME}(): error while upgrading udeb files OpenGnSys Admin Client"
365                        hayErrores=1
366                fi
367        else
368                echoAndLog "${FUNCNAME}(): Loading default Kernel and Initrd files."
369                $INSTALL_TARGET/bin/initrd-generator -t $INSTALL_TARGET/tftpboot 2>&1 | tee -a $LOG_FILE
370                if [ $? -ne 0 ]; then
371                        errorAndLog "${FUNCNAME}(): error while generating initrd OpenGnSys Admin Client"
372                        hayErrores=1
373                fi
374                echoAndLog "${FUNCNAME}(): Loading default udeb files."
375                $INSTALL_TARGET/bin/upgrade-clients-udeb.sh 2>&1 | tee -a $LOG_FILE
376                if [ $? -ne 0 ]; then
377                        errorAndLog "${FUNCNAME}(): error while upgrading udeb files OpenGnSys Admin Client"
378                        hayErrores=1
379                fi
380        fi
381
382        if [ $hayErrores -eq 0 ]; then
383                echoAndLog "${FUNCNAME}(): Client generation success."
384        else
385                errorAndLog "${FUNCNAME}(): Client generation with errors"
386        fi
387
388        return $hayErrores
389}
390
391
392
393#####################################################################
394####### Proceso de actualización de OpenGnSys
395#####################################################################
396
397
398echoAndLog "OpenGnSys update begins at $(date)"
399
400# Instalar dependencia.
401installDependencies $DEPS
402if [ $? -ne 0 ]; then
403        errorAndLog "Error: you may install all needed dependencies."
404        exit 1
405fi
406
407pushd $WORKDIR
408
409# Detectar parámetros de red por defecto
410getNetworkSettings
411if [ $? -ne 0 ]; then
412        errorAndLog "Error reading default network settings."
413        exit 1
414fi
415
416# Arbol de directorios de OpenGnSys.
417createDirs ${INSTALL_TARGET}
418if [ $? -ne 0 ]; then
419        errorAndLog "Error while creating directory paths!"
420        exit 1
421fi
422
423# Si es necesario, descarga el repositorio de código en directorio temporal
424if [ $USESVN -eq 1 ]; then
425        svnExportCode $SVN_URL
426        if [ $? -ne 0 ]; then
427                errorAndLog "Error while getting code from svn"
428                exit 1
429        fi
430else
431        ln -fs "$(dirname $PROGRAMDIR)" opengnsys
432fi
433
434# Copiando ficheros complementarios del servidor
435updateServerFiles
436if [ $? -ne 0 ]; then
437        errorAndLog "Error updating OpenGnSys Server files"
438        exit 1
439fi
440
441# Copiando paqinas web
442updateWebFiles
443if [ $? -ne 0 ]; then
444        errorAndLog "Error updating OpenGnSys Web Admin files"
445        exit 1
446fi
447# Generar páginas Doxygen para instalar en el web
448makeDoxygenFiles
449
450# Creando la estructura del cliente
451recompileClient
452updateClient
453if [ $? -ne 0 ]; then
454        errorAndLog "Error updating clients"
455        exit 1
456fi
457
458# Actualizamos el fichero que arranca los servicios de OpenGnSys
459updateServicesStart
460
461# Eliminamos el fichero de estado del tracker porque es incompatible entre los distintos paquetes
462if [ -r /tmp/dstate ]
463then
464    rm /tmp/dstate
465fi
466
467#rm -rf $WORKDIR
468echoAndLog "OpenGnSys update finished at $(date)"
469
470popd
471
Note: See TracBrowser for help on using the repository browser.