瀏覽代碼

Añadimos el sistema de comprobación de errores

Guzmán Castanedo Villalba 5 年之前
父節點
當前提交
c484872eb1
共有 1 個文件被更改,包括 176 次插入179 次删除
  1. 176 179
      install

+ 176 - 179
install

@@ -9,8 +9,133 @@
 #################################################################
 
 comprobarError() {
-	# Mejora: tratar todos los mensajes de error desde aquí
-	printf "SIN HACER...\n"
+	# comprobarError exitNum codeNum extraInfo
+	#	1xx: Servidor Web
+	#	2xx: Base de Datos
+	#	3xx: PHP
+	#	4xx: SSL/TLS
+	#	5xx: MediaWiki
+	#	6xx: Moodle
+	#	7xx: Actualizaciones automáticas
+	#	8xx: Dependencias
+	#	9xx: Servicios
+	type="ERROR"
+	exitNum=$1
+	codeNum=$2
+	extraInfo=$3
+	case $codeNum in
+		1)
+			error="$type $codeNum:\tInstalación interrumpida por el usuario.\n"
+			;;
+		2)
+			error="$type $codeNum:\tError interno (seleción express).\n"
+			;;
+		3)
+			error="$type $codeNum:\tError interno (seleción avanzada).\n"
+			;;
+		100)
+			error="$type $codeNum:\tError interno (seleción del servidor web).\n"
+			;;
+		101)
+			error="\n$type $codeNum:\tError al instalar Apache2.\nDetalles:\n$extraInfo\n"
+			;;
+		102)
+			error="\n$type $codeNum:\tError al instalar Nginx.\nDetalles:\n$extraInfo\n"
+			;;
+		103)
+			error="\n$type $codeNum:\tError al nstalar repositorio 'epel-release'.\nDetalles:\n$extraInfo\n"
+			;;
+		104)
+			error="\n$type $codeNum:\tError interno (instalación Apache2).\n"
+			;;
+		105)
+			error="\n$type $codeNum:\tError interno (instalación Nginx).\n"
+			;;
+		200)
+			error="$type $codeNum:\tError interno (seleción de base de datos).\n"
+			;;
+		201)
+			error="\n$type $codeNum:\tError al instalar MySQL.\nDetalles:\n$extraInfo\n"
+			;;
+		202)
+			error="\n$type $codeNum:\tError al instalar MariaDB.\nDetalles:\n$extraInfo\n"
+			;;
+		203)
+			error="\n$type $codeNum:\tError interno (instalación MySQL).\n"
+			;;
+		204)
+			error="\n$type $codeNum:\tError interno (instalación MariaDB).\n"
+			;;
+		205)
+			error="\n$type $codeNum:\t$DIST no incluye MySQL en sus repositorios.\n"
+			;;
+		206)
+			type="WARNING"
+			error="$type $codeNum:\tUsuario 'root' ya dispone de contraseña.\n"
+			;;
+		207)
+			error="$type $codeNum:\tImposible cambiar la contraseña de 'root'.\n"
+			;;
+		208)
+			error="$type $codeNum:\tImposible desactivar acceso 'root' desde el exterior.\n"
+			;;
+		209)
+			error="$type $codeNum:\tImposible eliminar usuarios anónimos.\n"
+			;;
+		210)
+			type="WARNING"
+			error="$type $codeNum:\tImposible eliminar bases de datos de pruebas.\n"
+			;;
+		211)
+			type="WARNING"
+			error="$type $codeNum:\tImposible eliminar los permisos de las bases de datos de pruebas.\n"
+			;;
+		212)
+			error="$type $codeNum:\tImposible recargar base de datos.\n"
+			;;
+		801)
+			error="$type $codeNum:\t'whiptail' no instalado.\n"
+			;;
+		802)
+			error="$type $codeNum:\t'hostnamectl' no instalado.\n"
+			;;
+		803)
+			error="$type $codeNum:\t'apt-get' no instalado.\n"
+			;;
+		804)
+			error="$type $codeNum:\t'yum' no instalado.\n"
+			;;
+		805)
+			error="\n$type $codeNum:\tImposible actualizar repositorio\nDetalles:\n$extraInfo.\n"
+			;;
+		806)
+			error="$type $codeNum:\t'ufw' no instalado.\n"
+			;;
+		807)
+			error="$type $codeNum:\t'firewall-cmd' no instalado.\n"
+			;;
+		900)
+			error="$type $codeNum:\tError interno (habilitar servicio).\n"
+			;;
+		901)
+			error="$type $codeNum:\tImposible encender servicio '$extraInfo'.\n"
+			;;
+		902)
+			error="$type $codeNum:\tImposible habilitar servicio '$extraInfo' durante el arranque.\n"
+			;;
+		*)
+			error="ERROR 13:\tError interno (comprobación de errores)\n"
+			exitNum=1
+			codeNum=13
+			;;
+	esac
+	if [ $exitNum -ne 0 ];then
+		printf "$error"
+		if [ $type = "ERROR" ];then
+			exit $codeNum
+		fi
+	fi
+	unset error exitNum codeNum type
 }
 
 OSInfo() {
@@ -76,61 +201,35 @@ comprobarRoot() {
 comprobarDependencias() {
 	# Comprobamos whiptail
 	which whiptail > /dev/null 2>&1
-	if [ $? -ne 0 ];then
-		printf "ERROR:\t'whiptail' no disponible.\n"
-		exit 1
-	fi
+	comprobarError $? 801
 	# Comprobamos hostnamectl
 	which hostnamectl > /dev/null 2>&1
-	if [ $? -ne 0 ];then
-		printf "ERROR:\t'hostnamectl' no disponible.\n"
-		exit 1
-	fi
+	comprobarError $? 802
 	if [ $debianOS = true ];then
 		# Comprobamos apt-get
 		which apt-get > /dev/null 2>&1
-		if [ $? -ne 0 ]; then
-			printf "ERROR:\t'apt-get' no está disponible.\n"
-			exit 1
-		fi
+		comprobarError $? 803
 		# Actualizamos base de datos del repositorio
 		printf "Actualizando repositorio APT..."
 		result=$(apt-get -q -y update)
-		if [ $? -ne 0 ]; then
-			printf "\nERROR:\tImposible actualizar repositorio.\n"
-			printf "Detalles:\n$result\n"
-			exit 1
-		fi
+		comprobarError $? 805 $result
 		printf " OK.\n"
 		# Comprobamos Firewall (ufw)
 		which ufw > /dev/null 2>&1
-		if [ $? -ne 0 ]; then
-			printf "ERROR:\t'ufw' no disponible.\n"
-			exit 1
-		fi
+		comprobarError $? 806
 	fi
 	if [ $rhelOS = true ]; then
 		# Comprobamos yum
 		which yum > /dev/null 2>&1
-		if [ $? -ne 0 ]; then
-			printf "ERROR:\t'yum' no está disponible.\n"
-			exit 1
-		fi
+		comprobarError $? 804
 		# Actualizamos base de datos del repositorio
 		printf "Actualizando repositorio YUM..."
 		result=$(yum -y makecache 2>&1)
-		if [ $? -ne 0 ]; then
-			printf "\nERROR:\tImposible actualizar repositorio.\n"
-			printf "Detalles:\n$result\n"
-			exit 1
-		fi
+		comprobarError $? 805 $result
 		printf " OK.\n"
 		# Comprobamos Firewall (firewall-cmd)
 		which firewall-cmd > /dev/null 2>&1
-		if [ $? -ne 0 ]; then
-			printf "ERROR:\t'firewall-cmd' no disponible.\n"
-			exit 1
-		fi
+		comprobarError $? 807
 	fi
 }
 
@@ -162,10 +261,7 @@ mostrarBienvenida() {
 		exit 1
 	fi
 	whiptail --title "INSTALACION MEDIAWIKI" --yesno "Este script automatiza completamente la instalación de una wiki.\nPara ello instala un servidor LAMP, el software MediaWiki y lo configura todo.\n\nInformación del sistema:\nOS: $OS $DIST $REV\n$HDInfo" 20 70 --yes-button "Continuar" --no-button "Salir"
-	if [ $? -ne 0 ]; then
-		printf "ERROR:\tInstalación interrumpida por el usuario.\n"
-		exit 2
-	fi
+	comprobarError $? 1
 }
 
 mostrarExpress() {
@@ -173,10 +269,7 @@ mostrarExpress() {
 	"Express" "Instalación rápida" ON \
 	"Avanzada" "Permite escoger todas las opciones disponibles" OFF \
 	--ok-button "Continuar" --cancel-button "Salir" 3>&1 1>&2 2>&3)
-	if [ $? -ne 0 ]; then
-		printf "ERROR:\tInstalación interrumpida por el usuario.\n"
-		exit 2
-	fi
+	comprobarError $? 1
 	case $express in
 		Express)
 			instalacionExpress
@@ -186,8 +279,7 @@ mostrarExpress() {
 			mostrarAvanzada
 			;;
 		*)
-			printf "ERROR:\tError interno (selección express).\n"
-			exit 1
+			comprobarError 1 2
 			;;
 	esac
 }
@@ -202,10 +294,7 @@ mostrarAvanzada() {
 	"Moodle" "Instalar campus virtual con Moodle" ON \
 	"Actualizaciones" "Programar actualizaciones automáticas" ON \
 	--ok-button "Continuar" --cancel-button "Salir" 3>&1 1>&2 2>&3)
-	if [ $? -ne 0 ]; then
-		printf "ERROR:\tInstalación interrumpida por el usuario.\n"
-		exit 2
-	fi
+	comprobarError $? 1
 	# Mejora: autodetección de componentes ya instalados
 	for i in $componentes; do
 		case $i in
@@ -231,8 +320,7 @@ mostrarAvanzada() {
 				actualizacionesOn=true
 				;;
 			*)
-				printf "ERROR:\tError interno (selección de componentes).\n"
-				exit 1
+				comprobarError 1 3
 				;;
 		esac
 	done
@@ -243,10 +331,7 @@ mostrarWebServer() {
 	"Apache" "Instalar el servidor web Apache2" ON \
 	"Nginx" "Instalar el servidor web Nginx" OFF \
 	--ok-button "Continuar" --cancel-button "Salir" 3>&1 1>&2 2>&3)
-	if [ $? -ne 0 ]; then
-		printf "ERROR:\tInstalación interrumpida por el usuario.\n"
-		exit 2
-	fi
+	comprobarError $? 1
 	case $webServer in
 		Apache)
 			apacheOn=true
