Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Dev Container for consistent dev setup #394

Merged
merged 4 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions .devcontainer/fedora/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2020-2024 Intel Corporation
# Copyright (c) 2024 Red Hat Inc.

FROM fedora:40 AS build

# Setup container to build CNDP applications
RUN dnf -y upgrade && dnf -y install \
@development-tools \
libbsd-devel \
json-c-devel \
libnl3-devel \
libnl3-cli \
numactl-libs \
libbpf-devel \
libbpf \
meson \
ninja-build \
gcc-c++ \
libpcap \
libpcap-devel \
golang \
clang \
llvm \
m4 \
bpftool \
python3-pip

# Install libxdp
RUN git clone https://github.com/xdp-project/xdp-tools.git
WORKDIR /xdp-tools/
RUN ./configure
RUN make -j; PREFIX=/usr make -j install
ENV PKG_CONFIG_PATH=/usr/lib/pkgconfig

# Install Rust bindgen dependencies.
RUN dnf -y install clang-devel curl

# Install Rust and Cargo.
RUN curl https://sh.rustup.rs -sSf | sh -s -- -y

# Set Cargo path.
ENV PATH="/root/.cargo/bin:${PATH}"
RUN pip install pre-commit
46 changes: 46 additions & 0 deletions .devcontainer/fedora/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "cndp-fedora",
"build": {
"dockerfile": "Dockerfile",
},
"remoteUser": "${localEnv:USER}",
"containerUser": "${localEnv:USER}",
// Add the IDs of extensions you want installed when the container is created.
"customizations": {
"vscode": {
"extensions": [
"766b.go-outliner",
"aldijav.golangwithdidi",
"bierner.github-markdown-preview",
"bierner.markdown-checkbox",
"bierner.markdown-emoji",
"bierner.markdown-footnotes",
"bierner.markdown-mermaid",
"bierner.markdown-preview-github-styles",
"bierner.markdown-yaml-preamble",
"davidanson.vscode-markdownlint",
"github.vscode-github-actions",
"golang.go",
"ms-vscode.cmake-tools",
"ms-vscode.cpptools",
"ms-vscode.cpptools-extension-pack",
"ms-vscode.cpptools-themes",
"ms-vscode.makefile-tools",
"msyrus.go-doc",
"neonxp.gotools",
"streetsidesoftware.code-spell-checker",
"twxs.cmake",
]
}
},
"postStartCommand": "${containerWorkspaceFolder}/.devcontainer/fedora/postStartCommand.sh",
"workspaceMount": "source=${localWorkspaceFolder},target=/workspace,type=bind",
"workspaceFolder": "/workspace",
"hostRequirements": {
"cpus": 4
},
"runArgs": [
"--privileged",
"--network=host"
]
}
6 changes: 6 additions & 0 deletions .devcontainer/fedora/postStartCommand.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#! /bin/sh -e
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (C) 2024 Red Hat, Inc.

pre-commit install
make clean
86 changes: 86 additions & 0 deletions .devcontainer/fedora/user.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#! /bin/bash -e
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (C) 2024 Red Hat, Inc.

set -euo pipefail

username=""
userid=""

usage() {
cat <<EOF >&2
Usage: $0
-u | --user <username>
-g | --gid <userid>
EOF
exit 1
}

# Parse command-line arguments
args=$(getopt -o u:g: --long user:,gid: -n "$0" -- "$@") || usage

eval set -- "$args"
while [ $# -gt 0 ]; do
case "$1" in
-h | --help)
usage
;;
-u | --user)
username="$2"
shift 2
;;
-g | --gid)
userid="$2"
shift 2
;;
--)
shift
break
;;
*)
echo "Unsupported option: $1" >&2
usage
;;
esac
done

# Validate required parameters
if [ -z "$username" ] || [ -z "$userid" ]; then
echo "Error: --user and --gid are required." >&2
usage
fi

USER_NAME="$username"
USER_UID="$userid"
USER_GID="$USER_UID"
HOME_DIR="/home/$USER_NAME"

# Exit if the user is root
if [ "$USER_NAME" = "root" ]; then
exit 0
fi

if ! getent group "$USER_NAME" >/dev/null; then
groupadd --gid "$USER_GID" "$USER_NAME"
fi

if ! getent passwd "$USER_NAME" >/dev/null; then
useradd --uid "$USER_UID" --gid "$USER_GID" -m "$USER_NAME"
fi

# Ensure $HOME exists when starting
if [ ! -d "${HOME_DIR}" ]; then
mkdir -p "${HOME_DIR}"
fi

# Add current (arbitrary) user to /etc/passwd and /etc/group
if [ -w /etc/passwd ]; then
echo "${USER_NAME:-user}:x:$(id -u):0:${USER_NAME:-user}:${HOME_DIR}:/bin/bash" >> /etc/passwd
echo "${USER_NAME:-user}:x:$(id -u):" >> /etc/group
fi

# Fix up permissions
chown "$USER_NAME:$USER_GID" -R "/home/$USER_NAME"
chown "$USER_NAME:$USER_GID" -R /opt
mkdir -p "/run/user/$USER_UID"
chown "$USER_NAME:$USER_GID" "/run/user/$USER_UID"
63 changes: 63 additions & 0 deletions .devcontainer/gsg/devcontainer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Device Containers Getting Started Guide

