#!/bin/bash # ------- # File: llxcfg-netconfig-client # Description: LliureX netconfig client utility # Author: Luis Garcia Gisbert # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin St, Fifth Floor, Boston MA 02110-1301 USA # -------- set -e # PATH y variables PATH="/sbin:/bin:/usr/sbin:/usr/bin" VAR_LIST="NETCFG_URL" LLX_GETVAR="/usr/sbin/llxcfg-showvars" NETCFG_PATH="/var/lib/netconfig-client" NETCFG_DOWNLOAD_DIR="download" NETCFG_DATA_DIR="data" # wget parameters WGET_TRIES=3 WGET_TIMEOUT=10 # ajusta el comando para descargar WGET_CMD="/usr/bin/wget -nc -q -t ${WGET_TRIES} -T ${WGET_TIMEOUT} -O" # Funcs error_message(){ echo -e "$1" >&2 } exit_message(){ error_message "$1" exit 1 } wget_file(){ GET_RC=0 printf "Trying to download ${1} ... " ${WGET_CMD} "${2}" "${1}" &>/dev/null || GET_RC=$? if [ $GET_RC -eq 0 ] ; then echo "[OK]" else echo "[*fail*]" fi return $GET_RC } extract_data(){ [ -r "${TARBALL_FILE}" ] || return 1 if ! tar -C "${TARBALL_DATA}/" -xf "${TARBALL_FILE}" &>/dev/null ; then error_message "Error extracting downloaded data" remove_tarball_data remove_data fail return 1 fi llxcfg-ncfg-scripts post-load || true return 0 } remove_tarball_data(){ rm -fr $(find "${TARBALL_DATA}/" -maxdepth 1 -mindepth 1) } remove_data(){ case $1 in pre-load|revert) llxcfg-ncfg-scripts pre-load || true rm -f "${TARBALL_FILE}" remove_tarball_data ;; fail) rm -f "${TARBALL_FILE}" llxcfg-ncfg-scripts fail || true ;; *) return 1 ;; esac return 0 } get_data(){ [ "${NETCFG_URL}" ] || return 0 # Get remote md5sum DOWNLOAD_STATUS="READY" if [ -d "${TARBALL_DATA}" ] ; then # check md5sum MD5NEW="$(tempfile)" rm -f "${MD5NEW}" if wget_file "${NETCFG_URL}.list" "${MD5NEW}" ; then MD5OLD="$(tempfile)" ( cd ${TARBALL_DATA}; find -xtype f -exec md5sum {} \; ) |sort > "${MD5OLD}" if diff -q "$MD5OLD" "$MD5NEW" &>/dev/null ; then DOWNLOAD_STATUS="NO-CHANGES" echo "No changes found in ${NETCFG_URL}." fi rm -f "$MD5OLD" else DOWNLOAD_STATUS="FAIL" fi rm -f "$MD5NEW" fi if [ "$DOWNLOAD_STATUS" = "NO-CHANGES" ] ; then # do not remove, nor extract but re-execute scripts echo "Running 'post-load' scripts" llxcfg-ncfg-scripts post-load || true return 0 fi remove_data pre-load if [ "$DOWNLOAD_STATUS" = "READY" ] ; then wget_file "${NETCFG_URL}" "${TARBALL_FILE}" || DOWNLOAD_STATUS="FAIL" fi if [ "$DOWNLOAD_STATUS" = "FAIL" ] ; then remove_data fail error_message "Configuration update from ${NETCFG_URL} FAILED" return 1 fi extract_data || return 1 echo "Configuration SUCCESSFULLY updated from ${NETCFG_URL}" return 0 } usage() { exit_message "Usage: $0 {get-data|remove-data}\n\ $0 {getdirs|getfiles} [NAME]" } # main # libs LIB_FILE="/usr/share/lliurex/llxcfg-netconfig/llxcfg-netconfig.sh" [ -e ${LIB_FILE} ] || exit_message "Missing variable library file ${LIB_FILE}!!" . ${LIB_FILE} TARBALL_FILE="${NETCFG_PATH}/${NETCFG_DOWNLOAD_DIR}/${NETCFG_TARBALL}" TARBALL_DATA="${NETCFG_PATH}/${NETCFG_DATA_DIR}" # read vars [ -x ${LLX_GETVAR} ] && eval `${LLX_GETVAR} ${VAR_LIST}` rc=0 DATA_FIND_OPTIONS="${NETCFG_PATH}/${NETCFG_DATA_DIR}/ -maxdepth 1 -mindepth 1 -type d" [ -z "$2" ] || DATA_FIND_OPTIONS="$DATA_FIND_OPTIONS -name $2" case "$1" in get-data) get_data || rc=$? ;; remove-data) remove_data revert || rc=$? ;; getdirs) find $DATA_FIND_OPTIONS ;; getfiles) find $DATA_FIND_OPTIONS -exec find {} -xtype f \; ;; *) usage ;; esac exit $rc