-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp
More file actions
executable file
·310 lines (282 loc) · 10.1 KB
/
Copy pathapp
File metadata and controls
executable file
·310 lines (282 loc) · 10.1 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/bin/bash
# Build and deploy Outer Shell directly to a target from target.env.
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "${ROOT}"
BUILD_DIR="${ROOT}/build/app-deploy"
RELEASE_DIR="${BUILD_DIR}/release"
SYMBOLS_DIR="${BUILD_DIR}/symbols"
BUILD_CONFIGURATION="${CONFIGURATION:-Release}"
TARGET_KIND=ssh
SSH_BASE=()
TARGET_OS=""
TARGET_ARCH=""
TARGET_LIBC=""
TARGET_UID=""
require_tool() {
command -v "$1" >/dev/null 2>&1 || {
echo "error: required tool '$1' was not found" >&2
exit 1
}
}
shell_quote() {
local value="$1"
printf "'%s'" "${value//\'/\'\\\'\'}"
}
load_target() {
if [[ ! -f "${ROOT}/target.env" ]]; then
echo "error: no deploy target configured" >&2
echo " ./app target \"ssh -p 22 you@server\"" >&2
exit 1
fi
# shellcheck source=/dev/null
source "${ROOT}/target.env"
TARGET_KIND="${OUTER_TARGET_KIND:-ssh}"
if [[ "${TARGET_KIND}" != ssh ]]; then
echo "error: Outer Shell deployment currently requires an SSH target" >&2
exit 1
fi
if [[ "$(declare -p OUTER_TARGET_SSH 2>/dev/null)" != declare\ -a* ]] ||
[[ "${#OUTER_TARGET_SSH[@]}" -eq 0 ]]; then
echo "error: target.env must define OUTER_TARGET_SSH as a non-empty array" >&2
exit 1
fi
if [[ "$(basename "${OUTER_TARGET_SSH[0]}")" == ssh ]]; then
mkdir -p "${HOME}/.ssh"
SSH_BASE=(
"${OUTER_TARGET_SSH[0]}"
-o ControlMaster=auto
-o "ControlPath=${HOME}/.ssh/app-%C"
-o ControlPersist=2m
"${OUTER_TARGET_SSH[@]:1}"
)
else
SSH_BASE=("${OUTER_TARGET_SSH[@]}")
fi
}
run_ssh() {
"${SSH_BASE[@]}" "$@"
}
run_ssh_interactive() {
if [[ "$(basename "${SSH_BASE[0]}")" == ssh ]]; then
"${SSH_BASE[0]}" -t "${SSH_BASE[@]:1}" "$@"
else
"${SSH_BASE[@]}" -t "$@"
fi
}
probe_target() {
load_target
TARGET_OS="$(run_ssh uname -s </dev/null)"
TARGET_ARCH="$(run_ssh uname -m </dev/null)"
TARGET_UID="$(run_ssh id -u </dev/null)"
case "${TARGET_ARCH}" in
aarch64|arm64) TARGET_ARCH=aarch64 ;;
x86_64|amd64) TARGET_ARCH=x86_64 ;;
*) echo "error: unsupported target architecture '${TARGET_ARCH}'" >&2; exit 1 ;;
esac
case "${TARGET_OS}" in
Darwin) TARGET_LIBC=darwin ;;
Linux)
if run_ssh 'test -e "/lib/ld-musl-$(uname -m).so.1" || (command -v ldd >/dev/null 2>&1 && ldd --version 2>&1 | grep -qi musl)' </dev/null; then
TARGET_LIBC=musl
else
TARGET_LIBC=glibc
fi
;;
*) echo "error: unsupported target operating system '${TARGET_OS}'" >&2; exit 1 ;;
esac
}
package_variant() {
if [[ "${TARGET_OS}" == Darwin ]]; then
[[ "${TARGET_ARCH}" == aarch64 ]] && printf 'macos-arm64\n' || printf 'macos-x86_64\n'
else
printf 'linux-%s-musl\n' "${TARGET_ARCH}"
fi
}
archive_name() {
local variant
variant="$(package_variant)"
if [[ "${variant}" == macos-* ]]; then
printf 'outer-shell-%s.zip\n' "${variant}"
else
printf 'outer-shell-%s.tar.gz\n' "${variant}"
fi
}
cmd_build_frontend() {
local clean_build="${1:-0}"
[[ "$(uname -s)" == Darwin ]] || {
echo "error: Outer Shell frontend and macOS agent must be built on macOS" >&2
exit 1
}
echo "==> Building Outer Shell macOS and frontend resources"
CLEAN_FRONTEND_BUILD="${clean_build}" \
CONFIGURATION="${BUILD_CONFIGURATION}" \
"${ROOT}/build_run.sh"
}
build_linux_target() {
require_tool docker
local image="alpine:3.20" platform
[[ "${TARGET_ARCH}" == aarch64 ]] && platform=linux/arm64 || platform=linux/amd64
echo "==> Building Outer Shell for Linux/${TARGET_ARCH} with musl"
docker run --rm --platform "${platform}" \
-v "${ROOT}:/work" \
-w /work \
"${image}" \
sh -lc 'apk add --no-cache bash build-base openssl-dev openssl-libs-static zlib-dev zlib-static libpng-dev libpng-static wget && ln -sf /lib/libz.a /usr/lib/libz.a && OUTER_SHELL_LINUX_LIBC=musl bash ./Scripts/build_linux_resources.sh'
}
archive_frontend_symbols() {
local source_dsym="${ROOT}/build/macos/${BUILD_CONFIGURATION}/Outer Shell.bundle.dSYM"
local arm64_uuid
local destination_dsym
arm64_uuid="$(dwarfdump --uuid "${source_dsym}" | awk '$3 == "(arm64)" { print $2; exit }')"
if [[ -z "${arm64_uuid}" ]]; then
echo "error: frontend dSYM does not contain an arm64 UUID" >&2
exit 1
fi
destination_dsym="${SYMBOLS_DIR}/${arm64_uuid}/Outer Shell.bundle.dSYM"
mkdir -p "$(dirname "${destination_dsym}")"
ditto "${source_dsym}" "${destination_dsym}"
FRONTEND_SYMBOLS_PATH="${destination_dsym}"
}
cmd_build() {
local clean_frontend="${1:-0}"
probe_target
cmd_build_frontend "${clean_frontend}"
if [[ "${TARGET_OS}" == Linux ]]; then
build_linux_target
fi
local variant public_base_url
variant="$(package_variant)"
public_base_url="${OUTER_TARGET_PUBLIC_BASE_URL:-https://outershell.org/outer-shell}"
echo "==> Packaging ${variant}"
OUTPUT_ROOT="${RELEASE_DIR}" \
PUBLIC_BASE_URL="${public_base_url}" \
MACOS_BUILD_ROOT="${ROOT}/build/macos/${BUILD_CONFIGURATION}" \
OUTER_SHELL_PACKAGE_VARIANTS="${variant}" \
OUTER_SHELL_CODESIGN_IDENTITY="${OUTER_SHELL_CODESIGN_IDENTITY:--}" \
"${ROOT}/Scripts/package_release.sh"
archive_frontend_symbols
echo "==> Deployable archive: ${RELEASE_DIR}/latest/$(archive_name)"
echo "==> Frontend profiling symbols: ${FRONTEND_SYMBOLS_PATH}"
}
cmd_deploy() {
cmd_build 1
local archive remote_dir install_command
archive="$(archive_name)"
remote_dir=".cache/outershell-deploy"
echo "==> Uploading Outer Shell installer and ${archive}"
COPYFILE_DISABLE=1 tar czf - \
-C "${RELEASE_DIR}/latest" install.sh "${archive}" | run_ssh "
set -e
rm -rf \"\$HOME/${remote_dir}\"
mkdir -p \"\$HOME/${remote_dir}\"
tar xzf - -C \"\$HOME/${remote_dir}\"
"
echo "==> Installing Outer Shell"
install_command="OUTERSHELL_INSTALL_ARCHIVE=\"\$HOME/${remote_dir}/${archive}\" sh \"\$HOME/${remote_dir}/install.sh\" install"
if [[ -t 0 ]]; then
run_ssh_interactive "${install_command}"
else
run_ssh "${install_command}"
fi
echo "Deployed Outer Shell."
echo "Frontend profiling symbols: ${FRONTEND_SYMBOLS_PATH}"
dwarfdump --uuid "${FRONTEND_SYMBOLS_PATH}"
}
cmd_push_frontend() {
probe_target
cmd_build_frontend
local remote_dir
if [[ "${TARGET_OS}" == Darwin ]]; then
remote_dir="${HOME}/Library/Application Support/outershell/apps/org.outershell.OuterShell/Outer Shell.app/Contents/Resources/bundles"
# Use the target's home, not the build host's home.
remote_dir='${HOME}/Library/Application Support/outershell/apps/org.outershell.OuterShell/Outer Shell.app/Contents/Resources/bundles'
elif [[ "${TARGET_UID}" == 0 ]]; then
remote_dir="/var/lib/outershell/outer-shell/bundles"
else
remote_dir='${XDG_STATE_HOME:-$HOME/.local/state}/outershell/outer-shell/bundles'
fi
COPYFILE_DISABLE=1 tar czf - -C "${ROOT}/build/run/bundles" \
OuterShell.bundle.macos-arm.aar OuterShell.bundle.macos-x86.aar | run_ssh "
set -e
remote_dir=\"${remote_dir}\"
mkdir -p \"\$remote_dir\"
tar xzf - -C \"\$remote_dir\"
"
echo "Pushed Outer Shell frontend bundles. Reload Outer Shell in Outer Loop."
}
cmd_uninstall() {
cmd_build
run_ssh "sh -s -- uninstall" < "${RELEASE_DIR}/latest/install.sh"
}
cmd_target() {
if [[ $# -ne 1 ]]; then
echo "usage: ./app target \"ssh -p 22 you@server\"" >&2
exit 1
fi
read -r -a words <<< "$1"
{
printf 'OUTER_TARGET_KIND=ssh\nOUTER_TARGET_SSH=(\n'
local word
for word in "${words[@]}"; do
printf ' '
shell_quote "${word}"
printf '\n'
done
printf ')\nOUTER_TARGET_PUBLIC_BASE_URL=https://outershell.org/outer-shell\n'
} > "${ROOT}/target.env"
echo "Wrote target.env"
}
cmd_status() {
probe_target
if [[ "${TARGET_OS}" == Darwin ]]; then
run_ssh "launchctl print gui/\$(id -u)/org.outershell.OuterShell" || true
elif [[ "${TARGET_UID}" == 0 ]]; then
run_ssh "systemctl --system status org.outershell.OuterShell.socket --no-pager" || true
else
run_ssh "systemctl --user status org.outershell.OuterShell.socket --no-pager" || true
fi
}
cmd_logs() {
probe_target
if [[ "${TARGET_OS}" == Darwin ]]; then
run_ssh_interactive 'tail -f "$HOME/Library/Logs/org.outershell.OuterShell/output.log"'
elif [[ "${TARGET_UID}" == 0 ]]; then
run_ssh_interactive 'tail -f /var/log/outershell/org.outershell.OuterShell.log'
else
run_ssh_interactive 'tail -f "${XDG_STATE_HOME:-$HOME/.local/state}/outershell/outer-shell/logs/OuterShellBackend.log"'
fi
}
cmd_clean() {
rm -rf "${BUILD_DIR}"
}
cmd_help() {
cat <<'EOF'
Outer Shell development tasks
./app target "ssh ..." write target.env
./app build build and package for target.env
./app deploy build, upload, and install Outer Shell
./app push-frontend update only the target frontend bundles
./app status show target service status
./app logs follow the target log
./app ssh [command] open a shell or run a target command
./app uninstall uninstall Outer Shell from the target
./app clean remove direct-deploy build products
EOF
}
command="${1:-help}"
shift $(( $# > 0 ? 1 : 0 ))
case "${command}" in
target) cmd_target "$@" ;;
build) cmd_build ;;
build-frontend) cmd_build_frontend ;;
deploy) cmd_deploy ;;
push-frontend) cmd_push_frontend ;;
status) cmd_status ;;
logs) cmd_logs ;;
ssh) load_target; run_ssh "$@" ;;
uninstall) cmd_uninstall ;;
clean) cmd_clean ;;
help|--help|-h) cmd_help ;;
*) echo "error: unknown command '${command}'" >&2; cmd_help >&2; exit 1 ;;
esac