source: installer/opengnsys_update.sh @ f6c1d2b

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

Versión 1.0: Modificado script de interfaz web para crear imagen desde cliente en modo no administrador.
Modificado #291.

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

  • Property mode set to 100755
File size: 16.3 KB
RevLine 
[a0867b37]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
[c1e00e4]9#@version 1.0 - adaptación a OpenGnSys 1.0
10#@author  Ramón Gómez - ETSII Univ. Sevilla
11#@date    2011/03/02
[a0867b37]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"))
[c1e00e4]24DEPS="build-essential g++-multilib rsync ctorrent samba unzip netpipes debootstrap schroot squashfs-tools"
[a0867b37]25if [ -d "$PROGRAMDIR/../installer" ]; then
26    USESVN=0
27else
28    USESVN=1
[c1e00e4]29    SVN_URL=http://www.opengnsys.es/svn/branches/version1.0
[a0867b37]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 $fichero file"
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."
[c1e00e4]123        else
124                while [ $# -gt 0 ]; do
125                        dpkg -s $1 &>/dev/null | grep Status | grep -qw install
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 install $INSTALLDEPS
133                        if [ $? -ne 0 ]; then
134                                errorAndLog "${FUNCNAME}(): cannot install some dependencies: $INSTALLDEPS."
135                                return 1
136                        fi
137                fi
138        fi
[a0867b37]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
[7ad4465]157        svn checkout "${url}" opengnsys
[a0867b37]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{
[5d6bf97]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
[a0867b37]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
[cf2142ea]197# Copiar ficheros de arranque de los servicios del sistema de OpenGnSys
198
199function updateServicesStart(){
200        echoAndLog "${FUNCNAME}(): Updating /etc/init.d/opengnsys ..."
[6b65dfd]201        cp -p $WORKDIR/opengnsys/admin/Sources/Services/opengnsys.init /etc/init.d/opengnsys
[cf2142ea]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
[45f1fe8]209# Actualizar cliente OpenGnSys
210function openGnsysCopyClientFiles()
211{
212        local hayErrores=0
213
214        echoAndLog "${FUNCNAME}(): Updating OpenGnSys Client files."
215        #cp -ar $WORKDIR/opengnsys/client/shared/* $INSTALL_TARGET/client
216        rsync --exclude .svn -irplt $WORKDIR/opengnsys/client/shared/* $INSTALL_TARGET/client
217        if [ $? -ne 0 ]; then
218                errorAndLog "${FUNCNAME}(): error while updating client structure"
219                hayErrores=1
220        fi
221        find $INSTALL_TARGET/client -name .svn -type d -exec rm -fr {} \; 2>/dev/null
222       
223        echoAndLog "${FUNCNAME}(): Updating OpenGnSys Cloning Engine files."
224        #mkdir -p $INSTALL_TARGET/client/lib/engine/bin
225        #cp -ar $WORKDIR/opengnsys/client/engine/*.lib $INSTALL_TARGET/client/lib/engine/bin
226        rsync --exclude .svn -irplt $WORKDIR/opengnsys/client/engine/*.lib $INSTALL_TARGET/client/lib/engine/bin
227        if [ $? -ne 0 ]; then
228                errorAndLog "${FUNCNAME}(): error while updating engine files"
229                hayErrores=1
230        fi
231       
232        if [ $hayErrores -eq 0 ]; then
233                echoAndLog "${FUNCNAME}(): client  files update success."
234        else
235                errorAndLog "${FUNCNAME}(): client files update with errors"
236        fi
237
238        return $hayErrores
239}
[a0867b37]240# Copiar ficheros del OpenGnSys Web Console.
241function updateWebFiles()
242{
[d655cc4]243        local ERRCODE
[a0867b37]244        echoAndLog "${FUNCNAME}(): Updating web files..."
[d655cc4]245        backupFile $INSTALL_TARGET/www/controlacceso.php
246        mv $INSTALL_TARGET/www $INSTALL_TARGET/WebConsole
[1db2ed8]247        rsync --exclude .svn -irplt $WORKDIR/opengnsys/admin/WebConsole $INSTALL_TARGET
[d655cc4]248        ERRCODE=$?
249        mv $INSTALL_TARGET/WebConsole $INSTALL_TARGET/www
[c1e00e4]250        unzip -o $WORKDIR/opengnsys/admin/xajax_0.5_standard.zip -d $INSTALL_TARGET/www/xajax
[d655cc4]251        if [ $ERRCODE != 0 ]; then
[a0867b37]252                errorAndLog "${FUNCNAME}(): Error updating web files."
253                exit 1
254        fi
[d655cc4]255        restoreFile $INSTALL_TARGET/www/controlacceso.php
[a0867b37]256        # Cambiar permisos para ficheros especiales.
[e473667]257    chown -R $APACHE_RUN_USER:$APACHE_RUN_GROUP $INSTALL_TARGET/www/includes $INSTALL_TARGET/www/images/iconos
[a0867b37]258        echoAndLog "${FUNCNAME}(): Web files updated successfully."
[e473667]259       
[a0867b37]260}
261
[72134d5]262# Copiar carpeta de Interface
263function updateInterfaceAdm ()
264{
265        local hayErrores=0
266         
267        # Crear carpeta y copiar Interface
268        echoAndLog "${FUNCNAME}(): Copying Administration Interface Folder"
269        mv $INSTALL_TARGET/client/interfaceAdm $INSTALL_TARGET/client/Interface
270        rsync --exclude .svn -irplt $WORKDIR/opengnsys/admin/Interface $INSTALL_TARGET/client
271        ERRCODE=$?
272        mv $INSTALL_TARGET/client/Interface $INSTALL_TARGET/client/interfaceAdm
273        if [ $? -ne 0 ]; then
274                echoAndLog "${FUNCNAME}(): error while updating admin interface"
275                exit 1
276        fi
277        chmod -R +x $INSTALL_TARGET/client/interfaceAdm
[f6c1d2b]278        chown root:root $INSTALL_TARGET/client/interfaceAdm/CambiarAcceso
279        chmod 700 $INSTALL_TARGET/client/interfaceAdm/CambiarAcceso
[72134d5]280        echoAndLog "${FUNCNAME}(): Admin interface updated successfully."
281}
[a0867b37]282
[5d6bf97]283# Crear documentación Doxygen para la consola web.
284function makeDoxygenFiles()
285{
286        echoAndLog "${FUNCNAME}(): Making Doxygen web files..."
287        $WORKDIR/opengnsys/installer/ogGenerateDoc.sh \
288                        $WORKDIR/opengnsys/client/engine $INSTALL_TARGET/www
289        if [ ! -d "$INSTALL_TARGET/www/html" ]; then
290                errorAndLog "${FUNCNAME}(): unable to create Doxygen web files."
291                return 1
292        fi
[45f1fe8]293        rm -fr "$INSTALL_TARGET/www/api"
[5d6bf97]294        mv "$INSTALL_TARGET/www/html" "$INSTALL_TARGET/www/api"
[45f1fe8]295    rm -fr $INSTALL_TARGET/www/{man,perlmod,rtf}
[5d6bf97]296        chown -R $APACHE_RUN_USER:$APACHE_RUN_GROUP $INSTALL_TARGET/www/api
297        echoAndLog "${FUNCNAME}(): Doxygen web files created successfully."
298}
299
300
[a0867b37]301# Crea la estructura base de la instalación de opengnsys
302function createDirs()
303{
304        echoAndLog "${FUNCNAME}(): creating directory paths in ${INSTALL_TARGET}"
305
306        mkdir -p ${INSTALL_TARGET}
307        mkdir -p ${INSTALL_TARGET}/bin
308        mkdir -p ${INSTALL_TARGET}/client
309        mkdir -p ${INSTALL_TARGET}/doc
310        mkdir -p ${INSTALL_TARGET}/etc
311        mkdir -p ${INSTALL_TARGET}/lib
312        mkdir -p ${INSTALL_TARGET}/log/clients
313        mkdir -p ${INSTALL_TARGET}/sbin
314        mkdir -p ${INSTALL_TARGET}/www
315        mkdir -p ${INSTALL_TARGET}/images
316        ln -fs /var/lib/tftpboot ${INSTALL_TARGET}
317        ln -fs ${INSTALL_TARGET}/log /var/log/opengnsys
318
319        if [ $? -ne 0 ]; then
320                errorAndLog "${FUNCNAME}(): error while creating dirs. Do you have write permissions?"
321                return 1
322        fi
323
324        echoAndLog "${FUNCNAME}(): directory paths created"
325        return 0
326}
327
328# Copia ficheros de configuración y ejecutables genéricos del servidor.
329function updateServerFiles () {
330
[c1e00e4]331        # No copiar ficheros del antiguo cliente Initrd
332        local SOURCES=( repoman/bin \
333                        server/bin \
[a0867b37]334                        doc )
[c1e00e4]335        local TARGETS=( bin \
[85b029f]336                        bin \
[a0867b37]337                        doc )
338
339        if [ ${#SOURCES[@]} != ${#TARGETS[@]} ]; then
340                errorAndLog "${FUNCNAME}(): inconsistent number of array items"
341                exit 1
342        fi
343
344        echoAndLog "${FUNCNAME}(): updating files in server directories"
345        pushd $WORKDIR/opengnsys >/dev/null
346        local i
347        for (( i = 0; i < ${#SOURCES[@]}; i++ )); do
348                rsync --exclude .svn -irplt "${SOURCES[$i]}" "${INSTALL_TARGET}/${TARGETS[$i]}"
349        done
350        popd >/dev/null
[9ee62ad]351        echoAndLog "${FUNCNAME}(): updating cron files"
352        echo "* * * * *   root   [ -x $INSTALL_TARGET/bin/torrent-creator ] && $INSTALL_TARGET/bin/torrent-creator" > /etc/cron.d/torrentcreator
[da8db11]353        echoAndLog "${FUNCNAME}(): server files updated successfully."
[a0867b37]354}
355
356####################################################################
357### Funciones de compilación de código fuente de servicios
358####################################################################
359
360# Recompilar y actualiza el binario del clinete
361function recompileClient ()
362{
363        # Compilar OpenGnSys Client
364        echoAndLog "${FUNCNAME}(): recompiling OpenGnSys Client"
[72134d5]365        pushd $WORKDIR/opengnsys/admin/Sources/Clients/ogAdmClient
[e473667]366        make && mv ogAdmClient $INSTALL_TARGET/client/bin
[a0867b37]367        if [ $? -ne 0 ]; then
368                echoAndLog "${FUNCNAME}(): error while compiling OpenGnSys Client"
369                hayErrores=1
370        fi
371        popd
372
373        return $hayErrores
374}
375
376
377####################################################################
378### Funciones instalacion cliente opengnsys
379####################################################################
380
[c1e00e4]381# Actualizar antiguo cliente Initrd.
382function updateOldClient()
[a0867b37]383{
384        local OSDISTRIB OSCODENAME
385
386        local hayErrores=0
387
388        echoAndLog "${FUNCNAME}(): Copying OpenGnSys Client files."
389        rsync --exclude .svn -irplt $WORKDIR/opengnsys/client/nfsexport/* $INSTALL_TARGET/client
390        echoAndLog "${FUNCNAME}(): Copying OpenGnSys Cloning Engine files."
391        mkdir -p $INSTALL_TARGET/client/lib/engine/bin
392        rsync -iplt $WORKDIR/opengnsys/client/engine/*.lib $INSTALL_TARGET/client/lib/engine/bin
393        if [ $? -ne 0 ]; then
394                errorAndLog "${FUNCNAME}(): error while copying engine files"
395                hayErrores=1
396        fi
397
398        # Cargar Kernel, Initrd y paquetes udeb para la distribución del servidor (o por defecto).
[72134d5]399        OSDISTRIB=$(lsb_release -is) 2>/dev/null
400        OSCODENAME=$(lsb_release -cs) 2>/dev/null
[a0867b37]401        if [ "$OSDISTRIB" = "Ubuntu" -a -n "$OSCODENAME" ]; then
402                echoAndLog "${FUNCNAME}(): Loading Kernel and Initrd files for $OSDISTRIB $OSCODENAME."
[da8db11]403                $INSTALL_TARGET/bin/initrd-generator -t $INSTALL_TARGET/tftpboot -v $OSCODENAME 2>&1 | tee -a $LOG_FILE
[a0867b37]404                if [ $? -ne 0 ]; then
405                        errorAndLog "${FUNCNAME}(): error while generating initrd OpenGnSys Admin Client"
406                        hayErrores=1
407                fi
408                echoAndLog "${FUNCNAME}(): Loading udeb files for $OSDISTRIB $OSCODENAME."
[da8db11]409                $INSTALL_TARGET/bin/upgrade-clients-udeb.sh $OSCODENAME 2>&1 | tee -a $LOG_FILE
[a0867b37]410                if [ $? -ne 0 ]; then
411                        errorAndLog "${FUNCNAME}(): error while upgrading udeb files OpenGnSys Admin Client"
412                        hayErrores=1
413                fi
414        else
415                echoAndLog "${FUNCNAME}(): Loading default Kernel and Initrd files."
[da8db11]416                $INSTALL_TARGET/bin/initrd-generator -t $INSTALL_TARGET/tftpboot 2>&1 | tee -a $LOG_FILE
[a0867b37]417                if [ $? -ne 0 ]; then
418                        errorAndLog "${FUNCNAME}(): error while generating initrd OpenGnSys Admin Client"
419                        hayErrores=1
420                fi
421                echoAndLog "${FUNCNAME}(): Loading default udeb files."
[da8db11]422                $INSTALL_TARGET/bin/upgrade-clients-udeb.sh 2>&1 | tee -a $LOG_FILE
[a0867b37]423                if [ $? -ne 0 ]; then
424                        errorAndLog "${FUNCNAME}(): error while upgrading udeb files OpenGnSys Admin Client"
425                        hayErrores=1
426                fi
427        fi
428
429        if [ $hayErrores -eq 0 ]; then
430                echoAndLog "${FUNCNAME}(): Client generation success."
431        else
432                errorAndLog "${FUNCNAME}(): Client generation with errors"
433        fi
434
435        return $hayErrores
436}
437
[c1e00e4]438# Actualizar nuevo cliente para OpenGnSys 1.0
439function updateClient()
440{
441        local DOWNLOADURL=http://www.opengnsys.es/downloads
[6ef9f23]442        local FILENAME=ogclient-1.0-lucid-32bit.tar.gz
443        local TMPFILE=/tmp/$FILENAME
[c1e00e4]444
445        echoAndLog "${FUNCNAME}(): Loading Client"
446        # Descargar y descomprimir cliente ogclient
[03882ae]447        wget $DOWNLOADURL/$FILENAME -O $TMPFILE
[6ef9f23]448        if [ ! -s $TMPFILE ]; then
449                errorAndLog "${FUNCNAME}(): Error loading OpenGnSys Client"
[c1e00e4]450                return 1
451        fi
[03882ae]452        echoAndLog "${FUNCNAME}(): Extracting Client files"
[6ef9f23]453        tar xzvf $TMPFILE -C $INSTALL_TARGET/tftpboot
454        rm -f $TMPFILE
[c1e00e4]455        # Usar la versión más reciente del Kernel y del Initrd para el cliente.
456        ln $(ls $INSTALL_TARGET/tftpboot/ogclient/vmlinuz-*|tail -1) $INSTALL_TARGET/tftpboot/ogclient/ogvmlinuz
457        ln $(ls $INSTALL_TARGET/tftpboot/ogclient/initrd.img-*|tail -1) $INSTALL_TARGET/tftpboot/ogclient/oginitrd.img
458        # Establecer los permisos.
459        chmod -R 755 $INSTALL_TARGET/tftpboot/ogclient
460        chown -R :$OPENGNSYS_CLIENT_USER $INSTALL_TARGET/tftpboot/ogclient
461        echoAndLog "${FUNCNAME}(): Client update successfully"
462}
[a0867b37]463
464
465#####################################################################
[3320409]466####### Proceso de actualización de OpenGnSys
[a0867b37]467#####################################################################
468
469
470echoAndLog "OpenGnSys update begins at $(date)"
471
472# Instalar dependencia.
473installDependencies $DEPS
474if [ $? -ne 0 ]; then
475        errorAndLog "Error: you may install all needed dependencies."
476        exit 1
477fi
478
479pushd $WORKDIR
480
481# Detectar parámetros de red por defecto
482getNetworkSettings
483if [ $? -ne 0 ]; then
484        errorAndLog "Error reading default network settings."
485        exit 1
486fi
487
488# Arbol de directorios de OpenGnSys.
489createDirs ${INSTALL_TARGET}
490if [ $? -ne 0 ]; then
491        errorAndLog "Error while creating directory paths!"
492        exit 1
493fi
494
495# Si es necesario, descarga el repositorio de código en directorio temporal
496if [ $USESVN -eq 1 ]; then
497        svnExportCode $SVN_URL
498        if [ $? -ne 0 ]; then
499                errorAndLog "Error while getting code from svn"
500                exit 1
501        fi
502else
503        ln -fs "$(dirname $PROGRAMDIR)" opengnsys
504fi
505
506# Copiando ficheros complementarios del servidor
507updateServerFiles
508if [ $? -ne 0 ]; then
[da8db11]509        errorAndLog "Error updating OpenGnSys Server files"
[a0867b37]510        exit 1
511fi
512
[45f1fe8]513# Actualizando cliente
514openGnsysCopyClientFiles
515
[a0867b37]516# Copiando paqinas web
517updateWebFiles
518if [ $? -ne 0 ]; then
[da8db11]519        errorAndLog "Error updating OpenGnSys Web Admin files"
[a0867b37]520        exit 1
521fi
[5d6bf97]522# Generar páginas Doxygen para instalar en el web
523makeDoxygenFiles
[a0867b37]524
525# Creando la estructura del cliente
526recompileClient
[c1e00e4]527# NO se actualiza el antiguo cliente Initrd
528#updateOldClient
[a0867b37]529updateClient
530if [ $? -ne 0 ]; then
531        errorAndLog "Error updating clients"
532        exit 1
533fi
[72134d5]534updateInterfaceAdm
[a0867b37]535
[95f1f68]536# Actualizamos el fichero que arranca los servicios de OpenGnSys
537updateServicesStart
538
[3320409]539# Eliminamos el fichero de estado del tracker porque es incompatible entre los distintos paquetes
540if [ -r /tmp/dstate ]
541then
542    rm /tmp/dstate
543fi
544
[a0867b37]545#rm -rf $WORKDIR
546echoAndLog "OpenGnSys update finished at $(date)"
547
548popd
549
Note: See TracBrowser for help on using the repository browser.