Skip to content

Add devcontainer configuration for YDB Python SDK #590

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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM mcr.microsoft.com/devcontainers/python:3.9-bookworm

# [Optional] Uncomment if you want to install more tools
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
# && apt-get -y install --no-install-recommends <your-pkg>

# [Optional] Uncomment if you want to install ydb cli
RUN curl -fsSL https://raw.githubusercontent.com/ydb-platform/ydb/refs/heads/main/ydb/apps/ydb/install/install.sh | bash
6 changes: 6 additions & 0 deletions .devcontainer/commands/initialize.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
set -e

git config --local format.signoff true
git config --local user.name "$(git config user.name)"
git config --local user.email "$(git config user.email)"
27 changes: 27 additions & 0 deletions .devcontainer/commands/postCreate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash
set -e

if git config --get commit.gpgsign | grep -q true; then
git config --global gpg.format ssh
git config --global gpg.ssh.defaultKeyCommand 'ssh-add -L'
git config --global gpg.ssh.allowedSigners '~/.ssh/allowed_signers'
fi

# Set up YDB profile if ydb cli exists
if which ydb > /dev/null 2>&1; then
ENDPOINT=$(echo ${YDB_CONNECTION_STRING_SECURE:-$YDB_CONNECTION_STRING} | awk -F/ '{print $1 "//" $3}')
DATABASE=$(echo ${YDB_CONNECTION_STRING_SECURE:-$YDB_CONNECTION_STRING} | cut -d/ -f4-)
CA_FILE_OPTION=""

if [ -n "$YDB_SSL_ROOT_CERTIFICATES_FILE" ]; then
ENDPOINT="${ENDPOINT/grpc:/grpcs:}"
CA_FILE_OPTION="--ca-file ${YDB_SSL_ROOT_CERTIFICATES_FILE}"
fi

ydb config profile replace local \
--endpoint "$ENDPOINT" \
--database "/$DATABASE" \
$CA_FILE_OPTION

ydb config profile activate local
fi
11 changes: 11 additions & 0 deletions .devcontainer/commands/postStart.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash
set -e


# Install dependencies
pip install -r requirements.txt
# Install ydb package
pip install -e .
# Install tox for CI
pip install tox

55 changes: 55 additions & 0 deletions .devcontainer/compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: ydb-python-sdk

volumes:
ydb-data:
# driver: local
# driver_opts:
# type: tmpfs
# device: tmpfs
# o: size=80g
ydb-certs:

services:
sdk:
build:
context: .
dockerfile: Dockerfile
hostname: sdk

volumes:
- ydb-certs:/ydb_certs
- ../:/workspaces/ydb-python-sdk:cached

environment:
- YDB_VERSION=25.1
- YDB_STATIC_CREDENTIALS_USER=root
- YDB_STATIC_CREDENTIALS_PASSWORD=1234
- YDB_STATIC_CREDENTIALS_ENDPOINT=grpc://ydb:2136
- YDB_CONNECTION_STRING=grpc://ydb:2136/local
- YDB_CONNECTION_STRING_SECURE=grpcs://ydb:2135/local
- YDB_SSL_ROOT_CERTIFICATES_FILE=/ydb_certs/ca.pem
- TEST_COMPOSE_FILE=compose.remote.yml

# Overrides default command so things don't shut down after the process ends.
command: sleep infinity

ydb:
image: ydbplatform/local-ydb:25.1
restart: unless-stopped
hostname: ydb
platform: linux/amd64

ports:
- 2135
- 2136
- 8765

volumes:
- ydb-data:/ydb_data
- ydb-certs:/ydb_certs

environment:
- YDB_USE_IN_MEMORY_PDISKS=true
- GRPC_TLS_PORT=2135
- GRPC_PORT=2136
- MON_PORT=8765
53 changes: 53 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"name": "Python & YDB",
"service": "sdk",
"dockerComposeFile": "compose.yml",
"workspaceFolder": "/workspaces/ydb-python-sdk",
// Allows the container to use ptrace, which is useful for debugging.
"capAdd": [
"SYS_PTRACE"
],
// Disables seccomp, which can be necessary for some debugging tools to function correctly.
"securityOpt": [
"seccomp=unconfined"
],
// Features to add to the dev container. More info: https://containers.dev/features.
"features": {
"ghcr.io/devcontainers/features/git": {},
"ghcr.io/devcontainers/features/common-utils": {},
"ghcr.io/devcontainers/features/github-cli:1": {},
"ghcr.io/devcontainers/features/docker-outside-of-docker:1": {}
},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
"forwardPorts": [
"ydb:2135",
"ydb:2136",
"ydb:8765"
],
// Use 'initializeCommand' to run commands before the container is created.
"initializeCommand": "chmod +x .devcontainer/commands/initialize.sh && .devcontainer/commands/initialize.sh",
// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "chmod +x .devcontainer/commands/postCreate.sh && .devcontainer/commands/postCreate.sh",
// Use 'postStartCommand' to run commands after the container is started.
"postStartCommand": "chmod +x .devcontainer/commands/postStart.sh && .devcontainer/commands/postStart.sh",
// Configure tool-specific properties.
"customizations": {
"vscode": {
"extensions": [
"ms-python.autopep8",
"ms-python.debugpy",
"ms-python.flake8",
"ms-python.isort",
"ms-python.pylint",
"ms-python.python",
"ms-python.vscode-pylance",
"ms-python.vscode-python-envs"
]
}
},
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
"remoteUser": "root",
"mounts": [
"source=${localEnv:HOME}/.ssh/id_ed25519_signing,target=/root/.ssh/id_ed25519_signing,type=bind,readonly"
]
}
13 changes: 11 additions & 2 deletions BUILD.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This document has detailed instructions on how to build ydb-python-sdk from source and run style and unit tests.

