#! /bin/bash # fetch-basefile, fetch a basefile via ftp or http using classes # # (c) Thomas Lange, 2011-2022 # # Try to download a file CLASSNAME.tar.gz (or tgz, or tar.xz,...) from an URL # CLASSNAME must match a FAI class # The URL must provide a (directory) listing of all files available for # download # e.g.: FAI_BASEFILEURL=https://fai-project.org/download/basefiles/ # # variables needed: $classes, $FAI, $FAI_BASEFILEURL mount_ramdisk() { # put ramdisk on config space and download file mount -t tmpfs tmpfs $FAI/basefiles # this makes files from NFS invisible if [ $? -eq 1 ]; then echo "mount ramdisk onto $FAI/basefiles failed." >&2 exit 3 fi } [ X$FAI_BASEFILEURL = X ] && exit 0 url=$FAI_BASEFILEURL [ X$verbose = X1 ] && echo "Fetching basefile from $url" error=0 found=0 mount=0 declare -a baselist while getopts m opt ; do case "$opt" in m) mount=1 ;; esac done #shift $((OPTIND - 1)) # get list of *.tar* files at URL flist=$(lftp -e "connect $url ;cls -1 *.tar*;exit" 2>/dev/null) # create an array of all lines baselist=($flist) # reverse order of classes for c in $classes; do revclasses="$c $revclasses" done # now search for each class, if a basename matches for c in $revclasses; do for f in "${baselist[@]}"; do base=${f%%.tar*} if [ "$c" = "$base" ]; then found=1 [ $mount = 1 ] && mount_ramdisk # Create folder in case it is not part of the configuration space mkdir -p $FAI/basefiles || echo "Could not create folder $FAI/basefiles" >&2 cd $FAI/basefiles || exit 3 if [ -f $f ]; then echo "$f already exists" >&2 error=1 break 2 fi echo "Downloading $url/$f" curl -s $url/$f > $f error=$? break 2 fi done done if [ X$found = X0 ]; then echo "No basefile matching a class name was found at $url" >&2 fi exit $error # the rest is done by task extrbase using ftar