-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun.sh
executable file
·385 lines (340 loc) · 10.5 KB
/
run.sh
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/bin/bash
set -euo pipefail
# configuration
readonly DOCKER_COMPOSE_FILE_BASE="docker/docker-compose.base.yml"
readonly DOCKER_COMPOSE_FILE="docker/docker-compose.dev.yml"
readonly USER_DOCKER_COMPOSE_FILE="docker/docker-compose.user.yml"
readonly PROJECT_NAME="chipper"
readonly LOCAL_URL="http://localhost:21200"
readonly ELASTICVUE_URL="http://localhost:21230"
readonly SCRIPT_VERSION="1.3.0"
# Container engine configuration
CONTAINER_ENGINE="${CONTAINER_ENGINE:-docker}"
# common messages
readonly WARN_CLEAN_PROMPT="⚠️ WARNING: This will delete all %s. Are you sure? [y/N] "
readonly ERR_DIR_NOT_FOUND="Error: Directory '%s' not found"
readonly ERR_CMD_NOT_FOUND="Error: '%s' is not available. Please install it and try again."
readonly ERR_ENGINE_NOT_RUNNING="Error: %s is not running. Please start %s and try again."
readonly ERR_COMPOSE_FILE_NOT_FOUND="Error: Docker compose file '%s' not found"
readonly ERR_INVALID_ENGINE="Error: Invalid container engine '%s'. Supported engines: docker, podman"
function show_welcome() {
printf "\n"
printf "\033[35m"
printf " __ _ \n"
printf " _____/ /_ (_)___ ____ ___ _____\n"
printf " / ___/ __ \/ / __ \/ __ \/ _ \/ ___/\n"
printf "/ /__/ / / / / /_/ / /_/ / __/ / \n"
printf "\___/_/ /_/_/ .___/ .___/\___/_/ \n"
printf " /_/ /_/ \n"
printf "\033[0m\n"
printf "\033[34m Chipper Run v%s\033[0m\n" "${SCRIPT_VERSION}"
printf "\033[34m Using %s engine\033[0m\n" "${CONTAINER_ENGINE}"
printf "\n"
}
show_welcome
function show_usage() {
cat << EOF
Usage: $0 <command> [args]
Options:
-f, --file - Specify custom docker-compose file path
-e, --engine - Specify container engine (docker|podman)
Commands:
up - Start containers in detached mode
down - Stop containers
rebuild - Clean, rebuild and recreate images and containers
logs - Show container logs
ps - Show container status
env - Create or update all dotfiles like .env and .systemprompt
clean-full - Cleans all project related files, including images, volumes and env files
clean-volumes - Delete all volumes
clean-env - Delete all dotfiles like .env and .systemprompt
embed [args] - Run embed tool with optional arguments
embed-testdata - Run embed tool with internal testdata
scrape [args] - Run scrape tool with optional arguments
dev-api - Start API in development mode
dev-web - Start web service in development mode
dev-docs - Run local vitepress server
css - Watch and rebuild CSS files
format - Run pre-commit formatting hooks
browser - Open web-interface in local browser
evue - Open elasticvue web-interface in local browser
cli - Run cli interface
EOF
}
function error_exit() {
echo "$1" >&2
exit 1
}
function confirm_clean() {
local target="$1"
printf "$WARN_CLEAN_PROMPT" "$target"
read -r response
case "$response" in
[yY]|[yY][eE][sS])
return 0
;;
*)
echo "Operation cancelled."
exit 0
;;
esac
}
function check_dependency() {
local cmd="$1"
local message="${2:-$(printf "$ERR_CMD_NOT_FOUND" "$cmd")}"
command -v "$cmd" &> /dev/null || error_exit "$message"
}
function check_directory() {
local dir="$1"
[ -d "$dir" ] || error_exit "$(printf "$ERR_DIR_NOT_FOUND" "$dir")"
}
function detect_compose_cmd() {
local engine="$1"
if [ "$engine" = "podman" ]; then
if podman-compose version >/dev/null 2>&1; then
echo "podman-compose"
else
error_exit "$(printf "$ERR_CMD_NOT_FOUND" "podman-compose")"
fi
else
if docker compose version >/dev/null 2>&1; then
echo "docker compose"
elif docker-compose version >/dev/null 2>&1; then
echo "docker-compose"
else
error_exit "Neither 'docker compose' nor 'docker-compose' is available"
fi
fi
}
function validate_container_engine() {
case "$1" in
docker|podman)
return 0
;;
*)
error_exit "$(printf "$ERR_INVALID_ENGINE" "$1")"
;;
esac
}
COMPOSE_CMD=$(detect_compose_cmd "$CONTAINER_ENGINE")
function compose_cmd() {
if [ "$COMPOSE_CMD" = "docker compose" ]; then
docker compose "${COMPOSE_FILES[@]}" "$@"
elif [ "$COMPOSE_CMD" = "podman-compose" ]; then
podman-compose "${COMPOSE_FILES[@]}" "$@"
else
docker-compose "${COMPOSE_FILES[@]}" "$@"
fi
}
function run_in_directory() {
local dir="$1"
shift
check_directory "$dir"
(cd "$dir" && "$@")
}
function open_url() {
local url="$1"
case "$(uname -s)" in
Darwin)
open "$url"
;;
Linux)
if command -v xdg-open >/dev/null; then
xdg-open "$url"
else
echo "Please install xdg-utils or manually open: $url"
fi
;;
MINGW*|MSYS*|CYGWIN*)
start "$url"
;;
*)
echo "Unsupported operating system for automatic browser opening"
echo "Please manually open: $url"
;;
esac
}
function open_browser() {
open_url "$LOCAL_URL"
}
function open_elasticvue() {
open_url "$ELASTICVUE_URL"
}
function check_engine_running() {
if [ "$CONTAINER_ENGINE" = "podman" ]; then
podman info >/dev/null 2>&1 || error_exit "$(printf "$ERR_ENGINE_NOT_RUNNING" "Podman" "Podman")"
else
docker info >/dev/null 2>&1 || error_exit "$(printf "$ERR_ENGINE_NOT_RUNNING" "Docker" "Docker")"
fi
}
function compose_down() {
echo "Stopping containers..."
compose_cmd -p "$PROJECT_NAME" down --remove-orphans
}
function compose_down_clean() {
echo "Stopping containers and removing volumes..."
compose_cmd -p "$PROJECT_NAME" down -v --remove-orphans
}
function clean_environment() {
echo "Cleaning environment files..."
python setup.py --clean
}
function ensure_environment() {
python setup.py
}
function clean_project_images() {
echo "Removing project-related images..."
$CONTAINER_ENGINE images --filter "reference=$PROJECT_NAME*" -q | xargs -r $CONTAINER_ENGINE rmi -f
echo "Project images cleaned"
}
function print_local_url() {
local green="\033[0;32m"
local reset="\033[0m"
local bold="\033[1m"
echo
echo -e "Open the Chipper Web-Interface at:"
echo -e "${bold}${green}➜${reset} ${bold}$LOCAL_URL${reset}"
echo
}
COMPOSE_FILES=(-f "$DOCKER_COMPOSE_FILE_BASE")
COMPOSE_FILES+=(-f "$DOCKER_COMPOSE_FILE")
# parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-f|--file)
[ -f "$2" ] || error_exit "$(printf "$ERR_COMPOSE_FILE_NOT_FOUND" "$2")"
COMPOSE_FILES+=(-f "$2")
shift 2
;;
-e|--engine)
validate_container_engine "$2"
CONTAINER_ENGINE="$2"
COMPOSE_CMD=$(detect_compose_cmd "$CONTAINER_ENGINE")
shift 2
;;
*)
break
;;
esac
done
# check for user compose file
if [ -f "$USER_DOCKER_COMPOSE_FILE" ]; then
echo "Found user compose file"
COMPOSE_FILES+=(-f "$USER_DOCKER_COMPOSE_FILE")
fi
# show usage if no command provided
[ $# -eq 0 ] && { show_usage; exit 1; }
# pre-command dependency checks
case "$1" in
up|down|logs|ps|rebuild|clean-volumes|embed*|scrape)
check_engine_running
check_dependency "$CONTAINER_ENGINE"
;;
dev-api|dev-web)
check_dependency make "Error: 'make' is required for development mode"
check_directory "services/${1#dev-}"
;;
css)
check_dependency make
check_dependency node
check_directory "services/web"
;;
format)
check_dependency pre-commit
;;
esac
# main command handling
case "$1" in
"up")
ensure_environment
compose_down
compose_cmd -p "$PROJECT_NAME" up -d
print_local_url
;;
"down")
compose_down
;;
"rebuild")
confirm_clean "containers and rebuild all images"
compose_down
clean_project_images
echo "Ensure valid environment..."
ensure_environment
echo "Rebuilding containers..."
compose_cmd build --no-cache
echo "Starting containers..."
compose_cmd -p "$PROJECT_NAME" up -d --force-recreate
echo "Clean and rebuild complete!"
print_local_url
;;
"logs")
compose_cmd -p "$PROJECT_NAME" logs -f
;;
"ps")
compose_cmd -p "$PROJECT_NAME" ps
;;
"env")
echo "Creating environment files..."
ensure_environment
;;
"clean-full")
confirm_clean "containers, volumes, environment files"
compose_down_clean
clean_project_images
clean_environment
;;
"clean-volumes")
confirm_clean "Docker volumes and volume directories"
ensure_environment
compose_down_clean
echo "Cleaning up volume directories..."
rm -rfv docker/volumes
echo "Volume directories cleaned"
echo "Clean complete!"
;;
"clean-env")
confirm_clean "environment files (.env, .systemprompt, .ragignore)"
clean_environment
;;
"embed-testdata")
shift
run_in_directory "tools/embed" ./run.sh "$(pwd)/tools/embed/testdata" "$@"
;;
"embed"|"scrape")
command="$1"
shift
[ $# -eq 0 ] && error_exit "Error: ${command} command requires arguments"
run_in_directory "tools/${command}" ./run.sh "$@"
;;
"dev-api"|"dev-web")
run_in_directory "services/${1#dev-}" make dev
;;
"dev-docs")
shift
echo "Starting vitepress..."
yarn add -D vitepress
yarn docs:dev "$@"
;;
"css")
run_in_directory "services/web" make build-watch-css
;;
"format")
echo "Running pre-commit hooks for formatting..."
pre-commit run --all-files
echo "Formatting completed successfully!"
;;
"browser")
open_browser
;;
"evue")
open_elasticvue
;;
"cli")
shift
run_in_directory "tools/cli" ./run.sh "$@"
;;
*)
show_usage
exit 1
;;
esac