Skip to content

Commit

Permalink
Merge pull request #8 from Caltech-AMBER/pixi-setup
Browse files Browse the repository at this point in the history
Pixi Setup (python)
  • Loading branch information
Zolkin1 authored Jun 20, 2024
2 parents 572da29 + 5cdcd7b commit 8da79aa
Show file tree
Hide file tree
Showing 38 changed files with 22,036 additions and 162 deletions.
22 changes: 22 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "Obelisk Dev Container",
"dockerComposeFile": "../docker/docker-compose.yml",
"service": "obelisk",
"workspaceFolder": "${localWorkspaceFolder}",
"shutdownAction": "stopCompose",
"customizations": {
"vscode": {
"extensions": [
"mutantdino.resourcemonitor",
"ms-azuretools.vscode-docker",
"nvidia.nsight-vscode-edition",
"ms-python.python",
"charliermarsh.ruff"
]
}
},
"initializeCommand": "export OBELISK_ROOT=${localWorkspaceFolder}",
"remoteEnv": {
"OBELISK_ROOT": "${localEnv: OBELISK_ROOT}"
},
}
41 changes: 41 additions & 0 deletions .github/workflows/code_checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Core Tests

on:
push:
branches: [main]
pull_request:
branches: [main]

permissions:
contents: read

jobs:
build:
runs-on: ubuntu-22.04

steps:
- uses: actions/checkout@v4
- name: Set up Python 3.12
uses: actions/setup-python@v5
with:
python-version: '3.12.4'
- uses: prefix-dev/[email protected]
with:
pixi-version: v0.24.2
cache: true
- uses: actions/cache@v4
with:
path: ${{ env.pythonLocation }}
key: ${{ env.pythonLocation }}-${{ hashFiles('pixi.toml') }}
- name: Run Ruff Linter
run: |
pixi run --environment test-workflow ruff check docs/ obelisk/ tests/ --output-format=github
- name: Run Ruff Formatter
run: |
pixi run --environment test-workflow ruff format docs/ obelisk/ tests/ --diff
- name: Run Pyright
run: |
pixi run --environment test-workflow pyright
- name: Test with pytest
run: |
pixi run --environment test-workflow pytest
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ jobs:
uses: sphinx-notes/pages@v3
with:
python_version: 3.11
documentation_path: ./docs/source
documentation_path: ./docs/source
15 changes: 14 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,17 @@ Thumbs.db

# Build directories
doctrees
html
html

# #### #
# PIXI #
# #### #
.pixi

