Skip to content

Commit 9438083

Browse files
First Commit
1 parent 46bebad commit 9438083

File tree

10 files changed

+192
-0
lines changed

10 files changed

+192
-0
lines changed

.github/workflows/ci-cd.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: CI/CD Pipeline
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v2
15+
16+
- name: Set up Python
17+
uses: actions/setup-python@v2
18+
with:
19+
python-version: "3.9"
20+
21+
- name: Install dependencies
22+
run: |
23+
pip install -r requirements.txt
24+
25+
- name: Run tests
26+
run: |
27+
pytest
28+
29+
- name: Build Docker image
30+
run: |
31+
docker build -t myapp .
32+
33+
- name: Push Docker image
34+
run: |
35+
docker push myapp
36+
37+
- name: Deploy to server
38+
run: |
39+
ssh user@server 'bash -s' < scripts/deploy.sh

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# README for DevOps Python Platform
2+
3+
## Overview
4+
5+
This project is a Python API that reads and writes to a database, fully automated on a Linux server, using Bash scripts for operations tasks and CI/CD for deployment.
6+
7+
## Tech Stack
8+
9+
- **Linux (Ubuntu Server)**
10+
- **Bash** (automation & deployment)
11+
- **Python** (Flask or FastAPI)
12+
- **PostgreSQL** (or MySQL)
13+
- **Docker**
14+
- **GitHub Actions**
15+
- **Nginx**
16+
- **Systemd**
17+
- **AWS EC2 (or any VPS)**
18+
19+
## Architecture
20+
21+
```
22+
User
23+
24+
Nginx (Linux)
25+
26+
Python API (Docker)
27+
28+
PostgreSQL
29+
```
30+
31+
## Core Features
32+
33+
- **REST API (CRUD)**
34+
- **/health endpoint**
35+
- **Reads & writes to DB**
36+
- **Uses env vars (12-factor style)**
37+
38+
## Scripts
39+
40+
- `deploy.sh` – pull image & restart service
41+
- `backup_db.sh` – scheduled DB backups
42+
- `health_check.sh` – service monitoring
43+
- `log_rotate.sh` – clean old logs
44+
- `setup_server.sh` – one-command server bootstrap
45+
46+
## CI/CD Pipeline
47+
48+
- Run Python tests
49+
- Lint code
50+
- Build Docker image
51+
- Push image to registry
52+
- SSH into Linux server
53+
- Run Bash deploy script
54+
55+
## Linux System Design
56+
57+
- systemd service for API
58+
- Nginx as reverse proxy
59+
- UFW firewall
60+
- Non-root user
61+
- SSH key auth only

TODO.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
3+
- [ ] Add a minimal API: create `app/main.py` (FastAPI) with a `/health` endpoint and a small CRUD example (SQLite or in-memory for now).
4+
- [ ] Add `requirements.txt` with `fastapi`, `uvicorn`, `pytest` (and `sqlalchemy`/`psycopg2-binary` Postgres).
5+
- [ ] Add basic tests in `app/tests/` — a health-check test and one CRUD test so CI has something to run.
6+
- [ ] Update CI (`.github/workflows/ci-cd.yml`) to install dependencies, run tests, and build/push the Docker image to the registry you choose.
7+
- [ ] Configure GitHub Secrets for deploy: `DOCKER_USERNAME`, `DOCKER_PASSWORD`, `DEPLOY_SSH_KEY` (or use names you prefer).
8+
- [ ] Run tests locally, fix any issues, then commit and push.
9+
- [ ] Optional: add a `systemd` unit file and server bootstrap docs in `scripts/` for automated server setup.
10+
11+
Quick commands I'll run locally once the app exists:
12+
13+
```bash
14+
python -m venv .venv
15+
# Windows PowerShell: .venv\\Scripts\\Activate.ps1
16+
# macOS/Linux: source .venv/bin/activate
17+
pip install -r requirements.txt
18+
uvicorn app.main:app --reload
19+
pytest
20+
```

docker-compose.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
version: "3.8"
2+
3+
services:
4+
app:
5+
build:
6+
context: ./docker
7+
ports:
8+
- "5000:5000"
9+
environment:
10+
- DATABASE_URL=postgresql://user:password@db:5432/mydatabase
11+
depends_on:
12+
- db
13+
14+
db:
15+
image: postgres:latest
16+
environment:
17+
POSTGRES_USER: user
18+
POSTGRES_PASSWORD: password
19+
POSTGRES_DB: mydatabase
20+
volumes:
21+
- db_data:/var/lib/postgresql/data
22+
23+
volumes:
24+
db_data:

docker/Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Dockerfile for Python API
2+
3+
FROM python:3.9-slim
4+
5+
WORKDIR /app
6+
7+
COPY requirements.txt .
8+
RUN pip install --no-cache-dir -r requirements.txt
9+
10+
COPY . .
11+
12+
CMD ["python", "main.py"]

nginx/default.conf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
server {
2+
listen 80;
3+
server_name localhost;
4+
5+
location / {
6+
proxy_pass http://app:5000;
7+
proxy_set_header Host $host;
8+
proxy_set_header X-Real-IP $remote_addr;
9+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
10+
proxy_set_header X-Forwarded-Proto $scheme;
11+
}
12+
}

scripts/backup_db.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
# backup_db.sh - Backup the PostgreSQL database
3+
4+
pg_dump -U user -h localhost mydatabase > backup_$(date +%Y%m%d).sql

scripts/deploy.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
# deploy.sh - Pull the latest Docker image and restart the service
3+
4+
docker-compose pull
5+
docker-compose up -d --remove-orphans

scripts/health_check.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
# health_check.sh - Check the health of the API
3+
4+
if curl -s http://localhost:5000/health | grep -q 'healthy'; then
5+
echo "API is healthy"
6+
else
7+
echo "API is down"
8+
fi

scripts/setup_server.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
# setup_server.sh - Bootstrap the server
3+
4+
apt update && apt upgrade -y
5+
apt install -y nginx docker.io docker-compose
6+
systemctl enable docker
7+
systemctl start docker

0 commit comments

Comments
 (0)