Skip to content
Open

Lab03 #2422

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
93 changes: 93 additions & 0 deletions .github/workflows/python-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
name: Python CI + Docker Build

on:
push:
branches:
- main
- lab03
tags:
- 'v*.*.*'
pull_request:
branches:
- main

jobs:
test:
name: Lint and Test
runs-on: ubuntu-latest

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

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.11"

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r app_python/requirements.txt
pip install pytest flake8

- name: Cache dev dependencies
uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-dev-${{ hashFiles('app_python/requirements-dev.txt') }}
restore-keys: |
${{ runner.os }}-pip-dev-

- name: Install dev dependencies
run: pip install -r app_python/requirements-dev.txt

- name: Run Snyk security scan
uses: snyk/actions/python@master
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
with:
args: --file=app_python/requirements.txt --severity-threshold=high --skip-unresolved

- name: Run linter
run: |
flake8 app_python

- name: Run tests
run: |
pytest app_python

docker:
name: Build and Push Docker Image
runs-on: ubuntu-latest
needs: test

if: startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/lab03'

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

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

- name: Extract Docker metadata (tags)
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ secrets.DOCKERHUB_USERNAME }}/devops-info-service
tags: |
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=raw,value=latest

- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: ./app_python
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
test
test
.idea
18 changes: 18 additions & 0 deletions app_python/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
__pycache__/
*.pyc
*.pyo
*.pyd

.git/
.gitignore

.env
.venv/
venv/

.idea/
.vscode/

tests/
docs/
README.md
11 changes: 11 additions & 0 deletions app_python/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Python
__pycache__/
*.py[cod]
venv/
*.log

# IDE
.idea/

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

ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1

RUN useradd --create-home --shell /bin/bash appuser

WORKDIR /app

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"]
150 changes: 150 additions & 0 deletions app_python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
# DevOps Info Service

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

## Overview

DevOps Info Service is a lightweight HTTP service that exposes runtime, system, and application information. The service is suitable for containerization, health monitoring, and deployment in orchestration platforms such as Kubernetes.

The application provides:

* Service metadata (name, version, framework)
* System information (OS, architecture, CPU count, Python version)
* Runtime information (uptime, current time)
* Health check endpoint for monitoring

---

## Prerequisites

* Python **3.10+** (recommended: 3.12 or newer)
* pip (Python package manager)
* Virtual environment support (`venv`)

To check the Python version:

```bash
python3 --version
```

---

## Installation

Clone the repository and navigate to the project directory:

```bash
cd app_python
```

Create and activate a virtual environment:

```bash
python3 -m venv venv
source venv/bin/activate
```

Install dependencies:

```bash
pip install -r requirements.txt
```

---

## Running the Application

Run with default configuration:

```bash
python3 app.py
```

Run with custom configuration:

```bash
PORT=8080 python3 app.py
HOST=127.0.0.1 PORT=3000 python3 app.py
```

By default, the service will be available at:

```
http://localhost:5000
```

---

## API Endpoints

### GET /

Returns service, system, runtime, and request information.

Example:

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

### GET /health

Health check endpoint for monitoring and readiness probes.

Example:

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

---

## Configuration

The application can be configured using environment variables.

| Variable | Description | Default |
| -------- | ------------------- | ------- |
| HOST | Server bind address | 0.0.0.0 |
| PORT | Server port | 5000 |
| DEBUG | Enable debug mode | False |

Example:

```bash
DEBUG=true PORT=8080 python3 app.py
```

---

## Notes

* This project follows Python best practices and PEP 8 style guidelines.
* Dependencies are pinned for reproducibility.
* The `/health` endpoint is suitable for Kubernetes liveness and readiness probes.

## Docker

The application can also be run as a Docker container.

### Build the image locally

```bash
docker build -t <image-name> .
```

### Run the container

```bash
docker run -p <host-port>:5000 <image-name>
```

### Pull from Docker Hub

```bash
docker pull <dockerhub-username>/<image-name>:<tag>
docker run -p <host-port>:5000 <dockerhub-username>/<image-name>:<tag>
```

These commands demonstrate the general usage pattern. Replace placeholders with your actual image name, Docker Hub username, port, and tag as needed.

Empty file added app_python/__init__.py
Empty file.
Loading