-
Notifications
You must be signed in to change notification settings - Fork 1
/
buildkitd
executable file
·146 lines (123 loc) · 4.24 KB
/
buildkitd
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
#! /usr/bin/env bash
#*******************************************************************************
# Copyright (c) 2018 Eclipse Foundation and others.
# This program and the accompanying materials are made available
# under the terms of the Eclipse Public License 2.0
# which is available at http://www.eclipse.org/legal/epl-v20.html,
# or the MIT License which is available at https://opensource.org/licenses/MIT.
# SPDX-License-Identifier: EPL-2.0 OR MIT
#*******************************************************************************
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
IFS=$'\n\t'
SCRIPT_FOLDER="$(dirname "$(readlink -f "${0}")")"
BUILDKIT_VERSION="${BUILDKIT_VERSION:-latest}"
BUILDKIT_CONTAINER_PORT="${BUILDKIT_CONTAINER_PORT:-1234}"
BUILDKIT_PORT="${BUILDKIT_PORT:-${BUILDKIT_CONTAINER_PORT}}"
BUILDKIT_VOLUMENAME="${BUILDKIT_VOLUMENAME:-buildkitd}"
export BUILDKIT_HOST="${BUILDKIT_HOST:-"tcp://0.0.0.0:${BUILDKIT_PORT}"}"
# Need docker
if ! command -v docker > /dev/null; then
>&2 echo "ERROR: this program requires 'docker'"
exit 1
fi
lock() {
local lockfile="${1}"
while ! mkdir "${lockfile}" &> /dev/null; do
sleep 1
done
trap 'unlock '"${1}" EXIT SIGINT SIGTERM
}
unlock() {
local lockfile="${1}"
rm -rf "${lockfile}"
}
lock "/tmp/buildctl_install.lock"
if [[ ! -x "${SCRIPT_FOLDER}/bin/buildctl" ]]; then
if [[ $(uname) = "Darwin" ]]; then
platform="darwin-amd64"
else
platform="linux-amd64"
fi
>&2 echo "INFO: Downloading buildkit (${BUILDKIT_VERSION}) binaries for platform ${platform}"
releaseinfo=$(mktemp)
if [[ "${BUILDKIT_VERSION}" = "latest" ]]; then
latest_tag_name="$(curl -sSL "https://api.github.com/repos/moby/buildkit/releases" | jq -r '.[] | .tag_name' | grep -E "v[0-9]+.*" | sort -V | tail -n 1)"
curl -sSL "https://api.github.com/repos/moby/buildkit/releases/tags/${latest_tag_name}" > "${releaseinfo}"
else
curl -sSL "https://api.github.com/repos/moby/buildkit/releases/tags/${BUILDKIT_VERSION}" > "${releaseinfo}"
fi
buildkit_url="$(jq -r '.assets[] | select(.name | test("'${platform}'")) | .browser_download_url' "${releaseinfo}" | grep '.tar.gz')"
>&2 echo "INFO: Downloading ${buildkit_url}"
curl -sSL "${buildkit_url}" | tar -C "${SCRIPT_FOLDER}" -zx
rm "${releaseinfo}"
fi
unlock "/tmp/buildctl_install.lock"
host() {
echo "${BUILDKIT_HOST}"
}
status() {
if "${SCRIPT_FOLDER}/bin/buildctl" du &> /dev/null; then
>&2 echo "INFO: buildkitd is running"
return 0
else
>&2 echo "INFO: buildkitd is not running"
return 1
fi
}
check_volume() {
lock "/tmp/buildkitd_check_volume.lock"
if ! docker volume inspect "${BUILDKIT_VOLUMENAME}" &> /dev/null; then
docker volume create "${BUILDKIT_VOLUMENAME}"
fi
unlock "/tmp/buildkitd_check_volume.lock"
}
start() {
lock "/tmp/buildkitd_start.lock"
if ! status 2> /dev/null; then
check_volume
docker pull "moby/buildkit:${BUILDKIT_VERSION}"
docker run -d --privileged --rm -p "${BUILDKIT_PORT}:${BUILDKIT_PORT}" --mount source="${BUILDKIT_VOLUMENAME}",target=/var/lib/buildkit "moby/buildkit:${BUILDKIT_VERSION}" --addr "${BUILDKIT_HOST}" "$@"
echo -n "Buildkitd is starting @ ${BUILDKIT_HOST}"
timeout 60s /bin/bash -c "while ! \"${SCRIPT_FOLDER}/buildkitd\" status 2> /dev/null; do sleep 1; echo -n '.'; done" || :
echo
if ! status 2> /dev/null; then
echo "ERROR: impossible to start buildkitd @ ${BUILDKIT_HOST}!"
unlock "/tmp/buildkitd_start.lock"
exit 1
fi
else
>&2 echo "WARNING: buildkitd is already running @ ${BUILDKIT_HOST}!"
fi
unlock "/tmp/buildkitd_start.lock"
}
stop() {
lock "/tmp/buildkitd_stop.lock"
if status 2> /dev/null; then
>&2 echo "INFO: stopping buildkitd @ ${BUILDKIT_HOST}"
docker stop "$(docker ps -qf "publish=${BUILDKIT_PORT}")" || :
if [[ "${BUILDKIT_REMOVE_VOLUME_AFTER_USE:-}" == "true" ]]; then
>&2 echo "INFO: removing buildkitd volume ${BUILDKIT_VOLUMENAME}"
docker volume rm -f "${BUILDKIT_VOLUMENAME}"
fi
else
>&2 echo "INFO: buildkitd is not running"
fi
unlock "/tmp/buildkitd_stop.lock"
}
main() {
echo "You must enter a valid command"
exit 1
}
run() {
local args=$*
local f="${1:-}"
if [[ "$f" == "" ]]; then
main
else
$args
fi
}
run "$@"