-
Notifications
You must be signed in to change notification settings - Fork 8
/
functions.sh
163 lines (136 loc) · 2.9 KB
/
functions.sh
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/bash
declare -Ag temp
cleanup_temp() {
for key in "${!temp[@]}"; do
echo "Clean up: ${temp[${key}]}"
rm -rf -- "${temp[${key}]}"
done
}
trap 'cleanup_temp' EXIT
panick() {
local mesg str
mesg=$1 && shift
str=$(printf "==> ERROR: ${mesg}\n" "$@")
echo "${str}" >&2
exit 1
}
url2grub() {
local url="$1"
local url_regex='^(http|tftp)://([^/]+)(/.*)?$'
if [[ "${url}" =~ ${url_regex} ]]; then
local protocol="${BASH_REMATCH[1]}"
local host="${BASH_REMATCH[2]}"
local path="${BASH_REMATCH[3]}"
echo "(${protocol},${host})${path}"
fi
}
net_extract() {
local url target workdir
url="$1"
target="$2"
workdir=$(dirname "${target}")
mkdir -p "${workdir}"
id="net_extract_${RANDOM}"
temp[${id}]=$(mktemp -d -p "${workdir}" .tmp.XXXXXXXX) || return 1
curl -fsL "${url}" | bsdtar -x -f - -C "${temp[${id}]}"
if [[ "${PIPESTATUS[0]}" == 0 && "$?" == 0 ]]; then
chmod 755 "${temp[${id}]}"
[[ -d "${target}" ]] && mv "${target}" "${target}.bak"
mv "${temp[${id}]}" "${target}"
else
echo "Download failed"
rm -rf "${temp[${id}]}"
return 1
fi
}
url_check() {
local url code
url="$1" && shift
code=$(curl -sIL -o /dev/null -w "%{http_code}" "${url}") || return 1
(( code < 400 )) && return 0
for white in "$@"; do
(( code == white )) && return 0
done
return 1
}
grep_web() {
local url="$1"
local regex="$2"
curl -fsL "${url}" | grep -Po "${regex}" | sort -uVr
[[ "${PIPESTATUS[0]}" == 0 ]]
}
grub_menu_sep() {
cat <<-EOF
menuentry '$1' {
true
}
EOF
}
grub_linux_entry() {
local title="$1"
local kernel="$2"
local initrd="$3"
local param="$4"
cat <<-EOF
menuentry '${title}' {
echo 'Loading kernel...'
linux ${kernel} ${param}
echo 'Loading initial ramdisk...'
initrd ${initrd}
}
EOF
}
grub_option() {
local var desc index title
var="$1" && shift
desc="$1" && shift
cat <<- EOF
if [ -z "\$${var}_INDEX" ]; then
set ${var}='$1'
set ${var}_DNAME='$2'
set ${var}_INDEX=0
fi
submenu "[option] ${desc} = \$${var}_DNAME" {
menuentry '${desc}:' {
export ${var}
export ${var}_DNAME
export ${var}_INDEX
configfile ${PXE_MENU_URL}
}
EOF
index=0
while (("$#" >= 3)); do
title="> $2"
[[ -n "$3" ]] && title="${title}: $3"
cat <<-EOF
menuentry '${title}' {
set ${var}='$1'
set ${var}_DNAME='$2'
set ${var}_INDEX=${index}
export ${var}
export ${var}_DNAME
export ${var}_INDEX
configfile ${PXE_MENU_URL}
}
EOF
shift 3
index=$((index+1))
done
echo "}"
}
grub_mirror_selector() {
local grub_var="$1" && shift
declare -a buf
for url in "$@"; do
buf=("${buf[@]}" "${url}" "${url}" "")
done
grub_option "${grub_var}" "Mirror URL" "${buf[@]}"
cat <<-EOF
regexp \\
--set=1:mirror_protocol \\
--set=2:mirror_host \\
--set=3:mirror_path \\
'^([^:]+)://([^/]+)(/.*)\$' "\$${grub_var}"
EOF
}
# vim: set ts=4 sw=4 sts=4 noexpandtab nosta: