summaryrefslogtreecommitdiffstats
path: root/opengnsys-installer/opengnsys_installer.sh
blob: 266b04c6d7bb5b89f18da34fa6c7b43bf5d79abc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
#!/bin/bash

#####################################################################
####### Script instalador OpenGnsys
####### autor: Luis Guillén <lguillen@unizar.es>
#####################################################################


WORKDIR=/tmp/opengnsys_installer
LOG_FILE=$WORKDIR/installation.log

# Array con las dependencias
DEPENDENCIES=( 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 )

INSTALL_TARGET=/opt/opengnsys

MYSQL_ROOT_PASSWORD="passwordroot"

# conexión al svn
SVN_URL=svn://www.informatica.us.es:3690/eac-hidra
SVN_USER=lguillen
SVN_PASSWORD=potupass

# Datos de base de datos
HIDRA_DATABASE=bdhidra
HIDRA_DB_USER=usuhidra
HIDRA_DB_PASSWD=passusuhidra
HIDRA_DB_CREATION_FILE=eac-hidra/branches/eac-hidra-us/Hidra/doc/hidra-bd.sql

USUARIO=`whoami`

if [ $USUARIO != 'root' ]
then
        echo "ERROR: this program must run under root privileges!!"
        exit 1
fi


mkdir -p $WORKDIR
pushd $WORKDIR

#####################################################################
####### Algunas funciones útiles de propósito general:
#####################################################################
getDateTime()
{
        echo `date +%Y%m%d-%H%M%S`
}

# Escribe a fichero y muestra por pantalla
echoAndLog()
{
        echo $1
        FECHAHORA=`getDateTime`
        echo "$FECHAHORA;$SSH_CLIENT;$1" >> $LOG_FILE
}

errorAndLog()
{
        echo "ERROR: $1"
        FECHAHORA=`getDateTime`
        echo "$FECHAHORA;$SSH_CLIENT;ERROR: $1" >> $LOG_FILE
}

