Skip to content

Commit 85a5266

Browse files
committed
Add comprehensive cleanup functionality to quick-start-containers.sh
- Add --stop option to gracefully stop running containers - Add --clean-all option for complete cleanup (containers, volumes, images, pods) - Add selective cleanup options: --remove-volumes, --remove-images, --remove-pods - Add comprehensive cleanup function with smart resource detection - Update help documentation and quick commands - Enhance user experience with detailed status reporting and next steps - Improve container lifecycle management with robust cleanup capabilities
1 parent 475880b commit 85a5266

File tree

1 file changed

+221
-15
lines changed

1 file changed

+221
-15
lines changed

quick-start-containers.sh

Lines changed: 221 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,159 @@ verify_deployment() {
256256
done
257257
}
258258

259+
# Function to stop containers
260+
stop_containers() {
261+
print_status "Stopping CI Analysis Agent containers..."
262+
263+
# Stop containers
264+
if podman container exists "$AGENT_CONTAINER" 2>/dev/null; then
265+
if podman ps | grep -q "$AGENT_CONTAINER"; then
266+
print_status "Stopping CI Analysis Agent container..."
267+
podman stop "$AGENT_CONTAINER"
268+
print_success "CI Analysis Agent container stopped"
269+
else
270+
print_warning "CI Analysis Agent container is not running"
271+
fi
272+
else
273+
print_warning "CI Analysis Agent container does not exist"
274+
fi
275+
276+
if podman container exists "$OLLAMA_CONTAINER" 2>/dev/null; then
277+
if podman ps | grep -q "$OLLAMA_CONTAINER"; then
278+
print_status "Stopping Ollama container..."
279+
podman stop "$OLLAMA_CONTAINER"
280+
print_success "Ollama container stopped"
281+
else
282+
print_warning "Ollama container is not running"
283+
fi
284+
else
285+
print_warning "Ollama container does not exist"
286+
fi
287+
288+
echo ""
289+
echo "================================================================="
290+
echo " 🛑 CONTAINERS STOPPED 🛑"
291+
echo "================================================================="
292+
echo ""
293+
echo "📊 Container Status:"
294+
podman ps -a --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" | grep -E "$OLLAMA_CONTAINER|$AGENT_CONTAINER" || echo " No containers found"
295+
echo ""
296+
echo "🎯 Quick Commands:"
297+
echo " • Start containers: podman start $OLLAMA_CONTAINER $AGENT_CONTAINER"
298+
echo " • Clean up all: $0 --clean-all"
299+
echo " • Remove volumes: $0 --remove-volumes"
300+
echo " • Remove images: $0 --remove-images"
301+
echo " • Check logs: podman logs $AGENT_CONTAINER"
302+
echo " • Restart deployment: $0"
303+
echo "================================================================="
304+
}
305+
306+
# Function to clean up all resources
307+
clean_all() {
308+
local remove_volumes="$1"
309+
local remove_images="$2"
310+
local remove_pods="$3"
311+
312+
print_status "Performing comprehensive cleanup..."
313+
314+
# Stop containers first
315+
print_status "Stopping containers..."
316+
podman stop "$OLLAMA_CONTAINER" "$AGENT_CONTAINER" 2>/dev/null || true
317+
318+
# Remove containers
319+
print_status "Removing containers..."
320+
if podman container exists "$AGENT_CONTAINER" 2>/dev/null; then
321+
podman rm -f "$AGENT_CONTAINER" 2>/dev/null || true
322+
print_success "Removed CI Analysis Agent container"
323+
fi
324+
325+
if podman container exists "$OLLAMA_CONTAINER" 2>/dev/null; then
326+
podman rm -f "$OLLAMA_CONTAINER" 2>/dev/null || true
327+
print_success "Removed Ollama container"
328+
fi
329+
330+
# Remove pods if requested
331+
if [ "$remove_pods" = true ]; then
332+
print_status "Removing pods..."
333+
if podman pod exists "ci-analysis-pod" 2>/dev/null; then
334+
podman pod rm -f "ci-analysis-pod" 2>/dev/null || true
335+
print_success "Removed ci-analysis-pod"
336+
fi
337+
338+
# Remove any other pods that might contain our containers
339+
for pod in $(podman pod ls --format "{{.Name}}" 2>/dev/null | grep -E "ci-analysis|ollama" || true); do
340+
if [ -n "$pod" ]; then
341+
podman pod rm -f "$pod" 2>/dev/null || true
342+
print_success "Removed pod: $pod"
343+
fi
344+
done
345+
fi
346+
347+
# Remove volumes if requested
348+
if [ "$remove_volumes" = true ]; then
349+
print_status "Removing volumes..."
350+
if podman volume exists "$OLLAMA_VOLUME" 2>/dev/null; then
351+
podman volume rm -f "$OLLAMA_VOLUME" 2>/dev/null || true
352+
print_success "Removed volume: $OLLAMA_VOLUME"
353+
fi
354+
355+
# Remove any other volumes that might be related
356+
for volume in $(podman volume ls --format "{{.Name}}" 2>/dev/null | grep -E "ollama|ci-analysis" || true); do
357+
if [ -n "$volume" ]; then
358+
podman volume rm -f "$volume" 2>/dev/null || true
359+
print_success "Removed volume: $volume"
360+
fi
361+
done
362+
fi
363+
364+
# Remove images if requested
365+
if [ "$remove_images" = true ]; then
366+
print_status "Removing images..."
367+
368+
# Remove CI Analysis Agent image
369+
if podman image exists "ci-analysis-agent:latest" 2>/dev/null; then
370+
podman rmi -f "ci-analysis-agent:latest" 2>/dev/null || true
371+
print_success "Removed image: ci-analysis-agent:latest"
372+
fi
373+
374+
# Remove Ollama image
375+
if podman image exists "ollama/ollama:latest" 2>/dev/null; then
376+
podman rmi -f "ollama/ollama:latest" 2>/dev/null || true
377+
print_success "Removed image: ollama/ollama:latest"
378+
fi
379+
380+
# Remove any other related images
381+
for image in $(podman images --format "{{.Repository}}:{{.Tag}}" 2>/dev/null | grep -E "ci-analysis|ollama" || true); do
382+
if [ -n "$image" ] && [ "$image" != "ollama/ollama:latest" ] && [ "$image" != "ci-analysis-agent:latest" ]; then
383+
podman rmi -f "$image" 2>/dev/null || true
384+
print_success "Removed image: $image"
385+
fi
386+
done
387+
fi
388+
389+
echo ""
390+
echo "================================================================="
391+
echo " 🧹 CLEANUP COMPLETED 🧹"
392+
echo "================================================================="
393+
echo ""
394+
echo "📊 Remaining Resources:"
395+
echo ""
396+
echo "Containers:"
397+
podman ps -a --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" | grep -E "$OLLAMA_CONTAINER|$AGENT_CONTAINER|ci-analysis" || echo " No related containers found"
398+
echo ""
399+
echo "Volumes:"
400+
podman volume ls --format "table {{.Name}}\t{{.Driver}}" | grep -E "ollama|ci-analysis" || echo " No related volumes found"
401+
echo ""
402+
echo "Images:"
403+
podman images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}" | grep -E "ollama|ci-analysis" || echo " No related images found"
404+
echo ""
405+
echo "🎯 Next Steps:"
406+
echo " • Fresh deployment: $0"
407+
echo " • Check system: podman system df"
408+
echo " • Prune unused: podman system prune -a"
409+
echo "================================================================="
410+
}
411+
259412
# Function to show status
260413
show_status() {
261414
local gpu_type="$1"
@@ -291,9 +444,11 @@ show_status() {
291444
echo "🎯 Quick Commands:"
292445
echo " • View logs: podman logs -f $AGENT_CONTAINER"
293446
echo " • Check Ollama models: podman exec $OLLAMA_CONTAINER ollama list"
294-
echo " • Stop containers: podman stop $OLLAMA_CONTAINER $AGENT_CONTAINER"
447+
echo " • Stop containers: $0 --stop"
295448
echo " • Start containers: podman start $OLLAMA_CONTAINER $AGENT_CONTAINER"
296-
echo " • Remove containers: podman rm -f $OLLAMA_CONTAINER $AGENT_CONTAINER"
449+
echo " • Clean up all: $0 --clean-all"
450+
echo " • Remove volumes: $0 --remove-volumes"
451+
echo " • Remove images: $0 --remove-images"
297452

298453
# GPU-specific commands
299454
if [ "$gpu_type" = "nvidia" ]; then
@@ -314,28 +469,44 @@ show_help() {
314469
echo "Usage: $0 [OPTIONS]"
315470
echo ""
316471
echo "Options:"
317-
echo " -h, --help Show this help message"
318-
echo " -c, --cleanup Clean up existing containers before starting"
319-
echo " -m, --model MODEL Set the Ollama model to use (default: $OLLAMA_MODEL)"
320-
echo " -p, --port PORT Set the agent port (default: $AGENT_PORT)"
321-
echo " --no-model Skip pulling the Ollama model"
322-
echo " --gpu TYPE GPU type to use: auto, nvidia, amd, none (default: $USE_GPU)"
323-
echo " --cpu-only Force CPU-only mode, disable GPU detection"
472+
echo " -h, --help Show this help message"
473+
echo " -s, --stop Stop running containers"
474+
echo " -c, --cleanup Clean up existing containers before starting"
475+
echo " -m, --model MODEL Set the Ollama model to use (default: $OLLAMA_MODEL)"
476+
echo " -p, --port PORT Set the agent port (default: $AGENT_PORT)"
477+
echo " --no-model Skip pulling the Ollama model"
478+
echo " --gpu TYPE GPU type to use: auto, nvidia, amd, none (default: $USE_GPU)"
479+
echo " --cpu-only Force CPU-only mode, disable GPU detection"
480+
echo ""
481+
echo "Cleanup Options:"
482+
echo " --clean-all Remove containers, volumes, images, and pods"
483+
echo " --remove-volumes Remove persistent volumes (loses model data)"
484+
echo " --remove-images Remove container images (forces re-download)"
485+
echo " --remove-pods Remove pods (for pod-based deployments)"
324486
echo ""
325487
echo "Examples:"
326-
echo " $0 # Start with default settings (auto GPU detection)"
327-
echo " $0 -c # Clean up and start fresh"
328-
echo " $0 -m llama3:8b # Use a different model"
329-
echo " $0 -p 3000 # Use port 3000 instead of 8000"
330-
echo " $0 --gpu nvidia # Force NVIDIA GPU usage"
331-
echo " $0 --cpu-only # Force CPU-only mode"
488+
echo " $0 # Start with default settings (auto GPU detection)"
489+
echo " $0 -s # Stop running containers"
490+
echo " $0 -c # Clean up and start fresh"
491+
echo " $0 --clean-all # Complete cleanup (containers, volumes, images, pods)"
492+
echo " $0 --remove-volumes # Remove only volumes"
493+
echo " $0 --remove-images # Remove only images"
494+
echo " $0 -m llama3:8b # Use a different model"
495+
echo " $0 -p 3000 # Use port 3000 instead of 8000"
496+
echo " $0 --gpu nvidia # Force NVIDIA GPU usage"
497+
echo " $0 --cpu-only # Force CPU-only mode"
332498
}
333499

334500
# Main function
335501
main() {
336502
local cleanup=false
337503
local skip_model=false
338504
local gpu_type="auto"
505+
local stop_containers_flag=false
506+
local clean_all_flag=false
507+
local remove_volumes_flag=false
508+
local remove_images_flag=false
509+
local remove_pods_flag=false
339510

340511
# Parse arguments
341512
while [[ $# -gt 0 ]]; do
@@ -344,10 +515,33 @@ main() {
344515
show_help
345516
exit 0
346517
;;
518+
-s|--stop)
519+
stop_containers_flag=true
520+
shift
521+
;;
347522
-c|--cleanup)
348523
cleanup=true
349524
shift
350525
;;
526+
--clean-all)
527+
clean_all_flag=true
528+
remove_volumes_flag=true
529+
remove_images_flag=true
530+
remove_pods_flag=true
531+
shift
532+
;;
533+
--remove-volumes)
534+
remove_volumes_flag=true
535+
shift
536+
;;
537+
--remove-images)
538+
remove_images_flag=true
539+
shift
540+
;;
541+
--remove-pods)
542+
remove_pods_flag=true
543+
shift
544+
;;
351545
-m|--model)
352546
OLLAMA_MODEL="$2"
353547
shift 2
@@ -376,6 +570,18 @@ main() {
376570
esac
377571
done
378572

573+
# Handle stop command
574+
if [ "$stop_containers_flag" = true ]; then
575+
stop_containers
576+
exit 0
577+
fi
578+
579+
# Handle cleanup commands
580+
if [ "$clean_all_flag" = true ] || [ "$remove_volumes_flag" = true ] || [ "$remove_images_flag" = true ] || [ "$remove_pods_flag" = true ]; then
581+
clean_all "$remove_volumes_flag" "$remove_images_flag" "$remove_pods_flag"
582+
exit 0
583+
fi
584+
379585
print_status "Starting CI Analysis Agent containerized deployment..."
380586

381587
# Check prerequisites

0 commit comments

Comments
 (0)