# #### #
# ROS2 #
# #### #
build
install
log
.built_by
16 changes: 16 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
default_language_version:
python: python3
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.4.9
hooks:
- id: ruff
types_or: [ python, pyi, jupyter ]
args: [ --fix ]
- id: ruff-format
types_or: [ python, pyi, jupyter ]
30 changes: 23 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,31 @@ This repository hosts generic interfaces for controlling the lab's robots with R
[Docs](https://caltech-amber.github.io/obelisk/)

## Development
We recommend developing locally inside a `conda` environment for `sphinx` integration. To set up the development workflow, run the following commands from the repository root
We recommend developing using `pixi` for dependency management. First, install required system-level dependencies for development and set environment variables by running the following script:
```
# for system-wide dependencies
bash dev_setup.sh
source dev_setup.sh
newgrp docker
```
This will do the following:
1. install `docker`
2. install `nvidia-container-toolkit`
3. install `pixi`
4. install `uv`
5. install `nvm` (required for `pyright`)
6. set the `OBELISK_ROOT` environment variable to the repo root

# for python dependencies (you can choose different dependency groups besides dev, see pyproject.toml)
conda env create -f environment.yml --name <my_env>
conda activate <my_env>
pip install -e .[dev]
### Docker
We recommend developing on `obelisk` using a Docker container (we provide `.devcontainer` support if you use an IDE supporting it). You can also bring up a container using docker-compose as follows from the repository root:
```
docker compose -f docker/docker-compose.yml run --build obelisk
```
To enter the container without rebuilding or to join from a different terminal window, run
```
docker compose -f docker/docker-compose.yml run obelisk
```
If you are running on a machine with no Nvidia GPUs, you can instead run
```
docker compose -f docker/docker-compose-no-gpu.yml run --build obelisk
```

### Building Docs
Expand Down
115 changes: 91 additions & 24 deletions dev_setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,97 @@ sudo apt-get install -y \

# docker
# also, see https://stackoverflow.com/questions/48957195/how-to-fix-docker-got-permission-denied-issue
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

sudo groupadd docker
sudo usermod -aG docker $USER
newgrp docker
sudo chmod 660 /var/run/docker.sock
sudo systemctl restart docker
if ! command -v docker &> /dev/null; then
echo -e "\033[1;32mDocker is not installed. Installing Docker...\033[0m"

# if the system has nvidia GPUs, install nvidia-container-toolkit
if (($(nvidia-smi -L | wc -l) > 0)); then
curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg && \
curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | \
sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list
sudo apt-get update
sudo apt-get install -y nvidia-container-toolkit
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

echo -e "\033[1;32mDocker setup complete. Continuing with the script.\033[0m"
else
echo -e "\033[1;33mDocker is already installed. Skipping Docker installation.\033[0m"
fi

# check docker group exists
if ! getent group docker &> /dev/null; then
echo -e "\033[1;32mDocker group does not exist. Creating Docker group and adding user...\033[0m"

sudo groupadd docker
sudo usermod -aG docker $USER
sg docker -c 'sudo chmod 660 /var/run/docker.sock && sudo systemctl restart docker'
else
echo -e "\033[1;33mDocker group already exists. Skipping Docker group creation.\033[0m"
fi

# if the system has nvidia GPUs, install nvidia-container-toolkit
is_nvidia_container_toolkit_installed() {
dpkg -s nvidia-container-toolkit &> /dev/null
}
if ! is_nvidia_container_toolkit_installed; then
echo -e "\033[1;32mNVIDIA Container Toolkit is not installed. Installing...\033[0m"
if (($(nvidia-smi -L | wc -l) > 0)); then
curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg -y && \
curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | \
sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list
sudo apt-get update
sudo apt-get install -y nvidia-container-toolkit
sudo nvidia-ctk runtime configure --runtime=docker
sudo systemctl restart docker
echo -e "\033[1;32mNVIDIA Container Toolkit installation complete!\033[0m"
else
echo -e "\033[1;33mNVIDIA GPU not detected. Skipping NVIDIA Container Toolkit installation.\033[0m"
fi
else
echo -e "\033[1;33mNVIDIA Container Toolkit is already installed. Skipping installation.\033[0m"
fi

# installing pixi
if ! command -v pixi &> /dev/null; then
echo -e "\033[1;32mPixi is not installed. Installing Pixi...\033[0m"
curl -fsSL https://pixi.sh/install.sh | bash
else
echo -e "\033[1;33mPixi is already installed. Skipping Pixi installation.\033[0m"
fi

# installing uv
if ! command -v uv &> /dev/null; then
echo -e "\033[1;32muv is not installed. Installing uv...\033[0m"
curl -LsSf https://astral.sh/uv/install.sh | sh
else
echo -e "\033[1;33muv is already installed. Skipping uv installation.\033[0m"
fi

# installing nvm
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
if ! command -v nvm &> /dev/null; then
echo -e "\033[1;32mnvm is not installed. Installing nvm...\033[0m"
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
source ~/.bashrc
nvm install 20
else
echo -e "\033[1;33mnvm is already installed. Skipping NVM installation.\033[0m"
fi

# set OBELISK_ROOT to the directory where dev_setup.sh is located if it doesn't exist already
if [ -z "$OBELISK_ROOT" ]; then
export OBELISK_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo "export OBELISK_ROOT=$OBELISK_ROOT" >> ~/.bashrc
echo -e "\033[1;32mOBELISK_ROOT is now set to $OBELISK_ROOT!\033[0m"
else
echo -e "\033[1;33mOBELISK_ROOT is already set to $OBELISK_ROOT, skipping...\033[0m"
fi
58 changes: 58 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# syntax=docker/dockerfile:1

# base image
FROM ubuntu:22.04 as base
SHELL ["/bin/bash", "-c"]

# username, uid, gid
ARG USER=user
ARG UID=1000
ARG GID=1000
ENV USER=$USER
ENV UID=$UID
ENV GID=$GID

# set timezone
ENV TZ=America/Los_Angeles
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# essential dependencies
RUN apt-get update -y && \
apt-get install -y \
curl \
build-essential \
cmake \
clang-tools-12 \
nano \
vim \
git \
python3-dev \
python-is-python3 \
python3-pip \
python3-argcomplete \
sudo && \
rm -rf /var/lib/apt/lists/*

# create non-root user with sudo privileges for certain commands
RUN groupadd --gid $GID $USER && \
useradd --uid $UID --gid $GID -m $USER -d /home/${USER} --shell /usr/bin/bash && \
echo "${USER}:password" | chpasswd && \
usermod -aG sudo ${USER} && \
echo "%${USER} ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers

# switch to new user and workdir
USER ${UID}

# pixi, uv, and nvm (for pyright)
RUN curl -fsSL https://pixi.sh/install.sh | bash && \
curl -LsSf https://astral.sh/uv/install.sh | sh && \
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash && \
source /home/${USER}/.bashrc && \
export NVM_DIR="$HOME/.nvm" && \
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" && \
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" && \
nvm install 20

# add local user binary folder to PATH variable
ENV PATH="${PATH}:/home/${USER}/.local/bin"
WORKDIR /home/${USER}
32 changes: 0 additions & 32 deletions docker/Dockerfile.minimal

This file was deleted.

35 changes: 35 additions & 0 deletions docker/docker-compose-no-gpu.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
services:
obelisk:
shm_size: '12gb'
build:
context: .
args:
USER: $USER
UID: $GID
GID: $GID
dockerfile: Dockerfile
network_mode: host
ipc: host
environment:
NVIDIA_DRIVER_CAPABILITIES: all
DISPLAY: $DISPLAY
USER: $USER
UID: $UID
GID: $UID
QT_X11_NO_MITSHM: 1
security_opt:
- seccomp=unconfined
cap_add:
- NET_ADMIN
volumes:
- $OBELISK_ROOT:$OBELISK_ROOT
- /tmp/.X11-unix:/tmp/.X11-unix
- $HOME/.Xauthority:/root/.Xauthority:rw
- $HOME/.bashrc:$HOME/.bashrc
ports:
- 7007:7007
privileged: true
working_dir: $OBELISK_ROOT
stdin_open: true
tty: true
command: /bin/bash
Loading

0 comments on commit 8da79aa

Please sign in to comment.