From 4972d15ecd21a6f8dfe6f8451d19ac678fa2865b Mon Sep 17 00:00:00 2001 From: Roberto Hueso Gómez Date: Mon, 19 Oct 2020 13:38:11 +0200 Subject: #988 Read json config file in scripts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch implements the function source_json_config() that is used to read the new ogserver.json config file in the scripts. This replaces the old 'source .../ogserver.cfg'. Co-authored-by: Javier Sánchez Parra --- installer/opengnsys_export.sh | 6 ++++-- installer/opengnsys_import.sh | 5 +++-- installer/opengnsys_update.sh | 7 ++++++- repoman/bin/importimage | 4 ++-- server/bin/addtodhcp | 4 ++-- server/bin/comparedhcpopengnsys | 4 ++-- server/bin/comparemac | 4 ++-- server/bin/listclientlive | 4 ++-- server/bin/listclientmode | 6 ++---- server/bin/ogagentqueue.cron | 4 ++-- server/bin/setclientlive | 4 ++-- server/bin/setserveraddr | 8 +++++--- server/lib/checkrest | 4 ++-- server/lib/ogfunctions.sh | 12 ++++++++++++ server/lib/supportsave | 4 ++-- 15 files changed, 50 insertions(+), 30 deletions(-) diff --git a/installer/opengnsys_export.sh b/installer/opengnsys_export.sh index 67ad008e..1fe7e081 100755 --- a/installer/opengnsys_export.sh +++ b/installer/opengnsys_export.sh @@ -57,7 +57,7 @@ if ! which realpath &>/dev/null ; then fi # Comprobamos acceso a ficheros de configuración -if ! [ -r $OPENGNSYS/etc/ogserver.cfg ]; then +if ! [ -r $OPENGNSYS/etc/ogserver.json ]; then echo "$PROG: ERROR: Sin acceso a la configuración de OpenGnsys." | tee -a $FILESAL exit 3 fi @@ -74,7 +74,9 @@ done # Exportar la base de datos echo "Exportamos la información de la base de datos." -source $OPENGNSYS/etc/ogserver.cfg +source $OPENGNSYS/lib/ogfunctions.sh || exit 1 +source_json_config $OPENGNSYS/etc/ogserver.json + # Crear fichero temporal de acceso a la BD MYCNF=$(mktemp /tmp/.my.cnf.XXXXX) chmod 600 $MYCNF diff --git a/installer/opengnsys_import.sh b/installer/opengnsys_import.sh index 0962e34b..d49c3312 100755 --- a/installer/opengnsys_import.sh +++ b/installer/opengnsys_import.sh @@ -278,7 +278,7 @@ if ! [ -r $BACKUPFILE ]; then fi # Comprobamos acceso a ficheros de configuración -if ! [ -r $OPENGNSYS/etc/ogserver.cfg ]; then +if ! [ -r $OPENGNSYS/etc/ogserver.json ]; then echo "$PROG: ERROR: Sin acceso a la configuración de OpenGnsys." | tee -a $FILESAL exit 3 fi @@ -333,7 +333,8 @@ fi # MYSQL echo " * Importamos informacion mysql." -source $OPENGNSYS/etc/ogserver.cfg +source $OPENGNSYS/lib/ogfunctions.sh || exit 1 +source_json_config $OPENGNSYS/etc/ogserver.json # Crear fichero temporal de acceso a la BD if [ ! -r $MYCNF ]; then chmod 600 $MYCNF diff --git a/installer/opengnsys_update.sh b/installer/opengnsys_update.sh index bd689008..11c64a8e 100755 --- a/installer/opengnsys_update.sh +++ b/installer/opengnsys_update.sh @@ -52,8 +52,13 @@ if [ ! -d $INSTALL_TARGET ]; then echo "ERROR: OpenGnsys is not installed, cannot update!!" exit 1 fi + +source $INSTALL_TARGET/lib/ogfunctions.sh || exit 1 + # Cargar configuración de acceso a la base de datos. -if [ -r $INSTALL_TARGET/etc/ogserver.cfg ]; then +if [ -r $INSTALL_TARGET/etc/ogserver.json ]; then + source_json_config $INSTALL_TARGET/etc/ogserver.json +elif [ -r $INSTALL_TARGET/etc/ogserver.cfg ]; then source $INSTALL_TARGET/etc/ogserver.cfg elif [ -r $INSTALL_TARGET/etc/ogAdmServer.cfg ]; then source $INSTALL_TARGET/etc/ogAdmServer.cfg diff --git a/repoman/bin/importimage b/repoman/bin/importimage index db652f9d..9ced5b78 100755 --- a/repoman/bin/importimage +++ b/repoman/bin/importimage @@ -20,16 +20,16 @@ OPENGNSYS=${OPENGNSYS:-"/opt/opengnsys"} REPODIR="$OPENGNSYS/images" REPOCONF="$OPENGNSYS/etc/ogAdmRepo.cfg" -SERVERCONF="$OPENGNSYS/etc/ogserver.cfg" +SERVERCONF="$OPENGNSYS/etc/ogserver.json" DEFAULTFILE="/etc/default/opengnsys" let BACKUP=0 source $DEFAULTFILE source $REPOCONF &>/dev/null -[ "$RUN_OGADMSERVER" == "yes" ] && source $SERVERCONF &>/dev/null # Functions. source $OPENGNSYS/lib/ogfunctions.sh || exit 1 +[ "$RUN_OGADMSERVER" == "yes" ] && source_json_config $SERVERCONF &>/dev/null # Main program. diff --git a/server/bin/addtodhcp b/server/bin/addtodhcp index 42fe7f8e..80fc5fac 100755 --- a/server/bin/addtodhcp +++ b/server/bin/addtodhcp @@ -15,7 +15,7 @@ # Variables. PROG="$(basename "$0")" OPENGNSYS=${OPENGNSYS:-"/opt/opengnsys"} -SERVERCONF=$OPENGNSYS/etc/ogserver.cfg +SERVERCONF=$OPENGNSYS/etc/ogserver.json DHCPCONF=/etc/dhcp/dhcpd.conf DHCPCONFBCK="$DHCPCONF-$(date +"%Y%m%d")" @@ -26,7 +26,7 @@ source $OPENGNSYS/lib/ogfunctions.sh || exit 1 [ "$*" == "version" ] && version # Error control. [ "$USER" != "root" ] && raiseError access "Need to be root" -source $SERVERCONF 2>/dev/null || raiseError access "Cannot read OpenGnsys Server configuration file" +source_json_config $SERVERCONF 2>/dev/null || raiseError access "Cannot read OpenGnsys Server configuration file" # Processing parameters. opts=$(getopt -n "$PROG" -l exam,file:,restart -o 'ef:r' -- "$@" ) || raiseError usage diff --git a/server/bin/comparedhcpopengnsys b/server/bin/comparedhcpopengnsys index b4743e42..64ccfbdc 100755 --- a/server/bin/comparedhcpopengnsys +++ b/server/bin/comparedhcpopengnsys @@ -16,7 +16,7 @@ PROG="$(basename "$0")" OPENGNSYS=${OPENGNSYS:-"/opt/opengnsys"} -SERVERCONF=$OPENGNSYS/etc/ogserver.cfg +SERVERCONF=$OPENGNSYS/etc/ogserver.json DHCPFILE=${DHCPFILE:-/etc/dhcp/dhcpd.conf} TMP_DHCP=/tmp/$PROG-dhcp$$ TMP_MYSQL=/tmp/$PROG-mysql$$ @@ -29,7 +29,7 @@ source $OPENGNSYS/lib/ogfunctions.sh || exit 1 [ -r $DHCPFILE ] || raiseError access "No tenemos acceso al fichero de configuracion del dhcp" # Obtener datos de acceso a la Base de datos. -source $SERVERCONF 2> /dev/null || raiseError access "Fichero de configuración del servidor" +source_json_config $SERVERCONF 2> /dev/null || raiseError access "Fichero de configuración del servidor" trap "rm -f $TMP_DHCP $TMP_MYSQL" 1 2 3 9 15 # Genero fichero temporal de dhcp recorriendo la configuración del dhcp cadena a cadena. diff --git a/server/bin/comparemac b/server/bin/comparemac index 6b1c6eff..7d893ec7 100755 --- a/server/bin/comparemac +++ b/server/bin/comparemac @@ -12,7 +12,7 @@ # Variables. OPENGNSYS=${OPENGNSYS:-"/opt/opengnsys"} -SERVERCONF=$OPENGNSYS/etc/ogserver.cfg +SERVERCONF=$OPENGNSYS/etc/ogserver.json # Funciones globales. source $OPENGNSYS/lib/ogfunctions.sh || exit 1 @@ -26,7 +26,7 @@ source $OPENGNSYS/lib/ogfunctions.sh || exit 1 which arp-scan &>/dev/null || raiseError notfound "Need to install \"arp-scan\"" # Obtener datos de acceso a la base de datos. -source $SERVERCONF +source_json_config $SERVERCONF # Borrar ficheros temporales si termina el proceso. trap "rm -f $TMPSQL $TMPSCAN" 0 1 2 3 6 9 15 diff --git a/server/bin/listclientlive b/server/bin/listclientlive index 0e7f1c4a..d8104d7a 100755 --- a/server/bin/listclientlive +++ b/server/bin/listclientlive @@ -12,7 +12,7 @@ # Variables. OPENGNSYS=${OPENGNSYS:-"/opt/opengnsys"} -SERVERCONF=$OPENGNSYS/etc/ogserver.cfg +SERVERCONF=$OPENGNSYS/etc/ogserver.json TFTPDIR=$OPENGNSYS/tftpboot source $OPENGNSYS/lib/ogfunctions.sh || exit 1 @@ -22,7 +22,7 @@ source $OPENGNSYS/lib/ogfunctions.sh || exit 1 [ "$*" == "version" ] && version [ $# -eq 1 ] || raiseError usage [ "$USER" != "root" ] && raiseError access "Need to be root" -source $SERVERCONF 2>/dev/null || raiseError access "Sin acceso a fichero de configuración" +source_json_config $SERVERCONF 2>/dev/null || raiseError access "Sin acceso a fichero de configuración" # Sustituir caracteres ' por \' para evitar inyección SQL. RESOURCE="${1//\'/\\\'}" diff --git a/server/bin/listclientmode b/server/bin/listclientmode index d4bac228..656ece99 100755 --- a/server/bin/listclientmode +++ b/server/bin/listclientmode @@ -15,7 +15,7 @@ # Variables. OPENGNSYS=${OPENGNSYS:-"/opt/opengnsys"} -SERVERCONF=$OPENGNSYS/etc/ogserver.cfg +SERVERCONF=$OPENGNSYS/etc/ogserver.json TMPLDIR=$OPENGNSYS/tftpboot/menu.lst/templates source $OPENGNSYS/lib/ogfunctions.sh || exit 1 @@ -25,10 +25,8 @@ source $OPENGNSYS/lib/ogfunctions.sh || exit 1 [ "$*" == "version" ] && version [ $# -eq 1 ] || raiseError usage [ "$USER" != "root" ] && raiseError access "Need to be root" -source $SERVERCONF 2>/dev/null || raiseError access "Sin acceso a fichero de configuración" +source_json_config $SERVERCONF 2>/dev/null || raiseError access "Sin acceso a fichero de configuración" -# Obtener datos de acceso a la Base de datos. -source $SERVERCONF # Sustituir caracteres ' por \' para evitar inyección SQL. RESOURCE="${1//\'/\\\'}" # Obtener nombre de ordenador individual o todos los de una aula e indicar su plantilla asociada. diff --git a/server/bin/ogagentqueue.cron b/server/bin/ogagentqueue.cron index 16f7c67e..80dc2a84 100755 --- a/server/bin/ogagentqueue.cron +++ b/server/bin/ogagentqueue.cron @@ -13,13 +13,13 @@ # Variables. PROG=$(basename "$0") OPENGNSYS=${OPENGNSYS:-"/opt/opengnsys"} -SERVERCONF=$OPENGNSYS/etc/ogserver.cfg +SERVERCONF=$OPENGNSYS/etc/ogserver.json LOGFILE=$OPENGNSYS/log/remotepc.log source $OPENGNSYS/lib/ogfunctions.sh || exit 1 # Basic error control -source $SERVERCONF 2> /dev/null || raiseError access "Server configuration file" +source_json_config $SERVERCONF 2> /dev/null || raiseError access "Server configuration file" touch "$LOGFILE" 2> /dev/null || raiseError access "Cannot write in the log file" # Reading pending operations. diff --git a/server/bin/setclientlive b/server/bin/setclientlive index 0424e605..64eedabf 100755 --- a/server/bin/setclientlive +++ b/server/bin/setclientlive @@ -17,7 +17,7 @@ PROG="$(basename "$0")" OPENGNSYS=${OPENGNSYS:-"/opt/opengnsys"} PATH=$PATH:$OPENGNSYS/bin -SERVERCONF=$OPENGNSYS/etc/ogserver.cfg +SERVERCONF=$OPENGNSYS/etc/ogserver.json TFTPDIR=$OPENGNSYS/tftpboot LOGFILE=$OPENGNSYS/log/opengnsys.log @@ -28,7 +28,7 @@ source $OPENGNSYS/lib/ogfunctions.sh || exit 1 [ "$*" == "version" ] && version [ $# -eq 2 ] || raiseError usage [ "$USER" != "root" ] && raiseError access "Need to be root" -source $SERVERCONF 2>/dev/null || raiseError access "Sin acceso a fichero de configuración" +source_json_config $SERVERCONF 2>/dev/null || raiseError access "Sin acceso a fichero de configuración" case "$1" in [0-9]*) DIR=$(oglivecli search $1 2>/dev/null) ;; diff --git a/server/bin/setserveraddr b/server/bin/setserveraddr index b5092ae5..b9249af7 100755 --- a/server/bin/setserveraddr +++ b/server/bin/setserveraddr @@ -33,7 +33,7 @@ source $OPENGNSYS/lib/ogfunctions.sh || exit 1 [ "$USER" != "root" ] && raiseError access "Need to be root" [ $# -ne 1 ] && raiseError usage [ -r $DEFAULTFILE ] || raiseError access "Cannot read default configuration file" -for f in $OPENGNSYS/{etc/{ogserver,ogAdmRepo}.cfg,www/controlacceso.php,client/ogClient/cfg/ogclient.json}; do +for f in $OPENGNSYS/{etc/ogserver.json,etc/ogAdmRepo.cfg,www/controlacceso.php,client/ogClient/cfg/ogclient.json}; do [ -w $f ] || raiseError access "Cannot write to file: $f" done @@ -84,15 +84,17 @@ if [ -n "$SERVERIP" ]; then fi # Saving old IP address. - source $OPENGNSYS/etc/ogserver.cfg + source_json_config $OPENGNSYS/etc/ogserver.json OLDSERVERIP=$ServidorAdm # Checking if configuration files need to be modified. CHANGE=0 - for f in $OPENGNSYS/{etc/{ogserver,ogAdmRepo}.cfg,www/controlacceso.php}; do + for f in $OPENGNSYS/{etc/ogserver.json,etc/ogAdmRepo.cfg,www/controlacceso.php}; do # Updating configuration variables (if URL does not contain "localhost"). sed -e "s,\(ServidorAdm\|IPlocal\)=.*,\1=$SERVERIP," \ -e "s,^INTERFACE=.*,INTERFACE=$SERVERDEV," \ -e "s,UrlMenu=https?://\([^/]*\)/\(.*\),UrlMenu=https://$SERVERIP/\2," \ + -e "s/\"ip\": \".*\"/\"ip\": \"$SERVERIP\"/" $SERVERCFG \ + -e "s/\"interface\": \".*\"/\"interface\": \"$SERVERDEV\"/" $SERVERCFG \ -e '/localhost/!s,https\?://[^/]*/\(.*\),https://'$SERVERIP'/\1,' $f >$tmpfile file="${f/./-$SERVERDEV.}" # Copying updated file, if needed. diff --git a/server/lib/checkrest b/server/lib/checkrest index 3fcd8bd1..c88ff53d 100755 --- a/server/lib/checkrest +++ b/server/lib/checkrest @@ -12,7 +12,7 @@ PROG=$(basename "$0") OPENGNSYS=/opt/opengnsys -CONFIGFILE=$OPENGNSYS/etc/ogserver.cfg +CONFIGFILE=$OPENGNSYS/etc/ogserver.json RESTURL="https://localhost/opengnsys/rest" source $OPENGNSYS/lib/ogfunctions.sh || exit 1 @@ -128,7 +128,7 @@ function runtests () { # Check dependencies. which jq &>/dev/null || raiseError notfound "Need to install \"jq\"." -source $CONFIGFILE +source_json_config $CONFIGFILE deletedata loaddata runtests diff --git a/server/lib/ogfunctions.sh b/server/lib/ogfunctions.sh index 30da3107..cf701925 100755 --- a/server/lib/ogfunctions.sh +++ b/server/lib/ogfunctions.sh @@ -110,6 +110,18 @@ function jq() { echo "$OUTPUT" } +function source_json_config() { + FILE=$1 + export ServidorAdm=$(jq -r ".rest.ip" $FILE) + export PUERTO=$(jq -r ".rest.port" $FILE) + export APITOKEN=$(jq -r ".rest.api_token" $FILE) + export USUARIO=$(jq -r ".database.user" $FILE) + export PASSWORD=$(jq -r ".database.pass" $FILE) + export datasource=$(jq -r ".database.ip" $FILE) + export CATALOG=$(jq -r ".database.name" $FILE) + export INTERFACE=$(jq -r ".wol.interface" $FILE) +} + # Private function to acts on a service (do not use directly). function _service() { local ACTION="$1" diff --git a/server/lib/supportsave b/server/lib/supportsave index 8d5c2dd9..5a20545d 100755 --- a/server/lib/supportsave +++ b/server/lib/supportsave @@ -180,9 +180,9 @@ fi echo "- Directory list" ls -Ral ${home_dir} >> $backup_dir/opengnsys_files.txt 2>&1 -if [ -r ${home_dir}/etc/ogserver.cfg ]; then +if [ -r ${home_dir}/etc/ogserver.json ]; then echo "- Database schema" - source ${home_dir}/etc/ogserver.cfg + source_json_config ${home_dir}/etc/ogserver.json mysqldump -u "$USUARIO" -p"$PASSWORD" -d "$CATALOG" >> ${backup_dir}/opengnsys_schema.sql 2>&1 else echo "- WARNING: The OpenGnsys database can not be accessed" | tee ${backup_dir}/db_schema.txt -- cgit v1.2.3-18-g5258