From c2796a39fa7dd3073874bf959346efaa507d039b Mon Sep 17 00:00:00 2001 From: lgromero Date: Tue, 13 Aug 2024 11:22:58 +0000 Subject: [PATCH] refs #503 #504 #639 updates installer with sudo commands, corrects download menu to show installed oglives correctly and changes download to accepts a url with the oglive to download --- bin/oglivecli | 271 ++++++++++++++++---------------------------------- 1 file changed, 85 insertions(+), 186 deletions(-) diff --git a/bin/oglivecli b/bin/oglivecli index a004bcd..9c17dc1 100755 --- a/bin/oglivecli +++ b/bin/oglivecli @@ -5,36 +5,24 @@ #@brief Command line tool to manage ogLive clients. #@usage oglivecli Command [Options ...] #@param Command: -#@param help show this help -#@param version show script version -#@param config [Parameter] show configuration parameters -#@param check check system consistency -#@param convert convert old ogclient to new default ogLive client -#@param list list installed ogLive clients -#@param show all show JSON information about all installed ogLive clients -#@param show default show JSON information about ogLive client marked as default -#@param show Index|Dir show JSON information about an installed ogLive client -#@param search Index|Dir show corresponding index or directory -#@param download show a menu to download an ogLive ISO image from the OpenGnsys website -#@param download Iso download an specific ogLive ISO image from the OpenGnsys website -#@param install Iso install a new ogLive client from a downloaded ISO image -#@param uninstall Iso remove ISO image and uninstall its ogLive client -#@param uninstall Index|Dir uninstall an ogLive client -#@param get-default get index value for default ogLive client -#@param set-default Index set default ogLive client -#@param rebuild rebuild a lost configuration file -#@param assign Iso Index assign an ISO file to a JSON entry -#@param Options: -#@param Index a number, starting by 0 -#@param Dir directory (relative to installation directory) -#@param Iso ISO file name (relative to download URL or download directory) +#@param help show this help +#@param version show script version +#@param config [Parameter] show configuration parameters +#@param disk_usage show disk usage information +#@param list_installed_oglives list installed ogLive clients +#@param check_services_status check status of critical services +#@param download show a menu to download an ogLive ISO image from the OpenGnsys website +#@param download Iso download a specific ogLive ISO image from the OpenGnsys website +#@param install Iso install a new ogLive client from a downloaded ISO image +#@param uninstall uuid uninstall an ogLive client using its UUID +#@param get_default get index value for default ogLive client +#@param set_default uuid set default ogLive client using its UUID +#@param get_info uuid get JSON information about an installed ogLive client using its UUID #@warning This script needs "jq" command. -#@version 1.1.0 - Initial version. +#@version 3.0.0 - Updated with to adapt to Ogboot. #@author Ramón M. Gómez - ETSII Univ. Sevilla -#@date 2016-12-05 -#@version 1.1.1b - Use reduced directory names. -#@author Ramón M. Gómez - ETSII Univ. Sevilla -#@date 2020-01-17 +#@author Ramón M. Gómez - Qindel formacion y servicios SL +#@date 2024-08-06 #*/ ## @@ -53,93 +41,7 @@ INFOFILE=$OPENGNSYS/etc/ogliveinfo.json # Configuration file. source $OPENGNSYS/lib/ogfunctions.sh || exit 1 -# Create/edit JSON file about installed ogLive clients. -function addToJson() { - local i DATA OGLIVEDIST="$1" OGLIVEKRNL="$2" OGLIVEARCH="$3" OGLIVEREV="$4" - local OGLIVEDIR=$(basename $5 2>/dev/null) OGLIVEISO=$(basename $6 2>/dev/null) - # JSON data for installed ogLive. - DATA=$(cat << EOT | jq . -{"distribution":"$OGLIVEDIST","kernel":"$OGLIVEKRNL","architecture":"$OGLIVEARCH","revision":"$OGLIVEREV","directory":"$OGLIVEDIR","iso":"$OGLIVEISO"} -EOT - ) - # Check JSON file consistency. - if [ "$(jq -c keys $INFOFILE 2>/dev/null)" == '["default","oglive"]' ]; then - # Check if ogLive is defined into JSON file. - n=$(jq ".oglive | length" $INFOFILE) - for ((i=0; i/dev/null || raiseError access "Installation directory." - [ ! -f $OGCLIENT/ogvmlinuz ] && raiseError notfound "\"ogclient\"." - # Add entry to JSON file using ogclient kernel version. - OGLIVEKRNL=$(file -bkr $OGCLIENT/ogvmlinuz | awk '/Linux/ {for(i=1;i<=NF;i++) if($i~/version/) {v=$(i+1);sub(/-.*/,"",v);print v}}') - OGLIVEDIR=$DEFOGLIVE-$OGLIVEKRNL - [ -r $OLDINFOFILE ] && OGLIVEISO="$(head -1 $OLDINFOFILE)" - addToJson "$(echo $OGLIVEISO|cut -f2 -d-)" "$OGLIVEKRNL" "i386" "${OGLIVEISO##*-}" "$OGLIVEDIR" "$OGLIVEISO.iso" - # Rename directory, link to default and clean old files. - mv -v $OGCLIENT $OGLIVEDIR - ln -vfs $OGLIVEDIR $DEFOGLIVE - rm -f $OGCLIENT - ln -vfs $DEFOGLIVE $OGCLIENT - mv -v $OGCLIENT.old $OGLIVEDIR.old 2>/dev/null - rm -fv {ogvmlinuz,oginitrd.img}{,.sum} $OLDINFOFILE - popd >/dev/null - # Delete old config file. - rm -f $OLDINFOFILE -} - -# Show script configuration parameters. -function config() { - local DATA - DATA=$(cat << EOT -[ - { "param": "config-file", "description": "Configuration file", "value": "$INFOFILE" }, - { "param": "download-url", "description": "ogLive download URL", "value": "$DOWNLOADURL" }, - { "param": "download-dir", "description": "ogLive download directory", "value": "$DOWNLOADDIR" }, - { "param": "install-dir", "description": "ogLive installation directory", "value": "$TFTPDIR" }, - { "param": "default-name", "description": "Default ogLive name", "value": "$DEFOGLIVE" }, - { "param": "min-release", "description": "Minimum compatibility release", "value": "r$MINREL" } -] -EOT - ) - case $# in - 0) # Show all parameters. - echo "$DATA" | jq -r '.[] | .description + " (" + .param + ")," + .value' | column -ts, - ;; - 1) # Show specified parameter. - DATA=$(echo "$DATA" | jq -r ".[] | select(.param==\"$1\").value") - [ "$DATA" ] || raiseError notfound "\"$1\"." - echo "$DATA" - ;; - *) # Usage error. - raiseError usage - ;; - esac -} # Check consistency, showing configuration problems. function check() { @@ -223,9 +125,8 @@ function search() { # Show corresponding index or directory. list | awk -v d="$1" '{if ($2==d) print $1; if ($1==d) print $2}' | grep . || raiseError notfound "Index/Directory \"$1\"." } - function download() { - local OGLIVE NISOS i HTTPCODE ISOREL + local OGLIVEFILE TARGETFILE SOURCELENGTH # Verificar si el directorio de descarga existe y tiene permisos de escritura. [ ! -d "$DOWNLOADDIR" ] && raiseError notfound "Directorio de descarga." @@ -235,46 +136,79 @@ function download() { if [ -z "$1" ]; then downloadMenu else - local selected_name="$1" + local download_url="$1" + OGLIVEFILE=$(basename "$download_url") - # Obtener la lista de archivos disponibles. - OGLIVE=( $(curl -k --silent $DOWNLOADURL | grep "$DEFOGLIVE.*iso") ) - - # Buscar el archivo seleccionado por nombre. - OGLIVEFILE="" - for iso in "${OGLIVE[@]}"; do - if [[ "$iso" == *"$selected_name"* ]]; then - OGLIVEFILE=$iso - break - fi - done - - [ -n "$OGLIVEFILE" ] || raiseError download "Nombre \"$selected_name\" inválido." + # Validar que la URL apunte a un archivo ISO. + if [[ ! "$OGLIVEFILE" =~ \.iso$ ]]; then + raiseError download "La URL no apunta a un archivo ISO." + fi # Obtener el tamaño de descarga. - local SOURCELENGTH=$(curl -k --head --retry 5 --retry-delay 5 --max-time 30 "$DOWNLOADURL/$OGLIVEFILE" | awk -F: '/Content-Length:/ {print $2}') - [ -n "$SOURCELENGTH" ] || raiseError download "$OGLIVEFILE" + SOURCELENGTH=$(curl -k --head --retry 5 --retry-delay 5 --max-time 30 "$download_url" | awk -F: '/Content-Length:/ {print $2}') + [ -n "$SOURCELENGTH" ] || raiseError download "No se pudo obtener el tamaño del archivo desde \"$download_url\"." # Descargar ogLive. - local TARGETFILE="$DOWNLOADDIR/$OGLIVEFILE" + TARGETFILE="$DOWNLOADDIR/$OGLIVEFILE" trap "rm -f $TARGETFILE" 1 2 3 6 9 15 - curl -k --retry 5 --retry-delay 5 "$DOWNLOADURL/$OGLIVEFILE" -o "$TARGETFILE" || raiseError download "\"$OGLIVEFILE\"." + curl -k --retry 5 --retry-delay 5 "$download_url" -o "$TARGETFILE" || raiseError download "No se pudo descargar \"$OGLIVEFILE\"." + + # Validar que el archivo descargado sea un ISO válido y que sea un ogLive. + if [ -f "$TARGETFILE" ]; then + local file_type=$(file -b "$TARGETFILE") + if [[ "$file_type" =~ "ISO 9660" ]] && [[ "$file_type" =~ "ogClient" ]]; then + echo "Archivo ISO válido descargado: $TARGETFILE" + else + raiseError download "El archivo descargado no es un ogLive ISO válido." + fi + else + raiseError download "El archivo no fue descargado correctamente." + fi + + # Continuar con la instalación. install "$OGLIVEFILE" fi } + + # Muestra un menú para seleccionar y descargar un archivo ogLive ISO del sitio web de OpenGnsys. function downloadMenu() { - local OGLIVE NISOS i HTTPCODE ISOREL - OGLIVE=( $(curl -k --silent $DOWNLOADURL | grep "$DEFOGLIVE.*iso") ) - NISOS=${#OGLIVE[@]} - - local downloads=() - - for i in $(seq 1 $NISOS); do - local installed=false - local compatible=false - - [ -e $DOWNLOADDIR/${OGLIVE[i-1]} ] && installed=true + local OGLIVE NISOS i HTTPCODE ISOREL + OGLIVE=( $(curl -k --silent $DOWNLOADURL | grep "$DEFOGLIVE.*iso") ) + NISOS=${#OGLIVE[@]} + + local downloads=() + + for i in $(seq 1 $NISOS); do + installed=false + compatible=false + + # Obtener el nombre completo del archivo ISO + OGLIVEFILE=$(realpath "$DOWNLOADDIR/${OGLIVE[i-1]}") + + # Extraer la distribución, revisión y arquitectura + OGLIVEDIST="$(echo $OGLIVEFILE | cut -f2 -d-)" + OGLIVEREV="${OGLIVEFILE##*-}"; OGLIVEREV="${OGLIVEREV%%.*}" + OGLIVEKRNL="$(echo $OGLIVEFILE | cut -f3- -d-)"; OGLIVEKRNL="${OGLIVEKRNL%-$OGLIVEREV.*}" + OGLIVEARCH="$(echo $OGLIVEFILE | awk -F- '{print $(NF-1)}')" + case "$OGLIVEARCH" in + i386|amd64) + OGLIVEKRNL="${OGLIVEKRNL%-$OGLIVEARCH}" ;; + *) + OGLIVEARCH="i386" ;; + esac + OGLIVEDIR="$TFTPDIR/$DEFOGLIVE-${OGLIVEKRNL%%-*}-$OGLIVEARCH-$OGLIVEREV" + OGLIVEDIR="${OGLIVEDIR/amd64-/}" + + echo "Verificando si está instalado: $OGLIVEDIR" + + # Verificar si el ogLive está instalado y no es un directorio .old + if [ -d "$OGLIVEDIR" ] && [[ ! "$OGLIVEDIR" =~ \.old$ ]]; then + installed=true + else + installed=false + fi + echo "Estado de 'installed': $installed" ISOREL=${OGLIVE[i-1]##*-r}; ISOREL=${ISOREL%%.*} [ $ISOREL -ge $MINREL ] && compatible=true @@ -373,7 +307,7 @@ function install() { sed -n '/^[ \t].*OPTIONS=/s/.*pass=\(\w*\).*/\1/p') fi - rm -fr ${OGLIVEDIR}.old + rm -fr ${OGLIVEDIR}.old 2>/dev/null mv -f $OGLIVEDIR ${OGLIVEDIR}.old 2>/dev/null TMPDIR=/tmp/${OGLIVEFILE%.iso} @@ -390,9 +324,9 @@ function install() { fi if [ -n "$SAMBAPASS" ]; then - echo -ne "$SAMBAPASS\n$SAMBAPASS\n" | $OPENGNSYS/bin/setsmbpass "$(basename $OGLIVEDIR)" || { echo "{\"status\": \"error\", \"error\": \"setsmbpass failed with SAMBAPASS.\"}"; exit 1; } + echo -ne "$SAMBAPASS\n$SAMBAPASS\n" | $OPENGNSYS/bin/setsmbpass "$(basename $OGLIVEDIR)" >/dev/null 2>&1 || { echo "{\"status\": \"error\", \"error\": \"setsmbpass failed with SAMBAPASS.\"}"; exit 1; } else - $OPENGNSYS/bin/setsmbpass "$(basename $OGLIVEDIR)" || { echo "{\"status\": \"error\", \"error\": \"setsmbpass failed no SAMBAPASS.\"}"; exit 1; } + $OPENGNSYS/bin/setsmbpass "$(basename $OGLIVEDIR)" >/dev/null 2>&1 || { echo "{\"status\": \"error\", \"error\": \"setsmbpass failed no SAMBAPASS.\"}"; exit 1; } fi find -L $OGLIVEDIR -type d -exec chmod 755 {} \; >/dev/null 2>&1 || { echo "{\"status\": \"error\", \"error\": \"chmod directories failed.\"}"; exit 1; } @@ -413,12 +347,12 @@ function install() { mv -f "$OPENGNSYS/client/bin/rsync" "$OPENGNSYS/client/bin/rsync-$($OPENGNSYS/client/bin/rsync --version 2>/dev/null | awk '/protocol/ {print $6}')" fi fi - echo "deleting tmpdir" sudo umount $TMPDIR >/dev/null 2>&1 sudo rmdir $TMPDIR >/dev/null 2>&1 || sudo rm -rf $TMPDIR >/dev/null 2>&1 fi - # Crear JSON output + # Confirmar y eliminar el archivo ISO + sudo rm -f "$OGLIVEFILE" JSON_OUTPUT=$(jq -n \ --arg dist "$OGLIVEDIST" \ --arg krnl "$OGLIVEKRNL" \ @@ -545,41 +479,6 @@ function set_default() { fi } -# Rebuild a lost configuration file. -function rebuild() { - local i INST NF DEF - [ $# -ne 0 ] && raiseError usage - [ -f $INFOFILE ] && raiseError access "Configuration file exists." - INST=$(find $TFTPDIR/ -type d -name "$DEFOGLIVE-*" -a ! -name "*.old" -printf "%f\n" | sort) - for i in $INST; do - NF=$(echo $i | awk -F- '{print NF-1}') - case $NF in - 1) addToJson "" "$(echo $i|cut -f2 -d-)" "i386" "" "$i" "" ;; - 2) eval addToJson $(echo $i | awk -F- '{printf "\"\" %s amd64 %s %s \"\"",$2,$3,$0}') ;; - 3) eval addToJson $(echo $i | awk -F- '{if ($3=="i386") printf "\"\" %s %s %s %s \"\"",$2,$3,$4,$0; else printf "%s %s i386 %s %s \"\"",$2,$3,$4,$0}') ;; - 4) eval addToJson $(echo $i | awk -F- '{printf "%s %s %s %s %s \"\"",$2,$3,$4,$5,$0}') ;; - esac - # Check for is default oglive. - [ -n "$(stat -c "%N" $TFTPDIR/$DEFOGLIVE | awk '$3~/'$i'/ {print}')" ] && DEF="$i" - done - # Set default ogLive. - [ -n "$DEF" ] && setdefault $(search $DEF) -} - -# Assign an ISO file to a JSON entry. -function assign() { -local ISOFILE DIR - [ $# -ne 2 ] && raiseError usage - [ ! -w $INFOFILE ] && raiseError access "Configuration file." - # Check if ISO file and index directory exist. - ISOFILE=$DOWNLOADFILE/$1 - [ ! -f $DOWNLOADDIR/$ISOFILE ] && raiseError notfound "ISO file \"$1\"." - DIR=$(search $2 2>/dev/null) - [ ! -d $TFTPDIR/$DIR ] && raiseError notfound "Directory for index \"$2\"." - # Assign ISO file to JSON entry. - jq ".oglive[$2].iso=\"$1\"" $INFOFILE | sponge $INFOFILE && jq ".oglive[$2]" $INFOFILE -} - # Get disk usage information function disk_usage() { DISK_INFO=$(df -h / | awk 'NR==2{print "{\"total\":\""$2"\", \"used\":\""$3"\", \"available\":\""$4"\", \"percentage\":\""$5"\"}"}') @@ -723,7 +622,7 @@ which sponge &>/dev/null || raiseError notfound "Need to install \"moreutils\"." # Commands control. shopt -s extglob -CMDS='+(help|version|convert|config|check|list|show|search|download|install|uninstall|get-default|set-default|rebuild|assign|disk_usage|list_installed_oglives|get_info|get_default|set_default|check_services_status)' +CMDS='+(help|version|disk_usage|list_installed_oglives|check_services_status|download|install|uninstall|get_default|set_default|get_info)' case "$1" in $CMDS) COMMAND="${1/-/}"; shift; $COMMAND "$@" ;;