Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·276 lines (248 loc) · 7.75 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·276 lines (248 loc) · 7.75 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env bash
#
# Cyclo Intelligence installer.
#
# Default behavior:
# - hosts named ffw* install the repository under /mnt/ssd/cyclo_intelligence
# and bind-mount it at ~/cyclo_intelligence.
# - other hosts install under ~/cyclo_intelligence.
# - only the common SDK is initialized; model sources are opt-in.
# - the user starts the main container explicitly after installation.
set -euo pipefail
REPO_URL="${CYCLO_INSTALL_REPO_URL:-https://github.com/ROBOTIS-GIT/cyclo_intelligence.git}"
REF="main"
MODE="auto"
SSD_ROOT="${CYCLO_INSTALL_SSD_ROOT:-/mnt/ssd}"
FSTAB_PATH="${CYCLO_INSTALL_FSTAB:-/etc/fstab}"
usage() {
cat <<EOF
Usage: install.sh [--robot|--local] [--ref <branch-or-tag>]
Options:
--robot Install under /mnt/ssd/cyclo_intelligence and bind-mount from home.
--local Install under ~/cyclo_intelligence.
--ref REF Git branch or tag to install (default: main).
-h, --help Show this help.
EOF
}
while [ "$#" -gt 0 ]; do
case "$1" in
--robot)
MODE="robot"
shift
;;
--local)
MODE="local"
shift
;;
--ref)
if [ "$#" -lt 2 ] || [ -z "$2" ]; then
echo "Error: --ref requires a value." >&2
exit 1
fi
REF="$2"
shift 2
;;
-h|--help)
usage
exit 0
;;
*)
echo "Error: unknown argument: $1" >&2
usage >&2
exit 1
;;
esac
done
require_command() {
if ! command -v "$1" >/dev/null 2>&1; then
echo "Error: required command not found: $1" >&2
exit 1
fi
}
hostname_short() {
hostname -s 2>/dev/null || hostname 2>/dev/null || printf '%s\n' ""
}
detect_mode() {
local host
host="$(hostname_short | tr '[:upper:]' '[:lower:]')"
case "$host" in
ffw*) printf '%s\n' "robot" ;;
*) printf '%s\n' "local" ;;
esac
}
path_exists_or_symlink() {
[ -e "$1" ] || [ -L "$1" ]
}
assert_available_path() {
local path="$1"
local label="$2"
if path_exists_or_symlink "$path"; then
echo "Error: ${label} already exists: $path" >&2
echo "Refusing to modify an existing Cyclo checkout automatically." >&2
echo "Stop active work, then update it manually with:" >&2
echo " cd \"$path\" && git pull --ff-only" >&2
echo " git submodule update --init -- cyclo_brain/sdk/zenoh_ros2_sdk" >&2
echo "Update selected model sources as described in docker/README.md (Selective Source Checkout)." >&2
exit 1
fi
}
run_privileged() {
if [ "$(id -u)" -eq 0 ]; then
"$@"
return
fi
if ! command -v sudo >/dev/null 2>&1; then
echo "Error: sudo is required to configure the robot bind mount." >&2
exit 1
fi
sudo "$@"
}
assert_ssd_ready() {
require_command mountpoint
if ! mountpoint -q "$SSD_ROOT"; then
echo "Error: robot mode requires mounted SSD: $SSD_ROOT" >&2
exit 1
fi
if [ ! -d "$SSD_ROOT" ]; then
echo "Error: SSD path is not a directory: $SSD_ROOT" >&2
exit 1
fi
}
fstab_has_exact_bind() {
awk -v src="$INSTALL_DIR" -v dst="$HOME_LINK" '
$0 !~ /^[[:space:]]*#/ && $1 == src && $2 == dst && $3 == "none" {
for (i = 4; i <= NF; i++) {
if ($i ~ /(^|,)bind(,|$)/) {
found = 1
}
}
}
END { exit(found ? 0 : 1) }
' "$FSTAB_PATH"
}
fstab_has_conflicting_target() {
awk -v src="$INSTALL_DIR" -v dst="$HOME_LINK" '
$0 !~ /^[[:space:]]*#/ && $2 == dst {
is_exact = 0
if ($1 == src && $3 == "none") {
for (i = 4; i <= NF; i++) {
if ($i ~ /(^|,)bind(,|$)/) {
is_exact = 1
}
}
}
if (!is_exact) {
conflict = 1
}
}
END { exit(conflict ? 0 : 1) }
' "$FSTAB_PATH"
}
append_fstab_line() {
local line="$1"
if [ -s "$FSTAB_PATH" ] && [ -n "$(tail -c 1 "$FSTAB_PATH" 2>/dev/null)" ]; then
if [ -w "$FSTAB_PATH" ]; then
printf '\n' >> "$FSTAB_PATH"
else
printf '\n' | run_privileged tee -a "$FSTAB_PATH" >/dev/null
fi
fi
if [ -w "$FSTAB_PATH" ]; then
printf '%s\n' "$line" >> "$FSTAB_PATH"
return
fi
printf '%s\n' "$line" | run_privileged tee -a "$FSTAB_PATH" >/dev/null
}
configure_robot_bind_mount() {
local fstab_line
if [ -L "$HOME_LINK" ]; then
echo "[install] Removing existing symlink at ${HOME_LINK}"
rm -f "$HOME_LINK"
fi
mkdir -p "$HOME_LINK"
if [ ! -f "$FSTAB_PATH" ]; then
echo "Error: fstab file not found: $FSTAB_PATH" >&2
exit 1
fi
if fstab_has_conflicting_target; then
echo "Error: fstab already contains a different mount for: $HOME_LINK" >&2
echo "Refusing to modify it automatically." >&2
exit 1
fi
if ! fstab_has_exact_bind; then
fstab_line="${INSTALL_DIR} ${HOME_LINK} none bind,nofail,x-systemd.requires-mounts-for=${SSD_ROOT%/} 0 0"
echo "[install] Adding bind mount to ${FSTAB_PATH}: ${HOME_LINK}"
append_fstab_line "$fstab_line"
fi
if mountpoint -q "$HOME_LINK"; then
echo "[install] ${HOME_LINK} is already mounted."
else
echo "[install] Mounting ${INSTALL_DIR} at ${HOME_LINK}"
run_privileged mount "$HOME_LINK"
if ! mountpoint -q "$HOME_LINK"; then
echo "Error: bind mount did not become active: $HOME_LINK" >&2
exit 1
fi
fi
}
prepare_ssd_install_dir() {
local install_dir="$1"
if mkdir -p "$install_dir" 2>/dev/null; then
return
fi
if [ "$(id -u)" -eq 0 ]; then
mkdir -p "$install_dir"
return
fi
if ! command -v sudo >/dev/null 2>&1; then
echo "Error: SSD path is not writable and sudo is not available: $SSD_ROOT" >&2
exit 1
fi
echo "[install] Creating SSD install directory with sudo: $install_dir"
sudo mkdir -p "$install_dir"
sudo chown "$(id -u):$(id -g)" "$install_dir"
}
clone_repo() {
local install_dir="$1"
local parent_dir
parent_dir="$(dirname "$install_dir")"
mkdir -p "$parent_dir"
echo "[install] Cloning ${REPO_URL} (${REF}) into ${install_dir}"
if ! git clone --no-recurse-submodules --branch "$REF" "$REPO_URL" "$install_dir" \
|| ! git -C "$install_dir" submodule update --init -- cyclo_brain/sdk/zenoh_ros2_sdk; then
echo "[install] Source setup failed; removing incomplete checkout: $install_dir" >&2
rm -rf "$install_dir"
exit 1
fi
}
require_command git
if [ "$MODE" = "auto" ]; then
MODE="$(detect_mode)"
fi
HOME_LINK="${HOME}/cyclo_intelligence"
case "$MODE" in
robot)
INSTALL_DIR="${SSD_ROOT%/}/cyclo_intelligence"
assert_ssd_ready
assert_available_path "$INSTALL_DIR" "SSD install path"
assert_available_path "$HOME_LINK" "home link path"
prepare_ssd_install_dir "$INSTALL_DIR"
clone_repo "$INSTALL_DIR"
configure_robot_bind_mount
;;
local)
INSTALL_DIR="$HOME_LINK"
assert_available_path "$INSTALL_DIR" "local install path"
clone_repo "$INSTALL_DIR"
;;
*)
echo "Error: invalid install mode: $MODE" >&2
exit 1
;;
esac
echo "[install] Installed Cyclo Intelligence at: $INSTALL_DIR"
echo "[install] Start Cyclo Intelligence manually with:"
echo " cd ${HOME_LINK}"
echo " ./docker/container.sh start"
echo "[install] Model sources were not downloaded. Before building a Worker, follow"
echo " ${HOME_LINK}/docker/README.md (Selective Source Checkout)."