diff --git a/bin/lib/base.lib b/bin/lib/base.lib index a7641a5..3b689cb 100644 --- a/bin/lib/base.lib +++ b/bin/lib/base.lib @@ -45,6 +45,49 @@ DOCKER_ENTRY=entrypoint.sh VERSION="$(cat < ${BIN_HOME}/${BIN_CONF}/version)" +# Verificar si el script se está ejecutando como usuario root (superusuario) +function is_root() { + if [ "$(id -u)" -eq 0 ]; then + return 0 # Es root + else + return 1 # No es root + fi +} + +# Escalar privilegios a superusuario si es necesario +function escalate_privileges() { + if ! is_root; then + echo -e "\n${head_info}: Se requieren privilegios de administrador para esta operación." + + # Verificar si sudo está instalado + command -v sudo >/dev/null 2>&1 + if [ $? -ne 0 ]; then + echo -e "${head_error}: El comando 'sudo' no está instalado. No se pueden escalar privilegios." + return 1 + fi + + # Intentar escalar privilegios con sudo + echo -e "${head_info}: Ingrese su contraseña si se solicita." + + # Verificar que sudo funciona + sudo -n true 2>/dev/null + if [ $? -ne 0 ]; then + # Sudo requiere contraseña, informar al usuario + echo -e "${head_info}: Elevando privilegios con sudo..." + fi + + # Ejecutar el script actual con sudo + sudo "$0" "$@" + EXIT_CODE=$? + + # Salir con el código devuelto por el comando sudo + exit $EXIT_CODE + fi + + # Ya es root, no necesita hacer nada + return 0 +} + # Test library function baselib_test() { echo "Base Library loaded!" diff --git a/bin/update.sh b/bin/update.sh index 86d2907..4d16059 100755 --- a/bin/update.sh +++ b/bin/update.sh @@ -61,6 +61,33 @@ fi # Load bootstrap library for update functions source $BIN_HOME/$BIN_LIBS/bootstrap.lib +# Verificar si se necesita ser superusuario para actualizar Python y pip +UPDATE_PACKAGES=false + +# Verificar si Python está instalado y necesitamos actualizarlo +command_installed python3 +if [ $? -eq 0 ]; then + UPDATE_PACKAGES=true +fi + +# Verificar si pip está instalado y necesitamos actualizarlo +command_installed pip3 +if [ $? -eq 0 ]; then + UPDATE_PACKAGES=true +fi + +# Si necesitamos actualizar paquetes, verificar privilegios +if [ "$UPDATE_PACKAGES" = true ]; then + # Escalar privilegios para actualizar Python y pip + escalate_privileges "$@" + + # Si llegamos aquí, es porque ya somos superusuario + if ! is_root; then + echo -e "\n${head_error}: No se pudieron obtener privilegios de administrador." + echo -e "Las actualizaciones de Python y pip no se realizarán." + fi +fi + # Update Python and pip separately echo -e "\n${head_info}: Verificando actualizaciones de Python y pip..."