Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·260 lines (235 loc) · 7.85 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·260 lines (235 loc) · 7.85 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env bash
# OpenWrt CLI — Linux / macOS installer (CLI/TUI palette)
# curl -fsSL https://raw.githubusercontent.com/Necho-dev/openwrt-cli/main/install.sh | bash
# ./install.sh (from a clone → pip install -e ".[mcp]")
set -euo pipefail
REPO="Necho-dev/openwrt-cli"
GIT_URL="https://github.com/${REPO}.git"
PIP_GIT="git+${GIT_URL}"
if [ -t 1 ] && [ -z "${NO_COLOR:-}" ]; then
C_ACCENT='\033[1;36m'
C_OK='\033[32m'
C_WARN='\033[33m'
C_ERR='\033[1;31m'
C_MUTED='\033[2m'
C_RESET='\033[0m'
else
C_ACCENT="" C_OK="" C_WARN="" C_ERR="" C_MUTED="" C_RESET=""
fi
info() { printf "${C_ACCENT}[i]${C_RESET} %s\n" "$*"; }
ok() { printf "${C_OK}[ok]${C_RESET} %s\n" "$*"; }
warn() { printf "${C_WARN}[!]${C_RESET} %s\n" "$*"; }
error() { printf "${C_ERR}[x]${C_RESET} %s\n" "$*" >&2; }
_norm_lang() {
printf '%s' "${1:-}" | tr '[:upper:]' '[:lower:]' | tr '-' '_'
}
detect_lang() {
local raw
raw="$(_norm_lang "${OPENWRT_LANG:-}")"
case "$raw" in
zh*|cn|chinese) echo zh; return ;;
en|en_*|english) echo en; return ;;
esac
for raw in \
"$(_norm_lang "${LC_ALL:-}")" \
"$(_norm_lang "${LC_MESSAGES:-}")" \
"$(_norm_lang "${LANG:-}")"
do
case "$raw" in
zh*|cn|chinese) echo zh; return ;;
en|en_*) echo en; return ;;
esac
done
echo en
}
LANG_CODE="$(detect_lang)"
_() {
if [ "$LANG_CODE" = zh ]; then
case "$1" in
sub) echo "远程管理 OpenWrt · CLI / TUI · SSH 与 HTTP" ;;
need_py) echo "需要 Python 3.12+" ;;
hint_py) echo "Ubuntu/Debian: sudo apt install python3 python3-pip python3-venv" ;;
hint_mac) echo "macOS: brew install python@3.12" ;;
found_py) echo "Python: $2" ;;
need_pip) echo "未找到 pip。请安装:python3 -m ensurepip --upgrade" ;;
old_py) echo "当前 $2,请升级到 Python 3.12+" ;;
local) echo "从本仓库安装(可编辑)…" ;;
remote) echo "从 GitHub 安装…" ;;
done) echo "安装完成" ;;
path) echo "openwrt 不在 PATH。请把下面目录加入 PATH 后重开终端:" ;;
next) echo "接下来" ;;
setup_q) echo "现在运行 openwrt setup?(连接向导)[Y/n] " ;;
skip_setup) echo "稍后运行: openwrt setup" ;;
skill_q) echo "现在安装 Agent skill(openwrt-ops)?[Y/n] " ;;
skip_skill) echo "稍后运行: openwrt skill install" ;;
no_bin) echo "已安装,但找不到 openwrt 命令。请重开终端后再试。" ;;
fail) echo "安装失败。可改用: pipx install '$PIP_GIT'" ;;
*) echo "$1" ;;
esac
else
case "$1" in
sub) echo "Remote OpenWrt admin · CLI / TUI · SSH and HTTP" ;;
need_py) echo "Python 3.12+ is required" ;;
hint_py) echo "Ubuntu/Debian: sudo apt install python3 python3-pip python3-venv" ;;
hint_mac) echo "macOS: brew install python@3.12" ;;
found_py) echo "Python: $2" ;;
need_pip) echo "pip not found. Try: python3 -m ensurepip --upgrade" ;;
old_py) echo "Found $2 — please upgrade to Python 3.12+" ;;
local) echo "Installing from this repo (editable)…" ;;
remote) echo "Installing from GitHub…" ;;
done) echo "Install complete" ;;
path) echo "openwrt is not on PATH. Add this directory, then open a new terminal:" ;;
next) echo "Next" ;;
setup_q) echo "Run openwrt setup now (connection wizard)? [Y/n] " ;;
skip_setup) echo "Later: openwrt setup" ;;
skill_q) echo "Install the Agent skill (openwrt-ops) now? [Y/n] " ;;
skip_skill) echo "Later: openwrt skill install" ;;
no_bin) echo "Installed, but the openwrt command was not found. Open a new terminal and retry." ;;
fail) echo "Install failed. Try: pipx install '$PIP_GIT'" ;;
*) echo "$1" ;;
esac
fi
}
banner() {
printf "\n"
printf "${C_ACCENT} ___ __ _____ _____${C_RESET}\n"
printf "${C_ACCENT} / _ \\ _ __ ___ _ \\ \\ / / _ \\_ _|${C_RESET}\n"
printf "${C_ACCENT}| (_) | '_ \\/ -_) ' \\ \\/\\/ /| / | |${C_RESET}\n"
printf "${C_ACCENT} \\___/| .__/\\___|_||_\\_/\\_/ |_|_\\ |_|${C_RESET}\n"
printf "${C_MUTED} %s${C_RESET}\n\n" "$(_ sub)"
}
detect_python() {
local cand
for cand in python3.13 python3.12 python3 python; do
if command -v "$cand" >/dev/null 2>&1 \
&& "$cand" -c "import sys; raise SystemExit(0 if sys.version_info >= (3, 12) else 1)" 2>/dev/null; then
printf '%s' "$cand"
return 0
fi
done
return 1
}
repo_root() {
local src="${BASH_SOURCE[0]:-}"
if [ -n "$src" ] && [ -f "$src" ] && [ -r "$src" ]; then
local dir
dir="$(cd "$(dirname "$src")" && pwd)"
if [ -f "${dir}/pyproject.toml" ]; then
printf '%s' "$dir"
return 0
fi
fi
return 1
}
user_bin() {
"$PYTHON" -c "import os, site; print(os.path.join(site.USER_BASE, 'bin'))" 2>/dev/null || true
}
resolve_openwrt() {
if command -v openwrt >/dev/null 2>&1; then
command -v openwrt
return 0
fi
local bin
bin="$(user_bin)"
if [ -n "$bin" ] && [ -x "${bin}/openwrt" ]; then
printf '%s' "${bin}/openwrt"
return 0
fi
return 1
}
pip_install() {
local extra=()
if [ -z "${VIRTUAL_ENV:-}" ] && [ -z "${CONDA_PREFIX:-}" ]; then
extra+=(--user)
fi
if "$PYTHON" -m pip install -q "${extra[@]}" "$@"; then
return 0
fi
if command -v pipx >/dev/null 2>&1; then
if [ "${1:-}" = "-e" ]; then
local target="${2%%\[*}"
pipx install -e "$target" || return 1
pipx inject openwrt-cli mcp >/dev/null 2>&1 || true
else
pipx install "$1" || return 1
pipx inject openwrt-cli mcp >/dev/null 2>&1 || true
fi
return 0
fi
return 1
}
has_tty() {
[ -e /dev/tty ] && [ -r /dev/tty ]
}
print_next() {
printf "\n${C_MUTED}── %s ──${C_RESET}\n\n" "$(_ next)"
printf " ${C_ACCENT}openwrt setup${C_RESET} ${C_MUTED}# language + connection wizard${C_RESET}\n"
printf " ${C_ACCENT}openwrt skill install${C_RESET} ${C_MUTED}# openwrt-ops for detected Agents${C_RESET}\n"
printf " ${C_ACCENT}openwrt mcp json${C_RESET} ${C_MUTED}# paste-ready mcpServers.openwrt${C_RESET}\n"
printf " ${C_ACCENT}openwrt doctor${C_RESET} ${C_MUTED}# SSH / HTTP health${C_RESET}\n"
printf " ${C_ACCENT}openwrt tui${C_RESET} ${C_MUTED}# dashboard${C_RESET}\n"
printf "\n"
}
main() {
banner
if ! PYTHON="$(detect_python)"; then
error "$(_ need_py)"
if command -v python3 >/dev/null 2>&1; then
error "$(_ old_py "$(python3 --version 2>&1)")"
fi
printf " %s\n %s\n" "$(_ hint_py)" "$(_ hint_mac)"
exit 1
fi
local ver
ver="$("$PYTHON" --version 2>&1)"
info "$(_ found_py "$ver")"
if ! "$PYTHON" -m pip --version >/dev/null 2>&1; then
error "$(_ need_pip)"
exit 1
fi
local root=""
root="$(repo_root || true)"
if [ -n "$root" ]; then
info "$(_ local)"
if ! pip_install -e "${root}[mcp]"; then
error "$(_ fail)"
exit 1
fi
else
info "$(_ remote)"
if ! pip_install "openwrt-cli[mcp] @ ${PIP_GIT}"; then
error "$(_ fail)"
exit 1
fi
fi
ok "$(_ done)"
local bin=""
if bin="$(resolve_openwrt)"; then
if [ "$(command -v openwrt 2>/dev/null || true)" != "$bin" ]; then
warn "$(_ path)"
printf " ${C_ACCENT}%s${C_RESET}\n" "$(dirname "$bin")"
fi
else
warn "$(_ no_bin)"
local ubin
ubin="$(user_bin)"
[ -n "$ubin" ] && printf " ${C_ACCENT}%s${C_RESET}\n" "$ubin"
fi
print_next
if has_tty && [ -n "${bin:-}" ]; then
local ans=""
printf "${C_ACCENT}%s${C_RESET}" "$(_ setup_q)"
IFS= read -r ans < /dev/tty || true
case "$ans" in
""|y|Y|yes|YES) "$bin" setup ;;
*) info "$(_ skip_setup)" ;;
esac
printf "${C_ACCENT}%s${C_RESET}" "$(_ skill_q)"
IFS= read -r ans < /dev/tty || true
case "$ans" in
""|y|Y|yes|YES) "$bin" skill install ;;
*) info "$(_ skip_skill)" ;;
esac
fi
}
main "$@"