This guide will walk you through using a [Development Container](https://containers.dev/)
configured with all the tools needed to build and run CNDP. The Dev
Container setup is compatible with local development in Visual Studio Code
and with GitHub Codespaces for cloud-based workflows.

## Prerequisites

- **VSCode**
- **Docker**
- **VSCode Dev Containers Extension**

> **_NOTE_**: Details of the Dev Container prerequisites can be found
[here](https://code.visualstudio.com/docs/devcontainers/tutorial#_prerequisites).

## Basic Workflow

1. Dev Container Configurations can be found in the `.devcontainer` directory.
Files are set up with configuration details: the Docker image to use,
extensions to install, environment variables...
> **_NOTE_**: The Dev Container configuration provided supports both root
(not recommended) and non-root (recommended) users.
2. Open project in Container: Open the project folder in VS Code or Github
workspaces to build and attach the development environment.
3. Development: Work as usual, with access to tools and dependencies defined
in the container.

The following sections will walk through Step 2 in detail.

### Running in Visual Studio Code

Follow these steps to launch and work with the Dev Container in Visual
Studio Code.

Open the project in Visual Studio Code. A pop up will appear asking to reopen
in project in a Dev Container.
![reopen-in-container](./images/reopen-in-container.png)

If the pop up doesn't appear, the container can be launched by accessing the
Visual Studio Code Command Palette and looking for the:
`Dev Containers: Reopen in Container` option as shown below.

![reopen-in-container](./images/rebuild-container.png)

Visual Studio Code will relaunch inside the Dev Container.

When the container is ready CNDP can be built as usual.

### Running in Github Codespace

Use GitHub Codespaces for cloud-based development with the same Dev Container configuration.

### Running the Tutorials in the Dev Container

1. Navigate to your repository and click the `< >Code` dropdown.
2. In the Codespaces tab, click the ellipsis (...), then select `+ New with Options`:
![codespaces-options](./images/codespaces-options.png)
3. (If needed) Select the Branch, the Dev Container configuration, number of CPUs:
![codespaces-config](./images/codespaces-config.png)
4. Click the Button to `Create codespace`.

When the codespace is available CNDP can be built as usual.
Binary file added .devcontainer/gsg/images/codespaces-config.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .devcontainer/gsg/images/codespaces-options.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .devcontainer/gsg/images/rebuild-container.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .devcontainer/gsg/images/reopen-in-container.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 57 additions & 0 deletions .devcontainer/ubuntu/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2020-2023 Intel Corporation.
# Copyright (c) 2024 Red Hat Inc.

FROM ubuntu:24.04

# Setup container to build CNDP applications
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y \
build-essential \
golang \
libelf-dev \
meson \
pkg-config \
libbsd-dev \
libjson-c-dev \
libnl-3-dev \
libnl-cli-3-dev \
libnuma-dev \
libpcap-dev \
wget \
llvm-dev \
libclang-dev \
clang \
curl \
git \
gcc-multilib \
llvm \
lld \
m4 \
linux-tools-common \
libbpf-dev \
python3-pip \
python3.12-venv \
&& rm -rf /var/lib/apt/lists/*

# Install libxdp
RUN git clone https://github.com/xdp-project/xdp-tools.git
WORKDIR /xdp-tools/
RUN git checkout v1.2.2
RUN ./configure
WORKDIR /xdp-tools/
RUN make -j; PREFIX=/usr make -j install
ENV PKG_CONFIG_PATH=/usr/lib/pkgconfig

# Install Rust and Cargo.
RUN curl https://sh.rustup.rs -sSf | sh -s -- -y

# Set Cargo path.
ENV PATH="/root/.cargo/bin:${PATH}"

# Set PKG_CONFIG_PATH to resolve libbpf dependency for Rust library build.
ENV PKG_CONFIG_PATH="/usr/lib64/pkgconfig"

RUN python3 -m venv /opt/cndp-venv
ENV PATH=/opt/cndp-venv/bin:$PATH
RUN echo "source /opt/cndp-venv/bin/activate" >> "${HOME}/.bashrc"
46 changes: 46 additions & 0 deletions .devcontainer/ubuntu/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "cndp-ubuntu",
"build": {
"dockerfile": "Dockerfile",
},
"remoteUser": "${localEnv:USER}",
"containerUser": "${localEnv:USER}",
// Add the IDs of extensions you want installed when the container is created.
"customizations": {
"vscode": {
"extensions": [
"766b.go-outliner",
"aldijav.golangwithdidi",
"bierner.github-markdown-preview",
"bierner.markdown-checkbox",
"bierner.markdown-emoji",
"bierner.markdown-footnotes",
"bierner.markdown-mermaid",
"bierner.markdown-preview-github-styles",
"bierner.markdown-yaml-preamble",
"davidanson.vscode-markdownlint",
"github.vscode-github-actions",
"golang.go",
"ms-vscode.cmake-tools",
"ms-vscode.cpptools",
"ms-vscode.cpptools-extension-pack",
"ms-vscode.cpptools-themes",
"ms-vscode.makefile-tools",
"msyrus.go-doc",
"neonxp.gotools",
"streetsidesoftware.code-spell-checker",
"twxs.cmake",
]
}
},
"postStartCommand": "${containerWorkspaceFolder}/.devcontainer/ubuntu/postStartCommand.sh",
"workspaceMount": "source=${localWorkspaceFolder},target=/workspace,type=bind",
"workspaceFolder": "/workspace",
"hostRequirements": {
"cpus": 4
},
"runArgs": [
"--privileged",
"--network=host"
]
}
6 changes: 6 additions & 0 deletions .devcontainer/ubuntu/postStartCommand.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#! /bin/sh -e
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (C) 2024 Red Hat, Inc.
pip install pre-commit
pre-commit install
make clean
Loading
Loading