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
Binary file added .DS_Store
Binary file not shown.
89 changes: 89 additions & 0 deletions .github/workflows/python-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
name: Python CI

on:
push:
branches: [master, main, lab03]
pull_request:
branches: [master, main, lab03]

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

env:
REGISTRY: docker.io
IMAGE_NAME: almaxgood/devops-info-service

jobs:
test:
runs-on: ubuntu-latest
defaults:
run:
working-directory: app_python
steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
cache: 'pip'
cache-dependency-path: app_python/requirements*.txt

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt -r requirements-dev.txt

- name: Run ruff
run: ruff check .

- name: Run tests
run: pytest tests/ -v

- name: Run Snyk
uses: snyk/actions/python@master
continue-on-error: true
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
with:
args: --severity-threshold=high

build:
needs: test
runs-on: ubuntu-latest
if: github.event_name == 'push' && (github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main' || github.ref == 'refs/heads/lab03')
steps:
- uses: actions/checkout@v4

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

- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Get version
id: version
run: echo "VERSION=$(date +%Y.%m)" >> $GITHUB_OUTPUT

- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.IMAGE_NAME }}
tags: |
type=raw,value=${{ steps.version.outputs.VERSION }}
type=raw,value=latest

- name: Build and push
uses: docker/build-push-action@v6
with:
context: ./app_python
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
25 changes: 25 additions & 0 deletions app_python/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
__pycache__
*.pyc
*.pyo
*.pyd
.Python
*.so
*.egg
*.egg-info
dist
build
.git
.gitignore
venv
.venv
env
.env
*.md
docs
tests
.pytest_cache
.coverage
htmlcov
.vscode
.idea
*.log
18 changes: 18 additions & 0 deletions app_python/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Python
__pycache__/
*.py[cod]
*.pyo
*.pyd
venv/
env/
*.log

# IDE
.vscode/
.idea/
*.swp
*.swo

# OS
.DS_Store
Thumbs.db
18 changes: 18 additions & 0 deletions app_python/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM python:3.13-slim

WORKDIR /app

RUN groupadd -r appuser && useradd -r -g appuser appuser

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY app.py .

RUN chown -R appuser:appuser /app

USER appuser

EXPOSE 5000

CMD ["python", "app.py"]
188 changes: 188 additions & 0 deletions app_python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
# DevOps Info Service

[![Python CI](https://github.com/almax07082005/DevOps-Core-Course/actions/workflows/python-ci.yml/badge.svg)](https://github.com/almax07082005/DevOps-Core-Course/actions/workflows/python-ci.yml)

A production-ready Python web service that provides comprehensive system and runtime information.

## Overview

The DevOps Info Service is a lightweight web application built to report detailed information about itself and its runtime environment. It exposes two main endpoints for service information and health checking, making it ideal for monitoring and DevOps workflows.

## Prerequisites

- Python 3.11 or higher
- pip (Python package installer)
- Virtual environment (recommended)

## Installation

1. Clone the repository and navigate to the app directory:
```bash
cd app_python
```

2. Create and activate a virtual environment:
```bash
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
```

3. Install dependencies:
```bash
pip install -r requirements.txt
```

## Running the Application

### Default Configuration

```bash
python app.py
```

The service will start on `0.0.0.0:5000` by default.

### Custom Configuration

Use environment variables to customize the service:

```bash
PORT=8080 python app.py
```

```bash
HOST=127.0.0.1 PORT=3000 DEBUG=true python app.py
```

## API Endpoints

### `GET /`

Returns comprehensive service and system information.

**Response:**
```json
{
"service": {
"name": "devops-info-service",
"version": "1.0.0",
"description": "DevOps course info service",
"framework": "Flask"
},
"system": {
"hostname": "my-laptop",
"platform": "Darwin",
"platform_version": "macOS-13.4-x86_64",
"architecture": "x86_64",
"cpu_count": 8,
"python_version": "3.11.5"
},
"runtime": {
"uptime_seconds": 3600,
"uptime_human": "1 hour, 0 minutes",
"current_time": "2026-01-07T14:30:00.000Z",
"timezone": "UTC"
},
"request": {
"client_ip": "127.0.0.1",
"user_agent": "curl/7.81.0",
"method": "GET",
"path": "/"
},
"endpoints": [
{"path": "/", "method": "GET", "description": "Service information"},
{"path": "/health", "method": "GET", "description": "Health check"}
]
}
```

### `GET /health`

Health check endpoint for monitoring and orchestration.

**Response:**
```json
{
"status": "healthy",
"timestamp": "2026-01-07T14:30:00.000Z",
"uptime_seconds": 3600
}
```

## Configuration

The service can be configured using environment variables:

| Variable | Default | Description |
|----------|---------|-------------|
| `HOST` | `0.0.0.0` | Host address to bind to |
| `PORT` | `5000` | Port number to listen on |
| `DEBUG` | `False` | Enable debug mode |

## Docker

The application is containerized and available on Docker Hub.

### Building the Image

```bash
docker build -t <your-username>/devops-info-service:latest .
```

### Running with Docker

```bash
docker run -d -p 5000:5000 --name devops-service <your-username>/devops-info-service:latest
```

### Pulling from Docker Hub

```bash
docker pull <your-username>/devops-info-service:latest
docker run -d -p 5000:5000 --name devops-service <your-username>/devops-info-service:latest
```

### Testing the Container

```bash
curl http://localhost:5000/
curl http://localhost:5000/health
```

## Testing

### Unit Tests

Install dev dependencies and run tests:

```bash
pip install -r requirements.txt -r requirements-dev.txt
pytest tests/ -v
```

With venv: `source venv/bin/activate && pytest tests/ -v`

### Manual Testing

```bash
curl http://localhost:5000/
curl http://localhost:5000/health
curl http://localhost:5000/ | jq .
```

## Architecture

The service is built with:
- **Flask 3.1**: Lightweight WSGI web framework
- **Python Standard Library**: Platform, socket, datetime modules for system introspection
- **Logging**: Structured logging for production monitoring
- **Error Handling**: Custom error handlers for 404 and 500 responses

## Development

The project follows Python best practices:
- PEP 8 compliant code style
- Type hints for better IDE support
- Modular function design
- Comprehensive error handling
- Production-ready logging
Loading