### Pre-requisites
## Pre-requisites

- Install [Docker](https://docs.docker.com/engine/install/).
- Install [Python](https://docs.python.org/3.8/)
Expand Down Expand Up @@ -52,4 +52,13 @@ Use the command below for regenerate protobuf code.

```sh
make protobuf
```
```

## Using Dev Containers

This repository includes Dev Containers configuration for a quick development environment setup. For general information about Dev Containers, see the [official guide](https://code.visualstudio.com/docs/devcontainers/containers).

The dev container automatically sets up:
- A local YDB database instance for testing
- YDB CLI with a pre-configured profile named `local` that connects to the database
- All Python dependencies and development tools
4 changes: 3 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

YDB is a free and open project and we appreciate to receive contributions from our community.

For information about setting up your development environment, please see [BUILD.md](BUILD.md).

## Contributing code changes

If you would like to contribute a new feature or a bug fix, please discuss your idea first on the GitHub issue.
Expand All @@ -19,4 +21,4 @@ if any changes are needed, we would love to work with you to get your pull reque

## Other questions

If you have any questions, please mail us at [email protected].
If you have any questions, please mail us at [email protected].
22 changes: 22 additions & 0 deletions compose.remote.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
networks:
default:
name: ydb-python-sdk_default
external: true

services:
py-sdk-ydb:
image: ydbplatform/local-ydb:trunk
restart: always
hostname: py-sdk-ydb
ports:
- 2135
- 2136
- 8765
volumes:
- ./ydb_certs:/ydb_certs
environment:
- YDB_USE_IN_MEMORY_PDISKS=true
- YDB_ENABLE_COLUMN_TABLES=true
- GRPC_TLS_PORT=2135
- GRPC_PORT=2136
- MON_PORT=8765
13 changes: 8 additions & 5 deletions docker-compose-tls.yml → compose.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
version: "3.9"
services:
ydb:
py-sdk-ydb:
image: ydbplatform/local-ydb:trunk
restart: always
ports:
- 2136:2136
- 2135:2135
hostname: localhost
ports:
- 2135
- 2136
- 8765
volumes:
- ./ydb_certs:/ydb_certs
environment:
- YDB_USE_IN_MEMORY_PDISKS=true
- YDB_ENABLE_COLUMN_TABLES=true
- GRPC_TLS_PORT=2135
- GRPC_PORT=2136
- MON_PORT=8765
11 changes: 0 additions & 11 deletions docker-compose.yml

This file was deleted.

4 changes: 2 additions & 2 deletions test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pycparser==2.20
PyNaCl==1.4.0
pyparsing==2.4.7
pyrsistent==0.18.0
pytest<8.0.0
pytest<9.0.0
pytest-asyncio==0.21.0
pytest-docker-compose==3.2.1
python-dotenv==0.18.0
Expand All @@ -46,6 +46,6 @@ sqlalchemy==1.4.26
pylint-protobuf
cython
freezegun==1.2.2
pytest-cov
# pytest-cov
yandexcloud
-e .
25 changes: 20 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

@pytest.fixture(scope="module")
def docker_compose_file(pytestconfig):
return os.path.join(str(pytestconfig.rootdir), "docker-compose.yml")
f = "compose.yml"
if os.environ.get("REMOTE_CONTAINERS") is not None:
f = "compose.remote.yml"

return os.path.join(str(pytestconfig.rootdir), f)


def wait_container_ready(driver):
Expand All @@ -32,9 +36,14 @@ def wait_container_ready(driver):

@pytest.fixture(scope="module")
def endpoint(pytestconfig, module_scoped_container_getter):
with ydb.Driver(endpoint="localhost:2136", database="/local") as driver:
e = "grpc://localhost:2136"
if os.environ.get("REMOTE_CONTAINERS") is not None:
e = "grpc://py-sdk-ydb:2136"

with ydb.Driver(endpoint=e, database="/local") as driver:
wait_container_ready(driver)
yield "localhost:2136"

yield e


@pytest.fixture(scope="session")
Expand All @@ -47,13 +56,19 @@ def secure_endpoint(pytestconfig, session_scoped_container_getter):

assert os.path.exists(ca_path)
os.environ["YDB_SSL_ROOT_CERTIFICATES_FILE"] = ca_path

e = "grpcs://localhost:2135"
if os.environ.get("REMOTE_CONTAINERS") is not None:
e = "grpcs://py-sdk-ydb:2135"

with ydb.Driver(
endpoint="grpcs://localhost:2135",
endpoint=e,
database="/local",
root_certificates=ydb.load_ydb_root_certificate(),
) as driver:
wait_container_ready(driver)
yield "localhost:2135"

yield e


@pytest.fixture(scope="module")
Expand Down
4 changes: 2 additions & 2 deletions tests/ssl/test_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
@pytest.mark.tls
def test_connect_secure(secure_endpoint, database):
with ydb.Driver(
endpoint="grpcs://localhost:2135",
database="/local",
endpoint=secure_endpoint,
database=database,
root_certificates=ydb.load_ydb_root_certificate(),
) as driver:
driver.wait(timeout=10)
Expand Down
Loading
Loading