diff options
Diffstat (limited to 'data/fetch-config')
| -rw-r--r-- | data/fetch-config | 178 |
1 files changed, 178 insertions, 0 deletions
diff --git a/data/fetch-config b/data/fetch-config new file mode 100644 index 00000000..556f0f41 --- /dev/null +++ b/data/fetch-config @@ -0,0 +1,178 @@ +#!/bin/sh +# Copyright (c) 2013 - OpenSLX GmbH +# +# This program is free software distributed under the GPL version 2. +# See http://openslx.org/COPYING +# +# If you have any feedback please consult http://openslx.org/feedback and +# send your feedback to feedback@openslx.org +# +# General information about OpenSLX can be found under http://openslx.org +# +# Fetch configuration from server ... + +############################################################################# + +# Depends on configured networking, provides configuration + + +# Todo: rewrite for next-gen system - principle would be the same ... + +# Function for retrieving configuration file (machine-setup) via tftp from a +# predefined server or given source (file=tftp-server:/path via kernel +# command line) +unpack () { +# $1 is config file name to get, $2 IP of server to get file from +local dst=$1 +if [ -s $dst ] ; then + # fixme: handle different types of packaging (gzip/bzip2)?? + if ! tar -xpzf $dst 2> /tmp/ConfTGZ-tar-error ; then + cat /tmp/ConfTGZ-tar-error + error "$unpack_ConfTGZ" nonfatal + rm /tmp/ConfTGZ-tar-error + fi + [ "$DEBUGLEVEL" -le 2 -o "$DEBUGLEVEL" -eq 8 ] && rm $dst + return 0 +else + return 1 +fi +} + +# tftp wrapper +# usage tftp_get <path> <tftpdserver> [count] +# count is optional - default is 3 - use -1 for indefinit +tftp_get () { + local file="$1" + local file_server="$2" + local download_successful=0 + local countdown="$3" + + if [ -z "$1" -o -z "$2" ]; then + [ $DEBUGLEVEL -ge 1 ] && \ + echo "[tftp_get] Usage: tftp_get <path> <server> [count]" + return 1; + fi + + [ "$countdown" = "" ] && countdown=3 + + until [ $download_successful -eq 1 ] + do + if [ "$countdown" = "0" ]; then + [ $DEBUGLEVEL -ge 1 ] && \ + echo "[tftp_get] download of \"$file\" from \"$file_server\" ... failed" + return 0; + fi + tftp -g -r "/$file" -l /tmp/$(basename $file) $file_server + [ -s /tmp/$(basename $file) ] && download_successful=1 + countdown=$(expr $countdown - 1) + usleep 200000 + done + [ $DEBUGLEVEL -ge 1 ] && \ + echo "[tftp_get] download of \"$file\" from \"$file_server\" ... successful" + return 0; +} + + +# wget wrapper +# usage wget_get <path> <ftp/httpserver> [count] +# count is optional - default is 3 - use -1 for indefinit +wget_get () { + local file="$1" + local file_server="$2" + local download_successful=0 + local countdown="$3" + + if [ -z "$1" -o -z "$2" ]; then + [ $DEBUGLEVEL -ge 1 ] && \ + echo "[wget_get] Usage: wget_get <path> <server> [count]" + return 1; + fi + + [ "$countdown" = "" ] && countdown=3 + + until [ $download_successful -eq 1 ] + do + if [ "$countdown" = "0" ]; then + [ $DEBUGLEVEL -ge 1 ] && \ + echo "[wget_get] download of \"$file\" from \"$file_server\" ... failed" + return 0; + fi + wget -q $file_server$file -O /tmp/$(basename $file) + [ -s /tmp/$(basename $file) ] && download_successful=1 + countdown=$(expr $countdown - 1) + usleep 200000 + done + [ $DEBUGLEVEL -ge 1 ] && \ + echo "[wget_get] download of \"$file\" from \"$file_server\" ... successful" + return 0; +} + + +fileget () { +# normally tftp would be used, alternatively use wget for ftp or http +# if local device file is specified - mount and unmount after copying +local cfgfile +[ "x$fileprot" = "x" ] && fileprot=tftp +if [ "x$filepath" != "x" ] ; then + cfgfile=${filepath} + [ "x$fileserv" = "x" ] && fileserv=$(checkip ${serverip}) + # wait for dns if "fileserv" is a name and not lbd device + [ "$fileprot" != "lbd" ] && \ + echo ${fileserv} | grep -qi [a-z] + [ $DEBUGLEVEL -ge 1 ] && echo "fileget - fileprot:$fileprot, filepath:\ +$filepath, fileserv:$fileserv" >>$LOGFILE + case "$fileprot" in + ftp|http) + wget_get $cfgfile $fileprot://$fileserv \ + && { unpack /tmp/$(basename $cfgfile) && break; } 2>>$LOGFILE + ;; + lbd) + local ldev=$fileserv + echo "Waiting for configuration file ${cfgfile} ...." + [ $DEBUGLEVEL -ge 1 ] && echo "fileget - fileprot:$fileprot, filepath:\ +$filepath, fileserv:$fileserv" >>$LOGFILE + waitfor /mnt/${cfgfile} 10000 + if [ -f /mnt/${cfgfile} ]; then + unpack /mnt/$cfgfile + else + error "$init_errlfg" + fi + ;; + *) + tftp_get $cfgfile $fileserv \ + && unpack /tmp/$(basename $cfgfile) 2>>$LOGFILE + ;; + esac +else + # predefined value for OpenSLX environment; it is expected that this + # directory is just below the tftpboot (path to which the daemon is + # restricted to) + filepath="client-config" + [ "x$fileserv" = "x" ] && fileserv=$(checkip ${serverip}) + [ $DEBUGLEVEL -ge 1 ] && echo "fileget - fileprot:$fileprot, filepath:\ +$filepath, fileserv:$fileserv" >>$LOGFILE + # try to get configuration files successively; start with distro client + # and try last distro default ... + mac=$(echo $macaddr|sed "s/:/-/g") + for cfgfile in ${filepath}/${SYSTEM_NAME}/01-$mac.tgz \ + ${filepath}/${SYSTEM_NAME}/default.tgz ; do + case "$fileprot" in + ftp|http) + wget $fileprot://$fileserv/$cfgfile -O /tmp/$(basename $cfgfile) \ + 2>>$LOGFILE && { unpack /tmp/$(basename $cfgfile) && break; } + ;; + tftp) + tftp_get $cfgfile $fileserv \ + 2>>$LOGFILE && { unpack /tmp/$(basename $cfgfile) && break; } + ;; + esac + done + echo -e "\n## Configuration via fileget from ${fileprot}://${fileserv}/\ +${cfgfile}\n# Hierarchy is distro client and as last distro/default" \ + >>/tmp/confviafile +fi +cat /initramfs/machine-setup >>/tmp/confviafile 2>/dev/null || \ + error "$nomachsetup" +echo "fileget via $fileprot from $fileserv/$cfgfile finished" >/tmp/file-done +[ $DEBUGLEVEL -ge 1 ] && echo "fileget from $cfgfile finished" >>$LOGFILE +} |
