Skip to content
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
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

Samples and Hints/Problem 3 /README.md
Samples and Hints/Problem 3 /README.md
Samples and Hints/Problem 3 /README.md
Samples and Hints/Problem 4/README.md
Samples and Hints/Problem 3 /README.md
Samples and Hints/Problem 1/README.md
Samples and Hints/Problem 2/README.md
README.md
Samples and Hints/Problem 1/README.md
Samples and Hints/Problem 2/README.md
Samples and Hints/Problem 3 /README.md
Samples and Hints/Problem 4/README.md
Samples and Hints/Problem 5/README.md
63 changes: 63 additions & 0 deletions Chatraei-Raoufimanesh/Problem4_WebAPI/Implmentation/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from fastapi import FastAPI, Depends
from tasks import start_container, stop_container
from sqlalchemy import create_engine, text
from sqlalchemy.orm import sessionmaker

DATABASE_URL = "postgresql://mot:mypassword@localhost:5432/mydb"
engine = create_engine(DATABASE_URL)
SessionLocal = sessionmaker(bind=engine)

app = FastAPI()

def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()

@app.post("/assign")
async def assign_challenge(team_id, challenge_id, db=Depends(get_db)):
image_name = "python:3.12"
task = start_container.delay(image_name, team_id, challenge_id)
result = task.get(timeout=20)

create_table_sql = text("""
CREATE TABLE IF NOT EXISTS assignments (
id SERIAL PRIMARY KEY,
team_id VARCHAR(50),
challenge_id VARCHAR(50),
container_id VARCHAR(100),
container_address VARCHAR(100)
)
""")
db.execute(create_table_sql)

insert_sql = text("""
INSERT INTO assignments (team_id, challenge_id, container_id, container_address)
VALUES (:team_id, :challenge_id, :container_id, :container_address)
""")
db.execute(insert_sql, {
"team_id": team_id,
"challenge_id": challenge_id,
"container_id": result['container_id'],
"container_address": result['container_address']
})
db.commit()

return {"status": "assigned", "container_address": result['container_address']}

@app.post("/remove")
async def remove_challenge(team_id, challenge_id, db=Depends(get_db)):
query = text("SELECT container_id FROM assignments WHERE team_id=:team_id AND challenge_id=:challenge_id")
container = db.execute(query, {"team_id": team_id, "challenge_id": challenge_id}).fetchone()
if not container:
return {"status": "not found"}

stop_container.delay(container[0])

delete_query = text("DELETE FROM assignments WHERE team_id=:team_id AND challenge_id=:challenge_id")
db.execute(delete_query, {"team_id": team_id, "challenge_id": challenge_id})
db.commit()

return {"status": "removed"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fastapi
sqlalchemy
psycopg2-binary
celery
redis
docker
24 changes: 24 additions & 0 deletions Chatraei-Raoufimanesh/Problem4_WebAPI/Implmentation/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from celery import Celery
import docker

celery_app = Celery(
'tasks',
broker='redis://localhost:6379/0',
backend='redis://localhost:6379/0'
)

client = docker.from_env()

@celery_app.task
def start_container(image_name, team_id, challenge_id):
container = client.containers.run(image_name, detach=True, ports={'80/tcp': None})
container.reload()
container_ip = container.attrs['NetworkSettings']['IPAddress']
return {'container_id': container.id, 'container_address': container_ip}

@celery_app.task
def stop_container(container_id):
container = client.containers.get(container_id)
container.stop()
container.remove()
return {'status': 'removed'}
Loading