From 9a8a24993204972dc0b9503f7fd43bf2fc0a7342 Mon Sep 17 00:00:00 2001 From: Dave Date: Tue, 20 Aug 2024 06:16:21 -0400 Subject: [PATCH] feat: devcontainer part 3 (#3318) * stash initial fixes, attempt to open branch inside container Signed-off-by: Dave Lee * add yq, from inside DC Signed-off-by: Dave Lee * stash progress, rebuild container Signed-off-by: Dave Lee * snap Signed-off-by: Dave Lee * split builder into builder-sd, will speed up devcontainer build times and potentially help caching in other situations. Signed-off-by: Dave Lee * fix yq Signed-off-by: Dave Lee * fix paths Signed-off-by: Dave Lee * fix paths - new folder to bypass the .dockerignore which _should_ exclude the other files Signed-off-by: Dave Lee * fix Signed-off-by: Dave Lee * fix ] Signed-off-by: Dave Lee --------- Signed-off-by: Dave Lee --- .devcontainer-scripts/postcreate.sh | 17 +++++++++++ .devcontainer-scripts/poststart.sh | 16 ++++++++++ .devcontainer/customization/README.md | 20 +++++++++++++ .devcontainer/devcontainer.json | 4 +-- .devcontainer/docker-compose-devcontainer.yml | 2 ++ .gitignore | 3 ++ Dockerfile | 29 ++++++++++++++----- 7 files changed, 82 insertions(+), 9 deletions(-) create mode 100644 .devcontainer-scripts/postcreate.sh create mode 100644 .devcontainer-scripts/poststart.sh create mode 100644 .devcontainer/customization/README.md diff --git a/.devcontainer-scripts/postcreate.sh b/.devcontainer-scripts/postcreate.sh new file mode 100644 index 00000000..3f903509 --- /dev/null +++ b/.devcontainer-scripts/postcreate.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +cd /workspace + +# Get the files into the volume without a bind mount +if [ ! -d ".git" ]; then + git clone https://github.com/mudler/LocalAI.git . +else + git fetch +fi + +echo "Standard Post-Create script completed." + +if [ -f "/devcontainer-customization/postcreate.sh" ]; then + echo "Launching customization postcreate.sh" + bash "/devcontainer-customization/postcreate.sh" +fi \ No newline at end of file diff --git a/.devcontainer-scripts/poststart.sh b/.devcontainer-scripts/poststart.sh new file mode 100644 index 00000000..196e821d --- /dev/null +++ b/.devcontainer-scripts/poststart.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +cd /workspace + +# Grab the pre-stashed backend assets to avoid build issues +cp -r /build/backend-assets /workspace/backend-assets + +# Ensures generated source files are present upon load +make prepare + +echo "Standard Post-Start script completed." + +if [ -f "/devcontainer-customization/poststart.sh" ]; then + echo "Launching customization poststart.sh" + bash "/devcontainer-customization/poststart.sh" +fi \ No newline at end of file diff --git a/.devcontainer/customization/README.md b/.devcontainer/customization/README.md new file mode 100644 index 00000000..3e2a3778 --- /dev/null +++ b/.devcontainer/customization/README.md @@ -0,0 +1,20 @@ +Place any additional resources your environment requires in this directory + +Script hooks are currently called for: +`postcreate.sh` and `poststart.sh` + +If files with those names exist here, they will be called at the end of the normal script. + +This is a good place to set things like `git config --global user.name` are set - and to handle any other files that are mounted via this directory. + +An example of a useful script might be: + +``` +#!/bin/bash +gcn=$(git config --global user.name) +if [ -z "$gcn" ]; then + git config --global user.name YOUR.NAME + git config --global user.email YOUR.EMAIL + git remote add PREFIX FORK_URL +fi +``` \ No newline at end of file diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 3ab37a2b..37c81ffc 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -19,6 +19,6 @@ } }, "forwardPorts": [8080, 3000], - "postCreateCommand": "git clone https://github.com/mudler/LocalAI.git .", - "postStartCommand": "make prepare && cp -r /build/backend-assets /workspace/backend-assets" + "postCreateCommand": "bash /.devcontainer-scripts/postcreate.sh", + "postStartCommand": "bash /.devcontainer-scripts/poststart.sh" } \ No newline at end of file diff --git a/.devcontainer/docker-compose-devcontainer.yml b/.devcontainer/docker-compose-devcontainer.yml index f7de406e..8795d64d 100644 --- a/.devcontainer/docker-compose-devcontainer.yml +++ b/.devcontainer/docker-compose-devcontainer.yml @@ -14,6 +14,8 @@ services: - 8080:8080 volumes: - localai_workspace:/workspace + - ../models:/host-models + - ./customization:/devcontainer-customization command: /bin/sh -c "while sleep 1000; do :; done" cap_add: - SYS_PTRACE diff --git a/.gitignore b/.gitignore index 096689c5..65eb9257 100644 --- a/.gitignore +++ b/.gitignore @@ -54,3 +54,6 @@ docs/static/gallery.html # backend virtual environments **/venv + +# per-developer customization files for the development container +.devcontainer/customization/* \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 3c0fbc7a..e38675c3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -260,9 +260,9 @@ EOT ################################### ################################### -# The builder target compiles LocalAI. This target is not the target that will be uploaded to the registry. -# Adjustments to the build process should likely be made here. -FROM builder-base AS builder +# This first portion of builder holds the layers specifically used to build backend-assets/grpc/stablediffusion +# In most cases, builder is the image you should be using - however, this can save build time if one just needs to copy backend-assets/grpc/stablediffusion and nothing else. +FROM builder-base AS builder-sd COPY . . COPY .git . @@ -273,6 +273,13 @@ RUN make prepare # stablediffusion does not tolerate a newer version of abseil, build it first RUN GRPC_BACKENDS=backend-assets/grpc/stablediffusion make build +################################### +################################### + +# The builder target compiles LocalAI. This target is not the target that will be uploaded to the registry. +# Adjustments to the build process should likely be made here. +FROM builder-sd AS builder + # Install the pre-built GRPC COPY --from=grpc /opt/grpc /usr/local @@ -299,9 +306,9 @@ ARG FFMPEG COPY --from=grpc /opt/grpc /usr/local -# This is somewhat of a dirty hack as this dev machine has issues with stablediffusion... but it should also speed up devcontainers? -# localai/localai:latest-aio-cpu -COPY --from=builder /build/backend-assets/grpc/stablediffusion /build/backend-assets/grpc/stablediffusion +COPY --from=builder-sd /build/backend-assets/grpc/stablediffusion /build/backend-assets/grpc/stablediffusion + +COPY .devcontainer-scripts /.devcontainer-scripts # Add FFmpeg RUN if [ "${FFMPEG}" = "true" ]; then \ @@ -312,8 +319,16 @@ RUN if [ "${FFMPEG}" = "true" ]; then \ rm -rf /var/lib/apt/lists/* \ ; fi +RUN apt-get update && \ + apt-get install -y --no-install-recommends \ + ssh less && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* + RUN go install github.com/go-delve/delve/cmd/dlv@latest +RUN go install github.com/mikefarah/yq/v4@latest + ################################### ################################### @@ -367,7 +382,7 @@ COPY --from=builder /build/local-ai ./ COPY --from=builder /build/sources/go-piper/piper-phonemize/pi/lib/* /usr/lib/ # do not let stablediffusion rebuild (requires an older version of absl) -COPY --from=builder /build/backend-assets/grpc/stablediffusion ./backend-assets/grpc/stablediffusion +COPY --from=builder-sd /build/backend-assets/grpc/stablediffusion ./backend-assets/grpc/stablediffusion # Change the shell to bash so we can use [[ tests below SHELL ["/bin/bash", "-c"]