-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintstack.sh
More file actions
executable file
·213 lines (170 loc) · 5.46 KB
/
Copy pathprintstack.sh
File metadata and controls
executable file
·213 lines (170 loc) · 5.46 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
#!/bin/bash
# printstack.sh -- CLI for the immutable USB/IP proxy + print server stack
set -euo pipefail
_PRINTSTACK_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
export SCRIPT_DIR="$_PRINTSTACK_DIR"
# shellcheck source=scripts/config.sh
source "${SCRIPT_DIR}/scripts/config.sh"
# shellcheck source=scripts/create-log.sh
source "${SCRIPT_DIR}/scripts/create-log.sh"
VERBOSE=0
QUIET=0
RED=$'\033[0;31m'
GRN=$'\033[0;32m'
YLW=$'\033[0;33m'
BLU=$'\033[0;34m'
DIM=$'\033[2m'
RST=$'\033[0m'
err() { echo -e "${RED}[ERROR]${RST} $*" >&2; exit 1; }
ok() { [[ $QUIET -eq 0 ]] && echo -e "${GRN}[OK]${RST} $*"; return 0; }
warn() { [[ $QUIET -eq 0 ]] && echo -e "${YLW}[WARN]${RST} $*"; return 0; }
debug() { [[ $VERBOSE -eq 1 && $QUIET -eq 0 ]] && echo -e "${DIM}[DEBUG]${RST} $*" >&2; return 0; }
info() {
[[ $QUIET -eq 0 ]] || return 0
if create_log_enabled; then
create_log_stamp_line "printstack.sh" "$*"
fi
echo -e "${BLU}[INFO]${RST} $*"
}
usage() {
cat <<'EOF'
printstack -- immutable USB/IP proxy + CUPS print server
Global flags (any position):
-v, --verbose Verbose output
-q, --quiet Suppress non-error output
--create-log Session log with GUID (implies --timestamp and -v)
--timestamp Prefix subprocess output with timestamps
-env=<file> Load ~/.printstack/<file> instead of default .env
Commands:
flash [--force] Flash Pi SD card (pi-bootstrap.sh --flash)
refresh Rebuild printserver image and reprovision container
help [command] Show help
Examples:
printstack --create-log flash --force
printstack --timestamp refresh
printstack help flash
Logs: ~/.printstack/logs/ (with --create-log)
Session registry: ~/.printstack/logs/sessions.watch
EOF
}
help_flash() {
cat <<'EOF'
printstack flash -- flash Ubuntu to the Pi SD card
Wraps pi-bootstrap.sh. Requires root (uses sudo when needed).
Options:
--force Skip SD card destruction confirmation (implies --flash)
Setup:
cp shared.env.example shared.env
cp pi-bootstrap.env.example pi-bootstrap.env
chmod 600 shared.env pi-bootstrap.env
Examples:
printstack flash
printstack --create-log flash --force
EOF
}
help_refresh() {
cat <<'EOF'
printstack refresh -- rebuild and redeploy the print server
Immutable refresh (no state preserved):
1. printserver-image-build.sh --force (rebuild printserver-base image)
2. printserver-bootstrap.sh --reprovision (destroy + recreate container)
Setup:
cp shared.env.example shared.env
cp printserver-bootstrap.env.example printserver-bootstrap.env
chmod 600 shared.env printserver-bootstrap.env
./printserver-image-build.sh (first deploy only; refresh always rebuilds)
Examples:
printstack refresh
printstack --create-log --timestamp refresh
EOF
}
cmd_flash() {
local force=false
local arg
for arg in "$@"; do
case "$arg" in
help) help_flash; return 0 ;;
--force) force=true ;;
*) err "Unknown flash argument: $arg (try: printstack help flash)" ;;
esac
done
local pi_script="${PROJECT_ROOT}/pi-bootstrap.sh"
[[ -x "$pi_script" || -f "$pi_script" ]] || err "pi-bootstrap.sh not found: $pi_script"
local -a flash_args=(--flash)
[[ "$force" == true ]] && flash_args+=(--force)
info "Flash Pi SD card (${flash_args[*]})..."
if [[ $EUID -ne 0 ]]; then
create_log_run "pi-bootstrap" sudo -E "$pi_script" "${flash_args[@]}"
else
create_log_run "pi-bootstrap" "$pi_script" "${flash_args[@]}"
fi
ok "Pi flash complete."
}
cmd_refresh() {
local arg
for arg in "$@"; do
case "$arg" in
help) help_refresh; return 0 ;;
*) err "Unknown refresh argument: $arg (try: printstack help refresh)" ;;
esac
done
local image_script="${PROJECT_ROOT}/printserver-image-build.sh"
local bootstrap_script="${PROJECT_ROOT}/printserver-bootstrap.sh"
[[ -f "$image_script" ]] || err "printserver-image-build.sh not found"
[[ -f "$bootstrap_script" ]] || err "printserver-bootstrap.sh not found"
info "Step 1/2: Rebuild printserver-base Incus image (--force)..."
create_log_run "printserver-image-build" bash "$image_script" --force
info "Step 2/2: Reprovision printserver container (--reprovision)..."
create_log_run "printserver-bootstrap" bash "$bootstrap_script" --reprovision
ok "Print server refresh complete."
}
main() {
local invocation_cmd="printstack" arg
for arg in "$@"; do
invocation_cmd+=" $(printf '%q' "$arg")"
done
printstack_parse_global_argv "$@"
set -- "${PRINTSTACK_ARGV[@]}"
eval "$(AGENTSTARTSTACK_CLI_TOOL=printstack \
"${SCRIPT_DIR}/.agentstartstack/scripts/cli-preamble.sh" "$SCRIPT_DIR")"
local command="${1:-help}"
[[ $# -gt 0 ]] && shift
create_log_setup "$command"
if create_log_enabled; then
info "Session log: tail -f ${PRINTSTACK_LOG_FILE}"
fi
create_log_write_header
create_log_watch_append "$invocation_cmd"
if [[ -f "$ENV_FILE" ]]; then
debug "Loading environment from: $ENV_FILE"
set +u
# shellcheck source=/dev/null
source "$ENV_FILE"
set -u
fi
case "$command" in
flash)
cmd_flash "$@"
;;
refresh)
cmd_refresh "$@"
;;
help|-h|--help)
if [[ $# -eq 0 ]]; then
usage
else
case "$1" in
flash) help_flash ;;
refresh) help_refresh ;;
*) err "Unknown help topic: $1" ;;
esac
fi
;;
*)
err "Unknown command: $command. Try 'printstack help'"
;;
esac
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi