source: installer/opengnsys_update.sh @ 062b55a

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

Corrección erratas en instalador y desinstalador.
Adaptación de actualizador a los últimos cambios.

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

  • Property mode set to 100755
File size: 14.4 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=http://www.opengnsys.es/svn/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# Copiar carpeta de Interface
230function updateInterfaceAdm ()
231{
232        local hayErrores=0
233         
234        # Crear carpeta y copiar Interface
235        echoAndLog "${FUNCNAME}(): Copying Administration Interface Folder"
236        mv $INSTALL_TARGET/client/interfaceAdm $INSTALL_TARGET/client/Interface
237        rsync --exclude .svn -irplt $WORKDIR/opengnsys/admin/Interface $INSTALL_TARGET/client
238        ERRCODE=$?
239        mv $INSTALL_TARGET/client/Interface $INSTALL_TARGET/client/interfaceAdm
240        if [ $? -ne 0 ]; then
241                echoAndLog "${FUNCNAME}(): error while updating admin interface"
242                exit 1
243        fi
244        chmod -R +x $INSTALL_TARGET/client/interfaceAdm
245        echoAndLog "${FUNCNAME}(): Admin interface updated successfully."
246}
247
248# Crear documentación Doxygen para la consola web.
249function makeDoxygenFiles()
250{
251        echoAndLog "${FUNCNAME}(): Making Doxygen web files..."
252        $WORKDIR/opengnsys/installer/ogGenerateDoc.sh \
253                        $WORKDIR/opengnsys/client/engine $INSTALL_TARGET/www
254        if [ ! -d "$INSTALL_TARGET/www/html" ]; then
255                errorAndLog "${FUNCNAME}(): unable to create Doxygen web files."
256                return 1
257        fi
258        mv "$INSTALL_TARGET/www/html" "$INSTALL_TARGET/www/api"
259        rm -fr $INSTALL_TARGET/www/{man,perlmod,rtf}
260        chown -R $APACHE_RUN_USER:$APACHE_RUN_GROUP $INSTALL_TARGET/www/api
261        echoAndLog "${FUNCNAME}(): Doxygen web files created successfully."
262}
263
264
265# Crea la estructura base de la instalación de opengnsys
266function createDirs()
267{
268        echoAndLog "${FUNCNAME}(): creating directory paths in ${INSTALL_TARGET}"
269
270        mkdir -p ${INSTALL_TARGET}
271        mkdir -p ${INSTALL_TARGET}/bin
272        mkdir -p ${INSTALL_TARGET}/client
273        mkdir -p ${INSTALL_TARGET}/doc
274        mkdir -p ${INSTALL_TARGET}/etc
275        mkdir -p ${INSTALL_TARGET}/lib
276        mkdir -p ${INSTALL_TARGET}/log/clients
277        mkdir -p ${INSTALL_TARGET}/sbin
278        mkdir -p ${INSTALL_TARGET}/www
279        mkdir -p ${INSTALL_TARGET}/images
280        ln -fs /var/lib/tftpboot ${INSTALL_TARGET}
281        ln -fs ${INSTALL_TARGET}/log /var/log/opengnsys
282
283        if [ $? -ne 0 ]; then
284                errorAndLog "${FUNCNAME}(): error while creating dirs. Do you have write permissions?"
285                return 1
286        fi
287
288        echoAndLog "${FUNCNAME}(): directory paths created"
289        return 0
290}
291
292# Copia ficheros de configuración y ejecutables genéricos del servidor.
293function updateServerFiles () {
294
295        local SOURCES=( client/boot/initrd-generator \
296                        client/boot/upgrade-clients-udeb.sh \
297                        client/boot/udeblist.conf  \
298                        client/boot/udeblist-jaunty.conf  \
299                        client/boot/udeblist-karmic.conf \
300                        client/boot/udeblist-lucid.conf \
301                        client/boot/udeblist-maverick.conf \
302                        repoman/bin \
303                        doc )
304        local TARGETS=( bin/initrd-generator \
305                        bin/upgrade-clients-udeb.sh \
306                        etc/udeblist.conf \
307                        etc/udeblist-jaunty.conf  \
308                        etc/udeblist-karmic.conf \
309                        etc/udeblist-lucid.conf \
310                        etc/udeblist-maverick.conf \
311                        bin \
312                        doc )
313
314        if [ ${#SOURCES[@]} != ${#TARGETS[@]} ]; then
315                errorAndLog "${FUNCNAME}(): inconsistent number of array items"
316                exit 1
317        fi
318
319        echoAndLog "${FUNCNAME}(): updating files in server directories"
320        pushd $WORKDIR/opengnsys >/dev/null
321        local i
322        for (( i = 0; i < ${#SOURCES[@]}; i++ )); do
323                rsync --exclude .svn -irplt "${SOURCES[$i]}" "${INSTALL_TARGET}/${TARGETS[$i]}"
324        done
325        popd >/dev/null
326        echoAndLog "${FUNCNAME}(): updating cron files"
327        echo "* * * * *   root   [ -x $INSTALL_TARGET/bin/torrent-creator ] && $INSTALL_TARGET/bin/torrent-creator" > /etc/cron.d/torrentcreator
328        echoAndLog "${FUNCNAME}(): server files updated successfully."
329}
330
331####################################################################
332### Funciones de compilación de código fuente de servicios
333####################################################################
334
335# Recompilar y actualiza el binario del clinete
336function recompileClient ()
337{
338        # Compilar OpenGnSys Client
339        echoAndLog "${FUNCNAME}(): recompiling OpenGnSys Client"
340        pushd $WORKDIR/opengnsys/admin/Sources/Clients/ogAdmClient
341        make && mv ogAdmClient ../../../client/nfsexport/bin
342        if [ $? -ne 0 ]; then
343                echoAndLog "${FUNCNAME}(): error while compiling OpenGnSys Client"
344                hayErrores=1
345        fi
346        popd
347
348        return $hayErrores
349}
350
351
352####################################################################
353### Funciones instalacion cliente opengnsys
354####################################################################
355
356function updateClient()
357{
358        local OSDISTRIB OSCODENAME
359
360        local hayErrores=0
361
362        echoAndLog "${FUNCNAME}(): Copying OpenGnSys Client files."
363        rsync --exclude .svn -irplt $WORKDIR/opengnsys/client/nfsexport/* $INSTALL_TARGET/client
364        echoAndLog "${FUNCNAME}(): Copying OpenGnSys Cloning Engine files."
365        mkdir -p $INSTALL_TARGET/client/lib/engine/bin
366        rsync -iplt $WORKDIR/opengnsys/client/engine/*.lib $INSTALL_TARGET/client/lib/engine/bin
367        if [ $? -ne 0 ]; then
368                errorAndLog "${FUNCNAME}(): error while copying engine files"
369                hayErrores=1
370        fi
371
372        # Cargar Kernel, Initrd y paquetes udeb para la distribución del servidor (o por defecto).
373        OSDISTRIB=$(lsb_release -is) 2>/dev/null
374        OSCODENAME=$(lsb_release -cs) 2>/dev/null
375        if [ "$OSDISTRIB" = "Ubuntu" -a -n "$OSCODENAME" ]; then
376                echoAndLog "${FUNCNAME}(): Loading Kernel and Initrd files for $OSDISTRIB $OSCODENAME."
377                $INSTALL_TARGET/bin/initrd-generator -t $INSTALL_TARGET/tftpboot -v $OSCODENAME 2>&1 | tee -a $LOG_FILE
378                if [ $? -ne 0 ]; then
379                        errorAndLog "${FUNCNAME}(): error while generating initrd OpenGnSys Admin Client"
380                        hayErrores=1
381                fi
382                echoAndLog "${FUNCNAME}(): Loading udeb files for $OSDISTRIB $OSCODENAME."
383                $INSTALL_TARGET/bin/upgrade-clients-udeb.sh $OSCODENAME 2>&1 | tee -a $LOG_FILE
384                if [ $? -ne 0 ]; then
385                        errorAndLog "${FUNCNAME}(): error while upgrading udeb files OpenGnSys Admin Client"
386                        hayErrores=1
387                fi
388        else
389                echoAndLog "${FUNCNAME}(): Loading default Kernel and Initrd files."
390                $INSTALL_TARGET/bin/initrd-generator -t $INSTALL_TARGET/tftpboot 2>&1 | tee -a $LOG_FILE
391                if [ $? -ne 0 ]; then
392                        errorAndLog "${FUNCNAME}(): error while generating initrd OpenGnSys Admin Client"
393                        hayErrores=1
394                fi
395                echoAndLog "${FUNCNAME}(): Loading default udeb files."
396                $INSTALL_TARGET/bin/upgrade-clients-udeb.sh 2>&1 | tee -a $LOG_FILE
397                if [ $? -ne 0 ]; then
398                        errorAndLog "${FUNCNAME}(): error while upgrading udeb files OpenGnSys Admin Client"
399                        hayErrores=1
400                fi
401        fi
402
403        if [ $hayErrores -eq 0 ]; then
404                echoAndLog "${FUNCNAME}(): Client generation success."
405        else
406                errorAndLog "${FUNCNAME}(): Client generation with errors"
407        fi
408
409        return $hayErrores
410}
411
412
413
414#####################################################################
415####### Proceso de actualización de OpenGnSys
416#####################################################################
417
418
419echoAndLog "OpenGnSys update begins at $(date)"
420
421# Instalar dependencia.
422installDependencies $DEPS
423if [ $? -ne 0 ]; then
424        errorAndLog "Error: you may install all needed dependencies."
425        exit 1
426fi
427
428pushd $WORKDIR
429
430# Detectar parámetros de red por defecto
431getNetworkSettings
432if [ $? -ne 0 ]; then
433        errorAndLog "Error reading default network settings."
434        exit 1
435fi
436
437# Arbol de directorios de OpenGnSys.
438createDirs ${INSTALL_TARGET}
439if [ $? -ne 0 ]; then
440        errorAndLog "Error while creating directory paths!"
441        exit 1
442fi
443
444# Si es necesario, descarga el repositorio de código en directorio temporal
445if [ $USESVN -eq 1 ]; then
446        svnExportCode $SVN_URL
447        if [ $? -ne 0 ]; then
448                errorAndLog "Error while getting code from svn"
449                exit 1
450        fi
451else
452        ln -fs "$(dirname $PROGRAMDIR)" opengnsys
453fi
454
455# Copiando ficheros complementarios del servidor
456updateServerFiles
457if [ $? -ne 0 ]; then
458        errorAndLog "Error updating OpenGnSys Server files"
459        exit 1
460fi
461
462# Copiando paqinas web
463updateWebFiles
464if [ $? -ne 0 ]; then
465        errorAndLog "Error updating OpenGnSys Web Admin files"
466        exit 1
467fi
468# Generar páginas Doxygen para instalar en el web
469makeDoxygenFiles
470
471# Creando la estructura del cliente
472recompileClient
473updateClient
474if [ $? -ne 0 ]; then
475        errorAndLog "Error updating clients"
476        exit 1
477fi
478updateInterfaceAdm
479
480# Actualizamos el fichero que arranca los servicios de OpenGnSys
481updateServicesStart
482
483# Eliminamos el fichero de estado del tracker porque es incompatible entre los distintos paquetes
484if [ -r /tmp/dstate ]
485then
486    rm /tmp/dstate
487fi
488
489#rm -rf $WORKDIR
490echoAndLog "OpenGnSys update finished at $(date)"
491
492popd
493
Note: See TracBrowser for help on using the repository browser.