source: installer/opengnsys_installer.sh @ b9a5881

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 b9a5881 was 50038e9, checked in by ramon <ramongomez@…>, 16 years ago

Pequeñas correcciones en instalador.

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

  • Property mode set to 100755
File size: 17.7 KB
Line 
1#!/bin/bash
2
3#####################################################################
4####### Script instalador OpenGnsys
5####### autor: Luis Guillén <lguillen@unizar.es>
6#####################################################################
7
8
9WORKDIR=/tmp/opengnsys_installer
10LOG_FILE=$WORKDIR/installation.log
11
12# Array con las dependencias
13DEPENDENCIES=( subversion php5 mysql-server nfs-kernel-server dhcp3-server udpcast bittorrent apache2 php5 mysql-server php5-mysql tftpd-hpa syslinux tftp-hpa openbsd-inetd update-inetd )
14
15INSTALL_TARGET=/opt/opengnsys
16
17# conexión al svn
18SVN_URL=svn://www.informatica.us.es:3690/opengnsys/trunk
19
20# Datos de base de datos
21MYSQL_ROOT_PASSWORD="passwordroot"
22#HIDRA_DATABASE=bdhidra
23#HIDRA_DB_USER=usuhidra
24#HIDRA_DB_PASSWD=passusuhidra
25#HIDRA_DB_CREATION_FILE=eac-hidra/branches/eac-hidra-us/Hidra/doc/hidra-bd.sql
26
27if [ "$(whoami)" != 'root' ]
28then
29        echo "ERROR: this program must run under root privileges!!"
30        exit 1
31fi
32
33
34mkdir -p $WORKDIR
35pushd $WORKDIR >/dev/null
36
37#####################################################################
38####### Algunas funciones útiles de propósito general:
39#####################################################################
40getDateTime()
41{
42        echo `date +%Y%m%d-%H%M%S`
43}
44
45# Escribe a fichero y muestra por pantalla
46echoAndLog()
47{
48        echo $1
49        FECHAHORA=`getDateTime`
50        echo "$FECHAHORA;$SSH_CLIENT;$1" >> $LOG_FILE
51}
52
53errorAndLog()
54{
55        echo "ERROR: $1"
56        FECHAHORA=`getDateTime`
57        echo "$FECHAHORA;$SSH_CLIENT;ERROR: $1" >> $LOG_FILE
58}
59
60# comprueba si el elemento pasado en $2 esta en el array $1
61isInArray()
62{
63        if [ $# -ne 2 ]; then
64                errorAndLog "isInArray(): invalid number of parameters"
65                exit 1
66        fi
67
68        echoAndLog "isInArray(): checking if $2 is in $1"
69        local deps
70        eval "deps=( \"\${$1[@]}\" )"
71        elemento=$2
72
73        local is_in_array=1
74        # copia local del array del parametro 1
75        for (( i = 0 ; i < ${#deps[@]} ; i++ ))
76        do
77                if [ "${deps[$i]}" = "${elemento}" ]; then
78                        echoAndLog "isInArray(): $elemento found in array"
79                        is_in_array=0
80                fi
81        done
82
83        if [ $is_in_array -ne 0 ]; then
84                echoAndLog "isInArray(): $elemento NOT found in array"
85        fi
86
87        return $is_in_array
88
89}
90
91#####################################################################
92####### Funciones de manejo de paquetes Debian
93#####################################################################
94
95checkPackage()
96{
97        package=$1
98        if [ -z $package ]; then
99                errorAndLog "checkPackage(): parameter required"
100                exit 1
101        fi
102        echoAndLog "checkPackage(): checking if package $package exists"
103        dpkg -L $package &>/dev/null
104        if [ $? -eq 0 ]; then
105                echoAndLog "checkPackage(): package $package exists"
106                return 0
107        else
108                echoAndLog "checkPackage(): package $package doesn't exists"
109                return 1
110        fi
111}
112
113# recibe array con dependencias
114# por referencia deja un array con las dependencias no resueltas
115# devuelve 1 si hay alguna dependencia no resuelta
116checkDependencies()
117{
118        if [ $# -ne 2 ]; then
119                errorAndLog "checkDependencies(): invalid number of parameters"
120                exit 1
121        fi
122
123        echoAndLog "checkDependencies(): checking dependences"
124        uncompletedeps=0
125
126        # copia local del array del parametro 1
127        local deps
128        eval "deps=( \"\${$1[@]}\" )"
129
130        declare -a local_notinstalled
131
132        for (( i = 0 ; i < ${#deps[@]} ; i++ ))
133        do
134                checkPackage ${deps[$i]}
135                if [ $? -ne 0 ]; then
136                        local_notinstalled[$uncompletedeps]=$package
137                        let uncompletedeps=uncompletedeps+1
138                fi
139        done
140
141        # relleno el array especificado en $2 por referencia
142        for (( i = 0 ; i < ${#local_notinstalled[@]} ; i++ ))
143        do
144                eval "${2}[$i]=${local_notinstalled[$i]}"
145        done
146
147        # retorna el numero de paquetes no resueltos
148        echoAndLog "checkDependencies(): dependencies uncompleted: $uncompletedeps"
149        return $uncompletedeps
150}
151
152# Recibe un array con las dependencias y lo instala
153installDependencies()
154{
155        if [ $# -ne 1 ]; then
156                errorAndLog "installDependencies(): invalid number of parameters"
157                exit 1
158        fi
159        echoAndLog "installDependencies(): installing uncompleted dependencies"
160
161        # copia local del array del parametro 1
162        local deps
163        eval "deps=( \"\${$1[@]}\" )"
164
165        local string_deps=""
166        for (( i = 0 ; i < ${#deps[@]} ; i++ ))
167        do
168                string_deps="$string_deps ${deps[$i]}"
169        done
170
171        if [ -z "${string_deps}" ]; then
172                errorAndLog "installDependencies(): array of dependeces is empty"
173                exit 1
174        fi
175
176        OLD_DEBIAN_FRONTEND=$DEBIAN_FRONTEND
177        export DEBIAN_FRONTEND=noninteractive
178
179        echoAndLog "installDependencies(): now ${string_deps} will be installed"
180        apt-get -y install --force-yes ${string_deps}
181        if [ $? -ne 0 ]; then
182                errorAndLog "installDependencies(): error installing dependencies"
183                return 1
184        fi
185
186        DEBIAN_FRONTEND=$OLD_DEBIAN_FRONTEND
187        echoAndLog "installDependencies(): dependencies installed"
188}
189
190#####################################################################
191####### Funciones para el manejo de bases de datos
192#####################################################################
193
194# This function set password to root
195mysqlSetRootPassword()
196{
197        if [ $# -ne 1 ]; then
198                errorAndLog "mysqlSetRootPassword(): invalid number of parameters"
199                exit 1
200        fi
201
202        local root_mysql=$1
203        echoAndLog "mysqlSetRootPassword(): setting root password in MySQL server"
204        /usr/bin/mysqladmin -u root password ${root_mysql}
205        if [ $? -ne 0 ]; then
206                errorAndLog "mysqlSetRootPassword(): error while setting root password in MySQL server"
207                return 1
208        fi
209        echoAndLog "mysqlSetRootPassword(): root password saved!"
210        return 0
211}
212
213# comprueba si puede conectar con mysql con el usuario root
214function mysqlTestConnection()
215{
216        if [ $# -ne 1 ]; then
217                errorAndLog "mysqlTestConnection(): invalid number of parameters"
218                exit 1
219        fi
220
221        local root_password="${1}"
222        echoAndLog "mysqlTestConnection(): checking connection to mysql..."
223        echo "" | mysql -uroot -p"${root_password}"
224        if [ $? -ne 0 ]; then
225                errorAndLog "mysqlTestConnection(): connection to mysql failed, check root password and if daemon is running!"
226                return 1
227        else
228                echoAndLog "mysqlTestConnection(): connection success"
229                return 0
230        fi
231}
232
233# comprueba si la base de datos existe
234function mysqlDbExists()
235{
236        if [ $# -ne 2 ]; then
237                errorAndLog "mysqlDbExists(): invalid number of parameters"
238                exit 1
239        fi
240
241        local root_password="${1}"
242        local database=$2
243        echoAndLog "mysqlDbExists(): checking if $database exists..."
244        echo "show databases" | mysql -uroot -p"${root_password}" | grep "^${database}$"
245        if [ $? -ne 0 ]; then
246                echoAndLog "mysqlDbExists():database $database doesn't exists"
247                return 1
248        else
249                echoAndLog "mysqlDbExists():database $database exists"
250                return 0
251        fi
252}
253
254function mysqlCheckDbIsEmpty()
255{
256        if [ $# -ne 2 ]; then
257                errorAndLog "mysqlCheckDbIsEmpty(): invalid number of parameters"
258                exit 1
259        fi
260
261        local root_password="${1}"
262        local database=$2
263        echoAndLog "mysqlCheckDbIsEmpty(): checking if $database is empty..."
264        num_tablas=`echo "show tables" | mysql -uroot -p"${root_password}" "${database}" | wc -l`
265        if [ $? -ne 0 ]; then
266                errorAndLog "mysqlCheckDbIsEmpty(): error executing query, check database and root password"
267                exit 1
268        fi
269
270        if [ $num_tablas -eq 0 ]; then
271                echoAndLog "mysqlCheckDbIsEmpty():database $database is empty"
272                return 0
273        else
274                echoAndLog "mysqlCheckDbIsEmpty():database $database has tables"
275                return 1
276        fi
277
278}
279
280
281function mysqlImportSqlFileToDb()
282{
283        if [ $# -ne 3 ]; then
284                errorAndLog "mysqlImportSqlFileToDb(): invalid number of parameters"
285                exit 1
286        fi
287
288        local root_password="${1}"
289        local database=$2
290        local sqlfile=$3
291
292        if [ ! -f $sqlfile ]; then
293                errorAndLog "mysqlImportSqlFileToDb(): Unable to locate $sqlfile!!"
294                return 1
295        fi
296
297        echoAndLog "mysqlImportSqlFileToDb(): importing sql file to ${database}..."
298        mysql -uroot -p"${root_password}" "${database}" < $sqlfile
299        if [ $? -ne 0 ]; then
300                errorAndLog "mysqlImportSqlFileToDb(): error while importing $sqlfile in database $database"
301                return 1
302        fi
303        echoAndLog "mysqlImportSqlFileToDb(): file imported to database $database"
304        return 0
305}
306
307# Crea la base de datos
308function mysqlCreateDb()
309{
310        if [ $# -ne 2 ]; then
311                errorAndLog "mysqlCreateDb(): invalid number of parameters"
312                exit 1
313        fi
314
315        local root_password="${1}"
316        local database=$2
317
318        echoAndLog "mysqlCreateDb(): creating database..."
319        mysqladmin -u root --password="${root_password}" create $database
320        if [ $? -ne 0 ]; then
321                errorAndLog "mysqlCreateDb(): error while creating database $database"
322                return 1
323        fi
324        errorAndLog "mysqlCreateDb(): database $database created"
325        return 0
326}
327
328
329function mysqlCheckUserExists()
330{
331        if [ $# -ne 2 ]; then
332                errorAndLog "mysqlCheckUserExists(): invalid number of parameters"
333                exit 1
334        fi
335
336        local root_password="${1}"
337        local userdb=$2
338
339        echoAndLog "mysqlCheckUserExists(): checking if $userdb exists..."
340        echo "select user from user where user='${userdb}'\\G" |mysql -uroot -p"${root_password}" mysql | grep user
341        if [ $? -ne 0 ]; then
342                echoAndLog "mysqlCheckUserExists(): user doesn't exists"
343                return 1
344        else
345                echoAndLog "mysqlCheckUserExists(): user already exists"
346                return 0
347        fi
348
349}
350
351# Crea un usuario administrativo para la base de datos
352function mysqlCreateAdminUserToDb()
353{
354        if [ $# -ne 4 ]; then
355                errorAndLog "mysqlCreateAdminUserToDb(): invalid number of parameters"
356                exit 1
357        fi
358
359        local root_password=$1
360        local database=$2
361        local userdb=$3
362        local passdb=$4
363
364        echoAndLog "mysqlCreateAdminUserToDb(): creating admin user ${userdb} to database ${database}"
365
366        cat > $WORKDIR/create_${database}.sql <<EOF
367GRANT USAGE ON *.* TO '${userdb}'@'localhost' IDENTIFIED BY '${passdb}' ;
368GRANT ALL PRIVILEGES ON ${database}.* TO '${userdb}'@'localhost' WITH GRANT OPTION ;
369FLUSH PRIVILEGES ;
370EOF
371        mysql -u root --password=${root_password} < $WORKDIR/create_${database}.sql
372        if [ $? -ne 0 ]; then
373                errorAndLog "mysqlCreateAdminUserToDb(): error while creating user in mysql"
374                rm -f $WORKDIR/create_${database}.sql
375                return 1
376        else
377                echoAndLog "mysqlCreateAdminUserToDb(): user created ok"
378                rm -f $WORKDIR/create_${database}.sql
379                return 0
380        fi
381}
382
383
384#####################################################################
385####### Funciones para el manejo de Subversion
386#####################################################################
387
388svnCheckoutCode()
389{
390        if [ $# -ne 1 ]; then
391                errorAndLog "svnCheckoutCode(): invalid number of parameters"
392                exit 1
393        fi
394
395        local url=$1
396
397        echoAndLog "svnCheckoutCode(): downloading subversion code..."
398
399        /usr/bin/svn co "${url}"
400        if [ $? -ne 0 ]; then
401                errorAndLog "svnCheckoutCode(): error getting code from ${url}, verify your user and password"
402                return 1
403        fi
404        echoAndLog "svnCheckoutCode(): subversion code downloaded"
405        return 0
406}
407
408#####################################################################
409####### Funciones específicas de la instalación de Opengnsys
410#####################################################################
411
412function openGnsysInstallHidraApacheConf()
413{
414        if [ $# -ne 2 ]; then
415                errorAndLog "openGnsysInstallHidraApacheConf(): invalid number of parameters"
416                exit 1
417        fi
418
419        local path_opengnsys_base=$1
420        local path_apache2_confd=$2
421        local path_web_hidra=${path_opengnsys_base}/www
422
423        echoAndLog "openGnsysInstallHidraApacheConf(): creating apache2 config file.."
424
425        # genera configuración
426        cat > $WORKDIR/apache.conf <<EOF
427# Hidra web interface configuration for Apache
428
429Alias /hidra ${path_web_hidra}
430
431<Directory ${path_web_hidra}>
432        Options -Indexes FollowSymLinks
433        DirectoryIndex acceso.php
434</Directory>
435EOF
436
437        if [ ! -d $path_apache2_confd ]; then
438                errorAndLog "openGnsysInstallHidraApacheConf(): path to apache2 conf.d can not found, verify your server installation"
439                rm -f $WORKDIR/apache.conf
440                return 1
441        fi
442        cp $WORKDIR/apache.conf $path_opengnsys_base/etc
443        ln -s $path_opengnsys_base/etc/apache.conf $path_apache2_confd/hidra.conf
444        if [ $? -ne 0 ]; then
445                errorAndLog "openGnsysInstallHidraApacheConf(): config file can't be linked to apache conf, verify your server installation"
446                rm -f $WORKDIR/apache.conf
447                return 1
448        else
449                echoAndLog "openGnsysInstallHidraApacheConf(): config file created and linked, restart your apache daemon"
450                rm -f $WORKDIR/apache.conf
451                return 0
452        fi
453}
454
455# Crea la estructura base de la instalación de opengnsys
456openGnsysInstallCreateDirs()
457{
458        if [ $# -ne 1 ]; then
459                errorAndLog "openGnsysInstallCreateDirs(): invalid number of parameters"
460                exit 1
461        fi
462
463        local path_opengnsys_base=$1
464
465        echoAndLog "openGnsysInstallCreateDirs(): creating directory paths in $path_opengnsys_base"
466
467        mkdir -p $path_opengnsys_base
468        mkdir -p $path_opengnsys_base/admin/{autoexec,comandos,usuarios}
469        mkdir -p $path_opengnsys_base/bin
470        mkdir -p $path_opengnsys_base/client
471        mkdir -p $path_opengnsys_base/etc
472        mkdir -p $path_opengnsys_base/lib
473        mkdir -p $path_opengnsys_base/log/clients
474        mkdir -p $path_opengnsys_base/www
475        mkdir -p $path_opengnsys_base/images
476        ln -fs /var/lib/tftpboot $path_opengnsys_base/tftpboot
477
478        if [ $? -ne 0 ]; then
479                errorAndLog "openGnsysInstallCreateDirs(): error while creating dirs. Do you have write permissions?"
480                return 1
481        fi
482
483        echoAndLog "openGnsysInstallCreateDirs(): directory paths created"
484        return 0
485}
486
487# Copia ficheros de configuración y ejecutables genéricos del servidor.
488openGnsysCopyServerFiles () {
489        if [ $# -ne 1 ]; then
490                errorAndLog "openGnsysCopyServerFiles(): invalid number of parameters"
491                exit 1
492        fi
493
494        local path_opengnsys_base=$1
495
496        local SOURCES=( admin/Sources/ogAdmServer/ogAdmServer.cfg \
497                        admin/Sources/ogAdmRepo/ogAdmRepo.cfg \
498                        client/boot/initrd-generator \
499                        client/boot/upgrade-clients-udeb.sh \
500                        client/boot/udeblist.conf )
501        local TARGETS=( etc/ogAdmServer.cfg \
502                        etc/ogAdmRepo.cfg \
503                        bin/initrd-generator \
504                        bin/upgrade-clients-udeb.sh \
505                        etc/udeblist.conf )
506
507        if [ ${#SOURCES[@]} != ${#TARGETS[@]} ]; then
508                errorAndLog "openGnsysCopyServerFiles(): inconsistent number of array items"
509                exit 1
510        fi
511
512    echoAndLog "openGnsysCopyServerFiles(): copying files to server directories"
513
514        local i
515        for (( i = 0; i < ${#SOURCES[@]}; i++ )); do
516                if [ -f "${SOURCES[$i]}" ]; then
517                        echoAndLog "copying ${SOURCES[$i]} to $path_opengnsys_base/${TARGETS[$i]}"
518                        cp -p "${SOURCES[$i]}" "${path_opengnsys_base}/${TARGETS[$i]}"
519                fi
520                if [ -d "${SOURCES[$i]}" ]; then
521                        echoAndLog "openGnsysCopyServerFiles(): copying content of ${SOURCES[$i]} to $path_opengnsys_base/${TARGETS[$i]}"
522                        cp -pr "${SOURCES[$i]}/*" "${path_opengnsys_base}/${TARGETS[$i]}"
523                fi
524        done
525}
526
527
528#####################################################################
529####### Proceso de instalación
530#####################################################################
531
532       
533# Proceso de instalación de opengnsys
534declare -a notinstalled
535checkDependencies DEPENDENCIES notinstalled
536if [ $? -ne 0 ]; then
537        installDependencies notinstalled
538        if [ $? -ne 0 ]; then
539                echoAndLog "Error while installing some dependeces, please verify your server installation before continue"
540                exit 1
541        fi
542fi
543
544#isInArray notinstalled "mysql-server"
545#if [ $? -eq 0 ]; then
546#       mysqlSetRootPassword ${MYSQL_ROOT_PASSWORD}
547fi
548
549openGnsysInstallCreateDirs ${INSTALL_TARGET}
550if [ $? -ne 0 ]; then
551        errorAndLog "Error while creating directory paths!"
552        exit 1
553fi
554svnCheckoutCode $SVN_URL
555if [ $? -ne 0 ]; then
556        errorAndLog "Error while getting code from svn"
557        exit 1
558fi
559
560openGnsysCopyServerFiles ${INSTALL_TARGET}
561if [ $? -ne 0 ]; then
562        errorAndLog "Error while copying the server files!"
563        exit 1
564fi
565
566
567mysqlTestConnection ${MYSQL_ROOT_PASSWORD}
568if [ $? -ne 0 ]; then
569        errorAndLog "Error while connection to mysql"
570        exit 1
571fi
572mysqlDbExists ${MYSQL_ROOT_PASSWORD} ${HIDRA_DATABASE}
573if [ $? -ne 0 ]; then
574        echoAndLog "Creating hidra database"
575        mysqlCreateDb ${MYSQL_ROOT_PASSWORD} ${HIDRA_DATABASE}
576        if [ $? -ne 0 ]; then
577                errorAndLog "Error while creating hidra database"
578                exit 1
579        fi
580else
581        echoAndLog "Hidra database exists, ommiting creation"
582fi
583
584mysqlCheckUserExists ${MYSQL_ROOT_PASSWORD} ${HIDRA_DB_USER}
585if [ $? -ne 0 ]; then
586        echoAndLog "Creating user in database"
587        mysqlCreateAdminUserToDb ${MYSQL_ROOT_PASSWORD} ${HIDRA_DATABASE} ${HIDRA_DB_USER} "${HIDRA_DB_PASS}"
588        if [ $? -ne 0 ]; then
589                errorAndLog "Error while creating hidra user"
590                exit 1
591        fi
592
593fi
594
595mysqlCheckDbIsEmpty ${MYSQL_ROOT_PASSWORD} ${HIDRA_DATABASE}
596if [ $? -eq 0 ]; then
597        echoAndLog "Creating tables..."
598        if [ -f $WORKDIR/$HIDRA_DB_CREATION_FILE ]; then
599                mysqlImportSqlFileToDb ${MYSQL_ROOT_PASSWORD} ${HIDRA_DATABASE} $WORKDIR/$HIDRA_DB_CREATION_FILE
600        else
601                errorAndLog "Unable to locate $WORKDIR/$HIDRA_DB_CREATION_FILE!!"
602                exit 1
603        fi
604fi
605
606echoAndLog "Installing web files..."
607# copiando paqinas web
608cp -pr eac-hidra/branches/eac-hidra-us/Hidra/webhidra/* $INSTALL_TARGET/www   #*/ comentario para doxigen
609
610# creando configuracion de apache2
611openGnsysInstallHidraApacheConf $INSTALL_TARGET /etc/apache2/conf.d
612if [ $? -ne 0 ]; then
613        errorAndLog "Error while creating hidra apache config"
614        exit 1
615fi
616
617popd
618#rm -rf $WORKDIR
619echoAndLog "Process finalized!"
620
621
622
623function TestPxe () {
624        cd /tmp
625        echo "comprobando servidio pxe ..... Espere"
626        tftp -v localhost -c get pxelinux.0 /tmp/pxelinux.0 && echo "servidor tftp OK" || echo "servidor tftp KO"
627        cd /
628}
629
630function preparacontenedortft() {
631############################################################
632### Esqueleto para el Servicio pxe y contenedor tftpboot ##############
633###########################################################
634        basetftp=/var/lib/tftpboot
635        basetftpaux=/tftpboot
636        basetftpog=/opt/opengnsys/tftpboot
637        # creamos los correspondientes enlaces hacia nuestro contenedor.
638        ln -s ${basetftp} ${basetftpog}
639        ln -s ${basetftpaux} ${basetftpog}
640
641        # reiniciamos demonio internet
642        /etc/init.d/openbsd-inetd start
643
644        ##preparcion contendor tftpboot
645        cp -pr /usr/lib/syslinux/ ${basetftpboot}/syslinux
646        cp /usr/lib/syslinux/pxelinux.0 $basetftpboot
647        # prepamos el directorio de la configuracion de pxe
648        mkdir -p ${basetftpboot}/pxelinux.cfg
649        touch ${basetftpboot}/pxelinux.cfg/default
650        # comprobamos el servicio tftp
651        sleep 1
652        TestPxe
653        ## damos perfimos de lectura a usuario web.
654        chown -R www-data:www-data /var/lib/tftpboot
655        ######### fin revisar2 contenedor tftp
656}
657
Note: See TracBrowser for help on using the repository browser.