source: installer/opengnsys_installer.sh @ 8964f9b

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

Resstructuración de trunk.

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

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