From 36a504077dd75927f15483813bba3a2f1bcff34c Mon Sep 17 00:00:00 2001 From: devops Date: Wed, 16 Sep 2026 16:26:15 +0000 Subject: [PATCH] hello-api: platform se scaffold --- .trivyignore | 2 ++ Dockerfile | 19 +++++++++++++++++++ Jenkinsfile | 9 +++++++++ app/__init__.py | 0 app/main.py | 15 +++++++++++++++ requirements-dev.txt | 2 ++ requirements.txt | 2 ++ sonar-project.properties | 4 ++++ tests/test_app.py | 11 +++++++++++ 9 files changed, 64 insertions(+) create mode 100644 .trivyignore create mode 100644 Dockerfile create mode 100644 Jenkinsfile create mode 100644 app/__init__.py create mode 100644 app/main.py create mode 100644 requirements-dev.txt create mode 100644 requirements.txt create mode 100644 sonar-project.properties create mode 100644 tests/test_app.py diff --git a/.trivyignore b/.trivyignore new file mode 100644 index 0000000..c275d11 --- /dev/null +++ b/.trivyignore @@ -0,0 +1,2 @@ +# Yahan sirf wo CVE daalo jinka business ne risk accept kiya hai. +# Format: CVE-2024-XXXXX diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e1d421e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM python:3.12-slim + +# Base image purana ho sakta hai. Security updates yahin lagao warna +# pipeline ka Trivy gate HIGH/CRITICAL par build rok dega. +RUN apt-get update && apt-get upgrade -y \ + && apt-get autoremove -y --purge \ + && rm -rf /var/lib/apt/lists/* + +WORKDIR /srv +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt +COPY app/ ./app/ +ARG APP_VERSION=dev +ENV APP_VERSION=${APP_VERSION} +RUN useradd -r -u 1001 appuser +USER appuser +EXPOSE 8000 +HEALTHCHECK --interval=30s --timeout=3s CMD python -c "import urllib.request;urllib.request.urlopen('http://127.0.0.1:8000/health')" +CMD ["gunicorn", "--bind", "0.0.0.0:8000", "--workers", "2", "app.main:app"] diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..4ecdb08 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,9 @@ +// Poori pipeline devops-platform shared library me hai. +@Library('devops-platform') _ + +devopsPipeline( + module: 'hello-api', + gitops: true, + deployVM: false, + k8sVerifyUrl: 'http://192.168.124.179:30894/version' +) diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/main.py b/app/main.py new file mode 100644 index 0000000..626242d --- /dev/null +++ b/app/main.py @@ -0,0 +1,15 @@ +import os +from flask import Flask, jsonify + +app = Flask(__name__) + + +@app.get("/health") +def health(): + return jsonify(status="ok") + + +@app.get("/version") +def version(): + # Pipeline ka verify stage isi field ko check karta hai. + return jsonify(app="hello-api", version=os.environ.get("APP_VERSION", "dev")) diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 0000000..406a7a5 --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1,2 @@ +pytest +flask diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e4a286c --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +flask +gunicorn diff --git a/sonar-project.properties b/sonar-project.properties new file mode 100644 index 0000000..593815e --- /dev/null +++ b/sonar-project.properties @@ -0,0 +1,4 @@ +sonar.projectKey=swim-hello-api +sonar.sources=app +sonar.tests=tests +sonar.python.version=3.12 diff --git a/tests/test_app.py b/tests/test_app.py new file mode 100644 index 0000000..56f0d37 --- /dev/null +++ b/tests/test_app.py @@ -0,0 +1,11 @@ +from app.main import app + + +def test_health(): + c = app.test_client() + assert c.get("/health").status_code == 200 + + +def test_version(): + c = app.test_client() + assert c.get("/version").get_json()["app"] == "hello-api"