/
fetch-basefile
executable file
·89 lines (74 loc) · 2.17 KB
/
fetch-basefile
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
83
84
85
86
87
88
89
#! /bin/bash
# fetch-basefile, fetch a basefile via ftp or http using classes
#
# (c) Thomas Lange, 2011-2012
#
# Try to download a CLASSNAME.tar.gz (or tgz, or tar.xz,...) from an URL which matches a class name
# The URL must provide a listing of all files in there
# e.g.: FAI_BASEFILEURL=http://fai-project.org/download/basefiles
#
# variables needed: $classes, $FAI, $FAI_BASEFILEURL
FAI_BASEFILEURL=http://fai-project.org/download/basefiles
classes="ABC A123 CENTOS SLC6_ ENTOS5_32 DEBIAN SQUEEZE"
FAI=/dev/shm
mkdir -p $FAI/basefiles
verbose=1
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."
exit 3
fi
}
[ X$FAI_BASEFILEURL = X ] && exit 0
url=$FAI_BASEFILEURL
[ "$verbose" ] && echo "Fetching basefile from $url"
error=0
found=0
mount=0
while getopts m opt ; do
case "$opt" in
m) mount=1 ;;
esac
done
#shift $(($OPTIND - 1))
# get list of all files at URL
flist=$(lftp -e 'cls;exit' $url 2>/dev/null)
# create a hash like thing
# key (here variable name) is the basename of the file found
# value is the complete filename
for f in $flist; do
# echo file found: $f
base=${f%%.*} # basename is the class name
eval "found_$base=$f"
done
# 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
id="found_$c" # prepare for indirect variable name
if [ X${!id} != X ]; then
# hash lookup succeeded
found=1
[ $mount = 1 ] && mount_ramdisk
cd $FAI/basefiles || exit 3
if [ -f ${!id} ]; then
echo "${!id} already exists"
error=1
break
fi
echo "Downloading $url/${!id}"
# wget -nv $url/${!id} # creates a new file with suffix .1, .2,.. if file already exists. Bad.
lftp -e "get $url/${!id};exit" # fails if file already exists. this is nice.
error=$?
break
fi
done
if [ X$found = X0 ]; then
echo No basefile matching a class name was found at $url
fi
exit $error
# the rest is done by task extrbase using ftar