@@ -255,8 +340,7 @@ mostrarWebServer() {
 			nginxOn=true
 			;;
 		*)
-			printf "ERROR:\tError interno (selección de web server).\n"
-			exit 1
+			comprobarError 1 100
 			;;
 	esac
 	# Otras opciones (FQDN)
@@ -278,49 +362,27 @@ establecerFQDN() {
 instalarApache() {
 	if [ $debianOS = true ];then
 		result=$(apt-get -q -y install apache2 2>&1)
-		if [ $? -ne 0 ]; then
-			printf "\nERROR:\tError al instalar Apache2.\n"
-			printf "Detalles:\n$result\n"
-			exit 1
-		fi
+		comprobarError $? 101 $result
 	elif [ $rhelOS = true ]; then
 		result=$(yum -y install httpd 2>&1)
-		if [ $? -ne 0 ]; then
-			printf "\nERROR:\tError al instalar Apache2.\n"
-			printf "Detalles:\n$result\n"
-			exit 1
-		fi
+		comprobarError $? 101 $result
 	else
-		printf "\nERROR:\tError interno (instalación Apache2).\n"
-		exit 1
+		comprobarError 1 104
 	fi
 }
 
 instalarNginx() {
 	if [ $debianOS = true ];then
 		result=$(apt-get -q -y install nginx 2>&1)
-		if [ $? -ne 0 ]; then
-			printf "\nERROR:\tError al instalar Nginx.\n"
-			printf "Detalles:\n$result\n"
-			exit 1
-		fi
+		comprobarError $? 102 $result
 	elif [ $rhelOS = true ]; then
 		# Hay que instalar primero otro repositorio
 		result=$(yum -y install epel-release)
-		if [ $? -ne 0 ]; then
-			printf "\nERROR:\tError al intalar repositorio 'epel-release'.\n"
-			printf "Detalles:\n$result\n"
-			exit 1
-		fi
+		comprobarError $? 103 $result
 		result=$(yum -y install nginx 2>&1)
-		if [ $? -ne 0 ]; then
-			printf "\nERROR:\tError al instalar Nginx.\n"
-			printf "Detalles:\n$result\n"
-			exit 1
-		fi
+		comprobarError $? 102 $result
 	else
-		printf "\nERROR:\tError interno (instalación Nginx).\n"
-		exit 1
+		comprobarError 1 105
 	fi
 }
 
@@ -330,10 +392,7 @@ mostrarDatabase() {
 		"MySQL" "Instalar la base de datos MySQL (no uso comercial)" ON \
 		"MariaDB" "Instalar la base de datos MariaDB (fork de MySQL)" OFF \
 		--ok-button "Continuar" --cancel-button "Salir" 3>&1 1>&2 2>&3)
-		if [ $? -ne 0 ]; then
-			printf "ERROR:\tInstalación interrumpida por el usuario.\n"
-			exit 2
-		fi
+		comprobarError $? 1
 		case $database in
 			MySQL)
 				mySQLOn=true
