refs #477 Adds oglivecli with new functions and configurationç
parent
72adbf37cf
commit
5b1cf84fe1
430
bin/oglivecli
430
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\"."
|
||||
}
|
||||
|
||||
# Show a menu to select and download an ogLive ISO image from the OpenGnsys website.
|
||||
function download() {
|
||||
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.
|
||||
function downloadMenu() {
|
||||
|
||||
local OGLIVE NISOS i HTTPCODE ISOREL
|
||||
OGLIVE=( $(curl -k --silent $DOWNLOADURL | grep "$DEFOGLIVE.*iso") )
|
||||
NISOS=${#OGLIVE[@]}
|
||||
echo "Descargas disponibles (+ = instalado, * = compatibilidad completa):"
|
||||
|
||||
local downloads=()
|
||||
|
||||
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 -ge $MINREL ] && OGLIVE[i-1]="(*) ${OGLIVE[i-1]}"
|
||||
echo "$i) ${OGLIVE[i-1]}"
|
||||
[ $ISOREL -ge $MINREL ] && compatible=true
|
||||
|
||||
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
|
||||
|
||||
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.
|
||||
|
@ -312,146 +326,224 @@ function download_old() {
|
|||
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.
|
||||
function install() {
|
||||
local OGLIVEFILE OGLIVEDIST OGLIVEREV OGLIVEKRNL OGLIVEDIR OGINITRD OGSQFS OGCLIENT=ogclient
|
||||
local COMPRESS SAMBAPASS TMPDIR RSYNCSERV RSYNCCLNT
|
||||
[ $# -ne 1 ] && raiseError usage
|
||||
local COMPRESS SAMBAPASS TMPDIR RSYNCSERV RSYNCCLNT JSON_OUTPUT
|
||||
[ $# -ne 1 ] && { echo "{\"status\": \"error\", \"error\": \"usage\"}"; exit 1; }
|
||||
|
||||
OGLIVEFILE=$(realpath $DOWNLOADDIR/$1)
|
||||
# Only 1 file in pathname expansion.
|
||||
[ $(echo $OGLIVEFILE | wc -w) -gt 1 ] && raiseError usage
|
||||
[ ! -f $OGLIVEFILE ] && raiseError notfound "Downloaded file: \"$1\"."
|
||||
[ ! -r $OGLIVEFILE ] && raiseError access "Downloaded file: \"$1\"."
|
||||
[ ! -w $(dirname $INFOFILE) ] && raiseError access "Configuration directory."
|
||||
[ ! -w $TFTPDIR ] && raiseError access "Installation directory."
|
||||
[ -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
|
||||
[ $(echo $OGLIVEFILE | wc -w) -gt 1 ] && { echo "{\"status\": \"error\", \"error\": \"usage\"}"; exit 1; }
|
||||
[ ! -f $OGLIVEFILE ] && { echo "{\"status\": \"error\", \"error\": \"not found $1.\"}"; exit 1; }
|
||||
[ ! -r $OGLIVEFILE ] && { echo "{\"status\": \"error\", \"error\": \"access $1.\"}"; exit 1; }
|
||||
[ ! -w $(dirname $INFOFILE) ] && { echo "{\"status\": \"error\", \"error\": \"access configuration directory.\"}"; exit 1; }
|
||||
[ ! -w $TFTPDIR ] && { echo "{\"status\": \"error\", \"error\": \"access installation directory.\"}"; exit 1; }
|
||||
[ -z "$(file -b $OGLIVEFILE | grep "ISO.*ogClient")" ] && { echo "{\"status\": \"error\", \"error\": \"File is not an ogLive ISO image.\"}"; exit 1; }
|
||||
|
||||
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) # Get architecture.
|
||||
i386|amd64)
|
||||
OGLIVEKRNL="${OGLIVEKRNL%-$OGLIVEARCH}" ;;
|
||||
*) # 32-bit by default.
|
||||
*)
|
||||
OGLIVEARCH="i386" ;;
|
||||
esac
|
||||
OGLIVEDIR="$TFTPDIR/$DEFOGLIVE-${OGLIVEKRNL%%-*}-$OGLIVEARCH-$OGLIVEREV"
|
||||
OGLIVEDIR="${OGLIVEDIR/amd64-/}"
|
||||
# Get current or default Samba key.
|
||||
|
||||
OGINITRD=$OGLIVEDIR/oginitrd.img
|
||||
[ ! -r $OGINITRD ] && OGINITRD=$TFTPDIR/$DEFOGLIVE/oginitrd.img
|
||||
if [ -r $OGINITRD ]; then
|
||||
COMPRESS=$(file -b "$OGINITRD" | awk '{print tolower($1);}')
|
||||
SAMBAPASS=$($COMPRESS -dc $OGINITRD | \
|
||||
cpio -i --to-stdout scripts/ogfunctions 2>&1 | \
|
||||
sed -n '/^[ ].*OPTIONS=/s/.*pass=\(\w*\).*/\1/p')
|
||||
cpio -i --to-stdout scripts/ogfunctions 2>/dev/null | \
|
||||
sed -n '/^[ \t].*OPTIONS=/s/.*pass=\(\w*\).*/\1/p')
|
||||
fi
|
||||
# Make ogLive backup.
|
||||
|
||||
rm -fr ${OGLIVEDIR}.old
|
||||
mv -fv $OGLIVEDIR ${OGLIVEDIR}.old 2>/dev/null
|
||||
# Mount ogLive ISO image, update its files and unmount it.
|
||||
mv -f $OGLIVEDIR ${OGLIVEDIR}.old 2>/dev/null
|
||||
|
||||
TMPDIR=/tmp/${OGLIVEFILE%.iso}
|
||||
mkdir -p $OGLIVEDIR $TMPDIR
|
||||
trap "umount $TMPDIR; rm -fr $TMPDIR" 1 2 3 6 9 15
|
||||
mount -o loop,ro $OGLIVEFILE $TMPDIR
|
||||
cp -va $TMPDIR/ogclient/* $OGLIVEDIR || raiseError access "Cannot copy files to ogLive directory."
|
||||
umount $TMPDIR
|
||||
# Link to default directory if it's the first ogLive.
|
||||
mount -o loop,ro $OGLIVEFILE $TMPDIR >/dev/null 2>&1 || { echo "{\"status\": \"error\", \"error\": \"mount failed.\"}"; exit 1; }
|
||||
cp -va $TMPDIR/ogclient/* $OGLIVEDIR >/dev/null 2>&1 || { echo "{\"status\": \"error\", \"error\": \"Cannot copy files to ogLive directory.\"}"; exit 1; }
|
||||
umount $TMPDIR >/dev/null 2>&1
|
||||
|
||||
if [ ! -f $INFOFILE ]; then
|
||||
rm -f $TFTPDIR/$DEFOGLIVE $TFTPDIR/$OGCLIENT
|
||||
ln -vfs $(basename $OGLIVEDIR) $TFTPDIR/$DEFOGLIVE
|
||||
ln -vfs $DEFOGLIVE $TFTPDIR/$OGCLIENT
|
||||
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 >/dev/null 2>&1 || { echo "{\"status\": \"error\", \"error\": \"Linking to $TFTPDIR/$OGCLIENT failed.\"}"; exit 1; }
|
||||
fi
|
||||
# Recover or ask for a new Samba access key.
|
||||
|
||||
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
|
||||
$OPENGNSYS/bin/setsmbpass "$(basename $OGLIVEDIR)"
|
||||
$OPENGNSYS/bin/setsmbpass "$(basename $OGLIVEDIR)" >/dev/null 2>&1 || { echo "{\"status\": \"error\", \"error\": \"setsmbpass failed.\"}"; exit 1; }
|
||||
fi
|
||||
# Set permissions.
|
||||
find -L $OGLIVEDIR -type d -exec chmod 755 {} \;
|
||||
find -L $OGLIVEDIR -type f -exec chmod 644 {} \;
|
||||
chown -R :opengnsys $OGLIVEDIR
|
||||
# Mount SquashFS and check Rsync version.
|
||||
|
||||
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 {} \; >/dev/null 2>&1 || { echo "{\"status\": \"error\", \"error\": \"chmod files failed.\"}"; exit 1; }
|
||||
chown -R :opengnsys $OGLIVEDIR >/dev/null 2>&1 || { echo "{\"status\": \"error\", \"error\": \"chown failed.\"}"; exit 1; }
|
||||
|
||||
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}')
|
||||
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
|
||||
else
|
||||
# Else, rename compiled file using Rsync protocol number.
|
||||
[ -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}')
|
||||
|
||||
if [ -z "$RSYNCSERV" ] || [ "$RSYNCSERV" -gt "${RSYNCCLNT:-1}" ]; then
|
||||
if [ -e "$OPENGNSYS/client/bin/rsync-$RSYNCSERV" ]; then
|
||||
mv -f "$OPENGNSYS/client/bin/rsync-$RSYNCSERV" "$OPENGNSYS/client/bin/rsync" 2>/dev/null
|
||||
fi
|
||||
# Unmount SquashFS.
|
||||
umount $TMPDIR
|
||||
rmdir $TMPDIR
|
||||
# Update JSON file.
|
||||
addToJson "$OGLIVEDIST" "$OGLIVEKRNL" "$OGLIVEARCH" "$OGLIVEREV" "$OGLIVEDIR" "$OGLIVEFILE"
|
||||
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
|
||||
|
||||
# Crear JSON output
|
||||
JSON_OUTPUT=$(jq -n \
|
||||
--arg dist "$OGLIVEDIST" \
|
||||
--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.
|
||||
function uninstall() {
|
||||
local ISO DIR INDEX DEFINDEX
|
||||
[ $# -ne 1 ] && raiseError usage
|
||||
[ ! -r $INFOFILE ] && raiseError access "Configuration file."
|
||||
[ ! -w $TFTPDIR ] && raiseError access "Installation directory."
|
||||
# Get index and directory for the entry.
|
||||
case "$1" in
|
||||
*/*) # Error (access to other directory).
|
||||
raiseError access "Cannot access outside installation directory."
|
||||
;;
|
||||
*.iso) # ISO file.
|
||||
ISO="$1"
|
||||
# Working directory (ogLive-Distribution-KernelVersion-CodeRevision).
|
||||
DIR="$(echo $ISO|cut -f1,3 -d-)-${ISO##*-}"; DIR=${DIR%.*}
|
||||
INDEX=$(search $DIR 2>/dev/null)
|
||||
;;
|
||||
[0-9]*) # Index.
|
||||
INDEX=$1; DIR=$(search $INDEX 2>/dev/null)
|
||||
;;
|
||||
*) # 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
|
||||
local CHECKSUM DIR
|
||||
|
||||
# Validar que se proporcionó exactamente un argumento (el checksum)
|
||||
[ $# -ne 1 ] && { echo "{\"error\": \"usage: uninstall {checksum}\"}"; exit 1; }
|
||||
|
||||
CHECKSUM=$1
|
||||
|
||||
# Verificar acceso a los directorios necesarios
|
||||
[ ! -w $TFTPDIR ] && { echo "{\"error\": \"access installation directory.\"}"; exit 1; }
|
||||
|
||||
# Buscar el directorio correspondiente al checksum
|
||||
DIR=$(find $TFTPDIR -type f -name 'linuxISO.sum' -exec grep -l "$CHECKSUM" {} \; | 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
|
||||
|
||||
# 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.
|
||||
function getdefault() {
|
||||
[ $# -ne 0 ] && raiseError usage
|
||||
[ ! -r $INFOFILE ] && raiseError access "Configuration file."
|
||||
# Read default parameter.
|
||||
jq -r .default $INFOFILE || raiseError notfound "Undefined default index."
|
||||
# Get information about the default ogLive client.
|
||||
function get_default() {
|
||||
local DEFAULT_LINK="$TFTPDIR/$DEFOGLIVE"
|
||||
local DIR OGLIVEDIST OGLIVEKRNL OGLIVEARCH OGLIVEREV OGLIVEISO OGLIVEDIR
|
||||
|
||||
# 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.
|
||||
function setdefault() {
|
||||
local INDEX OGLIVEDIR
|
||||
[ $# -ne 1 ] && raiseError usage
|
||||
[ ! -w $INFOFILE ] && raiseError access "Configuration file."
|
||||
INDEX=$1
|
||||
# Check if index entry exists.
|
||||
jq ".oglive[$INDEX]" $INFOFILE || raiseError notfound "Index \"$INDEX\"."
|
||||
# Get ogLive directory.
|
||||
OGLIVEDIR=$(jq -r ".oglive[$INDEX].directory" $INFOFILE) || raiseError notfound "Directory for index \"$INDEX\"."
|
||||
# Update default parameter.
|
||||
jq ".default=$INDEX" $INFOFILE | sponge $INFOFILE
|
||||
# Link to default directory.
|
||||
rm -f $TFTPDIR/$DEFOGLIVE
|
||||
ln -vfs $(basename $OGLIVEDIR) $TFTPDIR/$DEFOGLIVE
|
||||
# Set default ogLive client by checksum.
|
||||
function set_default() {
|
||||
local CHECKSUM=$1
|
||||
local DIR
|
||||
|
||||
# Validar que se proporcionó exactamente un argumento (el checksum)
|
||||
[ $# -ne 1 ] && { echo "{\"error\": \"usage: set-default {checksum}\"}"; exit 1; }
|
||||
|
||||
# Verificar acceso a los directorios necesarios
|
||||
[ ! -w $TFTPDIR ] && { echo "{\"error\": \"access installation directory.\"}"; exit 1; }
|
||||
|
||||
# Buscar el directorio correspondiente al checksum
|
||||
DIR=$(find $TFTPDIR -type f -name 'linuxISO.sum' -exec grep -l "$CHECKSUM" {} \; | xargs -I{} dirname {} | grep -v ".old")
|
||||
|
||||
# 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.
|
||||
|
@ -489,6 +581,135 @@ local ISOFILE DIR
|
|||
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.
|
||||
|
||||
|
@ -501,7 +722,7 @@ which sponge &>/dev/null || raiseError notfound "Need to install \"moreutils\"."
|
|||
# Commands control.
|
||||
shopt -s extglob
|
||||
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)' ;;
|
||||
esac
|
||||
case "$1" in
|
||||
|
@ -511,4 +732,3 @@ esac
|
|||
|
||||
exit $?
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue