Skip to content
Closed
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
68 changes: 68 additions & 0 deletions .github/workflows/publish-docker-images.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: Publish Docker images to GitHub Container Registry

on:
push:
branches:
- main
- develop

env:
REGISTRY: ghcr.io

jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

strategy:
fail-fast: false
matrix:
include:
# Configuration for the API
- context: ./iot-simulator-api
image-name: manager-api
dockerfile: ./iot-simulator-api/Dockerfile

# Configuration for the Simulator
# (Assuming 'mqtt-simulator-master' is the folder name)
- context: ./mqtt-simulator-master
image-name: mqtt-simulator
dockerfile: ./mqtt-simulator-master/Dockerfile

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

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

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
# This creates the full image name: ghcr.io/username/image-name
images: ${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ matrix.image-name }}
tags: |
type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' }}
type=sha
type=ref,event=branch

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: ${{ matrix.context }}
file: ${{ matrix.dockerfile }}
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
# Enable multi-arch builds if needed (optional)
# platforms: linux/amd64,linux/arm64
2 changes: 1 addition & 1 deletion .github/workflows/restrict-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ jobs:
- name: Ensure PR is from develop
run: |
if [ "${{ github.head_ref }}" != "develop" ]; then
echo "❌ Pull requests to main must come from develop"
echo " Pull requests to main must come from develop"
exit 1
fi
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ wheels/
venv/
ENV/
env/

myenv/
venv.bak/
# Environment variables
.env

Expand All @@ -38,3 +39,4 @@ env/
*.swo
*~
api/src/simulations.db
iot-simulator-api/src/simulations.db
9 changes: 9 additions & 0 deletions iot-simulator-api/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
node_modules
.git
.env
.DS_Store
__pycache__
*.pyc
.vscode
*.log
db/
20 changes: 20 additions & 0 deletions iot-simulator-api/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Python 3.11 slim image
FROM python:3.11-slim

# Set working directory
WORKDIR /code

# Copy requirements first to leverage Docker cache
COPY ./src/requirements.txt /code/requirements.txt

# Install dependencies
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt

# Copy source code
COPY ./src /code

# Expose port 80
EXPOSE 80

# Run with standard FastAPI command (Production mode)
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80"]
Empty file.
22 changes: 16 additions & 6 deletions iot-simulator-api/src/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from datetime import datetime
import os

SQLALCHEMY_DATABASE_URL = "sqlite:///./simulations.db"
# Ensure the 'db' folder exists, otherwise sqlite fails
if not os.path.exists("./db"):
os.makedirs("./db")

SQLALCHEMY_DATABASE_URL = "sqlite:///./db/simulations.db"

engine = create_engine(
SQLALCHEMY_DATABASE_URL,
Expand All @@ -17,10 +22,15 @@ class Simulation(Base):

id = Column(Integer, primary_key=True, index=True)
simulation_id = Column(String, unique=True, index=True, nullable=False)

# In K8s, this will store the Pod Name (e.g., "sim-abc-123")
container_id = Column(String, nullable=True)
config_path = Column(String, nullable=False)
config_json = Column(Text, nullable=False) # JSON como texto
status = Column(String, default="running") # running, stopped, failed, expired


config_path = Column(String, nullable=True)

config_json = Column(Text, nullable=False) # JSON stored as text
status = Column(String, default="running")
created_at = Column(DateTime, default=datetime.utcnow)
stopped_at = Column(DateTime, nullable=True)
expires_at = Column(DateTime, nullable=False)
Expand All @@ -29,10 +39,10 @@ class Simulation(Base):
def __repr__(self):
return f"<Simulation {self.simulation_id} - {self.status}>"

# Criar tabelas
# Create tables
Base.metadata.create_all(bind=engine)

# Dependency para obter sessão
# Dependency to get session
def get_db():
db = SessionLocal()
try:
Expand Down
Empty file.
Binary file not shown.
Loading
Loading