Skip to content

Commit 51b00fb

Browse files
authored
feat(context): Support unix context and daemonless (tensorchord#1062)
* feat(context): Support unix context Signed-off-by: Ce Gao <[email protected]> * chore: Auto-build image Signed-off-by: Ce Gao <[email protected]> * chore: Update release workflow Signed-off-by: Ce Gao <[email protected]> * fix: Update name Signed-off-by: Ce Gao <[email protected]> * chore: Add license for bash Signed-off-by: Ce Gao <[email protected]> Signed-off-by: Ce Gao <[email protected]>
1 parent d41b674 commit 51b00fb

File tree

12 files changed

+204
-8
lines changed

12 files changed

+204
-8
lines changed

.github/workflows/release.yml

+26-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,32 @@ jobs:
116116
- name: Docker Buildx
117117
run: |
118118
./base-images/build.sh
119-
DOCKER_IMAGE_TAG=latest ./base-images/build.sh
119+
envd_image_push:
120+
name: Build & push envd images
121+
# only trigger on main repo when tag starts with v
122+
if: github.repository == 'tensorchord/envd' && startsWith(github.ref, 'refs/tags/v')
123+
runs-on: ubuntu-latest
124+
needs: goreleaser
125+
steps:
126+
- uses: actions/checkout@v3
127+
- name: Set up Docker Buildx
128+
uses: docker/setup-buildx-action@v2
129+
- name: Cache Docker layers
130+
uses: actions/cache@v3
131+
id: cache
132+
with:
133+
path: /tmp/.buildx-cache
134+
key: ${{ runner.os }}-buildx-${{ github.sha }}
135+
restore-keys: |
136+
${{ runner.os }}-buildx-
137+
- name: Docker Login
138+
uses: docker/login-action@v2
139+
with:
140+
username: ${{ secrets.DOCKERIO_USERNAME }}
141+
password: ${{ secrets.DOCKERIO_TOKEN }}
142+
- name: Docker Buildx
143+
run: |
144+
./base-images/envd/build.sh
120145
cache_publish:
121146
name: Build & Push the remote cache
122147
# only trigger on main repo when tag starts with v

.goreleaser.yaml

+23-2
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,27 @@ changelog:
6868
- '^test:'
6969
- '^chore:'
7070
dockers:
71+
- image_templates:
72+
- "tensorchord/envd-from-scratch:v{{ .Version }}-amd64"
73+
use: buildx
74+
dockerfile: base-images/envd/envd.Dockerfile
75+
ids:
76+
- envd
77+
build_flag_templates:
78+
- "--platform=linux/amd64"
79+
- image_templates:
80+
- "tensorchord/envd-from-scratch:v{{ .Version }}-arm64v8"
81+
use: buildx
82+
goarch: arm64
83+
ids:
84+
- envd
85+
dockerfile: base-images/envd/envd.Dockerfile
86+
build_flag_templates:
87+
- "--platform=linux/arm64/v8"
7188
- image_templates:
7289
- "tensorchord/envd-sshd-from-scratch:v{{ .Version }}-amd64"
7390
use: buildx
74-
dockerfile: .goreleaser/envd-sshd.Dockerfile
91+
dockerfile: base-images/envd-sshd/envd-sshd.Dockerfile
7592
ids:
7693
- envd-sshd
7794
build_flag_templates:
@@ -82,14 +99,18 @@ dockers:
8299
goarch: arm64
83100
ids:
84101
- envd-sshd
85-
dockerfile: .goreleaser/envd-sshd.Dockerfile
102+
dockerfile: base-images/envd-sshd/envd-sshd.Dockerfile
86103
build_flag_templates:
87104
- "--platform=linux/arm64/v8"
88105
docker_manifests:
89106
- name_template: tensorchord/envd-sshd-from-scratch:v{{ .Version }}
90107
image_templates:
91108
- tensorchord/envd-sshd-from-scratch:v{{ .Version }}-amd64
92109
- tensorchord/envd-sshd-from-scratch:v{{ .Version }}-arm64v8
110+
- name_template: tensorchord/envd-from-scratch:v{{ .Version }}
111+
image_templates:
112+
- tensorchord/envd-from-scratch:v{{ .Version }}-amd64
113+
- tensorchord/envd-from-scratch:v{{ .Version }}-arm64v8
93114
# See https://github.com/tensorchord/envd/issues/908
94115
# brews:
95116
# - name: envd

base-images/build.sh

+14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
#!/usr/bin/env bash
2+
# Copyright 2022 The envd Authors
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
216

317
set -euo pipefail
418

File renamed without changes.

base-images/envd/buid.sh

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2022 The envd Authors
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
17+
set -euo pipefail
18+
19+
ROOT_DIR=`dirname $0`
20+
21+
GIT_TAG_VERSION=$(git describe --tags --abbrev=0 | sed -r 's/[v]+//g') # remove v from version
22+
ENVD_VERSION="${ENVD_VERSION:-$GIT_TAG_VERSION}"
23+
DOCKER_HUB_ORG="${DOCKER_HUB_ORG:-tensorchord}"
24+
TAG_SUFFIX="${TAG_SUFFIX:-}"
25+
26+
cd ${ROOT_DIR}
27+
28+
docker buildx build \
29+
--build-arg ENVD_VERSION=${ENVD_VERSION} \
30+
-t ${DOCKER_HUB_ORG}/envd:${ENVD_VERSION} \
31+
--pull --push --platform linux/x86_64,linux/arm64 \
32+
-f envd-daemonless.Dockerfile ../../
33+
34+
cd - > /dev/null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM tensorchord/envd:latest as envd
2+
3+
FROM moby/buildkit:v0.10.5-rootless
4+
COPY --from=envd /usr/bin/envd /usr/bin/envd
5+
COPY scripts/envd-daemonless.sh /envd-daemonless.sh
6+
7+
CMD [ "/envd-daemonless.sh" ]

base-images/envd/envd.Dockerfile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FROM scratch
2+
COPY envd /usr/bin/envd

base-images/remote-cache/build-and-push-remote-cache.sh

+14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
#!/usr/bin/env bash
2+
# Copyright 2022 The envd Authors
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
216

317
set -euo pipefail
418

pkg/app/context_create.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var CommandContextCreate = &cli.Command{
3535
},
3636
&cli.StringFlag{
3737
Name: "builder",
38-
Usage: "Builder to use (docker-container, kube-pod, tcp)",
38+
Usage: "Builder to use (docker-container, kube-pod, tcp, unix)",
3939
Value: string(types.BuilderTypeDocker),
4040
},
4141
&cli.StringFlag{

pkg/home/context.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ func (m *generalManager) ContextCreate(ctx types.Context, use bool) error {
9595
}
9696
}
9797
switch ctx.Builder {
98-
case types.BuilderTypeDocker, types.BuilderTypeKubernetes, types.BuilderTypeTCP:
98+
case types.BuilderTypeDocker, types.BuilderTypeKubernetes,
99+
types.BuilderTypeUNIXDomainSocket, types.BuilderTypeTCP:
99100
break
100101
default:
101102
return errors.New("unknown builder type")

pkg/types/envd.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,10 @@ type Context struct {
127127
type BuilderType string
128128

129129
const (
130-
BuilderTypeDocker BuilderType = "docker-container"
131-
BuilderTypeKubernetes BuilderType = "kube-pod"
132-
BuilderTypeTCP BuilderType = "tcp"
130+
BuilderTypeDocker BuilderType = "docker-container"
131+
BuilderTypeKubernetes BuilderType = "kube-pod"
132+
BuilderTypeTCP BuilderType = "tcp"
133+
BuilderTypeUNIXDomainSocket BuilderType = "unix"
133134
)
134135

135136
type RunnerType string

scripts/envd-daemonless.sh

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/bin/sh
2+
3+
# Copyright 2022 The envd Authors
4+
# Copyright 2022 The buildkit Authors
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
# envd-daemonless.sh spawns ephemeral buildkitd for executing envd.
19+
#
20+
# Usage: envd-daemonless.sh build ...
21+
#
22+
# Flags for buildkitd can be specified as $BUILDKITD_FLAGS .
23+
#
24+
# The script is compatible with BusyBox shell.
25+
set -eu
26+
27+
: ${ENVDCTL=envd}
28+
: ${BUILDCTL=buildctl}
29+
: ${BUILDCTL_CONNECT_RETRIES_MAX=10}
30+
: ${BUILDKITD=buildkitd}
31+
: ${BUILDKITD_FLAGS=}
32+
: ${ROOTLESSKIT=rootlesskit}
33+
34+
# $tmp holds the following files:
35+
# * pid
36+
# * addr
37+
# * log
38+
tmp=$(mktemp -d /tmp/envd-daemonless.XXXXXX)
39+
trap "kill \$(cat $tmp/pid) || true; wait \$(cat $tmp/pid) || true; rm -rf $tmp" EXIT
40+
41+
startBuildkitd() {
42+
addr=
43+
helper=
44+
if [ $(id -u) = 0 ]; then
45+
addr=/run/buildkit/buildkitd.sock
46+
else
47+
addr=$XDG_RUNTIME_DIR/buildkit/buildkitd.sock
48+
helper=$ROOTLESSKIT
49+
fi
50+
$helper $BUILDKITD $BUILDKITD_FLAGS --addr=unix://$addr >$tmp/log 2>&1 &
51+
pid=$!
52+
echo $pid >$tmp/pid
53+
echo $addr >$tmp/addr
54+
}
55+
56+
# buildkitd supports NOTIFY_SOCKET but as far as we know, there is no easy way
57+
# to wait for NOTIFY_SOCKET activation using busybox-builtin commands...
58+
waitForBuildkitd() {
59+
addr=unix://$(cat $tmp/addr)
60+
try=0
61+
max=$BUILDCTL_CONNECT_RETRIES_MAX
62+
until $BUILDCTL --addr=$addr debug workers >/dev/null 2>&1; do
63+
if [ $try -gt $max ]; then
64+
echo >&2 "could not connect to $addr after $max trials"
65+
echo >&2 "========== log =========="
66+
cat >&2 $tmp/log
67+
exit 1
68+
fi
69+
sleep $(awk "BEGIN{print (100 + $try * 20) * 0.001}")
70+
try=$(expr $try + 1)
71+
done
72+
}
73+
74+
startBuildkitd
75+
waitForBuildkitd
76+
$ENVDCTL context create --name daemonless --builder unix --builder-address $(cat $tmp/addr) --use
77+
$ENVDCTL "$@"

0 commit comments

Comments
 (0)