@@ -342,20 +401,15 @@ mostrarDatabase() {
 				mariaDBOn=true
 				;;
 			*)
-				printf "ERROR:\tError interno (selección de base de datos).\n"
-				exit 1
+				comprobarError 1 200
 				;;
 		esac
 	elif [ $rhelOS = true ]; then
 		whiptail --title "BASE DE DATOS" --yesno "Para su distribucion $OS $DIST $REV, sólo está disponible la base de datos MariaDB." 20 70 --yes-button "Continuar" --no-button "Salir"
-		if [ $? -ne 0 ]; then
-			printf "ERROR:\tInstalación interrumpida por el usuario.\n"
-			exit 2
-		fi
+		comprobarError $? 1
 		mariaDBOn=true
 	else
-		printf "ERROR:\tError interno (instalación Base de Datos).\n"
-		exit 1
+		comprobarError 1 200
 	fi
 	# Otras opciones (contraseña)
 	leerSQLPasswd
@@ -367,15 +421,9 @@ leerSQLPasswd() {
 	# Leemos la contreseña (stdin) y confirmamos
 	while [ $control = false ]; do
 		sqlPasswd=$(whiptail --title "CONTRASEÑA SQL" --passwordbox "$error""Introduzca la contraseña para el usuario 'root' de la base de datos:" 20 70 --ok-button "Continuar" --nocancel 3>&1 1>&2 2>&3)
-		if [ $? -ne 0 ]; then
-			printf "ERROR:\tInstalación interrumpida por el usuario.\n"
-			exit 2
-		fi
+		comprobarError $? 1
 		sqlPasswd2=$(whiptail --title "CONTRASEÑA SQL" --passwordbox "Confirme la contraseña:" 20 70 --ok-button "Continuar" --nocancel 3>&1 1>&2 2>&3)
-		if [ $? -ne 0 ];then
-			printf "ERROR:\tInstalación interrumpida por el usuario.\n"
-			exit 2
-		fi
+		comprobarError $? 1
 		#printf "SQL Password 1: $sqlPasswd\n"
 		#printf "SQL Password 2: $sqlPasswd2\n"
 		if [ -z $sqlPasswd ] || [ -z $sqlPasswd2 ]; then
@@ -397,81 +445,50 @@ establecerSQLPasswd() {
 	if [ $? -eq 0 ];then
 		# Establecemos contraseña del usuario root
 		mysql -e "UPDATE mysql.user SET Password = PASSWORD('$sqlPasswd') WHERE User = 'root'" > /dev/null 2>&1
-		if [ $? -ne 0 ];then
-			printf "SQL ERROR:\tImposible de cambiar la contraseña de 'root'.\n"
-			exit 3
-		fi
+		comprobarError $? 207
 		# Desactivamos acceso root desde el exterior (solo localhost)
 		mysql -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1')" > /dev/null 2>&1
-		if [ $? -ne 0 ];then
-			printf "SQL ERROR:\tImposible desactivar acceso 'root' desde el exterior.\n"
-			exit 3
-		fi
+		comprobarError $? 208
 		# Eliminamos todos los usuarios anónimos
 		mysql -e "DELETE FROM mysql.user WHERE User=''" > /dev/null 2>&1
-		if [ $? -ne 0 ];then
-			printf "SQL ERROR:\tImposible eliminar usuarios anónimos.\n"
-			exit 3
-		fi
+		comprobarError $? 209
 		# Eliminamos bases de datos 'test'
 		mysql -e "DROP DATABASE IF EXISTS test" > /dev/null 2>&1
-		if [ $? -ne 0 ];then
-			printf "SQL WARNING:\tImposible eliminar bases de datos de pruebas.\n"
-		fi
+		comprobarError $? 210
 		# Eliminamos privilegios de la base de datos 'test'
 		mysql -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%'" > /dev/null 2>&1
-		if [ $? -ne 0 ];then
-			printf "SQL WARNING:\tImposible eliminar los privilegios de la base de datos de pruebas.\n"
-		fi
+		comprobarError $? 211
 		# Aplicamos los cambios
 		mysql -e "FLUSH PRIVILEGES" > /dev/null 2>&1
-		if [ $? -ne 0 ];then
-			printf "SQL ERROR:\tImposible recargar base de datos.\n"
-			exit 3
-		fi
+		comprobarError $? 212
 	else
-		printf "WARNING:\tUsuario 'root' ya dispone de contraseña.\n"
+		comprobarError $? 206
 	fi
 }
 
 instalarMySQL() {
 	if [ $debianOS = true ];then
 		result=$(apt-get -q -y install mysql-server mysql-client 2>&1)
-		if [ $? -ne 0 ]; then
-			printf "\nERROR:\tError al instalar MySQL.\n"
-			printf "Detalles:\n$result\n"
-			exit 1
-		fi
+		comprobarError $? 201 $result
 	elif [ $rhelOS = true ]; then
 		# MySQL no disponible en RHEL. 2 opciones:
 		# 1) Instalar un repositorio adicional
 		# 2) No instalar MySQL en distribuciones RHEL
-		printf "\nERROR:\tRHEL no incluye MySQL en sus repositorios.\n"
-		exit 1
+		comprobarError 1 205
 	else
-		printf "\nERROR:\tError interno (instalación MySQL).\n"
-		exit 1
+		comprobarError 1 203
 	fi
 }
 
 instalarMariaDB() {
 	if [ $debianOS = true ];then
 		result=$(apt-get -q -y install mariadb-server mariadb-client 2>&1)
-		if [ $? -ne 0 ]; then
-			printf "\nERROR:\tError al instalar MariaDB.\n"
-			printf "Detalles:\n$result\n"
-			exit 1
-		fi
+		comprobarError $? 202 $result
 	elif [ $rhelOS = true ]; then
 		result=$(yum -y install mariadb-server mariadb 2>&1)
-		if [ $? -ne 0 ]; then
-			printf "\nERROR:\tError al instalar MariaDB.\n"
-			printf "Detalles:\n$result\n"
-			exit 1
-		fi
+		comprobarError $? 202 $result
 	else
-		printf "\nERROR:\tError interno (instalación MariaDB).\n"
-		exit 1
+		comprobarError 1 204
 	fi
 }
 
@@ -480,62 +497,42 @@ habilitarServicio() {
 	# Intentamos con systemctl (SystemD)
 	printf "Habilitando servicio $1"
 	if [ $# -le 0 ];then
-		printf "\nERROR:\tError interno (habilitar servicio).\n"
-		exit 1
+		comprobarError 1 900
 	fi
 	which systemctl > /dev/null 2>&1
 	if [ $? -eq 0 ]; then
 		printf " (SystemD)..."
 		systemctl start $1 > /dev/null 2>&1
-		if [ $? -ne 0 ];then
-			printf "\nERROR:\tImposible encender servicio '$1'.\n"
-			exit 1
-		fi
+		comprobarError $? 901 $1
 		systemctl enable $1 > /dev/null 2>&1
-		if [ $? -ne 0 ];then
-			printf "\nERROR:\tImposible habilitar servicio '$1' durante el arranque.\n"
-			exit 1
-		fi
+		comprobarError $? 902 $1
 	else
 		# Intentamos con service (Upstart)
 		which service > /dev/null 2>&1
 		if [ $? -eq 0 ]; then
 			printf " (Upstart)..."
 			service $1 start > /dev/null 2>&1
-			if [ $? -ne 0 ];then
-				printf "\nERROR:\tImposible encender el servicio '$1'.\n"
-				exit 1
-			fi
+			comprobarError $? 901 $1
 		else
 			# Intentamos con init.d (SystemV)
 			printf " (SystemV)..."
 			/etc/init.d/$1 start > /dev/null 2>&1
-			if [ $? -ne 0 ]; then
-				printf "\nERROR:\tImposible encender el servicio '$1'.\n"
-				exit 1
-			fi
+			comprobarError $? 901 $1
 		fi
 		# Intentamos habilitar en el arranque (Upstart)
 		which update-rc.d > /dev/null 2>&1
 		if [ $? -eq 0 ];then
 			update-rc.d $1 enable
-			if [ $? -ne 0 ];then
-				printf "\nERROR:\tImposible habilitar servicio '$1' durante el arranque.\n"
-				exit 1
-			fi
+			comprobarError $? 902 $1
 		else
 			# Intentamos habilitar en el arranque (SystemV)
 			which chkconfig > /dev/null 2>&1
 			if [ $? -eq 0 ];then
 				chkconfig $1 on
-				if [ $? -ne 0 ];then
-					printf "\nERROR:\tImposible habilitar servicio '$1' durante el arranque.\n"
-					exit 1
-				fi
+				comprobarError $? 902 $1
 			else
 				# ¿Qué mas opciones nos quedan?
-				printf "\nERROR:\tImposible habilitar servicio '$1' durante el arranque.\n"
-				exit 1
+				comprobarError 1 902 $1
 			fi
 		fi
 	fi