-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoc
executable file
·244 lines (218 loc) · 5.59 KB
/
oc
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env bash
# PRE:
# 1) telnet 2) passwd 3) reboot
CLEANUP=''
TIME=''
DEBUG=''
GEN_ONLY='no'
KEEP='rm'
PROXY=''
remote_modules="$(ls -1 remote-[0-9][0-9][0-9]-*)"
local_modules="$(ls -1 local-[0-9][0-9][0-9]-*)"
settings() {
local target
target="$1"
cat <<EOF
# ----------------------------------------------------------
# Default settings
EOF
cat default
cat <<EOF
# ----------------------------------------------------------
# Per device settings
EOF
if [ -f "$target" ]; then
cat "$target"
fi
if [ -f "${target}.gpg" ]; then
gpg --batch --no-tty -qd "${target}.gpg"
fi
cat <<EOF
# ----------------------------------------------------------
EOF
}
target="$1"
target="${target%.gpg}"
if [ -f "$target" ] || [ -f "${target}.gpg" ]; then
source <(settings "$target")
shift
else
echo "$target and ${target}.gpg not exist"
exit
fi
if [ x"$intra_host" != x ]; then
if [ x"$(ip route get "$(echo "$intra_host" | cut -d@ -f2)" | head -n 1 | cut -d' ' -f2)" != x'via' ]; then
host="$intra_host"
port="$intra_port"
fi
fi
while getopts ":cgh:kl:r:tp:xy" Option
do
case "$Option" in
c) CLEANUP='1' ;;
h) host="$OPTARG" ;;
g) GEN_ONLY='yes' ;;
k) KEEP='echo keep remote' ;;
l)
local_modules_overrided=1
local_modules="$OPTARG"
;;
r)
remote_modules_overrided=1
remote_modules="$OPTARG"
;;
t) TIME='time' ;;
p) port="$OPTARG" ;;
x) DEBUG='-x' ;;
y) PROXY='-R8123:127.0.0.1:8123' ;;
esac
done
scp="scp -Opq"
ssh="ssh"
if [ x"$port" != x ]; then
scp="scp -OpqP$port"
ssh="ssh -p$port"
fi
if [ x"$local_modules_overrided" != x ]; then
if [ x"$remote_modules_overrided" = x ]; then
remote_modules=''
fi
elif [ x"$remote_modules_overrided" != x ]; then
local_modules=''
fi
backup() {
remote 'sysupgrade --create-backup -' | tar zxf -
}
download() {
local url basename file
url="$1"
basename="$(basename "$url")"
file="$2"
if [ x"$file" = x ]; then
file="files/tmp/$basename"
fi
if [ -n "$CLEANUP" ] && [ -e "$file" ]
then
rm -v "$file"
fi
if [ ! -e "$file" ]; then
echo "download: $url -> $file"
curl --create-dirs -#fSLo "$file" "$url"
fi
}
push() {
local src dest md5sum_src md5sum_dest
src="$1"
dest="$2"
if [ ! -e "$src" ]; then
echo "push: $src does not exist"
return
fi
md5sum_src=$(md5sum "$src" | cut -d' ' -f1)
md5sum_dest=$(remote "md5sum $dest 2>/dev/null | cut -d' ' -f1")
if [ x"$md5sum_src" != x"$md5sum_dest" ]; then
echo "push: $src -> $dest"
$scp "$src" "${host}:${dest}"
fi
}
download_push() {
local url dest package
url="$1"
dest="$2"
package="$3"
if [ x"$package" = x ] || ! remote opkg status "$package" | grep -q installed ; then
download "$url"
push "files/tmp/$(basename "$url")" "$dest"
fi
}
generate_script() {
local remote_module config_module_enabled
if [ -n "$CLEANUP" ]
then
echo 'CLEANUP=1'
fi
if [ -n "$PROXY" ]
then
echo 'PROXY=http://127.0.0.1:8123'
else
echo 'PROXY='
fi
cat functions.sh
settings "$target"
for remote_module in $remote_modules
do
if [[ "$remote_module" == remote-* ]]
then
config_module_enabled="${remote_module#remote-[0-9][0-9][0-9]-}"
config_module_enabled="${config_module_enabled%.sh}"
config_module_enabled="config_${config_module_enabled}_enabled"
if [[ "$config_module_enabled" =~ ^[0-9a-z_]+$ ]]
then
config_module_enabled="${!config_module_enabled}"
else
config_module_enabled=0
fi
if [[ "$remote_modules_overrided" = 1 ]] || [ "$config_module_enabled" = 1 ]
then
cat <<EOF
echo "Begin $remote_module"
EOF
cat "$remote_module"
cat <<EOF
oc_hr
# ----------------------------------------------------------
EOF
fi
fi
done
}
if [ "$GEN_ONLY" = 'yes' ]; then
generate_script
exit
fi
remote() {
$ssh -n "$host" "$@"
}
if ! remote uptime >/dev/null; then
echo 'ssh failed'
exit
fi
arch_full="$(remote '. /etc/openwrt_release; echo $DISTRIB_TARGET')"
case "$arch_full" in
x86/64)
arch="x86_64"
;;
*)
arch="${arch_full%%/*}"
;;
esac
for local_module in $local_modules
do
if [[ "$local_module" == local-* ]]
then
config_module_enabled="${local_module#local-[0-9][0-9][0-9]-}"
config_module_enabled="${config_module_enabled%.sh}"
config_module_enabled="config_${config_module_enabled}_enabled"
if [[ "$config_module_enabled" =~ ^[0-9a-z_]+$ ]]
then
config_module_enabled="${!config_module_enabled}"
else
config_module_enabled=0
fi
if [[ "$local_modules_overrided" = 1 ]] || [ "$config_module_enabled" = 1 ]
then
echo "Begin $local_module"
if [ x"$DEBUG" != x ]; then
set -x
fi
source "$local_module"
if [ x"$DEBUG" != x ]; then
set +x
fi
printf '%s\n' ------------------------------------------------------------
fi
fi
done
if [ x"$remote_modules" != x ]; then
generate_script | $ssh "$host" "$PROXY" 's=/tmp/openwrt-config; cat > $s; chmod 400 $s; '$TIME' sh '$DEBUG' $s; '$KEEP' $s'
fi