source: installer/opengnsys_update.sh @ 7ad4465

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

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

  • Property mode set to 100755
File size: 12.5 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        # Variables globales definidas:
170        # - SERVERIP: IP local del servidor.
171        # - NETIP:    IP de la red.
172        # - NETMASK:  máscara de red.
173        # - NETBROAD: IP de difusión de la red.
174        # - ROUTERIP: IP del router.
175        # - DNSIP:    IP del servidor DNS.
176
177        echoAndLog "getNetworkSettings(): Detecting default network parameters."
178        SERVERIP=$(LANG=C ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | head -n1 | awk '{print $1}')
179        NETMASK=$(LANG=C ifconfig | grep 'Mask:'| grep -v '127.0.0.1' | cut -d: -f4 | head -n1 | awk '{print $1}')
180        NETBROAD=$(LANG=C ifconfig | grep 'Bcast:'| grep -v '127.0.0.1' | cut -d: -f3 | head -n1 | awk '{print $1}')
181        NETIP=$(netstat -r | grep $NETMASK | head -n1 | awk '{print $1}')
182        ROUTERIP=$(netstat -nr | awk '$1~/0\.0\.0\.0/ {print $2}')
183        DNSIP=$(awk '/nameserver/ {print $2}' /etc/resolv.conf | head -n1)
184        if [ -z "$NETIP" -o -z "$NETMASK" ]; then
185                errorAndLog "getNetworkSettings(): Network not detected."
186                exit 1
187        fi
188
189        # Variables de ejecución de Apache
190        # - APACHE_RUN_USER
191        # - APACHE_RUN_GROUP
192        if [ -f /etc/apache2/envvars ]; then
193                source /etc/apache2/envvars
194        fi
195        APACHE_RUN_USER=${APACHE_RUN_USER:-"www-data"}
196        APACHE_RUN_GROUP=${APACHE_RUN_GROUP:-"www-data"}
197}
198
199
200#####################################################################
201####### Funciones específicas de la instalación de Opengnsys
202#####################################################################
203
204# Copiar ficheros del OpenGnSys Web Console.
205function updateWebFiles()
206{
207        echoAndLog "${FUNCNAME}(): Updating web files..."
208    backupFile $INSTALL_TARGET/www/controlacceso.php
209        rsync --exclude .svn -irplt $WORKDIR/opengnsys/admin/WebConsole $INSTALL_TARGET/www
210        if [ $? != 0 ]; then
211                errorAndLog "${FUNCNAME}(): Error updating web files."
212                exit 1
213        fi
214    restoreFile $INSTALL_TARGET/www/controlacceso.php
215        # Cambiar permisos para ficheros especiales.
216        chown -R $APACHE_RUN_USER:$APACHE_RUN_GROUP \
217                        $INSTALL_TARGET/www/includes \
218                        $INSTALL_TARGET/www/comandos/gestores/filescripts \
219                        $INSTALL_TARGET/www/images/iconos
220        echoAndLog "${FUNCNAME}(): Web files updated successfully."
221}
222
223
224# Crea la estructura base de la instalación de opengnsys
225function createDirs()
226{
227        echoAndLog "${FUNCNAME}(): creating directory paths in ${INSTALL_TARGET}"
228
229        mkdir -p ${INSTALL_TARGET}
230        mkdir -p ${INSTALL_TARGET}/admin/{autoexec,comandos,menus,usuarios}
231        mkdir -p ${INSTALL_TARGET}/bin
232        mkdir -p ${INSTALL_TARGET}/client
233        mkdir -p ${INSTALL_TARGET}/doc
234        mkdir -p ${INSTALL_TARGET}/etc
235        mkdir -p ${INSTALL_TARGET}/lib
236        mkdir -p ${INSTALL_TARGET}/log/clients
237        mkdir -p ${INSTALL_TARGET}/sbin
238        mkdir -p ${INSTALL_TARGET}/www
239        mkdir -p ${INSTALL_TARGET}/images
240        ln -fs /var/lib/tftpboot ${INSTALL_TARGET}
241        ln -fs ${INSTALL_TARGET}/log /var/log/opengnsys
242
243        if [ $? -ne 0 ]; then
244                errorAndLog "${FUNCNAME}(): error while creating dirs. Do you have write permissions?"
245                return 1
246        fi
247
248        echoAndLog "${FUNCNAME}(): directory paths created"
249        return 0
250}
251
252# Copia ficheros de configuración y ejecutables genéricos del servidor.
253function updateServerFiles () {
254
255        local SOURCES=( client/boot/initrd-generator \
256                        client/boot/upgrade-clients-udeb.sh \
257                        client/boot/udeblist.conf  \
258                        client/boot/udeblist-jaunty.conf  \
259                        client/boot/udeblist-karmic.conf \
260                        doc )
261        local TARGETS=( bin/initrd-generator \
262                        bin/upgrade-clients-udeb.sh \
263                        etc/udeblist.conf \
264                        etc/udeblist-jaunty.conf  \
265                        etc/udeblist-karmic.conf \
266                        doc )
267
268        if [ ${#SOURCES[@]} != ${#TARGETS[@]} ]; then
269                errorAndLog "${FUNCNAME}(): inconsistent number of array items"
270                exit 1
271        fi
272
273        echoAndLog "${FUNCNAME}(): updating files in server directories"
274        pushd $WORKDIR/opengnsys >/dev/null
275        local i
276        for (( i = 0; i < ${#SOURCES[@]}; i++ )); do
277                rsync --exclude .svn -irplt "${SOURCES[$i]}" "${INSTALL_TARGET}/${TARGETS[$i]}"
278        done
279        popd >/dev/null
280    echoAndLog "${FUNCNAME}(): server files updated successfully."
281}
282
283####################################################################
284### Funciones de compilación de código fuente de servicios
285####################################################################
286
287# Recompilar y actualiza el binario del clinete
288function recompileClient ()
289{
290        # Compilar OpenGnSys Client
291        echoAndLog "${FUNCNAME}(): recompiling OpenGnSys Client"
292        pushd $WORKDIR/opengnsys/admin/Services/ogAdmClient
293        make && mv ogAdmClient ../../../client/nfsexport/bin
294        if [ $? -ne 0 ]; then
295                echoAndLog "${FUNCNAME}(): error while compiling OpenGnSys Client"
296                hayErrores=1
297        fi
298        popd
299
300        return $hayErrores
301}
302
303
304####################################################################
305### Funciones instalacion cliente opengnsys
306####################################################################
307
308function updateClient()
309{
310        local OSDISTRIB OSCODENAME
311
312        local hayErrores=0
313
314        echoAndLog "${FUNCNAME}(): Copying OpenGnSys Client files."
315        rsync --exclude .svn -irplt $WORKDIR/opengnsys/client/nfsexport/* $INSTALL_TARGET/client
316        echoAndLog "${FUNCNAME}(): Copying OpenGnSys Cloning Engine files."
317        mkdir -p $INSTALL_TARGET/client/lib/engine/bin
318        rsync -iplt $WORKDIR/opengnsys/client/engine/*.lib $INSTALL_TARGET/client/lib/engine/bin
319        if [ $? -ne 0 ]; then
320                errorAndLog "${FUNCNAME}(): error while copying engine files"
321                hayErrores=1
322        fi
323
324        # Cargar Kernel, Initrd y paquetes udeb para la distribución del servidor (o por defecto).
325        OSDISTRIB=$(lsb_release -i | awk -F: '{sub(/\t/,""); print $2}') 2>/dev/null
326        OSCODENAME=$(lsb_release -c | awk -F: '{sub(/\t/,""); print $2}') 2>/dev/null
327        if [ "$OSDISTRIB" = "Ubuntu" -a -n "$OSCODENAME" ]; then
328                echoAndLog "${FUNCNAME}(): Loading Kernel and Initrd files for $OSDISTRIB $OSCODENAME."
329                $INSTALL_TARGET/bin/initrd-generator -t $INSTALL_TARGET/tftpboot -v "$OSCODENAME"
330                if [ $? -ne 0 ]; then
331                        errorAndLog "${FUNCNAME}(): error while generating initrd OpenGnSys Admin Client"
332                        hayErrores=1
333                fi
334                echoAndLog "${FUNCNAME}(): Loading udeb files for $OSDISTRIB $OSCODENAME."
335                $INSTALL_TARGET/bin/upgrade-clients-udeb.sh "$OSCODENAME"
336                if [ $? -ne 0 ]; then
337                        errorAndLog "${FUNCNAME}(): error while upgrading udeb files OpenGnSys Admin Client"
338                        hayErrores=1
339                fi
340        else
341                echoAndLog "${FUNCNAME}(): Loading default Kernel and Initrd files."
342                $INSTALL_TARGET/bin/initrd-generator -t $INSTALL_TARGET/tftpboot/
343                if [ $? -ne 0 ]; then
344                        errorAndLog "${FUNCNAME}(): error while generating initrd OpenGnSys Admin Client"
345                        hayErrores=1
346                fi
347                echoAndLog "${FUNCNAME}(): Loading default udeb files."
348                $INSTALL_TARGET/bin/upgrade-clients-udeb.sh
349                if [ $? -ne 0 ]; then
350                        errorAndLog "${FUNCNAME}(): error while upgrading udeb files OpenGnSys Admin Client"
351                        hayErrores=1
352                fi
353        fi
354
355        if [ $hayErrores -eq 0 ]; then
356                echoAndLog "${FUNCNAME}(): Client generation success."
357        else
358                errorAndLog "${FUNCNAME}(): Client generation with errors"
359        fi
360
361        return $hayErrores
362}
363
364
365
366#####################################################################
367####### Proceso de actualizción de OpenGnSys
368#####################################################################
369
370
371echoAndLog "OpenGnSys update begins at $(date)"
372
373# Instalar dependencia.
374installDependencies $DEPS
375if [ $? -ne 0 ]; then
376        errorAndLog "Error: you may install all needed dependencies."
377        exit 1
378fi
379
380pushd $WORKDIR
381
382# Detectar parámetros de red por defecto
383getNetworkSettings
384if [ $? -ne 0 ]; then
385        errorAndLog "Error reading default network settings."
386        exit 1
387fi
388
389# Arbol de directorios de OpenGnSys.
390createDirs ${INSTALL_TARGET}
391if [ $? -ne 0 ]; then
392        errorAndLog "Error while creating directory paths!"
393        exit 1
394fi
395
396# Si es necesario, descarga el repositorio de código en directorio temporal
397if [ $USESVN -eq 1 ]; then
398        svnExportCode $SVN_URL
399        if [ $? -ne 0 ]; then
400                errorAndLog "Error while getting code from svn"
401                exit 1
402        fi
403else
404        ln -fs "$(dirname $PROGRAMDIR)" opengnsys
405fi
406
407# Copiando ficheros complementarios del servidor
408updateServerFiles
409if [ $? -ne 0 ]; then
410    errorAndLog "Error updating OpenGnSys Server files"
411        exit 1
412fi
413
414# Copiando paqinas web
415updateWebFiles
416if [ $? -ne 0 ]; then
417    errorAndLog "Error updating OpenGnSys Web Admin files"
418        exit 1
419fi
420
421# Creando la estructura del cliente
422recompileClient
423updateClient
424if [ $? -ne 0 ]; then
425        errorAndLog "Error updating clients"
426        exit 1
427fi
428
429#rm -rf $WORKDIR
430echoAndLog "OpenGnSys update finished at $(date)"
431
432popd
433
Note: See TracBrowser for help on using the repository browser.