source: installer/opengnsys_update.sh @ 81291ce

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 81291ce was ebbbfc01, checked in by ramon <ramongomez@…>, 14 years ago

Instalación automática de paquetes en script de actualización (versiones 0.10 y 1.0).

git-svn-id: https://opengnsys.es/svn/branches/version1.0@1629 a21b9725-9963-47de-94b9-378ad31fedc9

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