-
Notifications
You must be signed in to change notification settings - Fork 17
/
devops-toolkit-cli
executable file
·380 lines (344 loc) · 12.2 KB
/
devops-toolkit-cli
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
#!/bin/bash
# Color codes
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Logging function
log() {
local color=$1
local message=$2
echo -e "${color}${message}${NC}"
}
# Default configuration options
DOCKER_IMAGE="${DOCKER_IMAGE:-tungbq/devops-toolkit:latest}"
CONFIG_MOUNT_PATH="${CONFIG_MOUNT_PATH:-$HOME/.dtc}"
CONTAINER_SHELL="${CONTAINER_SHELL:-/bin/bash}"
RUN_MODE="${RUN_MODE:-run}"
NETWORK="${NETWORK:---net=host}"
CONTAINER_LIST_FILE="${CONFIG_MOUNT_PATH}/container_list.txt"
# Function to check if a container exists in the list
container_exists() {
local container_name="$1"
grep -q "^${container_name}$" "$CONTAINER_LIST_FILE" 2>/dev/null
}
# Function to add a container to the list
add_container() {
local container_name="$1"
if grep -q "^${container_name}$" "$CONTAINER_LIST_FILE" 2>/dev/null; then
log $YELLOW "Container '$container_name' already exists in the list. Skipping addition."
else
echo "$container_name" >> "$CONTAINER_LIST_FILE"
log $GREEN "Container '$container_name' added to the list."
fi
}
# Function to remove a container from the list
remove_container() {
local container_name="$1"
sed -i "/^${container_name}$/d" "$CONTAINER_LIST_FILE"
}
# Function to check if local image is up-to-date with DockerHub
is_image_up_to_date() {
local image="$1"
local repo="${image%:*}"
local tag="${image#*:}"
local local_digest=$(docker image inspect --format='{{index .RepoDigests 0}}' "$image" 2>/dev/null | cut -d'@' -f2)
local remote_digest=$(docker pull -q "$image" > /dev/null 2>&1 && docker image inspect --format='{{index .RepoDigests 0}}' "$image" | cut -d'@' -f2)
[ "$local_digest" = "$remote_digest" ]
}
pull_image() {
local image="$1"
if [[ "$image" != "sha256:"* ]]; then
if docker image inspect "$image" >/dev/null 2>&1; then
if is_image_up_to_date "$image"; then
log $YELLOW "Image $image is up-to-date. Skipping pull."
else
log $BLUE "Local image $image is outdated. Pulling latest version..."
docker pull "$image" || { log $RED "Failed to pull image $image"; exit 1; }
fi
else
log $BLUE "Pulling image $image..."
docker pull "$image" || { log $RED "Failed to pull image $image"; exit 1; }
fi
fi
}
shell_access() {
local container_name="$1"
if docker inspect --type=container "$container_name" > /dev/null 2>&1; then
log $BLUE "Accessing shell in container $container_name..."
docker exec -it "$container_name" $CONTAINER_SHELL
else
log $RED "Container $container_name does not exist."
fi
}
start_container() {
local image="$1"
local container_name="$2"
if ! docker inspect --type=container "$container_name" > /dev/null 2>&1; then
log $BLUE "Starting new container $container_name with image $image..."
docker run -d --name "$container_name" \
--volume "$PWD:$PWD" \
--volume "$CONFIG_MOUNT_PATH:/dtc" \
--volume "$HOME/.ssh:/root/.ssh" \
--workdir "$PWD" \
--init \
--entrypoint tail \
$NETWORK \
$DOCKER_ARGS \
"$image" -f /dev/null > /dev/null || { log $RED "Failed to start container"; exit 1; }
log $GREEN "Container $container_name started successfully"
else
log $YELLOW "Container $container_name already exists"
fi
}
update() {
local container_name="$1"
local version="$2"
local new_image
if [ -z "$version" ]; then
new_image="${DOCKER_IMAGE%:*}:latest"
log $BLUE "Updating $container_name to the latest version..."
else
new_image="${DOCKER_IMAGE%:*}:$version"
log $BLUE "Updating $container_name to version $version..."
fi
if [ "$new_image" == "$DOCKER_IMAGE" ]; then
if is_image_up_to_date "$new_image"; then
log $YELLOW "The specified image ($new_image) is already in use and up-to-date. No update needed."
return
else
log $BLUE "The specified image ($new_image) is in use but outdated. Updating..."
fi
fi
pull_image "$new_image"
cleanup "$container_name"
DOCKER_IMAGE="$new_image"
start_container "$DOCKER_IMAGE" "$container_name"
add_container "$container_name"
log $GREEN "Update completed successfully for $container_name. New image: $DOCKER_IMAGE"
}
version() {
local container_name="$1"
log $BLUE "DevOps Toolkit Version Information for $container_name:"
log $GREEN "Current Docker Image: $DOCKER_IMAGE"
if docker inspect --type=container "$container_name" > /dev/null 2>&1; then
local container_image=$(docker inspect --format='{{.Config.Image}}' "$container_name")
log $GREEN "Running Container Image: $container_image"
else
log $YELLOW "No running container found for $container_name."
fi
}
list_versions() {
log $BLUE "Available versions of DevOps Toolkit:"
local repo="${DOCKER_IMAGE%:*}"
curl -s "https://registry.hub.docker.com/v2/repositories/${repo}/tags?page_size=100" | \
jq -r '.results[].name' | sort -V
}
health_check() {
local container_name="$1"
if docker inspect --type=container "$container_name" > /dev/null 2>&1; then
local status=$(docker inspect --format='{{.State.Status}}' "$container_name")
local health=$(docker inspect --format='{{.State.Health.Status}}' "$container_name")
log $GREEN "Container Status for $container_name: $status"
log $GREEN "Container Health for $container_name: ${health:-N/A}"
else
log $RED "Container $container_name does not exist."
fi
}
show_logs() {
local container_name="$1"
if docker inspect --type=container "$container_name" > /dev/null 2>&1; then
docker logs "$container_name"
else
log $RED "Container $container_name does not exist."
fi
}
exec_in_container() {
local container_name="$1"
shift
local command="$@"
local RUN_CMD=$CONTAINER_SHELL
if [ -n "$command" ]; then
RUN_CMD+=" -c $command"
fi
log $BLUE "Executing command in container $container_name..."
docker exec -it "$container_name" $RUN_CMD || {
log $RED "Failed to execute command in container $container_name"; exit 1;
}
}
cleanup() {
local container_name="$1"
if [[ "$container_name" == "--all" ]]; then
cleanup_all
return
fi
read -p "Are you sure you want to remove the container '$container_name'? Type 'yes' to confirm: " confirmation
if [ "$confirmation" != "yes" ]; then
log $YELLOW "Cleanup aborted."
return
fi
log $BLUE "Cleaning up..."
if docker inspect --type=container "$container_name" > /dev/null 2>&1; then
docker stop "$container_name" > /dev/null
docker rm "$container_name" > /dev/null
remove_container "$container_name"
log $GREEN "Container $container_name has been removed."
else
log $YELLOW "Container $container_name does not exist."
fi
}
cleanup_all() {
read -p "Are you sure you want to remove ALL DevOps Toolkit containers? Type 'yes' to confirm: " confirmation
if [ "$confirmation" != "yes" ]; then
log $YELLOW "Cleanup all aborted."
return
fi
log $BLUE "Cleaning up all DevOps Toolkit containers..."
if [ -f "$CONTAINER_LIST_FILE" ]; then
while IFS= read -r container_name; do
if docker inspect --type=container "$container_name" > /dev/null 2>&1; then
docker stop "$container_name" > /dev/null
docker rm "$container_name" > /dev/null
log $GREEN "Container $container_name has been removed."
else
log $YELLOW "Container $container_name does not exist, but was in the list."
fi
done < "$CONTAINER_LIST_FILE"
# Clear the container list file
> "$CONTAINER_LIST_FILE"
log $GREEN "All DevOps Toolkit containers have been removed and the list has been cleared."
docker exec -it "$container_name" $CONTAINER_SHELL
log $YELLOW "No container list file found. Nothing to clean up."
log $RED "Container $container_name does not exist."
fi
}
usage() {
log $BLUE "Usage: $0 [command] <container_name> [options]"
echo "Commands:"
echo " init <container_name> [version]: Initialize a new container and configuration directory. If version is omitted, initializes with the latest image."
echo " run <container_name> [command]: Start a new container and run a command"
echo " exec <container_name> [command]: Execute a command in an existing container"
echo " cleanup --all: Remove all DevOps Toolkit containers"
echo " cleanup <container_name>: Remove the specified container"
echo " update <container_name> [version]: Update the container image. If version is omitted, updates to latest."
echo " version <container_name>: Display version information for the specified container"
echo " list-versions: List available versions of the DevOps toolkit"
echo " health <container_name>: Perform a health check on the specified running container"
echo " logs <container_name>: View the logs of the specified running container"
echo " shell <container_name>: Get a shell inside the specified running container"
echo " list: List all initialized containers"
}
init() {
local container_name="$1"
local version="$2"
local image="${DOCKER_IMAGE%:*}:${version:-latest}"
if [ -z "$container_name" ]; then
log $RED "No container name provided"
usage
exit 1
fi
# Check if the directory exists
if [ ! -d "$CONFIG_MOUNT_PATH" ]; then
# If the directory doesn't exist, create it
mkdir -p "$CONFIG_MOUNT_PATH"
echo "Config path $CONFIG_MOUNT_PATH created."
else
echo "Config path $CONFIG_MOUNT_PATH already exists."
fi
# Create container list file if it doesn't exist
touch "$CONTAINER_LIST_FILE"
if container_exists "$container_name"; then
log $YELLOW "Container $container_name already exists. Skipping initialization."
log $YELLOW "Run 'devops-toolkit-cli cleanup $container_name' then init again if you want to re-init!"
else
log $BLUE "Initializing container $container_name with image $image..."
pull_image "$image"
start_container "$image" "$container_name"
add_container "$container_name"
log $GREEN "Initialization complete for $container_name."
fi
}
list_containers() {
log $BLUE "Initialized containers:"
if [ -f "$CONTAINER_LIST_FILE" ]; then
cat "$CONTAINER_LIST_FILE"
else
log $YELLOW "No containers initialized yet."
fi
}
# Main script logic
case "$1" in
init)
shift
init "$@"
;;
run)
RUN_MODE="run"
shift
container_name="$1"
shift
if [ -z "$container_name" ]; then
log $RED "No container name provided"
usage
exit 1
fi
log $BLUE "Running in 'run' mode for container $container_name..."
pull_image "$DOCKER_IMAGE"
start_container "$DOCKER_IMAGE" "$container_name"
add_container "$container_name"
exec_in_container "$container_name" "$@"
;;
exec)
RUN_MODE="exec"
shift
container_name="$1"
shift
if [ -z "$container_name" ]; then
log $RED "No container name provided"
usage
exit 1
fi
log $BLUE "Running in 'exec' mode for container $container_name..."
exec_in_container "$container_name" "$@"
;;
cleanup)
shift
cleanup "$1"
;;
update)
shift
update "$@"
;;
version)
shift
version "$1"
;;
list-versions)
list_versions
;;
health)
shift
health_check "$1"
;;
logs)
shift
show_logs "$1"
;;
shell)
shift
shell_access "$1"
;;
list)
list_containers
;;
help)
usage
exit 0
;;
*)
log $RED "Invalid command: $1"
usage
exit 1
;;
esac