-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathdc.sh
More file actions
executable file
·167 lines (152 loc) · 6.05 KB
/
Copy pathdc.sh
File metadata and controls
executable file
·167 lines (152 loc) · 6.05 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
#!/bin/bash
function print_help() {
echo "Description: this cli script is a wrapper around Docker Compose commands. It runs Docker Compose commands with the specific Akash Network Console Docker Compose files based on the arguments provided."
echo ""
echo "Usage: $COMMAND_PREFIX <command> [--no-db] [additional docker compose options]"
echo ""
echo "Commands:"
echo " build Build the Docker images using the '${DC_FILES_DIR}docker-compose.build.yml' file."
echo " down Bring down the services and remove containers."
echo " up Bring up development services with Docker Compose."
echo ""
echo "Options:"
echo " --no-db Exclude the DB service from being started."
echo " -h, --help Show this help message."
echo ""
echo "Examples:"
echo " $COMMAND_PREFIX build <docker compose args> # Build Docker images"
echo " $COMMAND_PREFIX up <docker compose args> # Start dev services with DB"
echo " $COMMAND_PREFIX up --no-db <docker compose args> # Start dev services without DB"
echo " $COMMAND_PREFIX down <docker compose args> # Stop and remove containers"
echo " USE_DOCKER_BUILDKIT=1 $COMMAND_PREFIX build <docker compose args> # Builds Docker images using buildkit"
echo " USE_DOCKER_BUILDKIT=1 $COMMAND_PREFIX up:dev <docker compose args> # Builds Docker images using buildkit and start them"
}
remove_array_item_by_value() {
local -n arr="$1"
local value_to_remove="$2"
local new_array=()
for item in "${arr[@]}"; do
if [[ "$item" != "$value_to_remove" ]]; then
new_array+=("$item")
fi
done
arr=("${new_array[@]}")
}
ensure_builder() {
docker buildx version >/dev/null 2>&1 || { echo "Docker Buildx not available"; exit 1; }
if ! docker buildx inspect ci-builder >/dev/null 2>&1; then
docker buildx create --name ci-builder --use >/dev/null
else
docker buildx use ci-builder >/dev/null
fi
}
docker_buildx_bake() {
ensure_builder
# Build only requested services if any; otherwise build all targets from compose
local targets=()
if [[ ${#REQUESTED_SERVICES[@]} -gt 0 ]]; then
buildable_targets=$(docker buildx bake \
$(printf -- " --file %q" "${COMPOSE_FILES[@]}") \
--print | jq -r '.target | keys[]' | tr '\n' ' ')
for target in "${REQUESTED_SERVICES[@]}"; do
if [[ " $buildable_targets " == *" $target "* ]]; then
targets+=("$target")
fi
done
fi
local platform_args=()
if [[ -n "$TARGET_PLATFORM" ]]; then
platform_args=(--set "*.platform=${TARGET_PLATFORM}")
fi
echo "docker buildx bake $(printf -- " --file %q" "${COMPOSE_FILES[@]}") --set *.platform=${TARGET_PLATFORM} --set *.cache-from="type=gha,scope=${BUILDKIT_CACHE_SCOPE}" --set *.cache-from="type=gha,scope=${BUILDKIT_CACHE_FALLBACK_SCOPE}" --set *.cache-to="type=gha,mode=max,scope=${BUILDKIT_CACHE_FALLBACK_SCOPE}" --load ${targets[@]}"
docker buildx bake \
$(printf -- " --file %q" "${COMPOSE_FILES[@]}") \
"${platform_args[@]}" \
--set *.cache-from="type=gha,scope=${BUILDKIT_CACHE_SCOPE}" \
--set *.cache-from="type=gha,scope=${BUILDKIT_CACHE_FALLBACK_SCOPE}" \
--set *.cache-to="type=gha,mode=max,scope=${BUILDKIT_CACHE_FALLBACK_SCOPE}" \
--load \
"${targets[@]}"
}
bake_cached() {
(cd $DC_FILES_DIR && docker_buildx_bake)
}
docker_compose() {
echo "Running: docker compose $(printf -- " -f %q" "${COMPOSE_FILES[@]}") $@ $PASSTHRU_ARGS"
docker compose $(printf -- " -f %q" "${COMPOSE_FILES[@]}") $@ $PASSTHRU_ARGS
}
for arg in "$@"; do
case $arg in
-h|--help)
print_help
exit 0
;;
esac
done
if [ "$npm_command" = "run-script" ]; then
COMMAND_PREFIX="npm run dc --"
else
COMMAND_PREFIX="./dc.sh"
fi
COMMAND=$1
shift
USE_BUILDKIT="${USE_DOCKER_BUILDKIT:-0}"
TARGET_PLATFORM="${TARGET_PLATFORM:-}"
# Ensures the plain `docker compose build` path also targets amd64 (not the host arch, e.g. arm64)
export DOCKER_DEFAULT_PLATFORM="${TARGET_PLATFORM}"
BUILDKIT_CACHE_SCOPE="${DOCKER_BUILDKIT_CACHE_SCOPE:-main}"
BUILDKIT_CACHE_SCOPE="${BUILDKIT_CACHE_SCOPE// /-}"
# uses cache fallback scope in case package-lock.json changed in a branch PR
BUILDKIT_CACHE_FALLBACK_SCOPE="${DOCKER_BUILDKIT_CACHE_FALLBACK_SCOPE:-$BUILDKIT_CACHE_SCOPE}"
BUILDKIT_CACHE_FALLBACK_SCOPE="${BUILDKIT_CACHE_FALLBACK_SCOPE// /-}"
REQUESTED_SERVICES=()
DC_FILES_DIR="$(dirname "$(dirname "$(readlink -f "$0")")")/"
COMPOSE_FILES=(
"${DC_FILES_DIR}docker-compose.build.yml"
"${DC_FILES_DIR}docker-compose.runtime.yml"
"${DC_FILES_DIR}docker-compose.prod-with-db.yml"
"${DC_FILES_DIR}docker-compose.dev.yml"
)
KNOWN_SERVICES=$(docker compose $(printf -- " -f %q" "${COMPOSE_FILES[@]}") config --services | tr '\n' ' ')
PASSTHRU_ARGS=""
for arg in "$@"; do
case $arg in
--no-db)
remove_array_item_by_value COMPOSE_FILES "${DC_FILES_DIR}docker-compose.prod-with-db.yml"
echo "Excluding DB services from Docker Compose."
;;
*)
if [[ " $KNOWN_SERVICES " == *" $arg "* ]]; then
REQUESTED_SERVICES+=("$arg")
echo "requested services: ${REQUESTED_SERVICES[@]}"
else
PASSTHRU_ARGS="$PASSTHRU_ARGS $arg"
fi
;;
esac
done
case "$COMMAND" in
build)
COMPOSE_FILES=("${DC_FILES_DIR}docker-compose.build.yml")
if [[ "$USE_BUILDKIT" -eq 1 ]]; then
bake_cached || { echo "Docker build failed"; exit 1; }
else
docker_compose build ${REQUESTED_SERVICES[@]} || { echo "Docker build failed"; exit 1; }
fi
;;
down)
echo "Running: docker compose -p console down $*"
docker compose -p console down $PASSTHRU_ARGS || { echo "Docker down failed"; exit 1; }
;;
up:dev | up)
if [[ "$USE_BUILDKIT" -eq 1 ]]; then
bake_cached || { echo "Docker buildkit failed"; exit 1; }
fi
docker_compose -p console up --renew-anon-volumes ${REQUESTED_SERVICES[@]} || { echo "Docker $COMMAND failed"; exit 1; }
;;
*)
echo "Unknown command: $COMMAND"
echo "Use '$COMMAND_PREFIX --help' for more information."
exit 1
;;
esac