-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathutils.sh
More file actions
53 lines (43 loc) · 1.12 KB
/
utils.sh
File metadata and controls
53 lines (43 loc) · 1.12 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
#!/bin/bash
# Common functions for testing production environments with Podman or Docker
GOOD='\033[0;32m\033[1m'
BAD='\033[0;31m\033[1m'
ERROR='\033[0;31m'
WARN='\033[0;33m'
NOTE='\033[0;90m'
NC='\033[0m'
command_exists() {
command -v "$1" >/dev/null 2>&1
}
get_container_tool() {
if command_exists podman; then
echo "podman"
elif command_exists docker; then
echo "docker"
else
echo -e "${ERROR}No Podman or Docker found${NC}" >&2
exit 1
fi
}
run_container() {
local container_tool=$1
local port=$2
local envs=$3
local image_id=$4
$container_tool run --rm -p "$port:$port" -e PORT=$port $envs -it "$image_id"
}
build_and_run() {
local port=$1
local envs=$2
local tool=$(get_container_tool)
echo "Building image with $tool"
BUILD_OUTPUT=$($tool build . 2>&1) || {
echo -e "${ERROR}Build failed:${NC}\n$BUILD_OUTPUT"
exit 1
}
IMAGE_ID=$(echo "$BUILD_OUTPUT" | tail -1)
SIZE=$($tool image inspect "$IMAGE_ID" --format='{{.Size}}' | \
awk '{printf "%d MB", $1/1024/1024}')
echo -e "${WARN}Image size: ${SIZE}${NC}"
run_container "$tool" "$port" "$envs" "$IMAGE_ID"
}