File: fetch-basefile

package info (click to toggle)
fai 6.5.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,084 kB
  • sloc: sh: 6,774; perl: 5,665; makefile: 138
file content (82 lines) | stat: -rwxr-xr-x 2,002 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#! /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