Skip to content

Commit

Permalink
init python sand box
Browse files Browse the repository at this point in the history
  • Loading branch information
VillSource committed Mar 8, 2024
0 parents commit 30b3439
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
LIMIT_CPU=20 #percents per process
LIMIT_MEMORY=5242880 #byte per process
26 changes: 26 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@


name: Publish Image to GHCR
on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
publish-image:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build the Docker image
run: |
docker build . --tag ghcr.io/untitlecms/python-sandbox:v0
docker push ghcr.io/untitlecms/python-sandbox:v0
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
id_rsa*
authorized_keys

!config/.gitkeep
24 changes: 24 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM linuxserver/openssh-server:latest
LABEL org.opencontainers.image.source = "https://github.com/UntitleCMS/Python-Sandbox"

ENV PUBLIC_KEY_FILE=/config/authorized_keys
ENV PUID=1000
ENV PGID=1000
ENV TZ=Etc/UTC
ENV SUDO_ACCESS=true
ENV USER_NAME=runner

ENV LIMIT_CPU=10
ENV LIMIT_MEMORY=1000000

RUN mkdir /sourcecode

ENV PYTHONUNBUFFERED=1
RUN apk add --update --no-cache python3 cpulimit && ln -sf python3 /usr/bin/python
RUN python3 -m ensurepip
RUN pip3 install --no-cache --upgrade pip setuptools


RUN echo "cd /sourcecode" >> /config/.profile
COPY ./scripts/run /config/run
RUN chmod 777 /config/run
2 changes: 2 additions & 0 deletions code/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
Empty file added config/.gitkeep
Empty file.
15 changes: 15 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: '3.4'

services:
sandbox:
image: ghcr.io/untitlecms/python-sandbox:v0
env_file:
- .env
build:
context: .
dockerfile: Dockerfile
volumes:
- ./code:/sourcecode
- ./config/authorized_keys:/config/authorized_keys
ports:
- "7777:2222"
4 changes: 4 additions & 0 deletions genkey.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ssh-keygen -q -f %cd%\id_rsa -b 4096 -t rsa -q -N ""

move /Y id_rsa ./config/id_rsa
move /Y id_rsa.pub ./config/authorized_keys
18 changes: 18 additions & 0 deletions scripts/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#! /bin/bash

# Check if the source file is provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <source_file.py>"
exit 1
fi

# echo '===================================================='
# echo "LIMIT_CPU\t:\t[$LIMIT_CPU]"
# echo "LIMIT_MEMORY\t:\t[$LIMIT_MEMORY]"
# echo '===================================================='

# Set maximum virtual memory size to (Limit_MEMORY bytes)
ulimit -Sv $LIMIT_MEMORY

# Run the program with CPU limit
cpulimit -l $LIMIT_CPU python -u /sourcecode/.$1

0 comments on commit 30b3439

Please sign in to comment.