From fb608eb5ee079bff6ccb1f0bbf4bd461c638adba Mon Sep 17 00:00:00 2001 From: John Cairns Date: Wed, 8 Nov 2023 13:45:16 -0600 Subject: [PATCH] 1: XMTP standardized development environment for rust --- .devcontainer/devcontainer.json | 26 ++++++++ .dockerignore | 4 ++ .github/workflows/ci-image.yml | 33 ++++++++++ .vscode/extensions.json | 5 ++ .vscode/tasks.json | 71 ++++++++++++++++++++++ Dockerfile | 104 ++++++++++++++++++++++++++++++++ README.md | 16 ++++- 7 files changed, 258 insertions(+), 1 deletion(-) create mode 100644 .devcontainer/devcontainer.json create mode 100644 .dockerignore create mode 100644 .github/workflows/ci-image.yml create mode 100644 .vscode/extensions.json create mode 100644 .vscode/tasks.json create mode 100644 Dockerfile diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..8d96444 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,26 @@ +// For format details, see https://aka.ms/devcontainer.json. For config options, see the +// README at: https://github.com/devcontainers/templates/tree/main/src/docker-existing-dockerfile +{ + "name": "Existing Dockerfile", + "build": { + // Sets the run context to one level up instead of the .devcontainer folder. + "context": "..", + // Update the 'dockerFile' property if you aren't using the standard 'Dockerfile' filename. + "dockerfile": "../Dockerfile" + } + + // Features to add to the dev container. More info: https://containers.dev/features. + // "features": {}, + + // Use 'forwardPorts' to make a list of ports inside the container available locally. + // "forwardPorts": [], + + // Uncomment the next line to run commands after the container is created. + // "postCreateCommand": "cat /etc/os-release", + + // Configure tool-specific properties. + // "customizations": {}, + + // Uncomment to connect as an existing user other than the container default. More info: https://aka.ms/dev-containers-non-root. + // "remoteUser": "devcontainer" +} diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..81c7945 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +**/target/** +**/node_modules +**/.build/** +**/build/** \ No newline at end of file diff --git a/.github/workflows/ci-image.yml b/.github/workflows/ci-image.yml new file mode 100644 index 0000000..5ea4440 --- /dev/null +++ b/.github/workflows/ci-image.yml @@ -0,0 +1,33 @@ +name: Build Dev Image CI + +on: + workflow_dispatch: + pull_request: + branches: + - main + push: + branches: + - "*" + +jobs: + build: + runs-on: ubuntu-latest + steps: + - + name: Checkout + uses: actions/checkout@v3 + - + name: Set up QEMU + uses: docker/setup-qemu-action@v2 + - + name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Build and Push + uses: docker/build-push-action@v3 + with: + context: . + platforms: linux/amd64 + push: false + build-args: | + VERSION=latest diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..61ed5d4 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,5 @@ +{ + "recommendations": [ + "rust-lang.rust-analyzer" + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..c81e574 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,71 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "tasks": [ + { + "label": "fmt", + "type": "shell", + "command": "cargo fmt --check", + "options": { + "cwd": "${workspaceFolder}" + }, + "group": { + "kind": "build", + "isDefault": "false" + } + }, + { + "label": "lint", + "type": "shell", + "command": "cargo clippy --all-features --no-deps", + "options": { + "cwd": "${workspaceFolder}" + }, + "dependsOn": "fmt", + "group": { + "kind": "build", + "isDefault": "false" + } + }, + { + "label": "build", + "type": "shell", + "command": "cargo build", + "options": { + "cwd": "${workspaceFolder}" + }, + "dependsOn": "lint", + "group": { + "kind": "build", + "isDefault": "false" + } + }, + { + "label": "check", + "type": "shell", + "command": "cargo check", + "options": { + "cwd": "${workspaceFolder}" + }, + "dependsOn": "lint", + "group": { + "kind": "build", + "isDefault": "true" + } + }, + { + "label": "test", + "type": "shell", + "command": "cargo test", + "options": { + "cwd": "${workspaceFolder}" + }, + "dependsOn": "lint", + "group": { + "kind": "test", + "isDefault": "true" + } + } + ] +} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e68fa6c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,104 @@ +FROM debian:stable-slim as go-builder +# defined from build kit +# DOCKER_BUILDKIT=1 docker build . -t ... +ARG TARGETARCH + +FROM debian:stable-slim as builder +# defined from build kit +# DOCKER_BUILDKIT=1 docker build . -t ... +ARG TARGETARCH + +RUN export DEBIAN_FRONTEND=noninteractive && \ + apt update && \ + apt install -y -q --no-install-recommends \ + git curl gnupg2 build-essential \ + linux-headers-${TARGETARCH} libc6-dev \ + openssl libssl-dev pkg-config \ + ca-certificates apt-transport-https \ + python3 && \ + apt clean && \ + rm -rf /var/lib/apt/lists/* + +RUN useradd --create-home -s /bin/bash xmtp +RUN usermod -a -G sudo xmtp +RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers + +WORKDIR /rustup +## Rust +ADD https://sh.rustup.rs /rustup/rustup.sh +RUN chmod 755 /rustup/rustup.sh + +ENV USER=xmtp +USER xmtp +RUN /rustup/rustup.sh -y --default-toolchain stable --profile minimal + +ENV PATH=$PATH:~xmtp/.cargo/bin + +FROM debian:stable-slim +ARG TARGETARCH + +RUN export DEBIAN_FRONTEND=noninteractive && \ + apt update && \ + apt install -y -q --no-install-recommends \ + ca-certificates apt-transport-https \ + sudo ripgrep procps build-essential \ + python3 python3-pip python3-dev \ + git curl protobuf-compiler && \ + apt clean && \ + rm -rf /var/lib/apt/lists/* + +RUN echo "building platform $(uname -m)" + +RUN useradd --create-home -s /bin/bash xmtp +RUN usermod -a -G sudo xmtp +RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers + +## Node and NPM +RUN mkdir -p /usr/local/nvm +ENV NVM_DIR=/usr/local/nvm + +ENV NODE_VERSION=v20.9.0 + +ADD https://raw.githubusercontent.com/creationix/nvm/master/install.sh /usr/local/etc/nvm/install.sh +RUN bash /usr/local/etc/nvm/install.sh && \ + bash -c ". $NVM_DIR/nvm.sh && nvm install $NODE_VERSION && nvm alias default $NODE_VERSION && nvm use default" + +ENV NVM_NODE_PATH ${NVM_DIR}/versions/node/${NODE_VERSION} +ENV NODE_PATH ${NVM_NODE_PATH}/lib/node_modules +ENV PATH ${NVM_NODE_PATH}/bin:$PATH + +RUN npm install npm -g +RUN npm install yarn -g + + +## Rust from builder +COPY --chown=xmtp:xmtp --from=builder /home/xmtp/.cargo /home/xmtp/.cargo +COPY --chown=xmtp:xmtp --from=builder /home/xmtp/.rustup /home/xmtp/.rustup + +USER xmtp + +RUN ~xmtp/.cargo/bin/rustup toolchain install stable +RUN ~xmtp/.cargo/bin/rustup component add rustfmt +RUN ~xmtp/.cargo/bin/rustup component add clippy + +WORKDIR /workspaces/libxmtp +COPY --chown=xmtp:xmtp . . + +ENV PATH=~xmtp/.cargo/bin:$PATH +ENV USER=xmtp + +RUN ~xmtp/.cargo/bin/cargo check +RUN ~xmtp/.cargo/bin/cargo fmt --check +RUN ~xmtp/.cargo/bin/cargo clippy --all-features --no-deps +RUN ~xmtp/.cargo/bin/cargo test + +LABEL org.label-schema.build-date=$BUILD_DATE \ + org.label-schema.name="rustdev" \ + org.label-schema.description="Rust Development Container" \ + org.label-schema.url="https://github.com/xmtp/libxmtp" \ + org.label-schema.vcs-ref=$VCS_REF \ + org.label-schema.vcs-url="git@github.com:xmtp/libxmtp.git" \ + org.label-schema.vendor="xmtp" \ + org.label-schema.version=$VERSION \ + org.label-schema.schema-version="1.0" \ + org.opencontainers.image.description="Rust Development Container" diff --git a/README.md b/README.md index a43d211..ab58ce5 100644 --- a/README.md +++ b/README.md @@ -1 +1,15 @@ -# gateway +# XMTP Gateway Node + +## Quick Start (Dev Containers) + +This project supports containerized development. From Visual Studio Code Dev Containers extension specify the Dockerfile as the target: + +`Reopen in Container` + +or + +Command line build using docker + +```bash +$ docker build . -t xmtp_gateway:1 +```