refs #477 Adds oglivecli with new functions and configurationç
parent
72adbf37cf
commit
5b1cf84fe1
434
bin/oglivecli
434
bin/oglivecli
|
@ -224,7 +224,6 @@ function search() {
|
||||||
list | awk -v d="$1" '{if ($2==d) print $1; if ($1==d) print $2}' | grep . || raiseError notfound "Index/Directory \"$1\"."
|
list | awk -v d="$1" '{if ($2==d) print $1; if ($1==d) print $2}' | grep . || raiseError notfound "Index/Directory \"$1\"."
|
||||||
}
|
}
|
||||||
|
|
||||||
# Show a menu to select and download an ogLive ISO image from the OpenGnsys website.
|
|
||||||
function download() {
|
function download() {
|
||||||
local OGLIVE NISOS i HTTPCODE ISOREL
|
local OGLIVE NISOS i HTTPCODE ISOREL
|
||||||
|
|
||||||
|
@ -265,17 +264,32 @@ function download() {
|
||||||
}
|
}
|
||||||
# Muestra un menú para seleccionar y descargar un archivo ogLive ISO del sitio web de OpenGnsys.
|
# Muestra un menú para seleccionar y descargar un archivo ogLive ISO del sitio web de OpenGnsys.
|
||||||
function downloadMenu() {
|
function downloadMenu() {
|
||||||
|
|
||||||
local OGLIVE NISOS i HTTPCODE ISOREL
|
local OGLIVE NISOS i HTTPCODE ISOREL
|
||||||
OGLIVE=( $(curl -k --silent $DOWNLOADURL | grep "$DEFOGLIVE.*iso") )
|
OGLIVE=( $(curl -k --silent $DOWNLOADURL | grep "$DEFOGLIVE.*iso") )
|
||||||
NISOS=${#OGLIVE[@]}
|
NISOS=${#OGLIVE[@]}
|
||||||
echo "Descargas disponibles (+ = instalado, * = compatibilidad completa):"
|
|
||||||
|
local downloads=()
|
||||||
|
|
||||||
for i in $(seq 1 $NISOS); do
|
for i in $(seq 1 $NISOS); do
|
||||||
[ -e $DOWNLOADDIR/${OGLIVE[i-1]} ] && OGLIVE[i-1]="(+) ${OGLIVE[i-1]}"
|
local installed=false
|
||||||
|
local compatible=false
|
||||||
|
|
||||||
|
[ -e $DOWNLOADDIR/${OGLIVE[i-1]} ] && installed=true
|
||||||
ISOREL=${OGLIVE[i-1]##*-r}; ISOREL=${ISOREL%%.*}
|
ISOREL=${OGLIVE[i-1]##*-r}; ISOREL=${ISOREL%%.*}
|
||||||
[ $ISOREL -ge $MINREL ] && OGLIVE[i-1]="(*) ${OGLIVE[i-1]}"
|
[ $ISOREL -ge $MINREL ] && compatible=true
|
||||||
echo "$i) ${OGLIVE[i-1]}"
|
|
||||||
|
local DATA=$(jq -n \
|
||||||
|
--arg id "$i" \
|
||||||
|
--arg filename "${OGLIVE[i-1]}" \
|
||||||
|
--argjson installed "$installed" \
|
||||||
|
--argjson compatible "$compatible" \
|
||||||
|
'{id: $id, filename: $filename, installed: $installed, compatible: $compatible}')
|
||||||
|
|
||||||
|
downloads+=("$DATA")
|
||||||
done
|
done
|
||||||
|
|
||||||
|
jq -n --argjson downloads "$(printf '%s\n' "${downloads[@]}" | jq -s .)" \
|
||||||
|
'{downloads: $downloads}'
|
||||||
}
|
}
|
||||||
|
|
||||||
# Show a menu to select and download an ogLive ISO image from the OpenGnsys website.
|
# Show a menu to select and download an ogLive ISO image from the OpenGnsys website.
|
||||||
|
@ -312,146 +326,224 @@ function download_old() {
|
||||||
curl -k --retry 5 --retry-delay 5 --max-time 30 $DOWNLOADURL/$OGLIVEFILE -o $TARGETFILE || raiseError download "\"$OGLIVEFILE\"."
|
curl -k --retry 5 --retry-delay 5 --max-time 30 $DOWNLOADURL/$OGLIVEFILE -o $TARGETFILE || raiseError download "\"$OGLIVEFILE\"."
|
||||||
}
|
}
|
||||||
|
|
||||||
|
update_json() {
|
||||||
|
local key=$1
|
||||||
|
local value=$2
|
||||||
|
result_json=$(jq --arg key "$key" --arg value "$value" '.[$key] = $value' <<< "$result_json")
|
||||||
|
}
|
||||||
|
|
||||||
|
add_message() {
|
||||||
|
local message=$1
|
||||||
|
result_json=$(jq --arg message "$message" '.messages += [$message]' <<< "$result_json")
|
||||||
|
}
|
||||||
|
|
||||||
# Install an ogLive client from a previously downloaded ISO image.
|
# Install an ogLive client from a previously downloaded ISO image.
|
||||||
function install() {
|
function install() {
|
||||||
local OGLIVEFILE OGLIVEDIST OGLIVEREV OGLIVEKRNL OGLIVEDIR OGINITRD OGSQFS OGCLIENT=ogclient
|
local OGLIVEFILE OGLIVEDIST OGLIVEREV OGLIVEKRNL OGLIVEDIR OGINITRD OGSQFS OGCLIENT=ogclient
|
||||||
local COMPRESS SAMBAPASS TMPDIR RSYNCSERV RSYNCCLNT
|
local COMPRESS SAMBAPASS TMPDIR RSYNCSERV RSYNCCLNT JSON_OUTPUT
|
||||||
[ $# -ne 1 ] && raiseError usage
|
[ $# -ne 1 ] && { echo "{\"status\": \"error\", \"error\": \"usage\"}"; exit 1; }
|
||||||
|
|
||||||
OGLIVEFILE=$(realpath $DOWNLOADDIR/$1)
|
OGLIVEFILE=$(realpath $DOWNLOADDIR/$1)
|
||||||
# Only 1 file in pathname expansion.
|
[ $(echo $OGLIVEFILE | wc -w) -gt 1 ] && { echo "{\"status\": \"error\", \"error\": \"usage\"}"; exit 1; }
|
||||||
[ $(echo $OGLIVEFILE | wc -w) -gt 1 ] && raiseError usage
|
[ ! -f $OGLIVEFILE ] && { echo "{\"status\": \"error\", \"error\": \"not found $1.\"}"; exit 1; }
|
||||||
[ ! -f $OGLIVEFILE ] && raiseError notfound "Downloaded file: \"$1\"."
|
[ ! -r $OGLIVEFILE ] && { echo "{\"status\": \"error\", \"error\": \"access $1.\"}"; exit 1; }
|
||||||
[ ! -r $OGLIVEFILE ] && raiseError access "Downloaded file: \"$1\"."
|
[ ! -w $(dirname $INFOFILE) ] && { echo "{\"status\": \"error\", \"error\": \"access configuration directory.\"}"; exit 1; }
|
||||||
[ ! -w $(dirname $INFOFILE) ] && raiseError access "Configuration directory."
|
[ ! -w $TFTPDIR ] && { echo "{\"status\": \"error\", \"error\": \"access installation directory.\"}"; exit 1; }
|
||||||
[ ! -w $TFTPDIR ] && raiseError access "Installation directory."
|
[ -z "$(file -b $OGLIVEFILE | grep "ISO.*ogClient")" ] && { echo "{\"status\": \"error\", \"error\": \"File is not an ogLive ISO image.\"}"; exit 1; }
|
||||||
[ -z "$(file -b $OGLIVEFILE | grep "ISO.*ogClient")" ] && raiseError access "File is not an ogLive ISO image."
|
|
||||||
# Working directory:
|
|
||||||
# 64-bit: ogLive-KernelVersion-rCodeRelease
|
|
||||||
# 32-bit: ogLive-KernelVersion-i386-rCodeRelease
|
|
||||||
OGLIVEDIST="$(echo $OGLIVEFILE|cut -f2 -d-)"
|
OGLIVEDIST="$(echo $OGLIVEFILE|cut -f2 -d-)"
|
||||||
OGLIVEREV="${OGLIVEFILE##*-}"; OGLIVEREV="${OGLIVEREV%%.*}"
|
OGLIVEREV="${OGLIVEFILE##*-}"; OGLIVEREV="${OGLIVEREV%%.*}"
|
||||||
OGLIVEKRNL="$(echo $OGLIVEFILE|cut -f3- -d-)"; OGLIVEKRNL="${OGLIVEKRNL%-$OGLIVEREV.*}"
|
OGLIVEKRNL="$(echo $OGLIVEFILE|cut -f3- -d-)"; OGLIVEKRNL="${OGLIVEKRNL%-$OGLIVEREV.*}"
|
||||||
OGLIVEARCH="$(echo $OGLIVEFILE|awk -F- '{print $(NF-1)}')"
|
OGLIVEARCH="$(echo $OGLIVEFILE|awk -F- '{print $(NF-1)}')"
|
||||||
case "$OGLIVEARCH" in
|
case "$OGLIVEARCH" in
|
||||||
i386|amd64) # Get architecture.
|
i386|amd64)
|
||||||
OGLIVEKRNL="${OGLIVEKRNL%-$OGLIVEARCH}" ;;
|
OGLIVEKRNL="${OGLIVEKRNL%-$OGLIVEARCH}" ;;
|
||||||
*) # 32-bit by default.
|
*)
|
||||||
OGLIVEARCH="i386" ;;
|
OGLIVEARCH="i386" ;;
|
||||||
esac
|
esac
|
||||||
OGLIVEDIR="$TFTPDIR/$DEFOGLIVE-${OGLIVEKRNL%%-*}-$OGLIVEARCH-$OGLIVEREV"
|
OGLIVEDIR="$TFTPDIR/$DEFOGLIVE-${OGLIVEKRNL%%-*}-$OGLIVEARCH-$OGLIVEREV"
|
||||||
OGLIVEDIR="${OGLIVEDIR/amd64-/}"
|
OGLIVEDIR="${OGLIVEDIR/amd64-/}"
|
||||||
# Get current or default Samba key.
|
|
||||||
OGINITRD=$OGLIVEDIR/oginitrd.img
|
OGINITRD=$OGLIVEDIR/oginitrd.img
|
||||||
[ ! -r $OGINITRD ] && OGINITRD=$TFTPDIR/$DEFOGLIVE/oginitrd.img
|
[ ! -r $OGINITRD ] && OGINITRD=$TFTPDIR/$DEFOGLIVE/oginitrd.img
|
||||||
if [ -r $OGINITRD ]; then
|
if [ -r $OGINITRD ]; then
|
||||||
COMPRESS=$(file -b "$OGINITRD" | awk '{print tolower($1);}')
|
COMPRESS=$(file -b "$OGINITRD" | awk '{print tolower($1);}')
|
||||||
SAMBAPASS=$($COMPRESS -dc $OGINITRD | \
|
SAMBAPASS=$($COMPRESS -dc $OGINITRD | \
|
||||||
cpio -i --to-stdout scripts/ogfunctions 2>&1 | \
|
cpio -i --to-stdout scripts/ogfunctions 2>/dev/null | \
|
||||||
sed -n '/^[ ].*OPTIONS=/s/.*pass=\(\w*\).*/\1/p')
|
sed -n '/^[ \t].*OPTIONS=/s/.*pass=\(\w*\).*/\1/p')
|
||||||
fi
|
fi
|
||||||
# Make ogLive backup.
|
|
||||||
rm -fr ${OGLIVEDIR}.old
|
rm -fr ${OGLIVEDIR}.old
|
||||||
mv -fv $OGLIVEDIR ${OGLIVEDIR}.old 2>/dev/null
|
mv -f $OGLIVEDIR ${OGLIVEDIR}.old 2>/dev/null
|
||||||
# Mount ogLive ISO image, update its files and unmount it.
|
|
||||||
TMPDIR=/tmp/${OGLIVEFILE%.iso}
|
TMPDIR=/tmp/${OGLIVEFILE%.iso}
|
||||||
mkdir -p $OGLIVEDIR $TMPDIR
|
mkdir -p $OGLIVEDIR $TMPDIR
|
||||||
trap "umount $TMPDIR; rm -fr $TMPDIR" 1 2 3 6 9 15
|
trap "umount $TMPDIR; rm -fr $TMPDIR" 1 2 3 6 9 15
|
||||||
mount -o loop,ro $OGLIVEFILE $TMPDIR
|
mount -o loop,ro $OGLIVEFILE $TMPDIR >/dev/null 2>&1 || { echo "{\"status\": \"error\", \"error\": \"mount failed.\"}"; exit 1; }
|
||||||
cp -va $TMPDIR/ogclient/* $OGLIVEDIR || raiseError access "Cannot copy files to ogLive directory."
|
cp -va $TMPDIR/ogclient/* $OGLIVEDIR >/dev/null 2>&1 || { echo "{\"status\": \"error\", \"error\": \"Cannot copy files to ogLive directory.\"}"; exit 1; }
|
||||||
umount $TMPDIR
|
umount $TMPDIR >/dev/null 2>&1
|
||||||
# Link to default directory if it's the first ogLive.
|
|
||||||
if [ ! -f $INFOFILE ]; then
|
if [ ! -f $INFOFILE ]; then
|
||||||
rm -f $TFTPDIR/$DEFOGLIVE $TFTPDIR/$OGCLIENT
|
rm -f $TFTPDIR/$DEFOGLIVE $TFTPDIR/$OGCLIENT
|
||||||
ln -vfs $(basename $OGLIVEDIR) $TFTPDIR/$DEFOGLIVE
|
ln -vfs $(basename $OGLIVEDIR) $TFTPDIR/$DEFOGLIVE >/dev/null 2>&1 || { echo "{\"status\": \"error\", \"error\": \"Linking to $TFTPDIR/$DEFOGLIVE failed.\"}"; exit 1; }
|
||||||
ln -vfs $DEFOGLIVE $TFTPDIR/$OGCLIENT
|
ln -vfs $DEFOGLIVE $TFTPDIR/$OGCLIENT >/dev/null 2>&1 || { echo "{\"status\": \"error\", \"error\": \"Linking to $TFTPDIR/$OGCLIENT failed.\"}"; exit 1; }
|
||||||
fi
|
fi
|
||||||
# Recover or ask for a new Samba access key.
|
|
||||||
if [ -n "$SAMBAPASS" ]; then
|
if [ -n "$SAMBAPASS" ]; then
|
||||||
echo -ne "$SAMBAPASS\n$SAMBAPASS\n" | $OPENGNSYS/bin/setsmbpass "$(basename $OGLIVEDIR)"
|
echo -ne "$SAMBAPASS\n$SAMBAPASS\n" | $OPENGNSYS/bin/setsmbpass "$(basename $OGLIVEDIR)" >/dev/null 2>&1 || { echo "{\"status\": \"error\", \"error\": \"setsmbpass failed.\"}"; exit 1; }
|
||||||
else
|
else
|
||||||
$OPENGNSYS/bin/setsmbpass "$(basename $OGLIVEDIR)"
|
$OPENGNSYS/bin/setsmbpass "$(basename $OGLIVEDIR)" >/dev/null 2>&1 || { echo "{\"status\": \"error\", \"error\": \"setsmbpass failed.\"}"; exit 1; }
|
||||||
fi
|
fi
|
||||||
# Set permissions.
|
|
||||||
find -L $OGLIVEDIR -type d -exec chmod 755 {} \;
|
find -L $OGLIVEDIR -type d -exec chmod 755 {} \; >/dev/null 2>&1 || { echo "{\"status\": \"error\", \"error\": \"chmod directories failed.\"}"; exit 1; }
|
||||||
find -L $OGLIVEDIR -type f -exec chmod 644 {} \;
|
find -L $OGLIVEDIR -type f -exec chmod 644 {} \; >/dev/null 2>&1 || { echo "{\"status\": \"error\", \"error\": \"chmod files failed.\"}"; exit 1; }
|
||||||
chown -R :opengnsys $OGLIVEDIR
|
chown -R :opengnsys $OGLIVEDIR >/dev/null 2>&1 || { echo "{\"status\": \"error\", \"error\": \"chown failed.\"}"; exit 1; }
|
||||||
# Mount SquashFS and check Rsync version.
|
|
||||||
OGSQFS=$OGLIVEDIR/ogclient.sqfs
|
OGSQFS=$OGLIVEDIR/ogclient.sqfs
|
||||||
mount -o loop,ro $OGSQFS $TMPDIR
|
|
||||||
# If Rsync server version > client version, link to compiled file.
|
if mount -o loop,ro $OGSQFS $TMPDIR >/dev/null 2>&1; then
|
||||||
RSYNCSERV=$(rsync --version 2>/dev/null | awk '/protocol/ {print $6}')
|
RSYNCSERV=$(rsync --version 2>/dev/null | awk '/protocol/ {print $6}')
|
||||||
RSYNCCLNT=$(chroot $TMPDIR /usr/bin/rsync --version 2>/dev/null | awk '/protocol/ {print $6}')
|
RSYNCCLNT=$(chroot $TMPDIR /usr/bin/rsync --version 2>/dev/null | awk '/protocol/ {print $6}')
|
||||||
if [ -z "$RSYNCSERV" -o ${RSYNCSERV:-0} -gt ${RSYNCCLNT:-1} ]; then
|
|
||||||
[ -e $OPENGNSYS/client/bin/rsync-$RSYNCSERV ] && mv -f $OPENGNSYS/client/bin/rsync-$RSYNCSERV $OPENGNSYS/client/bin/rsync
|
if [ -z "$RSYNCSERV" ] || [ "$RSYNCSERV" -gt "${RSYNCCLNT:-1}" ]; then
|
||||||
else
|
if [ -e "$OPENGNSYS/client/bin/rsync-$RSYNCSERV" ]; then
|
||||||
# Else, rename compiled file using Rsync protocol number.
|
mv -f "$OPENGNSYS/client/bin/rsync-$RSYNCSERV" "$OPENGNSYS/client/bin/rsync" 2>/dev/null
|
||||||
[ -e $OPENGNSYS/client/bin/rsync ] && mv -f $OPENGNSYS/client/bin/rsync $OPENGNSYS/client/bin/rsync-$($OPENGNSYS/client/bin/rsync --version 2>/dev/null | awk '/protocol/ {print $6}')
|
fi
|
||||||
|
else
|
||||||
|
if [ -e "$OPENGNSYS/client/bin/rsync" ]; then
|
||||||
|
mv -f "$OPENGNSYS/client/bin/rsync" "$OPENGNSYS/client/bin/rsync-$($OPENGNSYS/client/bin/rsync --version 2>/dev/null | awk '/protocol/ {print $6}')"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
umount $TMPDIR >/dev/null 2>&1
|
||||||
|
rmdir $TMPDIR >/dev/null 2>&1 || rm -rf $TMPDIR >/dev/null 2>&1
|
||||||
fi
|
fi
|
||||||
# Unmount SquashFS.
|
|
||||||
umount $TMPDIR
|
# Crear JSON output
|
||||||
rmdir $TMPDIR
|
JSON_OUTPUT=$(jq -n \
|
||||||
# Update JSON file.
|
--arg dist "$OGLIVEDIST" \
|
||||||
addToJson "$OGLIVEDIST" "$OGLIVEKRNL" "$OGLIVEARCH" "$OGLIVEREV" "$OGLIVEDIR" "$OGLIVEFILE"
|
--arg krnl "$OGLIVEKRNL" \
|
||||||
|
--arg arch "$OGLIVEARCH" \
|
||||||
|
--arg rev "$OGLIVEREV" \
|
||||||
|
--arg dir "$OGLIVEDIR" \
|
||||||
|
--arg iso "$(basename "$OGLIVEFILE")" \
|
||||||
|
'{status: "success", messages: [], result: {distribution: $dist, kernel: $krnl, architecture: $arch, revision: $rev, directory: $dir, iso: $iso}}')
|
||||||
|
|
||||||
|
echo "$JSON_OUTPUT"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Uninstall an ogLive client.
|
# Uninstall an ogLive client.
|
||||||
function uninstall() {
|
function uninstall() {
|
||||||
local ISO DIR INDEX DEFINDEX
|
local CHECKSUM DIR
|
||||||
[ $# -ne 1 ] && raiseError usage
|
|
||||||
[ ! -r $INFOFILE ] && raiseError access "Configuration file."
|
# Validar que se proporcionó exactamente un argumento (el checksum)
|
||||||
[ ! -w $TFTPDIR ] && raiseError access "Installation directory."
|
[ $# -ne 1 ] && { echo "{\"error\": \"usage: uninstall {checksum}\"}"; exit 1; }
|
||||||
# Get index and directory for the entry.
|
|
||||||
case "$1" in
|
CHECKSUM=$1
|
||||||
*/*) # Error (access to other directory).
|
|
||||||
raiseError access "Cannot access outside installation directory."
|
# Verificar acceso a los directorios necesarios
|
||||||
;;
|
[ ! -w $TFTPDIR ] && { echo "{\"error\": \"access installation directory.\"}"; exit 1; }
|
||||||
*.iso) # ISO file.
|
|
||||||
ISO="$1"
|
# Buscar el directorio correspondiente al checksum
|
||||||
# Working directory (ogLive-Distribution-KernelVersion-CodeRevision).
|
DIR=$(find $TFTPDIR -type f -name 'linuxISO.sum' -exec grep -l "$CHECKSUM" {} \; | xargs -I{} dirname {})
|
||||||
DIR="$(echo $ISO|cut -f1,3 -d-)-${ISO##*-}"; DIR=${DIR%.*}
|
|
||||||
INDEX=$(search $DIR 2>/dev/null)
|
# Si no se encuentra el directorio, devolver error
|
||||||
;;
|
if [ -z "$DIR" ]; then
|
||||||
[0-9]*) # Index.
|
echo "{\"error\": \"ogLive client with checksum $CHECKSUM not found.\"}"
|
||||||
INDEX=$1; DIR=$(search $INDEX 2>/dev/null)
|
exit 1
|
||||||
;;
|
|
||||||
*) # Directory.
|
|
||||||
DIR="$1"; INDEX=$(search $DIR 2>/dev/null)
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
DEFINDEX=$(getdefault)
|
|
||||||
[[ $INDEX = $DEFINDEX ]] && raiseError access "Cannot uninstall default ogLive."
|
|
||||||
# Remove files and delete index entry.
|
|
||||||
rm -vfr ${ISO:+$DOWNLOADDIR/$ISO} ${DIR:+$TFTPDIR/$DIR} ### Remove $TFTPDIR/$DIR.old ?
|
|
||||||
if [ -n "$INDEX" ]; then
|
|
||||||
jq "del(.oglive[$INDEX])" $INFOFILE | sponge $INFOFILE
|
|
||||||
# Decrement default index if needed (removed < default).
|
|
||||||
[[ $INDEX < $DEFINDEX ]] && jq ".default=$((DEFINDEX-1))" $INFOFILE | sponge $INFOFILE
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Eliminar archivos y directorio, redirigiendo la salida a /dev/null
|
||||||
|
rm -vfr $DIR > /dev/null 2>&1
|
||||||
|
|
||||||
|
# Comprobar si la eliminación tuvo éxito
|
||||||
|
if [ -d "$DIR" ]; then
|
||||||
|
echo "{\"error\": \"Failed to uninstall ogLive client in $DIR.\"}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Devolver mensaje de éxito
|
||||||
|
echo "{\"message\": \"ogLive client uninstalled successfully.\", \"details\": \"Removed directory: $DIR\"}"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Get default ogLive index.
|
# Get information about the default ogLive client.
|
||||||
function getdefault() {
|
function get_default() {
|
||||||
[ $# -ne 0 ] && raiseError usage
|
local DEFAULT_LINK="$TFTPDIR/$DEFOGLIVE"
|
||||||
[ ! -r $INFOFILE ] && raiseError access "Configuration file."
|
local DIR OGLIVEDIST OGLIVEKRNL OGLIVEARCH OGLIVEREV OGLIVEISO OGLIVEDIR
|
||||||
# Read default parameter.
|
|
||||||
jq -r .default $INFOFILE || raiseError notfound "Undefined default index."
|
# Verificar que el enlace simbólico del ogLive por defecto existe.
|
||||||
|
if [ ! -L "$DEFAULT_LINK" ]; then
|
||||||
|
echo "{\"error\": \"Default ogLive client link not found.\"}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Obtener el directorio real al que apunta el enlace simbólico.
|
||||||
|
DIR=$(readlink -f "$DEFAULT_LINK")
|
||||||
|
|
||||||
|
# Si no se encuentra el directorio, devolver error.
|
||||||
|
if [ -z "$DIR" ]; then
|
||||||
|
echo "{\"error\": \"Default ogLive client directory not found.\"}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Obtener la información del ogLive a partir del nombre del directorio.
|
||||||
|
OGLIVEDIR=$(basename "$DIR")
|
||||||
|
OGLIVEDIST="$(echo $OGLIVEDIR | cut -d- -f2)"
|
||||||
|
OGLIVEKRNL="$(echo $OGLIVEDIR | cut -d- -f3)"
|
||||||
|
OGLIVEARCH="amd64" # Suponiendo que siempre es amd64
|
||||||
|
OGLIVEREV="$(echo $OGLIVEDIR | cut -d- -f4)"
|
||||||
|
OGLIVEISO="" # No tenemos la información del ISO aquí, podría necesitarse un ajuste si se requiere
|
||||||
|
|
||||||
|
# Construir el JSON con la información.
|
||||||
|
local INFO=$(cat << EOT
|
||||||
|
{
|
||||||
|
"distribution": "$OGLIVEDIST",
|
||||||
|
"kernel": "$OGLIVEKRNL",
|
||||||
|
"architecture": "$OGLIVEARCH",
|
||||||
|
"revision": "$OGLIVEREV",
|
||||||
|
"directory": "$DIR",
|
||||||
|
"iso": "$OGLIVEISO"
|
||||||
|
}
|
||||||
|
EOT
|
||||||
|
)
|
||||||
|
|
||||||
|
# Devolver la información en formato JSON.
|
||||||
|
echo "$INFO"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Set default ogLive index.
|
# Set default ogLive client by checksum.
|
||||||
function setdefault() {
|
function set_default() {
|
||||||
local INDEX OGLIVEDIR
|
local CHECKSUM=$1
|
||||||
[ $# -ne 1 ] && raiseError usage
|
local DIR
|
||||||
[ ! -w $INFOFILE ] && raiseError access "Configuration file."
|
|
||||||
INDEX=$1
|
# Validar que se proporcionó exactamente un argumento (el checksum)
|
||||||
# Check if index entry exists.
|
[ $# -ne 1 ] && { echo "{\"error\": \"usage: set-default {checksum}\"}"; exit 1; }
|
||||||
jq ".oglive[$INDEX]" $INFOFILE || raiseError notfound "Index \"$INDEX\"."
|
|
||||||
# Get ogLive directory.
|
# Verificar acceso a los directorios necesarios
|
||||||
OGLIVEDIR=$(jq -r ".oglive[$INDEX].directory" $INFOFILE) || raiseError notfound "Directory for index \"$INDEX\"."
|
[ ! -w $TFTPDIR ] && { echo "{\"error\": \"access installation directory.\"}"; exit 1; }
|
||||||
# Update default parameter.
|
|
||||||
jq ".default=$INDEX" $INFOFILE | sponge $INFOFILE
|
# Buscar el directorio correspondiente al checksum
|
||||||
# Link to default directory.
|
DIR=$(find $TFTPDIR -type f -name 'linuxISO.sum' -exec grep -l "$CHECKSUM" {} \; | xargs -I{} dirname {} | grep -v ".old")
|
||||||
rm -f $TFTPDIR/$DEFOGLIVE
|
|
||||||
ln -vfs $(basename $OGLIVEDIR) $TFTPDIR/$DEFOGLIVE
|
# Si no se encuentra el directorio, devolver error
|
||||||
|
if [ -z "$DIR" ]; then
|
||||||
|
echo "{\"error\": \"ogLive client with checksum $CHECKSUM not found.\"}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Eliminar el enlace simbólico existente y crear uno nuevo
|
||||||
|
rm -f $TFTPDIR/$DEFOGLIVE > /dev/null 2>&1
|
||||||
|
ln -vfs $(basename $DIR) $TFTPDIR/$DEFOGLIVE > /dev/null 2>&1
|
||||||
|
|
||||||
|
# Comprobar si el enlace simbólico se creó correctamente
|
||||||
|
if [ "$(readlink -f $TFTPDIR/$DEFOGLIVE)" == "$(readlink -f $DIR)" ]; then
|
||||||
|
echo "{\"message\": \"ogLive client set as default successfully.\", \"details\": \"$DIR\"}"
|
||||||
|
else
|
||||||
|
echo "{\"error\": \"Failed to set default ogLive client.\"}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Rebuild a lost configuration file.
|
# Rebuild a lost configuration file.
|
||||||
|
@ -489,6 +581,135 @@ local ISOFILE DIR
|
||||||
jq ".oglive[$2].iso=\"$1\"" $INFOFILE | sponge $INFOFILE && jq ".oglive[$2]" $INFOFILE
|
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"\"}"}')
|
||||||
|
echo $DISK_INFO
|
||||||
|
}
|
||||||
|
|
||||||
|
# Function to list installed ogLive clients and the default ogLive client
|
||||||
|
function list_installed_oglives() {
|
||||||
|
local INST NF DEF
|
||||||
|
INST=$(find $TFTPDIR/ -type d -name "$DEFOGLIVE-*" -a ! -name "*.old" -printf "%f\n" | sort)
|
||||||
|
local installed_ogLives=()
|
||||||
|
|
||||||
|
for i in $INST; do
|
||||||
|
NF=$(echo $i | awk -F- '{print NF-1}')
|
||||||
|
local OGLIVEDIST=""
|
||||||
|
local OGLIVEKRNL=""
|
||||||
|
local OGLIVEARCH=""
|
||||||
|
local OGLIVEREV=""
|
||||||
|
local CHECKSUM=""
|
||||||
|
local CHECKSUM_FILE="$TFTPDIR/$i/linuxISO.sum"
|
||||||
|
|
||||||
|
if [ -f "$CHECKSUM_FILE" ]; then
|
||||||
|
CHECKSUM=$(cat "$CHECKSUM_FILE" | cut -d ' ' -f 1)
|
||||||
|
fi
|
||||||
|
|
||||||
|
case $NF in
|
||||||
|
1) OGLIVEDIST="" OGLIVEKRNL=$(echo $i|cut -f2 -d-) OGLIVEARCH="i386" OGLIVEREV="" ;;
|
||||||
|
2) eval $(echo $i | awk -F- '{printf "OGLIVEDIST=\"\" OGLIVEKRNL=%s OGLIVEARCH=amd64 OGLIVEREV=%s OGLIVEDIR=%s",$2,$3,$0}') ;;
|
||||||
|
3) eval $(echo $i | awk -F- '{if ($3=="i386") printf "OGLIVEDIST=\"\" OGLIVEKRNL=%s OGLIVEARCH=%s OGLIVEREV=%s OGLIVEDIR=%s",$2,$3,$4,$0; else printf "OGLIVEDIST=%s OGLIVEKRNL=%s OGLIVEARCH=i386 OGLIVEREV=%s OGLIVEDIR=%s",$2,$3,$4,$0}') ;;
|
||||||
|
4) eval $(echo $i | awk -F- '{printf "OGLIVEDIST=%s OGLIVEKRNL=%s OGLIVEARCH=%s OGLIVEREV=%s OGLIVEDIR=%s",$2,$3,$4,$5,$0}') ;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
local DATA=$(jq -n \
|
||||||
|
--arg id "$CHECKSUM" \
|
||||||
|
--arg dist "$OGLIVEDIST" \
|
||||||
|
--arg krnl "$OGLIVEKRNL" \
|
||||||
|
--arg arch "$OGLIVEARCH" \
|
||||||
|
--arg rev "$OGLIVEREV" \
|
||||||
|
--arg dir "$TFTPDIR/$OGLIVEDIR" \
|
||||||
|
--arg iso "" \
|
||||||
|
'{id: $id, distribution: $dist, kernel: $krnl, architecture: $arch, revision: $rev, directory: $dir, iso: $iso}')
|
||||||
|
|
||||||
|
installed_ogLives+=("$DATA")
|
||||||
|
|
||||||
|
[ -n "$(stat -c "%N" $TFTPDIR/$DEFOGLIVE | awk '$3~/'$i'/ {print}')" ] && DEF="$i"
|
||||||
|
done
|
||||||
|
|
||||||
|
local default_oglive=$(basename $(readlink -f $TFTPDIR/$DEFOGLIVE))
|
||||||
|
|
||||||
|
jq -n \
|
||||||
|
--arg default_oglive "$default_oglive" \
|
||||||
|
--argjson installed_ogLives "$(printf '%s\n' "${installed_ogLives[@]}" | jq -s .)" \
|
||||||
|
'{
|
||||||
|
default_oglive: $default_oglive,
|
||||||
|
installed_ogLives: $installed_ogLives
|
||||||
|
}'
|
||||||
|
}
|
||||||
|
|
||||||
|
# Get information about an installed ogLive client.
|
||||||
|
function get_info() {
|
||||||
|
local CHECKSUM="$1"
|
||||||
|
local DIR OGLIVEDIST OGLIVEKRNL OGLIVEARCH OGLIVEREV OGLIVEISO OGLIVEDIR
|
||||||
|
|
||||||
|
# Verificar que se proporcionó un checksum.
|
||||||
|
[ -z "$CHECKSUM" ] && { echo "{\"error\": \"usage: get_info {checksum}\"}"; exit 1; }
|
||||||
|
|
||||||
|
# Verificar acceso al directorio de instalación.
|
||||||
|
[ ! -w $TFTPDIR ] && { echo "{\"error\": \"access installation directory.\"}"; exit 1; }
|
||||||
|
|
||||||
|
# Buscar el directorio correspondiente al checksum, excluyendo los que terminan en .old.
|
||||||
|
DIR=$(find $TFTPDIR -type f -name 'linuxISO.sum' -exec grep -l "$CHECKSUM" {} \; | grep -v '.old' | xargs -I{} dirname {})
|
||||||
|
|
||||||
|
# Si no se encuentra el directorio, devolver error.
|
||||||
|
if [ -z "$DIR" ]; then
|
||||||
|
echo "{\"error\": \"ogLive client with checksum $CHECKSUM not found.\"}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Obtener la información del ogLive a partir del nombre del directorio.
|
||||||
|
OGLIVEDIR=$(basename "$DIR")
|
||||||
|
OGLIVEDIST="$(echo $OGLIVEDIR | cut -d- -f2)"
|
||||||
|
OGLIVEKRNL="$(echo $OGLIVEDIR | cut -d- -f3)"
|
||||||
|
OGLIVEARCH="amd64" # Suponiendo que siempre es amd64
|
||||||
|
OGLIVEREV="$(echo $OGLIVEDIR | cut -d- -f4)"
|
||||||
|
OGLIVEISO="" # No tenemos la información del ISO aquí, podría necesitarse un ajuste si se requiere
|
||||||
|
|
||||||
|
# Construir el JSON con la información.
|
||||||
|
local INFO=$(cat << EOT
|
||||||
|
{
|
||||||
|
"distribution": "$OGLIVEDIST",
|
||||||
|
"kernel": "$OGLIVEKRNL",
|
||||||
|
"architecture": "$OGLIVEARCH",
|
||||||
|
"revision": "$OGLIVEREV",
|
||||||
|
"directory": "$DIR",
|
||||||
|
"iso": "$OGLIVEISO"
|
||||||
|
}
|
||||||
|
EOT
|
||||||
|
)
|
||||||
|
|
||||||
|
# Devolver la información en formato JSON.
|
||||||
|
echo "$INFO"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Function to check the status of services
|
||||||
|
function check_services_status() {
|
||||||
|
local SERVICES=("oglive_daemon.service" "tftpd-hpa.service" "nginx.service")
|
||||||
|
declare -A STATUS_MAP
|
||||||
|
|
||||||
|
for service in "${SERVICES[@]}"; do
|
||||||
|
if systemctl list-units --type=service --all | grep -q "$service"; then
|
||||||
|
STATUS=$(systemctl is-active "$service")
|
||||||
|
STATUS_MAP[$service]=$STATUS
|
||||||
|
else
|
||||||
|
STATUS_MAP[$service]="not installed"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
local json_output=$(jq -n \
|
||||||
|
--arg oglive_daemon "${STATUS_MAP['oglive_daemon.service']}" \
|
||||||
|
--arg tftpboot "${STATUS_MAP['tftpd-hpa.service']}" \
|
||||||
|
--arg nginx "${STATUS_MAP['nginx.service']}" \
|
||||||
|
'{
|
||||||
|
oglive_daemon: $oglive_daemon,
|
||||||
|
tftpboot: $tftpboot,
|
||||||
|
nginx: $nginx
|
||||||
|
}')
|
||||||
|
|
||||||
|
echo "$json_output"
|
||||||
|
}
|
||||||
|
|
||||||
# Main progrram.
|
# Main progrram.
|
||||||
|
|
||||||
|
@ -501,7 +722,7 @@ which sponge &>/dev/null || raiseError notfound "Need to install \"moreutils\"."
|
||||||
# Commands control.
|
# Commands control.
|
||||||
shopt -s extglob
|
shopt -s extglob
|
||||||
case "$ACCESS" in
|
case "$ACCESS" in
|
||||||
root) CMDS='+(help|version|convert|config|check|list|show|search|download|install|uninstall|get-default|set-default|rebuild|assign)' ;;
|
root) 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)' ;;
|
||||||
web) CMDS='+(list|show|search|get-default)' ;;
|
web) CMDS='+(list|show|search|get-default)' ;;
|
||||||
esac
|
esac
|
||||||
case "$1" in
|
case "$1" in
|
||||||
|
@ -511,4 +732,3 @@ esac
|
||||||
|
|
||||||
exit $?
|
exit $?
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue