Skip to content

Commit

Permalink
feat(oci): add server-side container support
Browse files Browse the repository at this point in the history
  • Loading branch information
HoKim98 committed Jul 9, 2024
1 parent ac51acd commit a97a52c
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 21 deletions.
12 changes: 7 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
[workspace]
default-members = ["crates/cassette"]
default-members = [
"crates/cassette", # exclude(server)
]
members = [
"crates/cassette",
"crates/cassette", # exclude(server)
"crates/cassette-core",
"crates/cassette-gateway",
"crates/cassette-operator",
"crates/cassette-plugin-kubernetes",
"crates/cassette-gateway", # exclude(client)
"crates/cassette-operator", # exclude(client)
"crates/cassette-plugin-kubernetes", # exclude(server)
]
resolver = "2"

Expand Down
33 changes: 21 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

# Configure environment variables
ARG DEBIAN_VERSION="bookworm"
ARG NGINX_VERSION="stable-${DEBIAN_VERSION}-otel"
ARG PACKAGE="cassette"

# Be ready for serving
FROM docker.io/library/debian:${DEBIAN_VERSION} as server
FROM docker.io/library/nginx:${NGINX_VERSION} as server

# Server Configuration
EXPOSE 6080/tcp
Expand All @@ -16,37 +17,45 @@ ENTRYPOINT [ "/usr/bin/env" ]
CMD [ "trunk", "serve" ]

# Be ready for building
FROM docker.io/library/rust:1-${DEBIAN_VERSION} as builder
FROM docker.io/library/rust:1-${DEBIAN_VERSION} as builder-dep

# Load source files
ADD . /src
WORKDIR /src

# Install dependencies
RUN \
# Cache build outputs
--mount=type=cache,target=/src/target \
--mount=type=cache,target=/usr/local/cargo/registry \
# Create an output directory
mkdir /out \
RUN true \
# Enable wasm32 target
&& rustup target add wasm32-unknown-unknown \
# Build
&& cargo install trunk --root /usr/local \
&& cargo install wasm-bindgen-cli --root /usr/local
&& cargo install --root /usr/local \
trunk \
wasm-bindgen-cli

# Copy executable dependency files
FROM server
COPY --from=builder /usr/local/bin/* /usr/local/bin/

# Build it!
FROM builder-dep as builder
RUN \
# Cache build outputs
--mount=type=cache,target=/src/target \
--mount=type=cache,target=/usr/local/cargo/registry \
# Create an output directory
mkdir /out \
# Exclude non-client packages
&& find ./ -type f -name Cargo.toml -exec sed -i 's/^\( *\)\(.*\# *exclude *( *client *)\)$/\1# \2/g' {} + \
&& find ./ -type f -name Cargo.toml -exec sed -i 's/^\( *\)\# *\(.*\# *include *( *client *)\)$/\1\2/g' {} + \
# Build
&& cargo build --all --workspace --release \
&& trunk build './crates/cassette/index.html' --release \
&& find ./target/release/ -maxdepth 1 -type f -perm -a=x -print0 | xargs -0 -I {} mv {} /out \
&& cp ./Trunk.toml /out \
&& mv ./LICENSE /LICENSE

# Copy executable files
FROM server
ARG PACKAGE
COPY --from=builder /out/* /usr/local/bin/
COPY --from=builder /usr/local/bin/* /usr/local/bin/
COPY --from=builder /out/* /usr/local/bin/
COPY --from=builder /LICENSE /usr/share/licenses/${PACKAGE}/LICENSE
46 changes: 46 additions & 0 deletions Dockerfile.server
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright (c) 2024 Ho Kim (ho.kim@ulagbulag.io). All rights reserved.
# Use of this source code is governed by a GPL-3-style license that can be
# found in the LICENSE file.

# Configure environment variables
ARG DEBIAN_VERSION="bookworm"
ARG PACKAGE="cassette-server"

# Be ready for serving
FROM docker.io/library/debian:${DEBIAN_VERSION} as server

# Server Configuration
EXPOSE 6080/tcp
WORKDIR /usr/local/bin
CMD [ "/bin/sh" ]

# Be ready for building
FROM docker.io/library/rust:1-${DEBIAN_VERSION} as builder

# Load source files
ADD . /src
WORKDIR /src

# Build it!
RUN \
# Cache build outputs
--mount=type=cache,target=/src/target \
--mount=type=cache,target=/usr/local/cargo/registry \
true \
# Exclude non-server packages
&& find ./ -type f -name Cargo.toml -exec sed -i 's/^\( *\)\(.*\# *exclude *( *server *)\)$/\1# \2/g' {} + \
&& find ./ -type f -name Cargo.toml -exec sed -i 's/^\( *\)\# *\(.*\# *include *( *server *)\)$/\1\2/g' {} + \
# Include target-dependent packages
&& find ./ -type f -name Cargo.toml -exec sed -i 's/^\( *\)\(.*\# *include *( *[_0-9a-z-]\+ *)\)$/\1# \2/g' {} + \
&& find ./ -type f -name Cargo.toml -exec sed -i "s/^\( *\)\# *\(.*\# *include *( *$(uname -m) *)\)$/\1\2/g" {} + \
# Build
&& cargo build --all --workspace --release \
&& find ./target/release/ -maxdepth 1 -type f -perm -a=x -print0 | xargs -0 -I {} mv {} /out \
&& cp ./Trunk.toml /out \
&& mv ./LICENSE /LICENSE

# Copy executable files
FROM server
ARG PACKAGE
COPY --from=builder /out/* /usr/local/bin/
COPY --from=builder /LICENSE /usr/share/licenses/${PACKAGE}/LICENSE
16 changes: 12 additions & 4 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ set dotenv-load

# Configure environment variables
export DEBIAN_VERSION := env_var_or_default('DEBIAN_VERSION', 'bookworm')
export NGINX_VERSION := env_var_or_default('NGINX_VERSION', "stable-bookworm-otel")
export OCI_BUILD_LOG_DIR := env_var_or_default('OCI_BUILD_LOG_DIR', './logs/')
export OCI_IMAGE := env_var_or_default('OCI_IMAGE', 'quay.io/ulagbulag/cassette')
export OCI_IMAGE_VERSION := env_var_or_default('OCI_IMAGE_VERSION', 'latest')
Expand Down Expand Up @@ -41,15 +42,22 @@ run-gateway *ARGS:
run-operator *ARGS:
cargo run --package 'cassette-operator' --release

oci-build *ARGS:
_oci-build file oci_suffix *ARGS:
mkdir -p "${OCI_BUILD_LOG_DIR}"
docker buildx build \
--file './Dockerfile' \
--tag "${OCI_IMAGE}:${OCI_IMAGE_VERSION}" \
--file "{{ file }}" \
--tag "${OCI_IMAGE}{{ oci_suffix }}:${OCI_IMAGE_VERSION}" \
--build-arg DEBIAN_VERSION="${DEBIAN_VERSION}" \
--build-arg NGINX_VERSION="${NGINX_VERSION}" \
--platform "${OCI_PLATFORMS}" \
--pull \
{{ ARGS }} \
. 2>&1 | tee "${OCI_BUILD_LOG_DIR}/build-base-$( date -u +%s ).log"

oci-push: (oci-build "--push")
oci-build: (_oci-build './Dockerfile' '')

oci-build-server: (_oci-build '-server' './Dockerfile.server')

oci-push: (_oci-build './Dockerfile' '' "--push")

oci-push-server: (_oci-build './Dockerfile.server' '-server' "--push")

0 comments on commit a97a52c

Please sign in to comment.