-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-docker.sh
More file actions
executable file
Β·283 lines (244 loc) Β· 7.58 KB
/
Copy pathrun-docker.sh
File metadata and controls
executable file
Β·283 lines (244 loc) Β· 7.58 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
#!/bin/bash
# RT Container Runtime - Docker Runner
# Script untuk menjalankan RT Container Runtime di dalam Docker container
set -euo pipefail
# Colors for output
readonly COLOR_RESET='\033[0m'
readonly COLOR_RED='\033[0;31m'
readonly COLOR_GREEN='\033[0;32m'
readonly COLOR_YELLOW='\033[1;33m'
readonly COLOR_BLUE='\033[0;34m'
readonly COLOR_CYAN='\033[0;36m'
# Script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Function to show usage
show_usage() {
echo -e "${COLOR_BLUE}ποΈ RT Container Runtime - Docker Runner${COLOR_RESET}"
echo -e "${COLOR_BLUE}===========================================${COLOR_RESET}"
echo
echo -e "${COLOR_GREEN}USAGE:${COLOR_RESET}"
echo -e " $0 [MODE] [ACTION]"
echo
echo -e "${COLOR_YELLOW}MODES:${COLOR_RESET}"
echo -e " ${COLOR_CYAN}dev${COLOR_RESET} : Development mode (privileged, full features)"
echo -e " ${COLOR_CYAN}rootless${COLOR_RESET} : Rootless mode (non-privileged, limited features)"
echo
echo -e "${COLOR_YELLOW}ACTIONS:${COLOR_RESET}"
echo -e " ${COLOR_CYAN}start${COLOR_RESET} : Start and enter the container"
echo -e " ${COLOR_CYAN}build${COLOR_RESET} : Build the Docker image"
echo -e " ${COLOR_CYAN}stop${COLOR_RESET} : Stop the container"
echo -e " ${COLOR_CYAN}clean${COLOR_RESET} : Clean up containers and images"
echo -e " ${COLOR_CYAN}logs${COLOR_RESET} : Show container logs"
echo -e " ${COLOR_CYAN}exec${COLOR_RESET} : Execute command in running container"
echo
echo -e "${COLOR_YELLOW}EXAMPLES:${COLOR_RESET}"
echo -e " ${COLOR_GREEN}# Start development environment${COLOR_RESET}"
echo -e " $0 dev start"
echo
echo -e " ${COLOR_GREEN}# Start rootless environment${COLOR_RESET}"
echo -e " $0 rootless start"
echo
echo -e " ${COLOR_GREEN}# Build Docker image${COLOR_RESET}"
echo -e " $0 dev build"
echo
echo -e " ${COLOR_GREEN}# Execute RT command in container${COLOR_RESET}"
echo -e " $0 dev exec ./rt.sh create webapp"
echo
echo -e "${COLOR_YELLOW}QUICK START:${COLOR_RESET}"
echo -e " 1. $0 dev build # Build the image"
echo -e " 2. $0 dev start # Start container"
echo -e " 3. Inside container: ./rt.sh create webapp"
echo
}
# Function to check if Docker is available
check_docker() {
if ! command -v docker &> /dev/null; then
echo -e "${COLOR_RED}β Error: Docker not found${COLOR_RESET}"
echo -e "${COLOR_YELLOW}π‘ Please install Docker first${COLOR_RESET}"
exit 1
fi
if ! docker info &> /dev/null; then
echo -e "${COLOR_RED}β Error: Docker daemon not running${COLOR_RESET}"
echo -e "${COLOR_YELLOW}π‘ Please start Docker daemon${COLOR_RESET}"
exit 1
fi
}
# Function to check if docker-compose is available
check_docker_compose() {
if command -v docker-compose &> /dev/null; then
COMPOSE_CMD="docker-compose"
elif docker compose version &> /dev/null; then
COMPOSE_CMD="docker compose"
else
echo -e "${COLOR_RED}β Error: docker-compose not found${COLOR_RESET}"
echo -e "${COLOR_YELLOW}π‘ Please install docker-compose${COLOR_RESET}"
exit 1
fi
}
# Function to build Docker image
build_image() {
local mode=$1
echo -e "${COLOR_BLUE}π¨ Building Docker image for $mode mode...${COLOR_RESET}"
cd "$SCRIPT_DIR"
case "$mode" in
dev)
$COMPOSE_CMD build rt-dev
;;
rootless)
$COMPOSE_CMD build rt-rootless
;;
*)
echo -e "${COLOR_RED}β Invalid mode: $mode${COLOR_RESET}"
exit 1
;;
esac
echo -e "${COLOR_GREEN}β
Docker image built successfully${COLOR_RESET}"
}
# Function to start container
start_container() {
local mode=$1
echo -e "${COLOR_BLUE}π Starting RT Container Runtime in $mode mode...${COLOR_RESET}"
cd "$SCRIPT_DIR"
case "$mode" in
dev)
$COMPOSE_CMD up -d rt-dev
echo -e "${COLOR_GREEN}β
Development container started${COLOR_RESET}"
echo -e "${COLOR_CYAN}π‘ Entering container...${COLOR_RESET}"
$COMPOSE_CMD exec rt-dev /bin/bash
;;
rootless)
$COMPOSE_CMD up -d rt-rootless
echo -e "${COLOR_GREEN}β
Rootless container started${COLOR_RESET}"
echo -e "${COLOR_CYAN}π‘ Entering container...${COLOR_RESET}"
$COMPOSE_CMD exec rt-rootless /bin/bash
;;
*)
echo -e "${COLOR_RED}β Invalid mode: $mode${COLOR_RESET}"
exit 1
;;
esac
}
# Function to stop container
stop_container() {
local mode=$1
echo -e "${COLOR_YELLOW}π Stopping $mode container...${COLOR_RESET}"
cd "$SCRIPT_DIR"
case "$mode" in
dev)
$COMPOSE_CMD stop rt-dev
;;
rootless)
$COMPOSE_CMD stop rt-rootless
;;
all)
$COMPOSE_CMD stop
;;
*)
echo -e "${COLOR_RED}β Invalid mode: $mode${COLOR_RESET}"
exit 1
;;
esac
echo -e "${COLOR_GREEN}β
Container stopped${COLOR_RESET}"
}
# Function to clean up
clean_up() {
echo -e "${COLOR_YELLOW}π§Ή Cleaning up Docker resources...${COLOR_RESET}"
cd "$SCRIPT_DIR"
# Stop and remove containers
$COMPOSE_CMD down -v
# Remove images
docker rmi $(docker images -q -f "reference=pak-rt*") 2>/dev/null || true
echo -e "${COLOR_GREEN}β
Cleanup completed${COLOR_RESET}"
}
# Function to show logs
show_logs() {
local mode=$1
cd "$SCRIPT_DIR"
case "$mode" in
dev)
$COMPOSE_CMD logs -f rt-dev
;;
rootless)
$COMPOSE_CMD logs -f rt-rootless
;;
*)
echo -e "${COLOR_RED}β Invalid mode: $mode${COLOR_RESET}"
exit 1
;;
esac
}
# Function to execute command in container
exec_command() {
local mode=$1
shift
local command="$*"
cd "$SCRIPT_DIR"
case "$mode" in
dev)
$COMPOSE_CMD exec rt-dev bash -c "$command"
;;
rootless)
$COMPOSE_CMD exec rt-rootless bash -c "$command"
;;
*)
echo -e "${COLOR_RED}β Invalid mode: $mode${COLOR_RESET}"
exit 1
;;
esac
}
# Main function
main() {
# Check prerequisites
check_docker
check_docker_compose
# If no arguments, show usage
if [[ $# -eq 0 ]]; then
show_usage
exit 0
fi
local mode=$1
local action=${2:-"start"}
# Validate mode
case "$mode" in
dev|rootless)
;;
help|--help|-h)
show_usage
exit 0
;;
*)
echo -e "${COLOR_RED}β Invalid mode: $mode${COLOR_RESET}"
show_usage
exit 1
;;
esac
# Execute action
case "$action" in
start)
start_container "$mode"
;;
build)
build_image "$mode"
;;
stop)
stop_container "$mode"
;;
clean)
clean_up
;;
logs)
show_logs "$mode"
;;
exec)
shift 2
exec_command "$mode" "$@"
;;
*)
echo -e "${COLOR_RED}β Invalid action: $action${COLOR_RESET}"
show_usage
exit 1
;;
esac
}
# Run main function
main "$@"