From b64082f62671ee2ff6a06d19423f5475f4cd8e8f Mon Sep 17 00:00:00 2001 From: Jack Hadrill Date: Wed, 24 Aug 2022 23:56:49 +0000 Subject: [PATCH] Experimental --- .drone.yml | 48 +++++++++++++++++++++++++++++++ .flake8 | 2 ++ .pylintrc | 2 ++ .vscode/settings.json | 10 +++++++ README.md | 44 +++++++++++++++++++++++++++- pyproject.toml | 21 ++++++++++++++ setup.cfg | 3 ++ setup.py | 10 +++++++ src/container_spawner/__init__.py | 0 src/container_spawner/__main__.py | 0 test/test_main.py | 2 ++ 11 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 .drone.yml create mode 100644 .flake8 create mode 100644 .pylintrc create mode 100644 .vscode/settings.json create mode 100644 pyproject.toml create mode 100644 setup.cfg create mode 100644 setup.py create mode 100644 src/container_spawner/__init__.py create mode 100644 src/container_spawner/__main__.py create mode 100644 test/test_main.py diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..54b9f3c --- /dev/null +++ b/.drone.yml @@ -0,0 +1,48 @@ +kind: pipeline +type: docker +name: lint +steps: + - name: lint + image: python:3-slim + commands: + - echo "Installing lint dependencies..." + - pip install --no-cache-dir .[lint] > /dev/null + - echo "Running flake8..." + - flake8 src + - echo "Running pylint..." + - pylint src +--- +kind: pipeline +type: docker +name: test + - name: test + image: python:3-slim + commands: + - echo "Installing test dependencies..." + - pip install --no-cache-dir .[test] > /dev/null + - echo "Running tests..." + - pytest +--- +kind: pipeline +type: docker +name: build-deploy +depends_on: + - lint + - test +steps: + - name: build + image: python:3-slim + commands: + - pip install --no-cache-dir build + - python -m build --wheel + - name: publish + image: plugins/pypi + when: + branch: + - master + settings: + username: jackhadrill + password: + from_secret: password + repository: https://git.jacknet.io/api/packages/jackhadrill/pypi + skip_build: true \ No newline at end of file diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..6deafc2 --- /dev/null +++ b/.flake8 @@ -0,0 +1,2 @@ +[flake8] +max-line-length = 120 diff --git a/.pylintrc b/.pylintrc new file mode 100644 index 0000000..7615b86 --- /dev/null +++ b/.pylintrc @@ -0,0 +1,2 @@ +[FORMAT] +max-line-length=120 diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..b730f39 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,10 @@ +{ + // The following will hide Python's build cache directories. + "files.exclude": { + "**/__pycache__": true, + "**/*.egg-info": true, + "**/.pytest_cache": true + }, + "python.linting.enabled": true, + "python.linting.pylintEnabled": true +} diff --git a/README.md b/README.md index 1f07412..60d4759 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,44 @@ -# code-spawner +# Code Spawner +[![Build Status](https://drone.jacknet.io/api/badges/jackhadrill/container-spawner/status.svg)](https://drone.jacknet.io/jackhadrill/container-spawner) + +A tool for spawning a bespoke container upon receiving an HTTP request. + +## Usage + +More details coming soon. + +An example `docker-compose.yml` config is show below: + +```yml +version: '3' +services: + ... + container-spawner: + image: git.jacknet.io/jackhadrill/container-spawner:latest + restart: always + environment: + CONTAINER_IMAGE: "codercom/code-server:latest" + CONTAINER_PREFIX: "vscode" + CONTAINER_NETWORK: "vscode_backend" + CONTAINER_PERSIST: "/home/coder" + ... +``` + +## Process description + +Upon receiving any HTTP request, **Code Spawner** will launch a bespoke container on behalf of the user (if not already existing), equivalent to having used the command below. The user's name is derived from the `X-Forwarded-Preferred-User` header, original sent by [**OAuth2 Proxy**](https://github.com/oauth2-proxy/oauth2-proxy). + +```bash +$ docker run -d --rm --name ${CONTAINER_PREFIX}-${X-Forwarded-Preferred-User} -v ${CONTAINER_PREFIX}-${X-Forwarded-Preferred-User}:${CONTAINER_PERSIST} --network ${CONTAINER_NETWORK} ${IMAGE_NAME} +``` + +For example, assuming `X-Forwarded-Preferred-User` is `jack`: + +```bash +$ export CONTAINER_IMAGE="codercom/code-server:latest" +$ export CONTAINER_PREFIX="vscode" +$ export CONTAINER_NETWORK="vscode_backend" +$ export CONTAINER_PERSIST="/home/coder" +$ docker run -d --rm --name vscode-jack -v vscode-jack:/home/coder --network vscode_backend codercom/code-server:latest +``` diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..c8676ed --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,21 @@ +[build-system] +requires = ["setuptools", "wheel"] +build-backend = "setuptools.build_meta" + +[project] +name = "containerspawner" +description = "A tool for spawning containers." +readme = "README.md" +requires-python = ">=3.9" +license = {text = "MIT License"} +dependencies = [] +dynamic = ["version"] + +[project.optional-dependencies] +lint = [ + "pylint>=2.14.5", + "flake8>=5.0.4" +] +test = [ + "pytest>=7.1.2" +] diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..2e97f84 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,3 @@ +[metadata] +name = containerspawner +version = 0.0.0 diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..430fb1f --- /dev/null +++ b/setup.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python3 + +# N.B. This is here for legacy support in order to support editable installs. Please see: +# https://setuptools.pypa.io/en/latest/userguide/pyproject_config.html#pep660-status +# https://setuptools.pypa.io/en/latest/userguide/pyproject_config.html#setupcfg-caveats + +from setuptools import setup + + +setup() diff --git a/src/container_spawner/__init__.py b/src/container_spawner/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/container_spawner/__main__.py b/src/container_spawner/__main__.py new file mode 100644 index 0000000..e69de29 diff --git a/test/test_main.py b/test/test_main.py new file mode 100644 index 0000000..52c2135 --- /dev/null +++ b/test/test_main.py @@ -0,0 +1,2 @@ +def test_main(): + assert True \ No newline at end of file