# comprueba si el elemento pasado en $2 esta en el array $1
isInArray()
{
	if [ $# -ne 2 ]; then
		errorAndLog "isInArray(): invalid number of parameters"
		exit 1
	fi

	echoAndLog "isInArray(): checking if $2 is in $1"
	local deps
	eval "deps=( \"\${$1[@]}\" )"
	elemento=$2

	local is_in_array=1
	# copia local del array del parametro 1
	for (( i = 0 ; i < ${#deps[@]} ; i++ ))
	do
		if [ "${deps[$i]}" = "${elemento}" ]; then
			echoAndLog "isInArray(): $elemento found in array"
			is_in_array=0
		fi
	done

	if [ $is_in_array -ne 0 ]; then
		echoAndLog "isInArray(): $elemento NOT found in array"
	fi

	return $is_in_array

}

#####################################################################
####### Funciones de manejo de paquetes Debian
#####################################################################

checkPackage()
{
	package=$1
	if [ -z $package ]; then
		errorAndLog "checkPackage(): parameter required"
		exit 1
	fi
	echoAndLog "checkPackage(): checking if package $package exists"
	dpkg -L $package &>/dev/null
	if [ $? -eq 0 ]; then
		echoAndLog "checkPackage(): package $package exists"
		return 0
	else
		echoAndLog "checkPackage(): package $package doesn't exists"
		return 1
	fi
}

# recibe array con dependencias
# por referencia deja un array con las dependencias no resueltas
# devuelve 1 si hay alguna dependencia no resuelta
checkDependencies()
{
	if [ $# -ne 2 ]; then
		errorAndLog "checkDependencies(): invalid number of parameters"
		exit 1
	fi

	echoAndLog "checkDependencies(): checking dependences"
	uncompletedeps=0

	# copia local del array del parametro 1
	local deps
	eval "deps=( \"\${$1[@]}\" )"

	declare -a local_notinstalled

	for (( i = 0 ; i < ${#deps[@]} ; i++ ))
	do
		checkPackage ${deps[$i]}
		if [ $? -ne 0 ]; then
			local_notinstalled[$uncompletedeps]=$package
			let uncompletedeps=uncompletedeps+1
		fi
	done

	# relleno el array especificado en $2 por referencia
	for (( i = 0 ; i < ${#local_notinstalled[@]} ; i++ ))
	do
		eval "${2}[$i]=${local_notinstalled[$i]}"
	done

	# retorna el numero de paquetes no resueltos
	echoAndLog "checkDependencies(): dependencies uncompleted: $uncompletedeps"
	return $uncompletedeps
}

# Recibe un array con las dependencias y lo instala
installDependencies()
{
	if [ $# -ne 1 ]; then
		errorAndLog "installDependencies(): invalid number of parameters"
		exit 1
	fi
	echoAndLog "installDependencies(): installing uncompleted dependencies"

	# copia local del array del parametro 1
	local deps
	eval "deps=( \"\${$1[@]}\" )"

	local string_deps=""
	for (( i = 0 ; i < ${#deps[@]} ; i++ ))
	do
		string_deps="$string_deps ${deps[$i]}"
	done

	if [ -z "${string_deps}" ]; then
		errorAndLog "installDependencies(): array of dependeces is empty"
		exit 1
	fi

	OLD_DEBIAN_FRONTEND=$DEBIAN_FRONTEND
	export DEBIAN_FRONTEND=noninteractive

	echoAndLog "installDependencies(): now ${string_deps} will be installed"
	apt-get -y install --force-yes ${string_deps}
	if [ $? -ne 0 ]; then
		errorAndLog "installDependencies(): error installing dependencies"
		return 1
	fi

	DEBIAN_FRONTEND=$OLD_DEBIAN_FRONTEND
	echoAndLog "installDependencies(): dependencies installed"
}

#####################################################################
####### Funciones para el manejo de bases de datos
#####################################################################

# This function set password to root
mysqlSetRootPassword()
{
	if [ $# -ne 1 ]; then
		errorAndLog "mysqlSetRootPassword(): invalid number of parameters"
		exit 1
	fi

	local root_mysql=$1
	echoAndLog "mysqlSetRootPassword(): setting root password in MySQL server"
	/usr/bin/mysqladmin -u root password ${root_mysql}
	if [ $? -ne 0 ]; then
		errorAndLog "mysqlSetRootPassword(): error while setting root password in MySQL server"
		return 1
	fi
	echoAndLog "mysqlSetRootPassword(): root password saved!"
	return 0
}

# comprueba si puede conectar con mysql con el usuario root
function mysqlTestConnection()
{
	if [ $# -ne 1 ]; then
		errorAndLog "mysqlTestConnection(): invalid number of parameters"
		exit 1
	fi

	local root_password="${1}"
	echoAndLog "mysqlTestConnection(): checking connection to mysql..."
	echo "" | mysql -uroot -p"${root_password}"
	if [ $? -ne 0 ]; then
		errorAndLog "mysqlTestConnection(): connection to mysql failed, check root password and if daemon is running!"
		return 1
	else
		echoAndLog "mysqlTestConnection(): connection success"
		return 0
	fi
}

# comprueba si la base de datos existe
function mysqlDbExists()
{
	if [ $# -ne 2 ]; then
		errorAndLog "mysqlDbExists(): invalid number of parameters"
		exit 1
	fi

	local root_password="${1}"
	local database=$2
	echoAndLog "mysqlDbExists(): checking if $database exists..."
	echo "show databases" | mysql -uroot -p"${root_password}" | grep "^${database}$"
	if [ $? -ne 0 ]; then
		echoAndLog "mysqlDbExists():database $database doesn't exists"
		return 1
	else
		echoAndLog "mysqlDbExists():database $database exists"
		return 0
	fi
}

function mysqlCheckDbIsEmpty()
{
	if [ $# -ne 2 ]; then
		errorAndLog "mysqlCheckDbIsEmpty(): invalid number of parameters"
		exit 1
	fi

	local root_password="${1}"
	local database=$2
	echoAndLog "mysqlCheckDbIsEmpty(): checking if $database is empty..."
	num_tablas=`echo "show tables" | mysql -uroot -p"${root_password}" "${database}" | wc -l`
	if [ $? -ne 0 ]; then
		errorAndLog "mysqlCheckDbIsEmpty(): error executing query, check database and root password"
		exit 1
	fi

	if [ $num_tablas -eq 0 ]; then
		echoAndLog "mysqlCheckDbIsEmpty():database $database is empty"
		return 0
	else
		echoAndLog "mysqlCheckDbIsEmpty():database $database has tables"
		return 1
	fi

}


function mysqlImportSqlFileToDb()
{
	if [ $# -ne 3 ]; then
		errorAndLog "mysqlImportSqlFileToDb(): invalid number of parameters"
		exit 1
	fi

	local root_password="${1}"
	local database=$2
	local sqlfile=$3

	if [ ! -f $sqlfile ]; then
		errorAndLog "mysqlImportSqlFileToDb(): Unable to locate $sqlfile!!"
		return 1
	fi

	echoAndLog "mysqlImportSqlFileToDb(): importing sql file to ${database}..."
	mysql -uroot -p"${root_password}" "${database}" < $sqlfile
	if [ $? -ne 0 ]; then
		errorAndLog "mysqlImportSqlFileToDb(): error while importing $sqlfile in database $database"
		return 1
	fi
	echoAndLog "mysqlImportSqlFileToDb(): file imported to database $database"
	return 0
}

# Crea la base de datos
function mysqlCreateDb()
{
	if [ $# -ne 2 ]; then
		errorAndLog "mysqlCreateDb(): invalid number of parameters"
		exit 1
	fi

	local root_password="${1}"
	local database=$2

	echoAndLog "mysqlCreateDb(): creating database..."
	mysqladmin -u root --password="${root_password}" create $database
	if [ $? -ne 0 ]; then
		errorAndLog "mysqlCreateDb(): error while creating database $database"
		return 1
	fi
	errorAndLog "mysqlCreateDb(): database $database created"
	return 0
}


function mysqlCheckUserExists()
{
	if [ $# -ne 2 ]; then
		errorAndLog "mysqlCheckUserExists(): invalid number of parameters"
		exit 1
	fi

	local root_password="${1}"
	local userdb=$2

	echoAndLog "mysqlCheckUserExists(): checking if $userdb exists..."
	echo "select user from user where user='${userdb}'\\G" |mysql -uroot -p"${root_password}" mysql | grep user
	if [ $? -ne 0 ]; then
		echoAndLog "mysqlCheckUserExists(): user doesn't exists"
		return 1
	else
		echoAndLog "mysqlCheckUserExists(): user already exists"
		return 0
	fi

}

# Crea un usuario administrativo para la base de datos
function mysqlCreateAdminUserToDb()
{
	if [ $# -ne 4 ]; then
		errorAndLog "mysqlCreateAdminUserToDb(): invalid number of parameters"
		exit 1
	fi

	local root_password=$1
	local database=$2
	local userdb=$3
	local passdb=$4

	echoAndLog "mysqlCreateAdminUserToDb(): creating admin user ${userdb} to database ${database}"

	cat > $WORKDIR/create_${database}.sql <<EOF
GRANT USAGE ON *.* TO '${userdb}'@'localhost' IDENTIFIED BY '${passdb}' ;
GRANT ALL PRIVILEGES ON ${database}.* TO '${userdb}'@'localhost' WITH GRANT OPTION ;
FLUSH PRIVILEGES ;
EOF
	mysql -u root --password=${root_password} < $WORKDIR/create_${database}.sql
	if [ $? -ne 0 ]; then
		errorAndLog "mysqlCreateAdminUserToDb(): error while creating user in mysql"
		rm -f $WORKDIR/create_${database}.sql
		return 1
	else
		echoAndLog "mysqlCreateAdminUserToDb(): user created ok"
		rm -f $WORKDIR/create_${database}.sql
		return 0
	fi
}


#####################################################################
####### Funciones para el manejo de Subversion
#####################################################################

svnCheckoutCode()
{
	if [ $# -lt 2 ]; then
		errorAndLog "svnCheckoutCode(): invalid number of parameters"
		exit 1
	fi

	local url=$1
	local user=$2
	if [ $# -gt 2 ]; then
		local password=$3
	else
		local password=""
	fi

	echoAndLog "svnCheckoutCode(): downloading subversion code..."

	/usr/bin/svn co "${url}" --username "${user}" --password "${password}"
	if [ $? -ne 0 ]; then
		errorAndLog "svnCheckoutCode(): error getting code from ${url}, verify your user and password"
		return 1
	fi
	echoAndLog "svnCheckoutCode(): subversion code downloaded"
	return 0
}

#####################################################################
####### Funciones específicas de la instalación de Opengnsys
#####################################################################

function openGnsysInstallHidraApacheConf()
{
	if [ $# -ne 2 ]; then
		errorAndLog "openGnsysInstallHidraApacheConf(): invalid number of parameters"
		exit 1
	fi

	local path_opengnsys_base=$1
	local path_apache2_confd=$2
	local path_web_hidra=${path_opengnsys_base}/www

	echoAndLog "openGnsysInstallHidraApacheConf(): creating apache2 config file.."

	# genera configuración
	cat > $WORKDIR/apache.conf <<EOF
# Hidra web interface configuration for Apache

Alias /hidra ${path_web_hidra}

<Directory ${path_web_hidra}>
	Options -Indexes FollowSymLinks
	DirectoryIndex acceso.php
</Directory>
EOF

	if [ ! -d $path_apache2_confd ]; then
		errorAndLog "openGnsysInstallHidraApacheConf(): path to apache2 conf.d can not found, verify your server installation"
		rm -f $WORKDIR/apache.conf
		return 1
	fi
	cp $WORKDIR/apache.conf $path_opengnsys_base/etc
	ln -s $path_opengnsys_base/etc/apache.conf $path_apache2_confd/hidra.conf
	if [ $? -ne 0 ]; then
		errorAndLog "openGnsysInstallHidraApacheConf(): config file can't be linked to apache conf, verify your server installation"
		rm -f $WORKDIR/apache.conf
		return 1
	else
		echoAndLog "openGnsysInstallHidraApacheConf(): config file created and linked, restart your apache daemon"
		rm -f $WORKDIR/apache.conf
		return 0
	fi
}

# Crea la estructura base de la instalación de opengnsys
openGnsysInstallCreateDirs()
{
	if [ $# -ne 1 ]; then
		errorAndLog "openGnsysInstallCreateDirs(): invalid number of parameters"
		exit 1
	fi

	local path_opengnsys_base=$1

	echoAndLog "openGnsysInstallCreateDirs(): creating directory paths in $path_opengnsys_base"

	mkdir -p $path_opengnsys_base
	mkdir -p $path_opengnsys_base/bin
	mkdir -p $path_opengnsys_base/client
	mkdir -p $path_opengnsys_base/etc
	mkdir -p $path_opengnsys_base/lib
	mkdir -p $path_opengnsys_base/log
	mkdir -p $path_opengnsys_base/www
	mkdir -p $path_opengnsys_base/tftpboot/ogclients
	mkdir -p $path_opengnsys_base/images

	if [ $? -ne 0 ]; then
		errorAndLog "openGnsysInstallCreateDirs(): error while creating dirs. Do you have write permissions?"
		return 1
	fi

	echoAndLog "openGnsysInstallCreateDirs(): directory paths created"
	return 0
}

#####################################################################
####### Proceso de instalación
#####################################################################


# Proceso de instalación de opengnsys
declare -a notinstalled
checkDependencies DEPENDENCIES notinstalled
if [ $? -ne 0 ]; then
	installDependencies notinstalled
	if [ $? -ne 0 ]; then
		echoAndLog "Error while installing some dependeces, please verify your server installation before continue"
		exit 1
	fi
fi

isInArray notinstalled "mysql-server"
if [ $? -eq 0 ]; then
	mysqlSetRootPassword ${MYSQL_ROOT_PASSWORD}
fi

openGnsysInstallCreateDirs ${INSTALL_TARGET}
if [ $? -ne 0 ]; then
	errorAndLog "Error while creating directory paths!"
	exit 1
fi

svnCheckoutCode $SVN_URL $SVN_USER $SVN_PASSWORD
if [ $? -ne 0 ]; then
	errorAndLog "Error while getting code from svn"
	exit 1
fi

mysqlTestConnection ${MYSQL_ROOT_PASSWORD}
if [ $? -ne 0 ]; then
	errorAndLog "Error while connection to mysql"
	exit 1
fi
mysqlDbExists ${MYSQL_ROOT_PASSWORD} ${HIDRA_DATABASE}
if [ $? -ne 0 ]; then
	echoAndLog "Creating hidra database"
	mysqlCreateDb ${MYSQL_ROOT_PASSWORD} ${HIDRA_DATABASE}
	if [ $? -ne 0 ]; then
		errorAndLog "Error while creating hidra database"
		exit 1
	fi
else
	echoAndLog "Hidra database exists, ommiting creation"
fi

mysqlCheckUserExists ${MYSQL_ROOT_PASSWORD} ${HIDRA_DB_USER}
if [ $? -ne 0 ]; then
	echoAndLog "Creating user in database"
	mysqlCreateAdminUserToDb ${MYSQL_ROOT_PASSWORD} ${HIDRA_DATABASE} ${HIDRA_DB_USER} "${HIDRA_DB_PASS}"
	if [ $? -ne 0 ]; then
		errorAndLog "Error while creating hidra user"
		exit 1
	fi

fi

mysqlCheckDbIsEmpty ${MYSQL_ROOT_PASSWORD} ${HIDRA_DATABASE}
if [ $? -eq 0 ]; then
	echoAndLog "Creating tables..."
	if [ -f $WORKDIR/$HIDRA_DB_CREATION_FILE ]; then
		mysqlImportSqlFileToDb ${MYSQL_ROOT_PASSWORD} ${HIDRA_DATABASE} $WORKDIR/$HIDRA_DB_CREATION_FILE
	else
		errorAndLog "Unable to locate $WORKDIR/$HIDRA_DB_CREATION_FILE!!"
		exit 1
	fi
fi

echoAndLog "Installing web files..."
# copiando paqinas web
cp -pr eac-hidra/branches/eac-hidra-us/Hidra/webhidra/* $INSTALL_TARGET/www   #*/ comentario para doxigen

# creando configuracion de apache2
openGnsysInstallHidraApacheConf $INSTALL_TARGET /etc/apache2/conf.d
if [ $? -ne 0 ]; then
	errorAndLog "Error while creating hidra apache config"
	exit 1
fi

popd
#rm -rf $WORKDIR
echoAndLog "Process finalized!"



function TestPxe () {
	cd /tmp
	echo "comprobando servidio pxe ..... Espere"
	tftp -v localhost -c get pxelinux.0 /tmp/pxelinux.0 && echo "servidor tftp OK" || echo "servidor tftp KO"
	cd /
}

function preparacontenedortft() {
############################################################
### Esqueleto para el Servicio pxe y contenedor tftpboot ##############
###########################################################
	basetftp=/var/lib/tftpboot
	basetftpaux=/tftpboot
	basetftpog=/opt/opengnsys/tftpboot
	# creamos los correspondientes enlaces hacia nuestro contenedor.
	ln -s ${basetftp} ${basetftpog}
	ln -s ${basetftpaux} ${basetftpog}

	# reiniciamos demonio internet
	/etc/init.d/openbsd-inetd start

	##preparcion contendor tftpboot
	cp -pr /usr/lib/syslinux/ ${basetftpboot}/syslinux
	cp /usr/lib/syslinux/pxelinux.0 $basetftpboot
	# prepamos el directorio de la configuracion de pxe
	mkdir -p ${basetftpboot}/pxelinux.cfg
	touch ${basetftpboot}/pxelinux.cfg/default
	# comprobamos el servicio tftp
	sleep 1
	TestPxe
	## damos perfimos de lectura a usuario web.
	chown -R www-data:www-data /var/lib/tftpboot
	######### fin revisar2 contenedor tftp
}