source: installer/opengnsys_update.sh @ 3320409

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 3320409 was 3320409, checked in by jcxifre <jcxifre@…>, 15 years ago

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

  • Property mode set to 100755
File size: 12.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"
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            if ! dpkg -s $1 &>/dev/null; then
123                INSTALLDEPS="$INSTALLDEPS $1"
124            fi
125            shift
126        done
127        if [ -n "$INSTALLDEPS" ]; then
128            apt-get update && apt-get install $INSTALLDEPS
129                if [ $? -ne 0 ]; then
130                        errorAndLog "${FUNCNAME}(): cannot install some dependencies: $INSTALLDEPS."
131                return 1
132                fi
133        fi
134    fi
135}
136
137
138#####################################################################
139####### Funciones para el manejo de Subversion
140#####################################################################
141
142function svnExportCode()
143{
144        if [ $# -ne 1 ]; then
145                errorAndLog "${FUNCNAME}(): invalid number of parameters"
146                exit 1
147        fi
148
149        local url=$1
150
151        echoAndLog "${FUNCNAME}(): downloading subversion code..."
152
153        svn checkout "${url}" opengnsys
154        if [ $? -ne 0 ]; then
155                errorAndLog "${FUNCNAME}(): error getting code from ${url}, verify your user and password"
156                return 1
157        fi
158        echoAndLog "${FUNCNAME}(): subversion code downloaded"
159        return 0
160}
161
162
163############################################################
164###  Detectar red
165############################################################
166
167function getNetworkSettings()
168{
169        local MAINDEV
170
171        echoAndLog "getNetworkSettings(): Detecting default network parameters."
172        MAINDEV=$(ip -o link show up | awk '!/loopback/ {d=d$2} END {sub(/:.*/,"",d); print d}')
173        if [ -z "$MAINDEV" ]; then
174                errorAndLog "${FUNCNAME}(): Network device not detected."
175                return 1
176        fi
177
178        # Variables de ejecución de Apache
179        # - APACHE_RUN_USER
180        # - APACHE_RUN_GROUP
181        if [ -f /etc/apache2/envvars ]; then
182                source /etc/apache2/envvars
183        fi
184        APACHE_RUN_USER=${APACHE_RUN_USER:-"www-data"}
185        APACHE_RUN_GROUP=${APACHE_RUN_GROUP:-"www-data"}
186}
187
188
189#####################################################################
190####### Funciones específicas de la instalación de Opengnsys
191#####################################################################
192
193# Copiar ficheros del OpenGnSys Web Console.
194function updateWebFiles()
195{
196        echoAndLog "${FUNCNAME}(): Updating web files..."
197    backupFile $INSTALL_TARGET/www/controlacceso.php
198        rsync --exclude .svn -irplt $WORKDIR/opengnsys/admin/WebConsole $INSTALL_TARGET/www
199        if [ $? != 0 ]; then
200                errorAndLog "${FUNCNAME}(): Error updating web files."
201                exit 1
202        fi
203    restoreFile $INSTALL_TARGET/www/controlacceso.php
204        # Cambiar permisos para ficheros especiales.
205        chown -R $APACHE_RUN_USER:$APACHE_RUN_GROUP \
206                        $INSTALL_TARGET/www/includes \
207                        $INSTALL_TARGET/www/comandos/gestores/filescripts \
208                        $INSTALL_TARGET/www/images/iconos
209        echoAndLog "${FUNCNAME}(): Web files updated successfully."
210}
211
212
213# Crear documentación Doxygen para la consola web.
214function makeDoxygenFiles()
215{
216        echoAndLog "${FUNCNAME}(): Making Doxygen web files..."
217        $WORKDIR/opengnsys/installer/ogGenerateDoc.sh \
218                        $WORKDIR/opengnsys/client/engine $INSTALL_TARGET/www
219        if [ ! -d "$INSTALL_TARGET/www/html" ]; then
220                errorAndLog "${FUNCNAME}(): unable to create Doxygen web files."
221                return 1
222        fi
223        mv "$INSTALL_TARGET/www/html" "$INSTALL_TARGET/www/api"
224        chown -R $APACHE_RUN_USER:$APACHE_RUN_GROUP $INSTALL_TARGET/www/api
225        echoAndLog "${FUNCNAME}(): Doxygen web files created successfully."
226}
227
228
229# Crea la estructura base de la instalación de opengnsys
230function createDirs()
231{
232        echoAndLog "${FUNCNAME}(): creating directory paths in ${INSTALL_TARGET}"
233
234        mkdir -p ${INSTALL_TARGET}
235        mkdir -p ${INSTALL_TARGET}/admin/{autoexec,comandos,menus,usuarios}
236        mkdir -p ${INSTALL_TARGET}/bin
237        mkdir -p ${INSTALL_TARGET}/client
238        mkdir -p ${INSTALL_TARGET}/doc
239        mkdir -p ${INSTALL_TARGET}/etc
240        mkdir -p ${INSTALL_TARGET}/lib
241        mkdir -p ${INSTALL_TARGET}/log/clients
242        mkdir -p ${INSTALL_TARGET}/sbin
243        mkdir -p ${INSTALL_TARGET}/www
244        mkdir -p ${INSTALL_TARGET}/images
245        ln -fs /var/lib/tftpboot ${INSTALL_TARGET}
246        ln -fs ${INSTALL_TARGET}/log /var/log/opengnsys
247
248        if [ $? -ne 0 ]; then
249                errorAndLog "${FUNCNAME}(): error while creating dirs. Do you have write permissions?"
250                return 1
251        fi
252
253        echoAndLog "${FUNCNAME}(): directory paths created"
254        return 0
255}
256
257# Copia ficheros de configuración y ejecutables genéricos del servidor.
258function updateServerFiles () {
259
260        local SOURCES=( client/boot/initrd-generator \
261                        client/boot/upgrade-clients-udeb.sh \
262                        client/boot/udeblist.conf  \
263                        client/boot/udeblist-jaunty.conf  \
264                        client/boot/udeblist-karmic.conf \
265                        doc )
266        local TARGETS=( bin/initrd-generator \
267                        bin/upgrade-clients-udeb.sh \
268                        etc/udeblist.conf \
269                        etc/udeblist-jaunty.conf  \
270                        etc/udeblist-karmic.conf \
271                        doc )
272
273        if [ ${#SOURCES[@]} != ${#TARGETS[@]} ]; then
274                errorAndLog "${FUNCNAME}(): inconsistent number of array items"
275                exit 1
276        fi
277
278        echoAndLog "${FUNCNAME}(): updating files in server directories"
279        pushd $WORKDIR/opengnsys >/dev/null
280        local i
281        for (( i = 0; i < ${#SOURCES[@]}; i++ )); do
282                rsync --exclude .svn -irplt "${SOURCES[$i]}" "${INSTALL_TARGET}/${TARGETS[$i]}"
283        done
284        popd >/dev/null
285    echoAndLog "${FUNCNAME}(): server files updated successfully."
286}
287
288####################################################################
289### Funciones de compilación de código fuente de servicios
290####################################################################
291
292# Recompilar y actualiza el binario del clinete
293function recompileClient ()
294{
295        # Compilar OpenGnSys Client
296        echoAndLog "${FUNCNAME}(): recompiling OpenGnSys Client"
297        pushd $WORKDIR/opengnsys/admin/Services/ogAdmClient
298        make && mv ogAdmClient ../../../client/nfsexport/bin
299        if [ $? -ne 0 ]; then
300                echoAndLog "${FUNCNAME}(): error while compiling OpenGnSys Client"
301                hayErrores=1
302        fi
303        popd
304
305        return $hayErrores
306}
307
308
309####################################################################
310### Funciones instalacion cliente opengnsys
311####################################################################
312
313function updateClient()
314{
315        local OSDISTRIB OSCODENAME
316
317        local hayErrores=0
318
319        echoAndLog "${FUNCNAME}(): Copying OpenGnSys Client files."
320        rsync --exclude .svn -irplt $WORKDIR/opengnsys/client/nfsexport/* $INSTALL_TARGET/client
321        echoAndLog "${FUNCNAME}(): Copying OpenGnSys Cloning Engine files."
322        mkdir -p $INSTALL_TARGET/client/lib/engine/bin
323        rsync -iplt $WORKDIR/opengnsys/client/engine/*.lib $INSTALL_TARGET/client/lib/engine/bin
324        if [ $? -ne 0 ]; then
325                errorAndLog "${FUNCNAME}(): error while copying engine files"
326                hayErrores=1
327        fi
328
329        # Cargar Kernel, Initrd y paquetes udeb para la distribución del servidor (o por defecto).
330        OSDISTRIB=$(lsb_release -i | awk -F: '{sub(/\t/,""); print $2}') 2>/dev/null
331        OSCODENAME=$(lsb_release -c | awk -F: '{sub(/\t/,""); print $2}') 2>/dev/null
332        if [ "$OSDISTRIB" = "Ubuntu" -a -n "$OSCODENAME" ]; then
333                echoAndLog "${FUNCNAME}(): Loading Kernel and Initrd files for $OSDISTRIB $OSCODENAME."
334                $INSTALL_TARGET/bin/initrd-generator -t $INSTALL_TARGET/tftpboot -v "$OSCODENAME"
335                if [ $? -ne 0 ]; then
336                        errorAndLog "${FUNCNAME}(): error while generating initrd OpenGnSys Admin Client"
337                        hayErrores=1
338                fi
339                echoAndLog "${FUNCNAME}(): Loading udeb files for $OSDISTRIB $OSCODENAME."
340                $INSTALL_TARGET/bin/upgrade-clients-udeb.sh "$OSCODENAME"
341                if [ $? -ne 0 ]; then
342                        errorAndLog "${FUNCNAME}(): error while upgrading udeb files OpenGnSys Admin Client"
343                        hayErrores=1
344                fi
345        else
346                echoAndLog "${FUNCNAME}(): Loading default Kernel and Initrd files."
347                $INSTALL_TARGET/bin/initrd-generator -t $INSTALL_TARGET/tftpboot/
348                if [ $? -ne 0 ]; then
349                        errorAndLog "${FUNCNAME}(): error while generating initrd OpenGnSys Admin Client"
350                        hayErrores=1
351                fi
352                echoAndLog "${FUNCNAME}(): Loading default udeb files."
353                $INSTALL_TARGET/bin/upgrade-clients-udeb.sh
354                if [ $? -ne 0 ]; then
355                        errorAndLog "${FUNCNAME}(): error while upgrading udeb files OpenGnSys Admin Client"
356                        hayErrores=1
357                fi
358        fi
359
360        if [ $hayErrores -eq 0 ]; then
361                echoAndLog "${FUNCNAME}(): Client generation success."
362        else
363                errorAndLog "${FUNCNAME}(): Client generation with errors"
364        fi
365
366        return $hayErrores
367}
368
369
370
371#####################################################################
372####### Proceso de actualización de OpenGnSys
373#####################################################################
374
375
376echoAndLog "OpenGnSys update begins at $(date)"
377
378# Instalar dependencia.
379installDependencies $DEPS
380if [ $? -ne 0 ]; then
381        errorAndLog "Error: you may install all needed dependencies."
382        exit 1
383fi
384
385pushd $WORKDIR
386
387# Detectar parámetros de red por defecto
388getNetworkSettings
389if [ $? -ne 0 ]; then
390        errorAndLog "Error reading default network settings."
391        exit 1
392fi
393
394# Arbol de directorios de OpenGnSys.
395createDirs ${INSTALL_TARGET}
396if [ $? -ne 0 ]; then
397        errorAndLog "Error while creating directory paths!"
398        exit 1
399fi
400
401# Si es necesario, descarga el repositorio de código en directorio temporal
402if [ $USESVN -eq 1 ]; then
403        svnExportCode $SVN_URL
404        if [ $? -ne 0 ]; then
405                errorAndLog "Error while getting code from svn"
406                exit 1
407        fi
408else
409        ln -fs "$(dirname $PROGRAMDIR)" opengnsys
410fi
411
412# Copiando ficheros complementarios del servidor
413updateServerFiles
414if [ $? -ne 0 ]; then
415    errorAndLog "Error updating OpenGnSys Server files"
416        exit 1
417fi
418
419# Copiando paqinas web
420updateWebFiles
421if [ $? -ne 0 ]; then
422    errorAndLog "Error updating OpenGnSys Web Admin files"
423        exit 1
424fi
425# Generar páginas Doxygen para instalar en el web
426makeDoxygenFiles
427
428# Creando la estructura del cliente
429recompileClient
430updateClient
431if [ $? -ne 0 ]; then
432        errorAndLog "Error updating clients"
433        exit 1
434fi
435
436# Eliminamos el fichero de estado del tracker porque es incompatible entre los distintos paquetes
437if [ -r /tmp/dstate ]
438then
439    rm /tmp/dstate
440fi
441
442#rm -rf $WORKDIR
443echoAndLog "OpenGnSys update finished at $(date)"
444
445popd
446
Note: See TracBrowser for help on using the repository browser.