Debian Codegraph @

lib/fetch-basefile

raw
1 #! /bin/bash
2
3 # fetch-basefile, fetch a basefile via ftp or http using classes
4 #
5 # (c) Thomas Lange, 2011-2022
6 #
7 # Try to download a file CLASSNAME.tar.gz (or tgz, or tar.xz,...) from an URL
8 # CLASSNAME must match a FAI class
9 # The URL must provide a (directory) listing of all files available for
10 # download
11 # e.g.: FAI_BASEFILEURL=https://fai-project.org/download/basefiles/
12 #
13 # variables needed: $classes, $FAI, $FAI_BASEFILEURL
14
15 mount_ramdisk() {
16
17 # put ramdisk on config space and download file
18 mount -t tmpfs tmpfs $FAI/basefiles # this makes files from NFS invisible
19 if [ $? -eq 1 ]; then
20 echo "mount ramdisk onto $FAI/basefiles failed." >&2
21 exit 3
22 fi
23 }
24
25 [ X$FAI_BASEFILEURL = X ] && exit 0
26 url=$FAI_BASEFILEURL
27 [ X$verbose = X1 ] && echo "Fetching basefile from $url"
28
29 error=0
30 found=0
31 mount=0
32 declare -a baselist
33
34 while getopts m opt ; do
35 case "$opt" in
36 m) mount=1 ;;
37 esac
38 done
39 #shift $((OPTIND - 1))
40
41 # get list of *.tar* files at URL
42 flist=$(lftp -e "connect $url ;cls -1 *.tar*;exit" 2>/dev/null)
43
44 # create an array of all lines
45 baselist=($flist)
46
47 # reverse order of classes
48 for c in $classes; do
49 revclasses="$c $revclasses"
50 done
51
52 # now search for each class, if a basename matches
53 for c in $revclasses; do
54 for f in "${baselist[@]}"; do
55 base=${f%%.tar*}
56 if [ "$c" = "$base" ]; then
57 found=1
58 [ $mount = 1 ] && mount_ramdisk
59 # Create folder in case it is not part of the configuration space
60 mkdir -p $FAI/basefiles || echo "Could not create folder $FAI/basefiles" >&2
61 cd $FAI/basefiles || exit 3
62 if [ -f $f ]; then
63 echo "$f already exists" >&2
64 error=1
65 break 2
66 fi
67
68 echo "Downloading $url/$f"
69 curl -s $url/$f > $f
70 error=$?
71 break 2
72 fi
73 done
74 done
75
76 if [ X$found = X0 ]; then
77 echo "No basefile matching a class name was found at $url" >&2
78 fi
79
80 exit $error
81
82 # the rest is done by task extrbase using ftar