-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathcontainer_build.sh
executable file
·224 lines (196 loc) · 5.1 KB
/
container_build.sh
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
#!/bin/bash
set -euo pipefail
available() {
command -v "$1" >/dev/null
}
select_container_manager() {
if available podman; then
conman_bin="podman"
return 0
elif available docker; then
conman_bin="docker"
return 0
fi
conman_bin="podman"
}
add_build_platform() {
conman_build+=("build")
# This build saves space
if ! $rm_after_build; then
conman_build+=("--no-cache")
fi
conman_build+=("--platform" "$platform")
conman_build+=("-t" "$REGISTRY_PATH/$image_name")
conman_build+=("-f" "$image_name/Containerfile" ".")
}
rm_container_image() {
if $rm_after_build; then
"$conman_bin" rmi -f "$image_name" || true
fi
}
add_entrypoint() {
containerfile=$(mktemp)
cat > "${containerfile}" <<EOF
FROM $2
ENTRYPOINT [ "/usr/bin/$3.sh" ]
EOF
echo "$1 build --no-cache -t $2-$3 -f ${containerfile} ."
eval "$1 build --no-cache -t $2-$3 -f ${containerfile} ."
rm "${containerfile}"
}
add_entrypoints() {
add_entrypoint "$1" "$2" "whisper-server"
add_entrypoint "$1" "$2" "llama-server"
}
build() {
cd "container-images"
local image_name="${1//container-images\//}"
local conman_build=("${conman[@]}")
local conman_show_size=("${conman[@]}" "images" "--filter" "reference='$REGISTRY_PATH/$image_name'")
if [ "$3" == "-d" ]; then
add_build_platform
echo "${conman_build[@]}"
cd - > /dev/null
return 0
fi
case "${2:-}" in
build)
add_build_platform
echo "${conman_build[@]}"
"${conman_build[@]}"
"${conman_show_size[@]}"
add_entrypoints "${conman[@]}" "$REGISTRY_PATH"/"$image_name"
rm_container_image
;;
push)
"${conman[@]}" push "$REGISTRY_PATH/$image_name"
;;
multi-arch)
podman farm build -t "$REGISTRY_PATH"/"$image_name" -f "$image_name"/Containerfile .
add_entrypoints "podman farm" "$REGISTRY_PATH"/"$image_name"
;;
*)
echo "Invalid command: ${2:-}. Use 'build', 'push' or 'multi-arch'."
return 1
;;
esac
cd - > /dev/null
}
determine_platform() {
local platform
case $conman_bin in
podman)
platform="$(podman info --format '{{ .Version.OsArch }}' 2>/dev/null)"
;;
docker)
platform="$(docker info --format '{{ .ClientInfo.Os }}/{{ .ClientInfo.Arch }}' 2>/dev/null)"
;;
esac
if [ "$(uname -m)" = "aarch64" ] || { [ "$(uname -s)" = "Darwin" ] && [ "$(uname -m)" = "arm64" ]; }; then
platform="linux/arm64"
fi
echo "$platform"
}
parse_arguments() {
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
print_usage
exit 0
;;
-c)
ci="true"
shift
;;
-d)
option="$1"
shift
;;
-r)
rm_after_build="true"
shift
;;
build|push|multi-arch)
command="$1"
shift
;;
*)
target="$1"
shift
;;
esac
done
}
process_all_targets() {
local command="$1"
local option="$2"
# build ramalama container image first, as other images inherit from it
build "container-images/ramalama" "$command" "$option"
for i in container-images/*; do
# skip these directories
if [[ "$i" =~ ^container-images/(scripts|ramalama|pragmatic)$ ]]; then
continue
fi
# todo, trim and get building in CI again
if $ci && [[ "$i" =~ ^container-images/rocm$ ]]; then
continue
fi
# skip images that don't make sense for multi-arch builds
if [ "$command" = "multi-arch" ]; then
if [[ "$i" =~ ^container-images/(rocm|intel-gpu)$ ]]; then
continue
fi
fi
build "$i" "$command" "$option"
done
}
print_usage() {
echo "Usage: $(basename "$0") [-h|--help] [-d] <command> [target]"
echo
echo "Commands:"
echo " build Build the container images"
echo " push Push the container images"
echo " multi-arch Build and Push multi-arch images with podman farm"
echo
echo "Options:"
echo " -d Some option description"
echo " -r Remove container image after build"
echo
echo "Targets:"
echo " Specify the target container image to build or push"
echo " If no target is specified, all container images will be processed"
echo "Destination Registry:"
echo " Override the target registry path by setting the env var REGISTRY_PATH"
echo " default - quay.io/ramalama"
}
main() {
local conman_bin
select_container_manager
local conman=("$conman_bin")
local platform
platform=$(determine_platform)
local target=""
local command=""
local option=""
local rm_after_build="false"
local ci="false"
parse_arguments "$@"
if [ -z "$command" ]; then
echo "Error: command is required (build or push)"
print_usage
exit 1
fi
if [ "$command" = "multi-arch" ] && [ "$conman_bin" != "podman" ]; then
echo "Error: command 'multi-arch' only works with podman farm"
print_usage
exit 1
fi
target="${target:-all}"
if [ "$target" = "all" ]; then
process_all_targets "$command" "$option"
else
build "container-images/$target" "$command" "$option"
fi
}
REGISTRY_PATH=${REGISTRY_PATH:-quay.io/ramalama}
main "$@"