Skip to content

Commit

Permalink
new
Browse files Browse the repository at this point in the history
  • Loading branch information
DoyunShin committed May 2, 2024
1 parent fed5e13 commit 0479078
Show file tree
Hide file tree
Showing 7 changed files with 885 additions and 0 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/docker-image-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Release Docker image to ghcr.io

on:
push:
tags: [ 'v*.*.*' ]

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}


jobs:
build:
runs-on: ubuntu-latest

permissions:
contents: read
packages: write

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Log in to Container registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Get metadata
id: metadata
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

- name: Build and push Docker image
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
push: true
tags: ${{ steps.metadata.outputs.tags }}
labels: ${{ steps.metadata.outputs.labels }}
26 changes: 26 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
FROM ubuntu:24.04

# Install dependencies
RUN DEBIAN_FRONTEND=noninteractive apt-get update && apt-get install -y \
git \
vim \
curl \
wget \
unzip \
openssh-server \
sudo

RUN useradd -ms /bin/bash sparcs && adduser sparcs sudo && echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers

USER sparcs
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash

RUN echo 'export NVM_DIR="$HOME/.nvm"' >> ~/.bashrc && \
echo '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> ~/.bashrc && \
echo '[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"' >> ~/.bashrc

USER root
ADD docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh

ENTRYPOINT ["/docker-entrypoint.sh"]
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions docker-compose.deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
version: '3'

services:
app:
image: ghcr.io/sparcs-kaist/newbie-image:latest
restart: always
ports:
- "15000:22"
- "15001:3000"
- "15002:8000"
volumes:
- "./home:/home/sparcs"
environment:
- NEW_PASSWORD=||NEW_PASSWORD||
depends_on:
- db

db:
image: mysql:8.4.0
restart: always
volumes:
- data:/data/db
environment:
- MYSQL_ROOT_PASSWORD=||MYSQL_ROOT_PASSWORD||
- MYSQL_DATABASE=db

volumes:
data:
29 changes: 29 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
version: '3'

services:
app:
build:
dockerfile: Dockerfile
restart: always
ports:
- "15000:22"
- "15001:3000"
- "15002:8000"
volumes:
- "./home:/home/sparcs"
environment:
- NEW_PASSWORD=1234
depends_on:
- db

db:
image: mysql:8.4.0
restart: always
volumes:
- data:/data/db
environment:
- MYSQL_ROOT_PASSWORD=1234
- MYSQL_DATABASE=db

volumes:
data:
6 changes: 6 additions & 0 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

echo "sparcs:$NEW_PASSWORD" | chpasswd
mkdir -p /var/run/sshd
/usr/sbin/sshd -D
tail -f /dev/null
78 changes: 78 additions & 0 deletions manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from pathlib import Path
from urllib.request import urlopen
import string
import random
import os

USERS = ["Alice", "Bob", "Charlie"]
DOCKERPATH = Path("./newbie")
MYSQLPASSWD = ''
PASSLENGTH = 32

users = []
deployComposePath = Path("docker-compose.deploy.yml")

def create():
DOCKERPATH.mkdir(exist_ok=True)
if deployComposePath.exists():
deploy = deployComposePath.read_text()
else:
url = urlopen("https://raw.githubusercontent.com/sparcs-kaist/newbie-image/main/docker-compose.deploy.yml")
deployComposePath.write_text(url.read().decode())

for user in USERS:
userPath = DOCKERPATH / user
userPath.mkdir(exist_ok=True)
userComposePath = userPath / "docker-compose.yml"

upass = ''.join(random.choices(string.ascii_letters + string.digits, k=PASSLENGTH))
uc = deploy.replace("||NEW_PASSWORD||", upass)
uc = uc.replace("||MYSQL_ROOT_PASSWORD||", MYSQLPASSWD)
userComposePath.write_text(uc)
users.append(userPath)

def stop():
for user in users:
if not (user / "docker-compose.yml").exists():
raise FileNotFoundError(f"docker-compose.yml not found for {user}")

os.system(f"cd {user} && docker-compose down")

def start():
for user in users:
if not (user / "docker-compose.yml").exists():
raise FileNotFoundError(f"docker-compose.yml not found for {user}")

os.system(f"cd {user} && docker-compose up -d --build")

def getpass():
for user in users:
if not (user / "docker-compose.yml").exists():
raise FileNotFoundError(f"docker-compose.yml not found for {user}")
passwd = (user / "docker-compose.yml").read_text().split("MYSQL_PASSWORD=")[1].split("\n")[0]
print(f"{user}:{passwd}")

def main():
while True:
print("1. Create docker-compose files")
print("2. Start docker-compose")
print("3. Stop docker-compose")
print("4. Get passwords")
print("5. Exit")
choice = input("Enter your choice: ")
if choice == "1":
create()
elif choice == "2":
start()
elif choice == "3":
stop()
elif choice == "4":
getpass()
elif choice == "5":
break
else:
print("Invalid choice")


if __name__ == "__main__":
main()

0 comments on commit 0479078

Please sign in to comment.