-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdep.sh
More file actions
executable file
·149 lines (124 loc) · 4.22 KB
/
Copy pathdep.sh
File metadata and controls
executable file
·149 lines (124 loc) · 4.22 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
#!/bin/bash
# CodeHub Deployment Terminal Script
#
# This script helps manage the CodeHub deployment container environment.
# It handles building and running the Docker container and provides various
# utility commands.
#
# Usage: ./dep.sh [command]
#
# =============================================================================
# Constants and Configuration
# =============================================================================
CONTAINER_NAME="codehub-deployment"
# Get the directory of the script
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Determine Docker Compose command based on availability
if command -v docker compose &> /dev/null; then
DOCKER_COMPOSE="docker compose"
elif command -v docker-compose &> /dev/null; then
DOCKER_COMPOSE="docker-compose"
else
echo "Error: Docker Compose is not installed"
exit 1
fi
# =============================================================================
# Helper Functions
# =============================================================================
# Display help information
show_help() {
echo ""
echo "Codehub Deployment Terminal — is a humble script created to help you deal with"
echo "the Docker container encapsulating CodeHub deployment tooling and setup."
echo ""
echo "Run it without any parameters to build (if necessary), start, and connect to the container."
echo "After you terminate the initial instance, the container will automatically shut down."
echo "If the container is already running, the script will connect you to it."
echo ""
echo "Commands for sophisticated troubleshooting and development:"
echo " dep.sh --rebuild - Force rebuild the container image"
echo " dep.sh --help - Display this help message"
echo ""
}
# Clean up resources when exiting
cleanup() {
echo "Cleaning up..."
$DOCKER_COMPOSE down
}
# Check if container is already running
is_container_running() {
if docker ps --format "{{.Names}}" | grep -q "$CONTAINER_NAME"; then
return 0
else
return 1
fi
}
# Check if container needs to be built
check_and_build() {
local force_rebuild=$1
# Skip build if image exists and no force rebuild requested
if docker images | grep -q "$CONTAINER_NAME" && [ "$force_rebuild" != "force" ]; then
echo "Container image already exists and no force rebuild requested. Skipping build."
return 0
fi
echo "Building container image..."
if ! docker build --no-cache .; then
echo "Error: Failed to build container image."
return 1
fi
return 0
}
# Connect to a running container
connect_to_container() {
cd "$SCRIPT_DIR"
local running_container=$(docker ps --format "{{.Names}}" | grep "$CONTAINER_NAME" | head -n 1)
if [ -z "$running_container" ]; then
echo "Error: No running $CONTAINER_NAME container found."
exit 1
fi
docker exec -it "$running_container" zsh -c "./utils/postrun.sh && zsh"
}
# =============================================================================
# Main Script Logic
# =============================================================================
# Make all .sh files in the utils directory executable
if [ -d "utils" ]; then
chmod +x utils/*.sh
fi
# Default command is 'default' if none provided
if [ -z "$1" ]; then
set -- "default"
fi
# Handle commands
case "$1" in
"default")
# Check if container is already running
if ! is_container_running; then
# Set up cleanup trap only if we're starting a new container
trap cleanup EXIT
# Build if necessary
check_and_build
# Start container
if ! $DOCKER_COMPOSE up -d; then
echo "Error: Failed to start the container."
exit 1
fi
echo "Container started successfully."
fi
# Clear screen and connect regardless of whether container was already running
clear
connect_to_container
;;
"--rebuild")
echo "Force rebuilding the container..."
check_and_build "force"
;;
"--help")
show_help
;;
*)
echo "Unknown command: $1"
show_help
exit 1
;;
esac