|
1 ## library functions |
|
2 |
|
3 function die () { |
|
4 echo "!!! $@" >&2 |
|
5 |
|
6 exit 1 |
|
7 } |
|
8 |
|
9 # Execute command verbosely, and exit on failure |
|
10 function cmd () { |
|
11 echo ">>> $@" |
|
12 |
|
13 [ $_MOCK ] && return 0 |
|
14 |
|
15 eval "$@" # return $? |
|
16 } |
|
17 |
|
18 function expand_MB () { |
|
19 local size=${1^} |
|
20 |
|
21 case ${size: -1} in |
|
22 G) |
|
23 size=$(( ${size%G} * 1024)) |
|
24 |
|
25 ;; |
|
26 esac |
|
27 |
|
28 echo $size |
|
29 } |
|
30 |
|
31 function extract_iso () { |
|
32 iso=$1 |
|
33 dst=$2 |
|
34 |
|
35 [ ! -r "$iso" ] && die "Given .iso is not readable: $iso" |
|
36 [ -z "$dst" ] && die "Must give destination: $dst" |
|
37 [ -e "$dst" ] && die "Given destination already exists: $dst" |
|
38 |
|
39 # temporary mount |
|
40 mnt=$(mktemp -d mnt.XXXX) |
|
41 |
|
42 # clean on exit |
|
43 function cleanup () { |
|
44 if mountpoint -q $mnt; then |
|
45 sudo umount $mnt |
|
46 fi |
|
47 |
|
48 [ -d $mnt ] && rmdir $mnt |
|
49 |
|
50 return $1 |
|
51 } |
|
52 |
|
53 # loop-mount |
|
54 cmd sudo mount -o loop $iso $mnt || cleanup 1 |
|
55 |
|
56 # copy |
|
57 cmd cp -r $mnt $dst || cleanup 1 |
|
58 |
|
59 # done, cleanup |
|
60 cleanup 0 |
|
61 } |
|
62 |
|
63 function my_md5sum () { |
|
64 /usr/bin/md5sum $1 | ( |
|
65 read md5sum path |
|
66 echo $md5sum |
|
67 ) |
|
68 } |
|
69 |
|
70 function expand_template () { |
|
71 local tpl=$1 |
|
72 local out=$2 |
|
73 |
|
74 local linecount=0 |
|
75 |
|
76 # read in each line at a time |
|
77 while IFS='' read -r line; do |
|
78 linecount=$((linecount + 1)) |
|
79 |
|
80 if [ "${line:0:1}" == "#" ]; then |
|
81 # ignore comments; pass through as-is |
|
82 echo "$line" |
|
83 |
|
84 else |
|
85 # evaluate {...} expressions |
|
86 # a slight hack, but it works \o/ |
|
87 # http://stackoverflow.com/questions/415677/how-to-replace-placeholders-in-a-text-file/7633579#7633579 |
|
88 line="${line//\\/\\\\}" |
|
89 line="${line//\"/\\\"}" |
|
90 line="${line//\`/\\\`}" |
|
91 line="${line//\$/\\\$}" |
|
92 line="${line//{/\${}" # This is just for vim: } " |
|
93 |
|
94 # echo "($line)" >&2 |
|
95 (eval "echo \"$line\"") || die "Error at $tpl:$linecount: $line" |
|
96 fi |
|
97 |
|
98 done < $tpl > $out |
|
99 } |
|
100 |
|
101 # Recursive expand_template files from src -> dst |
|
102 # XXX: not used |
|
103 function expand_tree () { |
|
104 local src=$1 |
|
105 local dst=$2 |
|
106 local filter=${3:-'*'} |
|
107 |
|
108 for path in ${src}/${filter}; do |
|
109 local name=$(basename $path) |
|
110 local target=$dst/$name |
|
111 |
|
112 if [ -d $path ]; then |
|
113 echo "expand_tree: $path -> $target" |
|
114 echo "expand_tree $path $target $filter" |
|
115 |
|
116 elif [ -f $path ]; then |
|
117 echo "expand_file: $path -> $target" |
|
118 echo "expand_file $path $target" |
|
119 |
|
120 else |
|
121 echo "XXX: ignore weird file: $path" |
|
122 |
|
123 fi |
|
124 done |
|
125 } |