-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp
More file actions
executable file
·362 lines (330 loc) · 11.5 KB
/
Copy pathapp
File metadata and controls
executable file
·362 lines (330 loc) · 11.5 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/bin/bash
# Development task runner for an official Outer Shell bundled app.
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "${ROOT}"
source "${ROOT}/app.env"
BUILD_DIR="${ROOT}/build/app-deploy"
STAGE_PARENT="${BUILD_DIR}/stage"
STAGE_ROOT="${STAGE_PARENT}/${APP_STAGE_NAME}"
FRONTEND_BUILD_DIR="${BUILD_DIR}/frontend"
MATRIX_ROOT="${ROOT}/build/linux-package"
TARGET_KIND=ssh
SSH_BASE=()
TARGET_OS=""
TARGET_ARCH=""
TARGET_LIBC=""
TARGET_UID=""
TARGET_HOME=""
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: official bundled-app 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[@]}" "$@"
}
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)"
TARGET_HOME="$(run_ssh 'printf %s "$HOME"' </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
if [[ "${TARGET_OS}" != Linux ]]; then
echo "error: ${APP_NAME} development deploys currently support Linux SSH targets" >&2
exit 1
fi
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
}
target_scope() {
local scope="${OUTER_TARGET_SCOPE:-${DEFAULT_DEPLOY_SCOPE:-user}}"
if [[ "${TARGET_UID}" == 0 ]]; then scope=system; fi
if [[ "${scope}" != user && "${scope}" != system ]]; then
echo "error: OUTER_TARGET_SCOPE must be user or system" >&2
exit 1
fi
if [[ "${ROOT_ONLY:-false}" == true && "${scope}" != system ]]; then
echo "error: ${APP_NAME} is root-only; set OUTER_TARGET_SCOPE=system" >&2
exit 1
fi
printf '%s\n' "${scope}"
}
cmd_build_frontend() {
[[ "$(uname -s)" == Darwin ]] || {
echo "error: frontend bundles must be built on macOS" >&2
exit 1
}
require_tool /usr/bin/xcodebuild
require_tool aa
require_tool lipo
rm -rf "${FRONTEND_BUILD_DIR}" "${STAGE_ROOT}/bundles"
mkdir -p "${FRONTEND_BUILD_DIR}" "${STAGE_ROOT}/bundles"
echo "==> Building ${APP_NAME} frontend"
/usr/bin/xcodebuild \
-project "${ROOT}/${XCODE_PROJECT}" \
-scheme "${XCODE_SCHEME}" \
-configuration Release \
SYMROOT="${FRONTEND_BUILD_DIR}" \
ARCHS="arm64 x86_64" \
ONLY_ACTIVE_ARCH=NO \
CODE_SIGNING_ALLOWED=NO \
CODE_SIGNING_REQUIRED=NO \
build
local bundle="${FRONTEND_BUILD_DIR}/Release/${XCODE_PRODUCT_NAME}.bundle"
[[ -d "${bundle}" ]] || { echo "error: expected ${bundle}" >&2; exit 1; }
"${ROOT}/${BUNDLE_ARCHIVE_SCRIPT}" \
"${bundle}" \
"${STAGE_ROOT}/bundles" \
"${BUNDLE_ARCHIVE_STEM}"
install -m 0644 "${ROOT}/app-icon.png" "${STAGE_ROOT}/app-icon.png"
}
backend_directory() {
local root="$1" libc="$2" arch="$3"
if [[ "${libc}" == musl ]]; then
printf '%s/RemoteLinuxBinariesMusl/%s\n' "${root}" "${arch}"
else
printf '%s/RemoteLinuxBinaries/%s\n' "${root}" "${arch}"
fi
}
backend_image() {
local libc="$1" arch="$2"
if [[ "${libc}" == musl ]]; then
printf 'quay.io/pypa/musllinux_1_2_%s\n' "${arch}"
else
printf 'quay.io/pypa/manylinux2014_%s\n' "${arch}"
fi
}
backend_platform() {
[[ "$1" == aarch64 ]] && printf 'linux/arm64\n' || printf 'linux/amd64\n'
}
build_backend_in_docker() {
local libc="$1" arch="$2" output_root="$3"
local image platform output_dir
image="$(backend_image "${libc}" "${arch}")"
platform="$(backend_platform "${arch}")"
output_dir="$(backend_directory "${output_root}" "${libc}" "${arch}")"
echo "==> Building ${BACKEND_EXECUTABLE_NAME} for Linux/${arch}/${libc}"
mkdir -p "${output_dir}"
docker run --rm --platform "${platform}" \
-v "${ROOT}:/src:ro" \
-v "${output_dir}:/out" \
-e CC=gcc \
-e "BACKEND_SOURCE=${BACKEND_SOURCE}" \
-e "BACKEND_EXECUTABLE_NAME=${BACKEND_EXECUTABLE_NAME}" \
-e "BACKEND_CFLAGS=${BACKEND_CFLAGS}" \
-e "BACKEND_LDFLAGS=${BACKEND_LDFLAGS}" \
"${image}" \
bash -lc '"$CC" $BACKEND_CFLAGS -o "/out/$BACKEND_EXECUTABLE_NAME" "/src/$BACKEND_SOURCE" $BACKEND_LDFLAGS && strip --strip-unneeded "/out/$BACKEND_EXECUTABLE_NAME"'
}
build_backend_on_target() {
local output_dir remote_build=".cache/outershell-app-build/${APP_ID}"
output_dir="$(backend_directory "${STAGE_ROOT}" "${TARGET_LIBC}" "${TARGET_ARCH}")"
echo "==> Docker is unavailable; building ${BACKEND_EXECUTABLE_NAME} on the ${TARGET_LIBC} target"
COPYFILE_DISABLE=1 tar czf - -C "${ROOT}" Backend | run_ssh "
set -e
rm -rf \"\$HOME/${remote_build}\"
mkdir -p \"\$HOME/${remote_build}\"
tar xzf - -C \"\$HOME/${remote_build}\"
cc ${BACKEND_CFLAGS} -o \"\$HOME/${remote_build}/${BACKEND_EXECUTABLE_NAME}\" \
\"\$HOME/${remote_build}/${BACKEND_SOURCE}\" ${BACKEND_LDFLAGS}
if command -v strip >/dev/null 2>&1; then
strip --strip-unneeded \"\$HOME/${remote_build}/${BACKEND_EXECUTABLE_NAME}\"
fi
"
mkdir -p "${output_dir}"
run_ssh "cat \"\$HOME/${remote_build}/${BACKEND_EXECUTABLE_NAME}\"" > "${output_dir}/${BACKEND_EXECUTABLE_NAME}"
chmod 0755 "${output_dir}/${BACKEND_EXECUTABLE_NAME}"
}
build_target_backend() {
if [[ "${OUTER_BUILD_MODE:-auto}" != target ]] &&
command -v docker >/dev/null 2>&1 && docker info >/dev/null 2>&1; then
build_backend_in_docker "${TARGET_LIBC}" "${TARGET_ARCH}" "${STAGE_ROOT}"
else
build_backend_on_target
fi
}
cmd_build_backend() {
probe_target
build_target_backend
}
cmd_build_matrix() {
require_tool docker
docker info >/dev/null
rm -rf "${MATRIX_ROOT}/RemoteLinuxBinaries" "${MATRIX_ROOT}/RemoteLinuxBinariesMusl"
local libc arch
for libc in glibc musl; do
for arch in aarch64 x86_64; do
build_backend_in_docker "${libc}" "${arch}" "${MATRIX_ROOT}"
done
done
echo "==> Linux backend matrix ready at ${MATRIX_ROOT}"
}
cmd_build() {
probe_target
rm -rf "${STAGE_ROOT}"
mkdir -p "${STAGE_ROOT}"
cmd_build_frontend
build_target_backend
echo "==> Payload ready at ${STAGE_ROOT}"
}
find_outerctl_command() {
printf '%s' 'outerctl="${XDG_STATE_HOME:-$HOME/.local/state}/outershell/bin/outerctl"; '
printf '%s' 'if [ ! -x "$outerctl" ]; then outerctl="$(command -v outerctl 2>/dev/null || true)"; fi; '
printf '%s' 'if [ ! -x "$outerctl" ]; then echo "error: install the current Outer Shell on this target first" >&2; exit 1; fi; '
}
run_bundled_control() {
local action="$1"
local stage_root="${2:-}"
local scope command
scope="$(target_scope)"
command="$(find_outerctl_command)"
command+="\"\$outerctl\" bundled-app ${action} --backend $(shell_quote "${APP_ID}") --scope $(shell_quote "${scope}")"
if [[ "${action}" == install ]]; then
command+=" --stage-root $(shell_quote "${stage_root}")"
fi
if [[ "${scope}" == system && "${TARGET_UID}" != 0 ]]; then
local password
read -r -s -p "Administrator password for target: " password </dev/tty
printf '\n' >/dev/tty
printf '%s\n' "${password}" | run_ssh "${command} --sudo-password-stdin"
unset password
else
run_ssh "${command}"
fi
}
cmd_deploy() {
cmd_build
local remote_parent=".cache/outershell-app-deploy/${APP_ID}"
local remote_stage="${TARGET_HOME}/${remote_parent}/${APP_STAGE_NAME}"
echo "==> Uploading ${APP_NAME} payload"
COPYFILE_DISABLE=1 tar czf - -C "${STAGE_PARENT}" "${APP_STAGE_NAME}" | run_ssh "
set -e
rm -rf \"\$HOME/${remote_parent}\"
mkdir -p \"\$HOME/${remote_parent}\"
tar xzf - -C \"\$HOME/${remote_parent}\"
"
echo "==> Installing ${APP_NAME} through Outer Shell"
run_bundled_control install "${remote_stage}"
echo "Deployed ${APP_NAME}."
}
cmd_uninstall() {
probe_target
run_bundled_control uninstall ""
}
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_SCOPE=%s\n' "${DEFAULT_DEPLOY_SCOPE:-user}"
} > "${ROOT}/target.env"
echo "Wrote target.env"
}
cmd_status() {
probe_target
local scope
scope="$(target_scope)"
if [[ "${scope}" == system ]]; then
run_ssh "systemctl --system status '${APP_ID}.service' --no-pager" || true
else
run_ssh "systemctl --user status '${APP_ID}.service' --no-pager" || true
fi
}
cmd_logs() {
probe_target
local scope
scope="$(target_scope)"
if [[ "${scope}" == system ]]; then
run_ssh -t "journalctl --system -u '${APP_ID}.service' -f"
else
run_ssh -t "journalctl --user -u '${APP_ID}.service' -f"
fi
}
cmd_clean() {
rm -rf "${BUILD_DIR}"
}
cmd_help() {
cat <<EOF
${APP_NAME} development tasks
./app target "ssh ..." write target.env
./app build build a payload for target.env
./app build-matrix build all Linux libc/architecture release variants
./app deploy build, upload, and install through Outer Shell
./app status show the target service status
./app logs follow the target service log
./app ssh [command] open a shell or run a target command
./app uninstall uninstall the target app
./app clean remove development build products
EOF
}
command="${1:-help}"
shift $(( $# > 0 ? 1 : 0 ))
case "${command}" in
target) cmd_target "$@" ;;
build) cmd_build ;;
build-frontend) cmd_build_frontend ;;
build-backend) cmd_build_backend ;;
build-matrix) cmd_build_matrix ;;
deploy) cmd_deploy ;;
status) cmd_status ;;
logs) cmd_logs ;;
ssh) probe_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