source: installer/opengnsys_installer.sh @ 180a07dd

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

OpenGNSys Installer muestra información de instalación e instrucciones de post-instalación; scripts de ejemplo compatibles con Browser.

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

  • Property mode set to 100755
File size: 32.7 KB
RevLine 
[a01156a]1#!/bin/bash
2
3#####################################################################
4####### Script instalador OpenGnsys
5####### autor: Luis Guillén <lguillen@unizar.es>
6#####################################################################
7
8
9WORKDIR=/tmp/opengnsys_installer
[13a01a7]10LOG_FILE=/tmp/opengnsys_installation.log
[a01156a]11
12# Array con las dependencias
[13a01a7]13DEPENDENCIES=( subversion apache2 php5 mysql-server php5-mysql nfs-kernel-server dhcp3-server udpcast bittorrent tftp-hpa tftpd-hpa syslinux openbsd-inetd update-inetd build-essential libmysqlclient15-dev wget )
[a01156a]14
15INSTALL_TARGET=/opt/opengnsys
16
[892606b9]17MYSQL_ROOT_PASSWORD="passwordroot"
18
[a01156a]19# conexión al svn
[1b92629]20SVN_URL=svn://www.informatica.us.es:3690/opengnsys/trunk
[a01156a]21
22# Datos de base de datos
[892606b9]23OPENGNSYS_DATABASE=ogBDAdmin
24OPENGNSYS_DB_USER=usuog
25OPENGNSYS_DB_PASSWD=passusuog
26OPENGNSYS_DB_CREATION_FILE=opengnsys/admin/Database/ogBDAdmin.sql
27
28USUARIO=`whoami`
[a01156a]29
[892606b9]30if [ $USUARIO != 'root' ]
[a01156a]31then
32        echo "ERROR: this program must run under root privileges!!"
33        exit 1
34fi
35
36
37mkdir -p $WORKDIR
[892606b9]38pushd $WORKDIR
[a01156a]39
40#####################################################################
41####### Algunas funciones útiles de propósito general:
42#####################################################################
[13a01a7]43function getDateTime()
[a01156a]44{
45        echo `date +%Y%m%d-%H%M%S`
46}
47
48# Escribe a fichero y muestra por pantalla
[13a01a7]49function echoAndLog()
[a01156a]50{
51        echo $1
52        FECHAHORA=`getDateTime`
53        echo "$FECHAHORA;$SSH_CLIENT;$1" >> $LOG_FILE
54}
55
[13a01a7]56function errorAndLog()
[a01156a]57{
58        echo "ERROR: $1"
59        FECHAHORA=`getDateTime`
60        echo "$FECHAHORA;$SSH_CLIENT;ERROR: $1" >> $LOG_FILE
61}
62
63# comprueba si el elemento pasado en $2 esta en el array $1
[13a01a7]64function isInArray()
[a01156a]65{
66        if [ $# -ne 2 ]; then
[13a01a7]67                errorAndLog "${FUNCNAME}(): invalid number of parameters"
[a01156a]68                exit 1
69        fi
70
[13a01a7]71        echoAndLog "${FUNCNAME}(): checking if $2 is in $1"
[a01156a]72        local deps
[5c33840]73        eval "deps=( \"\${$1[@]}\" )"
[a01156a]74        elemento=$2
75
76        local is_in_array=1
77        # copia local del array del parametro 1
78        for (( i = 0 ; i < ${#deps[@]} ; i++ ))
79        do
80                if [ "${deps[$i]}" = "${elemento}" ]; then
81                        echoAndLog "isInArray(): $elemento found in array"
82                        is_in_array=0
83                fi
84        done
85
86        if [ $is_in_array -ne 0 ]; then
[13a01a7]87                echoAndLog "${FUNCNAME}(): $elemento NOT found in array"
[a01156a]88        fi
89
90        return $is_in_array
91
92}
93
94#####################################################################
95####### Funciones de manejo de paquetes Debian
96#####################################################################
97
[13a01a7]98function checkPackage()
[a01156a]99{
100        package=$1
101        if [ -z $package ]; then
102                errorAndLog "checkPackage(): parameter required"
103                exit 1
104        fi
105        echoAndLog "checkPackage(): checking if package $package exists"
106        dpkg -L $package &>/dev/null
107        if [ $? -eq 0 ]; then
108                echoAndLog "checkPackage(): package $package exists"
109                return 0
110        else
111                echoAndLog "checkPackage(): package $package doesn't exists"
112                return 1
113        fi
114}
115
116# recibe array con dependencias
117# por referencia deja un array con las dependencias no resueltas
118# devuelve 1 si hay alguna dependencia no resuelta
[13a01a7]119function checkDependencies()
[a01156a]120{
121        if [ $# -ne 2 ]; then
122                errorAndLog "checkDependencies(): invalid number of parameters"
123                exit 1
124        fi
125
126        echoAndLog "checkDependencies(): checking dependences"
127        uncompletedeps=0
128
129        # copia local del array del parametro 1
130        local deps
[5c33840]131        eval "deps=( \"\${$1[@]}\" )"
[a01156a]132
133        declare -a local_notinstalled
[5c33840]134
[a01156a]135        for (( i = 0 ; i < ${#deps[@]} ; i++ ))
136        do
137                checkPackage ${deps[$i]}
138                if [ $? -ne 0 ]; then
139                        local_notinstalled[$uncompletedeps]=$package
140                        let uncompletedeps=uncompletedeps+1
141                fi
142        done
143
144        # relleno el array especificado en $2 por referencia
145        for (( i = 0 ; i < ${#local_notinstalled[@]} ; i++ ))
146        do
147                eval "${2}[$i]=${local_notinstalled[$i]}"
148        done
149
150        # retorna el numero de paquetes no resueltos
151        echoAndLog "checkDependencies(): dependencies uncompleted: $uncompletedeps"
152        return $uncompletedeps
153}
154
155# Recibe un array con las dependencias y lo instala
[13a01a7]156function installDependencies()
[a01156a]157{
158        if [ $# -ne 1 ]; then
159                errorAndLog "installDependencies(): invalid number of parameters"
160                exit 1
161        fi
162        echoAndLog "installDependencies(): installing uncompleted dependencies"
163
164        # copia local del array del parametro 1
165        local deps
[5c33840]166        eval "deps=( \"\${$1[@]}\" )"
[a01156a]167
168        local string_deps=""
169        for (( i = 0 ; i < ${#deps[@]} ; i++ ))
170        do
171                string_deps="$string_deps ${deps[$i]}"
172        done
173
174        if [ -z "${string_deps}" ]; then
175                errorAndLog "installDependencies(): array of dependeces is empty"
176                exit 1
177        fi
178
179        OLD_DEBIAN_FRONTEND=$DEBIAN_FRONTEND
180        export DEBIAN_FRONTEND=noninteractive
181
182        echoAndLog "installDependencies(): now ${string_deps} will be installed"
183        apt-get -y install --force-yes ${string_deps}
184        if [ $? -ne 0 ]; then
185                errorAndLog "installDependencies(): error installing dependencies"
186                return 1
187        fi
188
189        DEBIAN_FRONTEND=$OLD_DEBIAN_FRONTEND
190        echoAndLog "installDependencies(): dependencies installed"
191}
192
[13a01a7]193# Hace un backup del fichero pasado por parámetro
194# deja un -last y uno para el día
195function backupFile()
196{
197        if [ $# -ne 1 ]; then
198                errorAndLog "${FUNCNAME}(): invalid number of parameters"
199                exit 1
200        fi
201
202        local fichero=$1
203        local fecha=`date +%Y%m%d`
204
205        if [ ! -f $fichero ]; then
206                errorAndLog "${FUNCNAME}(): file $fichero doesn't exists"
207                return 1
208        fi
209
210        echoAndLog "${FUNCNAME}(): realizando backup de $fichero"
211
212        # realiza una copia de la última configuración como last
213        cp -p $fichero "${fichero}-LAST"
214
215        # si para el día no hay backup lo hace, sino no
216        if [ ! -f "${fichero}-${fecha}" ]; then
217                cp -p $fichero "${fichero}-${fecha}"
218        fi
219
220        echoAndLog "${FUNCNAME}(): backup realizado"
221}
222
[a01156a]223#####################################################################
224####### Funciones para el manejo de bases de datos
225#####################################################################
226
227# This function set password to root
[13a01a7]228function mysqlSetRootPassword()
[a01156a]229{
230        if [ $# -ne 1 ]; then
231                errorAndLog "mysqlSetRootPassword(): invalid number of parameters"
232                exit 1
233        fi
234
235        local root_mysql=$1
236        echoAndLog "mysqlSetRootPassword(): setting root password in MySQL server"
237        /usr/bin/mysqladmin -u root password ${root_mysql}
238        if [ $? -ne 0 ]; then
239                errorAndLog "mysqlSetRootPassword(): error while setting root password in MySQL server"
240                return 1
241        fi
242        echoAndLog "mysqlSetRootPassword(): root password saved!"
243        return 0
244}
245
[892606b9]246# Si el servicio mysql esta ya instalado cambia la variable de la clave del root por la ya existente
[13a01a7]247function mysqlGetRootPassword(){
[892606b9]248        local pass_mysql
249        local pass_mysql2
250        stty -echo
251        echo "Existe un servicio mysql ya instalado"
252        read -p  "Insertar clave de root de Mysql: " pass_mysql
253        echo ""
254        read -p "Confirmar clave:" pass_mysql2
255        echo ""
256        stty echo
257        if [ "$pass_mysql" == "$pass_mysql2" ] ;then
[8125e7c]258                MYSQL_ROOT_PASSWORD=$pass_mysql
[892606b9]259                echo "La clave es: ${MYSQL_ROOT_PASSWORD}"
260                return 0
261        else
262                echo "Las claves no coinciden no se configura la clave del servidor de base de datos."
263                echo "las operaciones con la base de datos daran error"
264                return 1
265        fi
266
267
268}
269
[a01156a]270# comprueba si puede conectar con mysql con el usuario root
271function mysqlTestConnection()
272{
273        if [ $# -ne 1 ]; then
274                errorAndLog "mysqlTestConnection(): invalid number of parameters"
275                exit 1
276        fi
277
278        local root_password="${1}"
279        echoAndLog "mysqlTestConnection(): checking connection to mysql..."
280        echo "" | mysql -uroot -p"${root_password}"
281        if [ $? -ne 0 ]; then
282                errorAndLog "mysqlTestConnection(): connection to mysql failed, check root password and if daemon is running!"
283                return 1
284        else
285                echoAndLog "mysqlTestConnection(): connection success"
286                return 0
287        fi
288}
289
290# comprueba si la base de datos existe
291function mysqlDbExists()
292{
293        if [ $# -ne 2 ]; then
294                errorAndLog "mysqlDbExists(): invalid number of parameters"
295                exit 1
296        fi
297
298        local root_password="${1}"
299        local database=$2
300        echoAndLog "mysqlDbExists(): checking if $database exists..."
301        echo "show databases" | mysql -uroot -p"${root_password}" | grep "^${database}$"
302        if [ $? -ne 0 ]; then
303                echoAndLog "mysqlDbExists():database $database doesn't exists"
304                return 1
305        else
306                echoAndLog "mysqlDbExists():database $database exists"
307                return 0
308        fi
309}
310
311function mysqlCheckDbIsEmpty()
312{
313        if [ $# -ne 2 ]; then
314                errorAndLog "mysqlCheckDbIsEmpty(): invalid number of parameters"
315                exit 1
316        fi
317
318        local root_password="${1}"
319        local database=$2
320        echoAndLog "mysqlCheckDbIsEmpty(): checking if $database is empty..."
321        num_tablas=`echo "show tables" | mysql -uroot -p"${root_password}" "${database}" | wc -l`
322        if [ $? -ne 0 ]; then
323                errorAndLog "mysqlCheckDbIsEmpty(): error executing query, check database and root password"
324                exit 1
325        fi
326
327        if [ $num_tablas -eq 0 ]; then
328                echoAndLog "mysqlCheckDbIsEmpty():database $database is empty"
329                return 0
330        else
331                echoAndLog "mysqlCheckDbIsEmpty():database $database has tables"
332                return 1
333        fi
334
335}
336
337
338function mysqlImportSqlFileToDb()
339{
340        if [ $# -ne 3 ]; then
341                errorAndLog "mysqlImportSqlFileToDb(): invalid number of parameters"
342                exit 1
343        fi
344
345        local root_password="${1}"
346        local database=$2
347        local sqlfile=$3
348
349        if [ ! -f $sqlfile ]; then
350                errorAndLog "mysqlImportSqlFileToDb(): Unable to locate $sqlfile!!"
351                return 1
352        fi
353
354        echoAndLog "mysqlImportSqlFileToDb(): importing sql file to ${database}..."
355        mysql -uroot -p"${root_password}" "${database}" < $sqlfile
356        if [ $? -ne 0 ]; then
357                errorAndLog "mysqlImportSqlFileToDb(): error while importing $sqlfile in database $database"
358                return 1
359        fi
360        echoAndLog "mysqlImportSqlFileToDb(): file imported to database $database"
361        return 0
362}
363
364# Crea la base de datos
365function mysqlCreateDb()
366{
367        if [ $# -ne 2 ]; then
[a555f49]368                errorAndLog "${FUNCNAME}(): invalid number of parameters"
[a01156a]369                exit 1
370        fi
371
372        local root_password="${1}"
373        local database=$2
374
[a555f49]375        echoAndLog "${FUNCNAME}(): creating database..."
[a01156a]376        mysqladmin -u root --password="${root_password}" create $database
377        if [ $? -ne 0 ]; then
[a555f49]378                errorAndLog "${FUNCNAME}(): error while creating database $database"
[a01156a]379                return 1
380        fi
[a555f49]381        echoAndLog "${FUNCNAME}(): database $database created"
[a01156a]382        return 0
383}
384
385
386function mysqlCheckUserExists()
387{
388        if [ $# -ne 2 ]; then
389                errorAndLog "mysqlCheckUserExists(): invalid number of parameters"
390                exit 1
391        fi
392
393        local root_password="${1}"
394        local userdb=$2
395
396        echoAndLog "mysqlCheckUserExists(): checking if $userdb exists..."
397        echo "select user from user where user='${userdb}'\\G" |mysql -uroot -p"${root_password}" mysql | grep user
398        if [ $? -ne 0 ]; then
399                echoAndLog "mysqlCheckUserExists(): user doesn't exists"
400                return 1
401        else
402                echoAndLog "mysqlCheckUserExists(): user already exists"
403                return 0
404        fi
405
406}
407
408# Crea un usuario administrativo para la base de datos
409function mysqlCreateAdminUserToDb()
410{
411        if [ $# -ne 4 ]; then
412                errorAndLog "mysqlCreateAdminUserToDb(): invalid number of parameters"
413                exit 1
414        fi
415
416        local root_password=$1
417        local database=$2
418        local userdb=$3
419        local passdb=$4
420
421        echoAndLog "mysqlCreateAdminUserToDb(): creating admin user ${userdb} to database ${database}"
422
423        cat > $WORKDIR/create_${database}.sql <<EOF
424GRANT USAGE ON *.* TO '${userdb}'@'localhost' IDENTIFIED BY '${passdb}' ;
425GRANT ALL PRIVILEGES ON ${database}.* TO '${userdb}'@'localhost' WITH GRANT OPTION ;
426FLUSH PRIVILEGES ;
427EOF
428        mysql -u root --password=${root_password} < $WORKDIR/create_${database}.sql
429        if [ $? -ne 0 ]; then
430                errorAndLog "mysqlCreateAdminUserToDb(): error while creating user in mysql"
431                rm -f $WORKDIR/create_${database}.sql
432                return 1
433        else
434                echoAndLog "mysqlCreateAdminUserToDb(): user created ok"
435                rm -f $WORKDIR/create_${database}.sql
436                return 0
437        fi
438}
439
440
441#####################################################################
442####### Funciones para el manejo de Subversion
443#####################################################################
444
[7586ca3]445function svnCheckoutCode()
[a01156a]446{
[9fb5086]447        if [ $# -ne 1 ]; then
[13a01a7]448                errorAndLog "${FUNCNAME}(): invalid number of parameters"
[a01156a]449                exit 1
450        fi
451
452        local url=$1
453
[13a01a7]454        echoAndLog "${FUNCNAME}(): downloading subversion code..."
[a01156a]455
[892606b9]456        /usr/bin/svn co "${url}" opengnsys
[a01156a]457        if [ $? -ne 0 ]; then
[13a01a7]458                errorAndLog "${FUNCNAME}(): error getting code from ${url}, verify your user and password"
[a01156a]459                return 1
460        fi
[13a01a7]461        echoAndLog "${FUNCNAME}(): subversion code downloaded"
[a01156a]462        return 0
463}
464
[13a01a7]465
466function svnExportCode()
467{
468        if [ $# -ne 1 ]; then
469                errorAndLog "${FUNCNAME}(): invalid number of parameters"
470                exit 1
471        fi
472
473        local url=$1
474
475        echoAndLog "${FUNCNAME}(): downloading subversion code..."
476
477        /usr/bin/svn export "${url}" opengnsys
478        if [ $? -ne 0 ]; then
479                errorAndLog "${FUNCNAME}(): error getting code from ${url}, verify your user and password"
480                return 1
481        fi
482        echoAndLog "${FUNCNAME}(): subversion code downloaded"
483        return 0
484}
485
486
[892606b9]487############################################################
[7586ca3]488###  Detectar red
489############################################################
490
491function getNetworkSettings()
492{
493        # Variables globales definidas:
494        # - SERVERIP: IP local del servidor.
495        # - NETIP:    IP de la red.
496        # - NETMASK:  máscara de red.
497        # - NETBROAD: IP de difusión de la red.
498        # - ROUTERIP: IP del router.
499        # - DNSIP:    IP del servidor DNS.
500
501        echoAndLog "getNetworkSettings(): Detecting default network parameters."
[10023eb]502        SERVERIP=$(LANG=C ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | head -n1 | awk '{print $1}')
503        NETMASK=$(LANG=C ifconfig | grep 'Mask:'| grep -v '127.0.0.1' | cut -d: -f4 | head -n1 | awk '{print $1}')
504        NETBROAD=$(LANG=C ifconfig | grep 'Bcast:'| grep -v '127.0.0.1' | cut -d: -f3 | head -n1 | awk '{print $1}')
[13a01a7]505        NETIP=$(netstat -r | grep $NETMASK | head -n1 | awk '{print $1}')
[7586ca3]506        ROUTERIP=$(netstat -nr | awk '$1~/0\.0\.0\.0/ {print $2}')
[a555f49]507        DNSIP=$(awk '/nameserver/ {print $2}' /etc/resolv.conf | head -n1)
[7586ca3]508        if [ -z "$NETIP" -o -z "$NETMASK" ]; then
509                errorAndLog "getNetworkSettings(): Network not detected."
510                exit 1
511        fi
[cc7eab7]512
513        # Variables de ejecución de Apache
514        # - APACHE_RUN_USER
515        # - APACHE_RUN_GROUP
516        if [ -f /etc/apache2/envvars ]; then
517                source /etc/apache2/envvars
518        fi
519        APACHE_RUN_USER=${APACHE_RUN_USER:-"www-data"}
520        APACHE_RUN_GROUP=${APACHE_RUN_GROUP:-"www-data"}
[7586ca3]521}
522
523
524############################################################
[892606b9]525### Esqueleto para el Servicio pxe y contenedor tftpboot ###
526############################################################
527
528function tftpConfigure() {
529        echo "Configurando el servicio tftp"
530        basetftp=/var/lib/tftpboot
531
532        # reiniciamos demonio internet ????? porque ????
533        /etc/init.d/openbsd-inetd start
534
535        # preparacion contenedor tftpboot
536        cp -pr /usr/lib/syslinux/ ${basetftp}/syslinux
537        cp /usr/lib/syslinux/pxelinux.0 ${basetftp}
538        # prepamos el directorio de la configuracion de pxe
539        mkdir -p ${basetftp}/pxelinux.cfg
540        cat > ${basetftp}/pxelinux.cfg/default <<EOF
541DEFAULT pxe
542
543LABEL pxe
544KERNEL linux
545APPEND initrd=initrd.gz ip=dhcp ro vga=788 irqpoll acpi=on
546EOF
547        # comprobamos el servicio tftp
548        sleep 1
549        testPxe
550        ## damos perfimos de lectura a usuario web.
[cc7eab7]551        chown -R $APACHE_RUN_USER:$APACHE_RUN_GROUP ${basetftp}
[892606b9]552}
553
554function testPxe () {
555        cd /tmp
556        echo "comprobando servicio pxe ..... Espere"
557        tftp -v localhost -c get pxelinux.0 /tmp/pxelinux.0 && echo "servidor tftp OK" || echo "servidor tftp KO"
558        cd /
559}
560
561########################################################################
[b6906f7]562## Configuracion servicio NFS
[892606b9]563########################################################################
564
[13a01a7]565# function nfsConfigure ()
566# {
567#         echoAndLog "${FUNCNAME}(): Sample NFS Configuration."
568#         sed -e "s/NETIP/$NETIP/g" -e "s/NETMASK/$NETMASK/g" $WORKDIR/opengnsys/server/NFS/exports  >> /etc/exports
569#       /etc/init.d/nfs-kernel-server restart
570#       exportfs -va
571#         echoAndLog "${FUNCNAME}(): Sample NFS Configured in file \"/etc/exports\"."
572# }
573
574# ADVERTENCIA: usa variables globales NETIP y NETMASK!
575function nfsConfigure()
[7586ca3]576{
[13a01a7]577        echoAndLog "${FUNCNAME}(): Config nfs server."
578
579        backupFile /etc/exports
580
581        nfsAddExport /opt/opengnsys/client ${NETIP}/${NETMASK}:ro,no_subtree_check,no_root_squash,sync
582        if [ $? -ne 0 ]; then
583                errorAndLog "${FUNCNAME}(): error while adding nfs client config"
584                return 1
585        fi
586
587        nfsAddExport /opt/opengnsys/images ${NETIP}/${NETMASK}:rw,no_subtree_check,no_root_squash,sync,crossmnt
588        if [ $? -ne 0 ]; then
589                errorAndLog "${FUNCNAME}(): error while adding nfs images config"
590                return 1
591        fi
592
593        nfsAddExport /opt/opengnsys/log/clients ${NETIP}/${NETMASK}:rw,no_subtree_check,no_root_squash,sync
594        if [ $? -ne 0 ]; then
595                errorAndLog "${FUNCNAME}(): error while adding logging client config"
596                return 1
597        fi
598
[7586ca3]599        /etc/init.d/nfs-kernel-server restart
[13a01a7]600
[b6906f7]601        exportfs -va
[13a01a7]602        if [ $? -ne 0 ]; then
603                errorAndLog "${FUNCNAME}(): error while configure exports"
604                return 1
605        fi
606
607        echoAndLog "${FUNCNAME}(): Added NFS configuration to file \"/etc/exports\"."
608        return 0
[b6906f7]609}
[892606b9]610
[b6906f7]611
[13a01a7]612# ejemplos:
613#nfsAddExport /opt/opengnsys 192.168.0.0/255.255.255.0:ro,no_subtree_check,no_root_squash,sync
614#nfsAddExport /opt/opengnsys 192.168.0.0/255.255.255.0
615#nfsAddExport /opt/opengnsys 80.20.2.1:ro 192.123.32.2:rw
616function nfsAddExport()
617{
618        if [ $# -lt 2 ]; then
619                errorAndLog "${FUNCNAME}(): invalid number of parameters"
620                exit 1
621        fi
622
623        if [ ! -f /etc/exports ]; then
624                errorAndLog "${FUNCNAME}(): /etc/exports don't exists"
625                return 1
626        fi
627
628        local export="${1}"
629        local contador=0
630        local cadenaexport
631
632        grep "^${export}" /etc/exports > /dev/null
633        if [ $? -eq 0 ]; then
634                echoAndLog "${FUNCNAME}(): $export exists in /etc/exports, omiting"
635                return 0
636        fi
637
638        cadenaexport="${export}"
639        for parametro in $*
640        do
641                if [ $contador -gt 0 ]
642                then
643                        host=`echo $parametro | awk -F: '{print $1}'`
644                        options=`echo $parametro | awk -F: '{print $2}'`
645                        if [ "${host}" == "" ]; then
646                                errorAndLog "${FUNCNAME}(): host can't be empty"
647                                return 1
648                        fi
649                        cadenaexport="${cadenaexport}\t${host}"
650
651                        if [ "${options}" != "" ]; then
652                                cadenaexport="${cadenaexport}(${options})"
653                        fi
654                fi
655                let contador=contador+1
656        done
657
658        echo -en "$cadenaexport\n" >> /etc/exports
659
660        echoAndLog "${FUNCNAME}(): add $export to /etc/exports"
661
662        return 0
663}
664
[b6906f7]665########################################################################
666## Configuracion servicio DHCP
667########################################################################
668
[7586ca3]669function dhcpConfigure()
670{
[a555f49]671        echoAndLog "${FUNCNAME}(): Sample DHCP Configuration."
672
673        backupFile /etc/dhcp3/dhcpd.conf
674
[7586ca3]675        sed -e "s/SERVERIP/$SERVERIP/g" \
676            -e "s/NETIP/$NETIP/g" \
677            -e "s/NETMASK/$NETMASK/g" \
678            -e "s/NETBROAD/$NETBROAD/g" \
679            -e "s/ROUTERIP/$ROUTERIP/g" \
680            -e "s/DNSIP/$DNSIP/g" \
681            $WORKDIR/opengnsys/server/DHCP/dhcpd.conf > /etc/dhcp3/dhcpd.conf
[a555f49]682        if [ $? -ne 0 ]; then
683                errorAndLog "${FUNCNAME}(): error while configuring dhcp server"
684                return 1
685        fi
686
[7586ca3]687        /etc/init.d/dhcp3-server restart
[a555f49]688        echoAndLog "${FUNCNAME}(): Sample DHCP Configured in file \"/etc/dhcp3/dhcpd.conf\"."
689        return 0
[892606b9]690}
691
692
[a01156a]693#####################################################################
694####### Funciones específicas de la instalación de Opengnsys
695#####################################################################
696
[7586ca3]697# Copiar ficheros del OpenGNSys Web Console.
698function installWebFiles()
699{
700        echoAndLog "installWebFiles(): Installing web files..."
701        cp -ar $WORKDIR/opengnsys/admin/WebConsole/* $INSTALL_TARGET/www   #*/ comentario para doxigen
702        if [ $? != 0 ]; then
703                errorAndLog "installWebFiles(): Error copying web files."
704                exit 1
705        fi
706        find $INSTALL_TARGET/www -name .svn -type d -exec rm -fr {} \; 2>/dev/null
707        # Cambiar permisos para ficheros especiales.
[cc7eab7]708        chown -R $APACHE_RUN_USER:$APACHE_RUN_GROUP \
709                        $INSTALL_TARGET/www/includes \
710                        $INSTALL_TARGET/www/comandos/gestores/filescripts \
711                        $INSTALL_TARGET/www/images/icons
[7586ca3]712        echoAndLog "installWebFiles(): Web files installed successfully."
713}
714
715# Configuración específica de Apache.
[892606b9]716function openGnsysInstallWebConsoleApacheConf()
[a01156a]717{
718        if [ $# -ne 2 ]; then
[892606b9]719                errorAndLog "openGnsysInstallWebConsoleApacheConf(): invalid number of parameters"
[a01156a]720                exit 1
721        fi
722
723        local path_opengnsys_base=$1
724        local path_apache2_confd=$2
[892606b9]725        local path_web_console=${path_opengnsys_base}/www
[a01156a]726
[892606b9]727        if [ ! -d $path_apache2_confd ]; then
728                errorAndLog "openGnsysInstallWebConsoleApacheConf(): path to apache2 conf.d can not found, verify your server installation"
729                return 1
730        fi
731
[7586ca3]732        mkdir -p $path_apache2_confd/{sites-available,sites-enabled}
[892606b9]733
734        echoAndLog "openGnsysInstallWebConsoleApacheConf(): creating apache2 config file.."
[a01156a]735
736        # genera configuración
[892606b9]737        cat > $path_opengnsys_base/etc/apache.conf <<EOF
[cc7eab7]738# OpenGNSys Web Console configuration for Apache
[a01156a]739
[892606b9]740Alias /opengnsys ${path_web_console}
[a01156a]741
[892606b9]742<Directory ${path_web_console}>
[a01156a]743        Options -Indexes FollowSymLinks
744        DirectoryIndex acceso.php
745</Directory>
746EOF
747
[7586ca3]748        ln -fs $path_opengnsys_base/etc/apache.conf $path_apache2_confd/sites-available/opengnsys.conf
749        ln -fs $path_apache2_confd/sites-available/opengnsys.conf $path_apache2_confd/sites-enabled/opengnsys.conf
[a01156a]750        if [ $? -ne 0 ]; then
[892606b9]751                errorAndLog "openGnsysInstallWebConsoleApacheConf(): config file can't be linked to apache conf, verify your server installation"
[a01156a]752                return 1
753        else
[077dd7c5]754                echoAndLog "openGnsysInstallWebConsoleApacheConf(): config file created and linked, restarting apache daemon"
755                /etc/init.d/apache2 restart
[a01156a]756                return 0
757        fi
758}
759
760# Crea la estructura base de la instalación de opengnsys
[318efac]761function openGnsysInstallCreateDirs()
[a01156a]762{
763        if [ $# -ne 1 ]; then
764                errorAndLog "openGnsysInstallCreateDirs(): invalid number of parameters"
765                exit 1
766        fi
767
768        local path_opengnsys_base=$1
769
770        echoAndLog "openGnsysInstallCreateDirs(): creating directory paths in $path_opengnsys_base"
771
772        mkdir -p $path_opengnsys_base
[077dd7c5]773        mkdir -p $path_opengnsys_base/admin/{autoexec,comandos,menus,usuarios}
[a01156a]774        mkdir -p $path_opengnsys_base/bin
[5c33840]775        mkdir -p $path_opengnsys_base/client
776        mkdir -p $path_opengnsys_base/etc
[a01156a]777        mkdir -p $path_opengnsys_base/lib
[1cc5697]778        mkdir -p $path_opengnsys_base/log/clients
[7586ca3]779        mkdir -p $path_opengnsys_base/sbin
[a01156a]780        mkdir -p $path_opengnsys_base/www
[5c33840]781        mkdir -p $path_opengnsys_base/images
[b6906f7]782        ln -fs /var/lib/tftpboot $path_opengnsys_base
783        ln -fs $path_opengnsys_base/log /var/log/opengnsys
[5c33840]784
[a01156a]785        if [ $? -ne 0 ]; then
786                errorAndLog "openGnsysInstallCreateDirs(): error while creating dirs. Do you have write permissions?"
787                return 1
788        fi
789
790        echoAndLog "openGnsysInstallCreateDirs(): directory paths created"
791        return 0
792}
793
[463a1d49]794# Copia ficheros de configuración y ejecutables genéricos del servidor.
[318efac]795function openGnsysCopyServerFiles () {
[463a1d49]796        if [ $# -ne 1 ]; then
797                errorAndLog "openGnsysCopyServerFiles(): invalid number of parameters"
798                exit 1
799        fi
800
801        local path_opengnsys_base=$1
802
[892606b9]803        local SOURCES=( client/boot/initrd-generator \
[b6906f7]804                        client/boot/upgrade-clients-udeb.sh \
[a555f49]805                        client/boot/udeblist.conf  \
806                        client/boot/udeblist-jaunty.conf  \
807                        client/boot/udeblist-karmic.conf )
[892606b9]808        local TARGETS=( bin/initrd-generator \
[b6906f7]809                        bin/upgrade-clients-udeb.sh \
[a555f49]810                        etc/udeblist.conf \
811                        etc/udeblist-jaunty.conf  \
812                        etc/udeblist-karmic.conf )
[463a1d49]813
814        if [ ${#SOURCES[@]} != ${#TARGETS[@]} ]; then
815                errorAndLog "openGnsysCopyServerFiles(): inconsistent number of array items"
816                exit 1
817        fi
818
[7586ca3]819        echoAndLog "openGnsysCopyServerFiles(): copying files to server directories"
[f2bb433]820
[7586ca3]821        pushd $WORKDIR/opengnsys
[463a1d49]822        local i
823        for (( i = 0; i < ${#SOURCES[@]}; i++ )); do
824                if [ -f "${SOURCES[$i]}" ]; then
[f2bb433]825                        echoAndLog "copying ${SOURCES[$i]} to $path_opengnsys_base/${TARGETS[$i]}"
[463a1d49]826                        cp -p "${SOURCES[$i]}" "${path_opengnsys_base}/${TARGETS[$i]}"
827                fi
828                if [ -d "${SOURCES[$i]}" ]; then
829                        echoAndLog "openGnsysCopyServerFiles(): copying content of ${SOURCES[$i]} to $path_opengnsys_base/${TARGETS[$i]}"
[7586ca3]830                        cp -ar "${SOURCES[$i]}/*" "${path_opengnsys_base}/${TARGETS[$i]}"
[463a1d49]831                fi
832        done
[7586ca3]833        popd
[892606b9]834}
835
[7586ca3]836####################################################################
837### Funciones de compilación de códifo fuente de servicios
838####################################################################
839
[b6906f7]840# Compilar los servicios de OpenGNsys
[7586ca3]841function servicesCompilation ()
842{
[13a01a7]843        local hayErrores=0
844       
[b6906f7]845        # Compilar OpenGNSys Server
[13a01a7]846        echoAndLog "${FUNCNAME}(): Compiling OpenGNSys Admin Server"
[318efac]847        pushd $WORKDIR/opengnsys/admin/Services/ogAdmServer
[b6906f7]848        make && make install
[13a01a7]849        if [ $? -ne 0 ]; then
850                echoAndLog "${FUNCNAME}(): error while compiling OpenGNSys Admin Server"
851                hayErrores=1
852        fi
[7586ca3]853        popd
[b6906f7]854        # Compilar OpenGNSys Repository Manager
[13a01a7]855        echoAndLog "${FUNCNAME}(): Compiling OpenGNSys Repository Manager"
[318efac]856        pushd $WORKDIR/opengnsys/admin/Services/ogAdmRepo
[b6906f7]857        make && make install
[13a01a7]858        if [ $? -ne 0 ]; then
859                echoAndLog "${FUNCNAME}(): error while compiling OpenGNSys Repository Manager"
860                hayErrores=1
861        fi
[8125e7c]862        popd
[b6906f7]863        # Compilar OpenGNSys Client
864        echoAndLog "servicesCompilation(): Compiling OpenGNSys Admin Client"
[318efac]865        pushd $WORKDIR/opengnsys/admin/Services/ogAdmClient
866        make && mv ogAdmClient ../../../client/nfsexport/bin
[13a01a7]867        if [ $? -ne 0 ]; then
868                echoAndLog "${FUNCNAME}(): error while compiling OpenGNSys Admin Client"
869                hayErrores=1
870        fi
[318efac]871        popd
[13a01a7]872
873        return $hayErrores
[b6906f7]874}
875
876
[892606b9]877####################################################################
878### Funciones instalacion cliente opengnsys
879####################################################################
880
[a555f49]881function openGnsysClientCreate()
[7586ca3]882{
883        local OSDISTRIB OSCODENAME
[892606b9]884
[a555f49]885        local hayErrores=0
886
887        echoAndLog "${FUNCNAME}(): Copying OpenGNSys Client files."
[892606b9]888        cp -ar $WORKDIR/opengnsys/client/nfsexport/* $INSTALL_TARGET/client
[b6906f7]889        find $INSTALL_TARGET/client -name .svn -type d -exec rm -fr {} \; 2>/dev/null
[23388bd5]890        chmod +x $INSTALL_TARGET/client/admin/scripts/*
[a555f49]891        echoAndLog "${FUNCNAME}(): Copying OpenGNSys Cloning Engine files."
[b6906f7]892        mkdir -p $INSTALL_TARGET/client/lib/engine/bin
[892606b9]893        cp -ar $WORKDIR/opengnsys/client/engine/*.lib $INSTALL_TARGET/client/lib/engine/bin
[a555f49]894        if [ $? -ne 0 ]; then
895                errorAndLog "${FUNCNAME}(): error while copying engine files"
896                hayErrores=1
897        fi
[892606b9]898
[cc7eab7]899        # Cargar Kernel, Initrd y paquetes udeb para la distribución del servidor (o por defecto).
[d345fa6]900        OSDISTRIB=$(lsb_release -i | awk -F: '{sub(/\t/,""); print $2}') 2>/dev/null
901        OSCODENAME=$(lsb_release -c | awk -F: '{sub(/\t/,""); print $2}') 2>/dev/null
[7586ca3]902        if [ "$OSDISTRIB" = "Ubuntu" -a -n "$OSCODENAME" ]; then
[a555f49]903                echoAndLog "${FUNCNAME}(): Loading Kernel and Initrd files for $OSDISTRIB $OSCODENAME."
[7586ca3]904                $INSTALL_TARGET/bin/initrd-generator -t $INSTALL_TARGET/tftpboot -v "$OSCODENAME"
[a555f49]905                if [ $? -ne 0 ]; then
906                        errorAndLog "${FUNCNAME}(): error while generating initrd OpenGNSys Admin Client"
907                        hayErrores=1
908                fi
909                echoAndLog "${FUNCNAME}(): Loading udeb files for $OSDISTRIB $OSCODENAME."
[7586ca3]910                $INSTALL_TARGET/bin/upgrade-clients-udeb.sh "$OSCODENAME"
[a555f49]911                if [ $? -ne 0 ]; then
912                        errorAndLog "${FUNCNAME}(): error while upgrading udeb files OpenGNSys Admin Client"
913                        hayErrores=1
914                fi
[7586ca3]915        else
[a555f49]916                echoAndLog "${FUNCNAME}(): Loading default Kernel and Initrd files."
[7586ca3]917                $INSTALL_TARGET/bin/initrd-generator -t $INSTALL_TARGET/tftpboot/
[a555f49]918                if [ $? -ne 0 ]; then
919                        errorAndLog "${FUNCNAME}(): error while generating initrd OpenGNSys Admin Client"
920                        hayErrores=1
921                fi
922                echoAndLog "${FUNCNAME}(): Loading default udeb files."
[7586ca3]923                $INSTALL_TARGET/bin/upgrade-clients-udeb.sh
[a555f49]924                if [ $? -ne 0 ]; then
925                        errorAndLog "${FUNCNAME}(): error while upgrading udeb files OpenGNSys Admin Client"
926                        hayErrores=1
927                fi
928        fi
929
930        if [ $hayErrores -eq 0 ]; then
931                echoAndLog "${FUNCNAME}(): Client generation success."
932        else
933                errorAndLog "${FUNCNAME}(): Client generation with errors"
[7586ca3]934        fi
[a555f49]935
936        return $hayErrores
[463a1d49]937}
938
939
[cc7eab7]940# Configuración básica de servicios de OpenGNSys
941function openGnsysConfigure()
942{
943        echoAndLog "openGnsysConfigure(): Copying init files."
944        cp -p $WORKDIR/opengnsys/admin/Services/opengnsys.init /etc/init.d/opengnsys
945        cp -p $WORKDIR/opengnsys/admin/Services/opengnsys.default /etc/default/opengnsys
946        update-rc.d opengnsys defaults
947        echoAndLog "openGnsysConfigure(): Creating OpenGNSys config file in \"$INSTALL_TARGET/etc\"."
[1a22cd2]948        perl -pi -e "s/SERVERIP/$SERVERIP/g" $INSTALL_TARGET/etc/ogAdmServer.cfg
949        perl -pi -e "s/SERVERIP/$SERVERIP/g" $INSTALL_TARGET/etc/ogAdmRepo.cfg
950        sed -e "s/SERVERIP/$SERVERIP/g" $WORKDIR/opengnsys/admin/Services/ogAdmClient/ogAdmClient.cfg > $INSTALL_TARGET/client/etc/ogAdmClient.cfg
951        echoAndLog "openGnsysConfigure(): Creating Web Console config file"
[077dd7c5]952        perl -pi -e "s/SERVERIP/$SERVERIP/g; s/OPENGNSYSURL/http:\/\/$SERVERIP\/opengnsys/g" $INSTALL_TARGET/www/controlacceso.php
[180a07dd]953        OPENGNSYS_CONSOLEURL=$(awk -F\" '$1~/\$wac/ {print $2}' $INSTALL_TARGET/www/controlacceso.php)
[cc7eab7]954        echoAndLog "openGnsysConfiguration(): Starting OpenGNSys services."
955        /etc/init.d/opengnsys start
956}
957
[b6906f7]958
[a01156a]959#####################################################################
[180a07dd]960#######  Función de resumen informativo de la instalación
961#####################################################################
962
963function installationSummary(){
964        echo
965        echoAndLog "OpenGNSys Installation Summary"
966        echo       "=============================="
967        echoAndLog "OpenGNSys installation directory: $INSTALL_TARGET"
968        echoAndLog "OpenGNSys repository directory:   $INSTALL_TARGET/images"
969        echoAndLog "TFTP configuracion directory:     /var/lib/tftpboot"
970        echoAndLog "DHCP configuracion file:          /etc/dhcp3/dhcpd.conf"
971        echoAndLog "NFS configuracion file:           /etc/exports"
972        echoAndLog "OpenGNSys Web Console URL:        $OPENGNSYS_CONSOLEURL"
973        echoAndLog "Web Console admin user:           $OPENGNSYS_DB_USER"
974        echoAndLog "Web Console admin password:       $OPENGNSYS_DB_PASSWD"
975        echo
976        echoAndLog "Post-Installation Instructions:"
977        echo       "==============================="
978        echoAndLog "Review or edit all configuration files."
979        echoAndLog "Log-in as Web Console admin user."
980        echoAndLog " - Insert Organization data and Organization user."
981        echoAndLog "Log-in as Web Console organization user."
982        echoAndLog " - Insert OpenGNSys Server configuration data."
983        echoAndLog "Restart OpenGNSys services."
984echo
985}
986
987
988
989#####################################################################
[b6906f7]990####### Proceso de instalación de OpenGNSys
[a01156a]991#####################################################################
992
[7586ca3]993
[cc7eab7]994echoAndLog "OpenGNSys installation begins at $(date)"
995
[7586ca3]996# Detectar parámetros de red por defecto
997getNetworkSettings
998if [ $? -ne 0 ]; then
999        errorAndLog "Error reading default network settings."
1000        exit 1
1001fi
1002
[318efac]1003# Actualizar repositorios
1004apt-get update
1005
[b6906f7]1006# Instalación de dependencias (paquetes de sistema operativo).
[a01156a]1007declare -a notinstalled
1008checkDependencies DEPENDENCIES notinstalled
1009if [ $? -ne 0 ]; then
1010        installDependencies notinstalled
1011        if [ $? -ne 0 ]; then
1012                echoAndLog "Error while installing some dependeces, please verify your server installation before continue"
1013                exit 1
1014        fi
1015fi
1016
[b6906f7]1017# Arbol de directorios de OpenGNSys.
[a01156a]1018openGnsysInstallCreateDirs ${INSTALL_TARGET}
1019if [ $? -ne 0 ]; then
1020        errorAndLog "Error while creating directory paths!"
1021        exit 1
1022fi
[b6906f7]1023
1024# Descarga del repositorio de código en directorio temporal
[13a01a7]1025#svnCheckoutCode $SVN_URL
1026svnExportCode $SVN_URL
[a01156a]1027if [ $? -ne 0 ]; then
1028        errorAndLog "Error while getting code from svn"
1029        exit 1
1030fi
1031
[b6906f7]1032# Compilar código fuente de los servicios de OpenGNSys.
1033servicesCompilation
[13a01a7]1034if [ $? -ne 0 ]; then
1035        errorAndLog "Error while compiling OpenGnsys services"
1036        exit 1
1037fi
[b6906f7]1038
1039# Configurando tftp
[318efac]1040tftpConfigure
[b6906f7]1041
1042# Configuración NFS
1043nfsConfigure
[13a01a7]1044if [ $? -ne 0 ]; then
1045        errorAndLog "Error while configuring nfs server!"
1046        exit 1
1047fi
[b6906f7]1048
1049# Configuración ejemplo DHCP
1050dhcpConfigure
[a555f49]1051if [ $? -ne 0 ]; then
1052        errorAndLog "Error while copying your dhcp server files!"
1053        exit 1
1054fi
[b6906f7]1055
1056# Copiar ficheros de servicios OpenGNSys Server.
[463a1d49]1057openGnsysCopyServerFiles ${INSTALL_TARGET}
1058if [ $? -ne 0 ]; then
1059        errorAndLog "Error while copying the server files!"
1060        exit 1
1061fi
1062
[b6906f7]1063# Instalar Base de datos de OpenGNSys Admin.
1064isInArray notinstalled "mysql-server"
1065if [ $? -eq 0 ]; then
1066        mysqlSetRootPassword ${MYSQL_ROOT_PASSWORD}
1067else
1068        mysqlGetRootPassword
1069
1070fi
[463a1d49]1071
[a01156a]1072mysqlTestConnection ${MYSQL_ROOT_PASSWORD}
1073if [ $? -ne 0 ]; then
1074        errorAndLog "Error while connection to mysql"
1075        exit 1
1076fi
[892606b9]1077mysqlDbExists ${MYSQL_ROOT_PASSWORD} ${OPENGNSYS_DATABASE}
[a01156a]1078if [ $? -ne 0 ]; then
[cc7eab7]1079        echoAndLog "Creating Web Console database"
[892606b9]1080        mysqlCreateDb ${MYSQL_ROOT_PASSWORD} ${OPENGNSYS_DATABASE}
[a01156a]1081        if [ $? -ne 0 ]; then
[cc7eab7]1082                errorAndLog "Error while creating Web Console database"
[a01156a]1083                exit 1
1084        fi
1085else
[cc7eab7]1086        echoAndLog "Web Console database exists, ommiting creation"
[a01156a]1087fi
1088
[892606b9]1089mysqlCheckUserExists ${MYSQL_ROOT_PASSWORD} ${OPENGNSYS_DB_USER}
[a01156a]1090if [ $? -ne 0 ]; then
1091        echoAndLog "Creating user in database"
[892606b9]1092        mysqlCreateAdminUserToDb ${MYSQL_ROOT_PASSWORD} ${OPENGNSYS_DATABASE} ${OPENGNSYS_DB_USER} "${OPENGNSYS_DB_PASSWD}"
[a01156a]1093        if [ $? -ne 0 ]; then
[cc7eab7]1094                errorAndLog "Error while creating database user"
[a01156a]1095                exit 1
1096        fi
1097
1098fi
1099
[892606b9]1100mysqlCheckDbIsEmpty ${MYSQL_ROOT_PASSWORD} ${OPENGNSYS_DATABASE}
[a01156a]1101if [ $? -eq 0 ]; then
1102        echoAndLog "Creating tables..."
[892606b9]1103        if [ -f $WORKDIR/$OPENGNSYS_DB_CREATION_FILE ]; then
1104                mysqlImportSqlFileToDb ${MYSQL_ROOT_PASSWORD} ${OPENGNSYS_DATABASE} $WORKDIR/$OPENGNSYS_DB_CREATION_FILE
[a01156a]1105        else
[892606b9]1106                errorAndLog "Unable to locate $WORKDIR/$OPENGNSYS_DB_CREATION_FILE!!"
[a01156a]1107                exit 1
1108        fi
1109fi
1110
1111# copiando paqinas web
[7586ca3]1112installWebFiles
[a01156a]1113
1114# creando configuracion de apache2
[892606b9]1115openGnsysInstallWebConsoleApacheConf $INSTALL_TARGET /etc/apache2
[a01156a]1116if [ $? -ne 0 ]; then
[7586ca3]1117        errorAndLog "Error configuring Apache for OpenGNSYS Admin"
[a01156a]1118        exit 1
1119fi
1120
1121popd
[892606b9]1122
1123# Creando la estructura del cliente
1124openGnsysClientCreate
[a555f49]1125if [ $? -ne 0 ]; then
1126        errorAndLog "Error creating clients"
1127        exit 1
1128fi
[892606b9]1129
[cc7eab7]1130# Configuración de servicios de OpenGNSys
1131openGnsysConfigure
[2308fc7]1132
[180a07dd]1133# Mostrar sumario de la instalación e instrucciones de post-instalación.
1134installationSummary
1135
[077dd7c5]1136#rm -rf $WORKDIR
[cc7eab7]1137echoAndLog "OpenGNSys installation finished at $(date)"
[2308fc7]1138
Note: See TracBrowser for help on using the repository browser.