From be4210f19746991627f1310d67a5c16e0dff56c7 Mon Sep 17 00:00:00 2001 From: Charlyne Wargnier-Potier Date: Thu, 17 Apr 2025 22:51:48 +0000 Subject: [PATCH] add scripts, dockerfile and readme --- z2/Dockerfile | 20 ++++++++++++++ z2/README.md | 68 +++++++++++++++++++++++++++++++++++++++++++++++ z2/prepare-app.sh | 15 +++++++++++ z2/start-app.sh | 41 ++++++++++++++++++++++++++++ z2/stop-app.sh | 46 ++++++++++++++++++++++++++++++++ 5 files changed, 190 insertions(+) create mode 100644 z2/Dockerfile create mode 100644 z2/README.md create mode 100644 z2/prepare-app.sh create mode 100644 z2/start-app.sh create mode 100644 z2/stop-app.sh diff --git a/z2/Dockerfile b/z2/Dockerfile new file mode 100644 index 0000000..08f157b --- /dev/null +++ b/z2/Dockerfile @@ -0,0 +1,20 @@ +FROM python:3.9-slim + +# Set the working directory to /app +WORKDIR /app + +# Copy requirements.txt from the app folder +COPY ./app/requirements.txt . + +# Install dependencies +RUN pip install -r requirements.txt + +# Copy the entire app folder into the container +COPY ./app /app + +# Expose port 5000 +EXPOSE 5000 + +# Run the app +CMD ["python", "app.py"] + diff --git a/z2/README.md b/z2/README.md new file mode 100644 index 0000000..cbbe481 --- /dev/null +++ b/z2/README.md @@ -0,0 +1,68 @@ +# Travel Planner Application + +This is a simple Flask-based web application that helps users plan and manage their travels, including the ability to add, view, edit, and remove trips and stages. + +## Prerequisites + +- Minikube installed and running (for Kubernetes setup). +- Docker installed (for building containers). +- Python 3.x +- PostgreSQL database (set up using Kubernetes and Docker). +- Flask library installed. + +## Setup + +To prepare and start the application, you can use the following bash scripts. + +### 1. Prepare the Application + +First, ensure you have everything set up correctly by running the `prepare-app.sh` script. This script prepares the application for deployment. + +```bash +./prepare-app.sh +``` + +### 2. Start the Application + +After preparing the application, use the `start-app.sh` script to start it. This will launch your application and give you the URL to access it at the end of the script. + +```bash +./start-app.sh +``` + +The URL to access the app will be displayed at the end of the script output. + +## Functionality + +### Travel Management + +- **Add a new trip**: Fill in the traveler name, trip name, start date, and end date. +- **View all trips**: A list of all trips is displayed. +- **Edit a trip**: You can modify the details of an existing trip. +- **Remove a trip**: Delete a trip from the database. + +### Stage Management + +- **Add a stage**: For each trip, you can add stages (cities) along with their order. +- **View stages**: The stages for each trip are listed with their order. +- **Edit a stage**: You can modify the details of each stage. +- **Remove a stage**: Delete a stage from a trip. + +## Architecture + +The application uses the following technologies: + +- **Flask** for the web framework. +- **PostgreSQL** for the database (configured to run with Kubernetes). +- **Kubernetes** for deploying the application. +- **Docker** for containerizing the app. + +## Troubleshooting + +- Ensure that the PostgreSQL database is correctly running and connected to the application. +- Check the logs if any errors occur during the start-up process. +- Make sure the Kubernetes cluster is set up correctly and all pods are running. + +## License + +This project is licensed under the MIT License. diff --git a/z2/prepare-app.sh b/z2/prepare-app.sh new file mode 100644 index 0000000..355b235 --- /dev/null +++ b/z2/prepare-app.sh @@ -0,0 +1,15 @@ +#!/bin/bash +echo "Preparing app" + +minikube start +# build image Docker +docker build -t chabooo/travel-planner:latest . + +echo "Sucessfuly built" + +# push docker image +docker push chabooo/travel-planner:latest + +echo "sucessfuly pushed" + +echo "Preparation completed." diff --git a/z2/start-app.sh b/z2/start-app.sh new file mode 100644 index 0000000..36e147c --- /dev/null +++ b/z2/start-app.sh @@ -0,0 +1,41 @@ +#!/bin/bash +echo "Start Kubernetes objects " + +# Apply Kubernetes resources +kubectl apply -f k8s/namespace.yaml +kubectl apply -f k8s/persistant-volume.yaml +kubectl apply -f k8s/persistant-volume-claim.yaml +kubectl apply -f k8s/postgres-service.yaml +kubectl apply -f k8s/statefulset.yaml +kubectl apply -f k8s/deployment.yaml +kubectl apply -f k8s/service.yaml + +echo "Kubernetes objects are applied. Waiting for pods to be ready..." + +# Wait for all pods in the namespace to be in Running state +namespace="travel-planner" +timeout=300 # Timeout in seconds +interval=5 # Interval between checks + +for ((i=1; i<=$timeout; i+=$interval)); do + ready_pods=$(kubectl get pods -n $namespace --field-selector=status.phase=Running --no-headers | wc -l) + total_pods=$(kubectl get pods -n $namespace --no-headers | wc -l) + + if [ "$ready_pods" -eq "$total_pods" ] && [ "$total_pods" -gt 0 ]; then + echo "All pods are ready!" + break + fi + + echo "Waiting for pods to be ready... ($ready_pods/$total_pods)" + sleep $interval +done + +if [ "$ready_pods" -ne "$total_pods" ]; then + echo "Timeout reached. Some pods are not ready." + kubectl get pods -n $namespace + exit 1 +fi + +echo "App is running." +echo "Go here to view the app" +minikube service travel-planner -n travel-planner --url diff --git a/z2/stop-app.sh b/z2/stop-app.sh new file mode 100644 index 0000000..d727928 --- /dev/null +++ b/z2/stop-app.sh @@ -0,0 +1,46 @@ +#!/bin/bash +echo "Stopping Kubernetes objects..." + +# Namespace variable +namespace="travel-planner" + +# Delete resources in the namespace +kubectl delete -f k8s/service.yaml -n $namespace +kubectl delete -f k8s/deployment.yaml -n $namespace +kubectl delete -f k8s/postgres-service.yaml -n $namespace +kubectl delete -f k8s/persistant-volume-claim.yaml -n $namespace +kubectl delete -f k8s/persistant-volume.yaml -n $namespace +kubectl delete -f k8s/statefulset.yaml -n $namespace + +# Delete namespace last +kubectl delete -f k8s/namespace.yaml + +# Check if namespace is fully deleted +timeout=60 +for ((i=1; i<=$timeout; i++)); do + if ! kubectl get namespace $namespace &> /dev/null; then + echo "Namespace $namespace successfully deleted." + break + fi + echo "Waiting for namespace $namespace to be deleted..." + sleep 1 +done + +if kubectl get namespace $namespace &> /dev/null; then + echo "Error: Namespace $namespace was not deleted within the timeout." + exit 1 +fi + +echo "All Kubernetes objects have been deleted." + +# Stop Minikube +echo "Stopping Minikube..." +minikube stop +if [ $? -eq 0 ]; then + echo "Minikube stopped successfully." +else + echo "Error: Failed to stop Minikube." + exit 1 +fi + +echo "Application and Minikube stopped successfully."