From 6a0b81d888ad57c6c2ccb23955da0f22783f876f Mon Sep 17 00:00:00 2001 From: Santhosh Date: Wed, 30 Apr 2025 10:01:31 +0200 Subject: [PATCH] ssk --- sk1/README.md | 0 sk1/myproject/Dockerfile | 28 +++ sk1/myproject/README.md | 102 ++++++++++ sk1/myproject/docker-compose.yml | 35 ++++ sk1/myproject/manage.py | 22 +++ sk1/myproject/myapp/__init__.py | 0 sk1/myproject/myapp/admin.py | 3 + sk1/myproject/myapp/apps.py | 6 + sk1/myproject/myapp/forms.py | 7 + .../myapp/migrations/0001_initial.py | 24 +++ sk1/myproject/myapp/migrations/__init__.py | 0 sk1/myproject/myapp/models.py | 10 + sk1/myproject/myapp/static/styles.css | 27 +++ .../myapp/templates/student_form.html | 17 ++ .../myapp/templates/student_list.html | 16 ++ sk1/myproject/myapp/tests.py | 3 + sk1/myproject/myapp/urls.py | 7 + sk1/myproject/myapp/views.py | 17 ++ sk1/myproject/myproject/__init__.py | 0 .../__pycache__/__init__.cpython-312.pyc | Bin 0 -> 144 bytes .../__pycache__/settings.cpython-312.pyc | Bin 0 -> 2527 bytes sk1/myproject/myproject/asgi.py | 16 ++ sk1/myproject/myproject/settings.py | 139 +++++++++++++ sk1/myproject/myproject/urls.py | 23 +++ sk1/myproject/myproject/wsgi.py | 16 ++ sk1/myproject/prepare-app.sh | 17 ++ sk1/myproject/remove-app.sh | 16 ++ sk1/myproject/requirements.txt | 3 + sk1/myproject/start-app.sh | 20 ++ sk1/myproject/stop-app.sh | 11 ++ sk1/myproject/wait-for-it.sh | 182 ++++++++++++++++++ z2/myproject/.start.sh.swp | Bin 0 -> 1024 bytes z2/myproject/README.md | 143 ++++++-------- z2/myproject/{stat.sh => start.sh} | 0 34 files changed, 822 insertions(+), 88 deletions(-) create mode 100644 sk1/README.md create mode 100644 sk1/myproject/Dockerfile create mode 100644 sk1/myproject/README.md create mode 100644 sk1/myproject/docker-compose.yml create mode 100755 sk1/myproject/manage.py create mode 100644 sk1/myproject/myapp/__init__.py create mode 100644 sk1/myproject/myapp/admin.py create mode 100644 sk1/myproject/myapp/apps.py create mode 100644 sk1/myproject/myapp/forms.py create mode 100644 sk1/myproject/myapp/migrations/0001_initial.py create mode 100644 sk1/myproject/myapp/migrations/__init__.py create mode 100644 sk1/myproject/myapp/models.py create mode 100644 sk1/myproject/myapp/static/styles.css create mode 100644 sk1/myproject/myapp/templates/student_form.html create mode 100644 sk1/myproject/myapp/templates/student_list.html create mode 100644 sk1/myproject/myapp/tests.py create mode 100644 sk1/myproject/myapp/urls.py create mode 100644 sk1/myproject/myapp/views.py create mode 100644 sk1/myproject/myproject/__init__.py create mode 100644 sk1/myproject/myproject/__pycache__/__init__.cpython-312.pyc create mode 100644 sk1/myproject/myproject/__pycache__/settings.cpython-312.pyc create mode 100644 sk1/myproject/myproject/asgi.py create mode 100644 sk1/myproject/myproject/settings.py create mode 100644 sk1/myproject/myproject/urls.py create mode 100644 sk1/myproject/myproject/wsgi.py create mode 100755 sk1/myproject/prepare-app.sh create mode 100755 sk1/myproject/remove-app.sh create mode 100644 sk1/myproject/requirements.txt create mode 100755 sk1/myproject/start-app.sh create mode 100755 sk1/myproject/stop-app.sh create mode 100755 sk1/myproject/wait-for-it.sh create mode 100644 z2/myproject/.start.sh.swp rename z2/myproject/{stat.sh => start.sh} (100%) diff --git a/sk1/README.md b/sk1/README.md new file mode 100644 index 0000000..e69de29 diff --git a/sk1/myproject/Dockerfile b/sk1/myproject/Dockerfile new file mode 100644 index 0000000..e2c70f9 --- /dev/null +++ b/sk1/myproject/Dockerfile @@ -0,0 +1,28 @@ +# Use an official Python runtime as a parent image +FROM python:3.9-slim + +# Set environment variables +ENV PYTHONDONTWRITEBYTECODE=1 +ENV PYTHONUNBUFFERED=1 + +# Set the working directory in the container +WORKDIR /app + +# Copy the requirements file into the container +COPY requirements.txt . + +# Install dependencies +RUN pip install --no-cache-dir -r requirements.txt + +# Copy the current directory contents into the container +COPY . . + +# Copy wait-for-it script and make it executable +COPY wait-for-it.sh /wait-for-it.sh +RUN chmod +x /wait-for-it.sh + +# Expose port 8000 for the Django app +EXPOSE 8000 + +# Run the Django development server, wait for the DB to be ready first +CMD ["/wait-for-it.sh", "db:5432", "--", "python", "manage.py", "runserver", "0.0.0.0:8000"] diff --git a/sk1/myproject/README.md b/sk1/myproject/README.md new file mode 100644 index 0000000..4808970 --- /dev/null +++ b/sk1/myproject/README.md @@ -0,0 +1,102 @@ +Application Overview +This web application is a basic Django project connected to a PostgreSQL database. + +Django web service: Runs a Django development server. +PostgreSQL database service: Stores data for the Django application. +The services are configured to communicate with each other over a custom virtual network, and the database data is persisted in a named volume. + +Project Structure +The repository contains the following key files: + +docker-compose.yml: Defines the services (Django and PostgreSQL) and configurations. +Dockerfile: Defines the configuration for building the Django web service. +wait-for-it.sh: A script that ensures the Django app waits for PostgreSQL to be ready before starting. +requirements.txt: Python dependencies for the Django application. +manage.py: The Django project’s command-line utility. +prepare-app.sh: Script to prepare the application environment (build images, create volumes, and networks). +start-app.sh: Script to start the application (launch containers and configure them to restart on failure). +stop-app.sh: Script to stop the containers (pause the application without resetting the state). +remove-app.sh: Script to remove all traces of the application (cleanup). +Virtual Networks and Named Volumes +Network: mic_default_san +Custom network for communication between the services. +Volume: postgres_data +A named volume used to persist PostgreSQL data so that data is not lost if the container is stopped or removed. +Container Configuration +PostgreSQL Service +Image: postgres:13 +Environment variables: +POSTGRES_DB: Database name (mydatabase) +POSTGRES_USER: Database user (myuser) +POSTGRES_PASSWORD: Database password (mypassword) +Ports: Exposes PostgreSQL on localhost:5432 +Volume: Persists data in the postgres_data volume. +Django Web Service +Build Context: The Dockerfile is used to build the web service container. +Command: Waits for PostgreSQL to be available before starting the Django development server. +Ports: Exposes the Django app on localhost:8000 +Environment Variables: +DATABASE_URL: Connection string for the database. +Instructions for Running the Application +Step 1: Prepare the Application +Run the prepare-app.sh script to build the Docker images, create named volumes, and set up networks. + +bash +Copy +Edit +./prepare-app.sh +Step 2: Start the Application +Run the start-app.sh script to start the application. This will launch the containers, and you can access the web application in your browser. + +bash +Copy +Edit +./start-app.sh +After running the script, you will see the following message: + +arduino +Copy +Edit +The app is available at http://localhost:8000 +Step 3: Open the Web Application +Open a browser and navigate to http://localhost:8000 to view the Django application. +Step 4: Stop the Application +To stop the application without removing containers and volumes, use the stop-app.sh script. This will pause the services but retain the current state. + +bash +Copy +Edit +./stop-app.sh +Step 5: Remove the Application +If you want to completely remove all containers, networks, and volumes created by the application, use the remove-app.sh script. + +bash +Copy +Edit +./remove-app.sh +After running the script, everything related to the application will be removed. + +Example Workflow +Here’s an example of working with the application: + +bash +Copy +Edit +# Prepare the application +./prepare-app.sh +Preparing app ... + +# Run the application +./start-app.sh +Running app ... +The app is available at http://localhost:8000 + +# Open the web application in a browser. + +# Stop the application +./stop-app.sh +Stopping app ... + +# Remove the application +./remove-app.sh +Removed app. diff --git a/sk1/myproject/docker-compose.yml b/sk1/myproject/docker-compose.yml new file mode 100644 index 0000000..e9948d5 --- /dev/null +++ b/sk1/myproject/docker-compose.yml @@ -0,0 +1,35 @@ +version: '3.8' + +services: + db: + image: postgres:13 + environment: + POSTGRES_DB: mydatabase + POSTGRES_USER: myuser + POSTGRES_PASSWORD: mypassword + volumes: + - postgres_data:/var/lib/postgresql/data/ + ports: + - "5432:5432" + networks: + - mic_default_san + + web: + build: . + command: /wait-for-it.sh db:5432 -- python manage.py runserver 0.0.0.0:8000 + volumes: + - .:/app + ports: + - "8000:8000" + depends_on: + - db + environment: + DATABASE_URL: postgres://myuser:mypassword@db:5432/mydatabase + networks: + mic_default_san: + +volumes: + postgres_data: + +networks: + mic_default_san: diff --git a/sk1/myproject/manage.py b/sk1/myproject/manage.py new file mode 100755 index 0000000..92bb9a3 --- /dev/null +++ b/sk1/myproject/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings') + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == '__main__': + main() diff --git a/sk1/myproject/myapp/__init__.py b/sk1/myproject/myapp/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sk1/myproject/myapp/admin.py b/sk1/myproject/myapp/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/sk1/myproject/myapp/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/sk1/myproject/myapp/apps.py b/sk1/myproject/myapp/apps.py new file mode 100644 index 0000000..c34fb20 --- /dev/null +++ b/sk1/myproject/myapp/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class MyappConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'myapp' diff --git a/sk1/myproject/myapp/forms.py b/sk1/myproject/myapp/forms.py new file mode 100644 index 0000000..b1c54f2 --- /dev/null +++ b/sk1/myproject/myapp/forms.py @@ -0,0 +1,7 @@ +from django import forms +from .models import Student + +class StudentForm(forms.ModelForm): + class Meta: + model = Student + fields = ['name', 'age', 'university', 'degree'] diff --git a/sk1/myproject/myapp/migrations/0001_initial.py b/sk1/myproject/myapp/migrations/0001_initial.py new file mode 100644 index 0000000..4ab4e49 --- /dev/null +++ b/sk1/myproject/myapp/migrations/0001_initial.py @@ -0,0 +1,24 @@ +# Generated by Django 4.2.19 on 2025-02-19 10:29 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Student', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=100)), + ('age', models.IntegerField()), + ('university', models.CharField(max_length=100)), + ('degree', models.CharField(max_length=100)), + ], + ), + ] diff --git a/sk1/myproject/myapp/migrations/__init__.py b/sk1/myproject/myapp/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sk1/myproject/myapp/models.py b/sk1/myproject/myapp/models.py new file mode 100644 index 0000000..04f732f --- /dev/null +++ b/sk1/myproject/myapp/models.py @@ -0,0 +1,10 @@ +from django.db import models + +class Student(models.Model): + name = models.CharField(max_length=100) + age = models.IntegerField() + university = models.CharField(max_length=100) + degree = models.CharField(max_length=100) + + def __str__(self): + return self.name diff --git a/sk1/myproject/myapp/static/styles.css b/sk1/myproject/myapp/static/styles.css new file mode 100644 index 0000000..eff96a9 --- /dev/null +++ b/sk1/myproject/myapp/static/styles.css @@ -0,0 +1,27 @@ +body { + font-family: Arial, sans-serif; + margin: 20px; +} + +form { + max-width: 400px; + margin: 0 auto; +} + +input, button { + width: 100%; + padding: 10px; + margin: 10px 0; +} + +ul { + list-style-type: none; + padding: 0; +} + +li { + background: #f9f9f9; + padding: 10px; + margin: 5px 0; + border: 1px solid #ddd; +} diff --git a/sk1/myproject/myapp/templates/student_form.html b/sk1/myproject/myapp/templates/student_form.html new file mode 100644 index 0000000..e54a223 --- /dev/null +++ b/sk1/myproject/myapp/templates/student_form.html @@ -0,0 +1,17 @@ + + + + Student Form + + + +

Student Information Form

+
+ {% csrf_token %} + {{ form.as_p }} + +
+
+ View All Students + + diff --git a/sk1/myproject/myapp/templates/student_list.html b/sk1/myproject/myapp/templates/student_list.html new file mode 100644 index 0000000..386d60f --- /dev/null +++ b/sk1/myproject/myapp/templates/student_list.html @@ -0,0 +1,16 @@ + + + + Student List + + +

Student List

+ +
+ Add New Student + + diff --git a/sk1/myproject/myapp/tests.py b/sk1/myproject/myapp/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/sk1/myproject/myapp/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/sk1/myproject/myapp/urls.py b/sk1/myproject/myapp/urls.py new file mode 100644 index 0000000..d74d117 --- /dev/null +++ b/sk1/myproject/myapp/urls.py @@ -0,0 +1,7 @@ +from django.urls import path +from . import views + +urlpatterns = [ + path('', views.student_form, name='student_form'), + path('list/', views.student_list, name='student_list'), +] diff --git a/sk1/myproject/myapp/views.py b/sk1/myproject/myapp/views.py new file mode 100644 index 0000000..abab14b --- /dev/null +++ b/sk1/myproject/myapp/views.py @@ -0,0 +1,17 @@ +from django.shortcuts import render, redirect +from .forms import StudentForm +from .models import Student + +def student_form(request): + if request.method == 'POST': + form = StudentForm(request.POST) + if form.is_valid(): + form.save() + return redirect('student_list') # Redirect to a list view after submission + else: + form = StudentForm() + return render(request, 'student_form.html', {'form': form}) + +def student_list(request): + students = Student.objects.all() + return render(request, 'student_list.html', {'students': students}) diff --git a/sk1/myproject/myproject/__init__.py b/sk1/myproject/myproject/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sk1/myproject/myproject/__pycache__/__init__.cpython-312.pyc b/sk1/myproject/myproject/__pycache__/__init__.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..3c761a6f51a85d077be9b4e303140bc55630535c GIT binary patch literal 144 zcmX@j%ge<81l^0ari19mAOanHW&w&!XQ*V*Wb|9fP{ah}eFmxdrLUh~P@?aWpPZdq zq@SCate;z1P?VpQnp}d!ijU9C%PfhH*DI*}#bJ}1pHiBWYFESx)XxaS#UREHt_6esolZCO!b$7uluNs}n4skr%QS_ElniFSm@l4?qE%OM1%JIl2Bq421< z;-o!Gw{{3RWGg!Nujmq^Lkvy^Iu+<3C+L!?kCbE!vVppS#JhVRzxVEU?;Z7fG8v=b z^YZm??Vl4A^{2ZSe?p_l`@doGBZVlDLNxNJwB{3iH07hP5Bqu9>| z#WHb@3W$+5B}Us63cjYr7>pqp<0vd95NJ>Uxw_bETA@+=RiI6aNt6&% zD2Y-ygl6nGMWOU7pF)c>XclF_Y8IXIo>}jCUZL9oF^w*WvuIAtK)iGC%c6PlJh~`e z=q=Y8}5CtbyNo7S`4tu1ux!L!9L^zI9! zVRHX!`|@Wu(B1Z(9c=7;(z@0&cCF{P<)&TFh3xDQhHV+TWhzb9Bh@}PE$u?YOH)GE z1?!g8>tJHfO(_W`1ZX~~fQ*RT29Jel4+{>a#q0;6%7uP4Z^p zYOP+XRs?(Dn6h2cR#>^y0j9lUbixR4%3akus%z9dW{^Br6${_7V;~&Ob3P};S5`|E z-U(E=HQt0vm9igCrS8>EMLT3AptMU~x_csjH&O#9JU zk=7Jl(Yjh0>uqc6*#E1u`VU*BHp=lB;OnyjRJs~AmDWjwAl4VV#PK)k1v?D)JhV*S z{&)lnOlk(gDq)wEHnfznqF@zOa&t~tlHdxLBq!W~i&9mZrXQ$vB9i1@F0S0f#86*g zC)9y^MYo*DGAHovSvxU-FRb%*=?VXx6D;z}ja4VfmCMx)z9>De3U$Frl`29VL?D0y z5uDgssaP!Y8{9hYB-X3dy3|-N7pj#NCtBy%YGtm@3r>1NSS`8sH`t{0`JT)70wDwJTd%vDw!+$t{>szn|EOKZF&R-t4f4M@1O^q}H| z+*w_O%&vGPR&THDNPK76Q8(-RsFZCmX6qQ=1`>{cYiYEKPgLI~!d>0F6eErnBOeEz4O_oYC{lp+b zWqEo(o!QUMA0(J}RPah_;16eGhtWhZH1MaAzJ7GTP}$pWs5GDkQ~McqKRy4`!@czN z!=OJ6HbKv3fr`$&Ns9x&5BwtDqBgnsW^%DUT%1pS&2; fi } + +usage() +{ + cat << USAGE >&2 +Usage: + $WAITFORIT_cmdname host:port [-s] [-t timeout] [-- command args] + -h HOST | --host=HOST Host or IP under test + -p PORT | --port=PORT TCP port under test + Alternatively, you specify the host and port as host:port + -s | --strict Only execute subcommand if the test succeeds + -q | --quiet Don't output any status messages + -t TIMEOUT | --timeout=TIMEOUT + Timeout in seconds, zero for no timeout + -- COMMAND ARGS Execute command with args after the test finishes +USAGE + exit 1 +} + +wait_for() +{ + if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then + echoerr "$WAITFORIT_cmdname: waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT" + else + echoerr "$WAITFORIT_cmdname: waiting for $WAITFORIT_HOST:$WAITFORIT_PORT without a timeout" + fi + WAITFORIT_start_ts=$(date +%s) + while : + do + if [[ $WAITFORIT_ISBUSY -eq 1 ]]; then + nc -z $WAITFORIT_HOST $WAITFORIT_PORT + WAITFORIT_result=$? + else + (echo -n > /dev/tcp/$WAITFORIT_HOST/$WAITFORIT_PORT) >/dev/null 2>&1 + WAITFORIT_result=$? + fi + if [[ $WAITFORIT_result -eq 0 ]]; then + WAITFORIT_end_ts=$(date +%s) + echoerr "$WAITFORIT_cmdname: $WAITFORIT_HOST:$WAITFORIT_PORT is available after $((WAITFORIT_end_ts - WAITFORIT_start_ts)) seconds" + break + fi + sleep 1 + done + return $WAITFORIT_result +} + +wait_for_wrapper() +{ + # In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692 + if [[ $WAITFORIT_QUIET -eq 1 ]]; then + timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --quiet --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT & + else + timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT & + fi + WAITFORIT_PID=$! + trap "kill -INT -$WAITFORIT_PID" INT + wait $WAITFORIT_PID + WAITFORIT_RESULT=$? + if [[ $WAITFORIT_RESULT -ne 0 ]]; then + echoerr "$WAITFORIT_cmdname: timeout occurred after waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT" + fi + return $WAITFORIT_RESULT +} + +# process arguments +while [[ $# -gt 0 ]] +do + case "$1" in + *:* ) + WAITFORIT_hostport=(${1//:/ }) + WAITFORIT_HOST=${WAITFORIT_hostport[0]} + WAITFORIT_PORT=${WAITFORIT_hostport[1]} + shift 1 + ;; + --child) + WAITFORIT_CHILD=1 + shift 1 + ;; + -q | --quiet) + WAITFORIT_QUIET=1 + shift 1 + ;; + -s | --strict) + WAITFORIT_STRICT=1 + shift 1 + ;; + -h) + WAITFORIT_HOST="$2" + if [[ $WAITFORIT_HOST == "" ]]; then break; fi + shift 2 + ;; + --host=*) + WAITFORIT_HOST="${1#*=}" + shift 1 + ;; + -p) + WAITFORIT_PORT="$2" + if [[ $WAITFORIT_PORT == "" ]]; then break; fi + shift 2 + ;; + --port=*) + WAITFORIT_PORT="${1#*=}" + shift 1 + ;; + -t) + WAITFORIT_TIMEOUT="$2" + if [[ $WAITFORIT_TIMEOUT == "" ]]; then break; fi + shift 2 + ;; + --timeout=*) + WAITFORIT_TIMEOUT="${1#*=}" + shift 1 + ;; + --) + shift + WAITFORIT_CLI=("$@") + break + ;; + --help) + usage + ;; + *) + echoerr "Unknown argument: $1" + usage + ;; + esac +done + +if [[ "$WAITFORIT_HOST" == "" || "$WAITFORIT_PORT" == "" ]]; then + echoerr "Error: you need to provide a host and port to test." + usage +fi + +WAITFORIT_TIMEOUT=${WAITFORIT_TIMEOUT:-15} +WAITFORIT_STRICT=${WAITFORIT_STRICT:-0} +WAITFORIT_CHILD=${WAITFORIT_CHILD:-0} +WAITFORIT_QUIET=${WAITFORIT_QUIET:-0} + +# Check to see if timeout is from busybox? +WAITFORIT_TIMEOUT_PATH=$(type -p timeout) +WAITFORIT_TIMEOUT_PATH=$(realpath $WAITFORIT_TIMEOUT_PATH 2>/dev/null || readlink -f $WAITFORIT_TIMEOUT_PATH) + +WAITFORIT_BUSYTIMEFLAG="" +if [[ $WAITFORIT_TIMEOUT_PATH =~ "busybox" ]]; then + WAITFORIT_ISBUSY=1 + # Check if busybox timeout uses -t flag + # (recent Alpine versions don't support -t anymore) + if timeout &>/dev/stdout | grep -q -e '-t '; then + WAITFORIT_BUSYTIMEFLAG="-t" + fi +else + WAITFORIT_ISBUSY=0 +fi + +if [[ $WAITFORIT_CHILD -gt 0 ]]; then + wait_for + WAITFORIT_RESULT=$? + exit $WAITFORIT_RESULT +else + if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then + wait_for_wrapper + WAITFORIT_RESULT=$? + else + wait_for + WAITFORIT_RESULT=$? + fi +fi + +if [[ $WAITFORIT_CLI != "" ]]; then + if [[ $WAITFORIT_RESULT -ne 0 && $WAITFORIT_STRICT -eq 1 ]]; then + echoerr "$WAITFORIT_cmdname: strict mode, refusing to execute subprocess" + exit $WAITFORIT_RESULT + fi + exec "${WAITFORIT_CLI[@]}" +else + exit $WAITFORIT_RESULT +fi diff --git a/z2/myproject/.start.sh.swp b/z2/myproject/.start.sh.swp new file mode 100644 index 0000000000000000000000000000000000000000..d147d11c61ebb3e308df499ccb9d53f091b6f42c GIT binary patch literal 1024 zcmYc?$V<%2S1{KzVn6}++8G#%6Z2A%i%P3dB(QLR>Pj;5i!-oDp$HY1Bo>wE5mtwy PX_PY>0;3^7vk(9Ppgj&` literal 0 HcmV?d00001 diff --git a/z2/myproject/README.md b/z2/myproject/README.md index 4808970..240054c 100644 --- a/z2/myproject/README.md +++ b/z2/myproject/README.md @@ -1,102 +1,69 @@ -Application Overview -This web application is a basic Django project connected to a PostgreSQL database. +# Django with PostgreSQL on Kubernetes -Django web service: Runs a Django development server. -PostgreSQL database service: Stores data for the Django application. -The services are configured to communicate with each other over a custom virtual network, and the database data is persisted in a named volume. +This project demonstrates how to deploy a Django application with PostgreSQL database on Kubernetes. -Project Structure -The repository contains the following key files: +## Project Structure -docker-compose.yml: Defines the services (Django and PostgreSQL) and configurations. -Dockerfile: Defines the configuration for building the Django web service. -wait-for-it.sh: A script that ensures the Django app waits for PostgreSQL to be ready before starting. -requirements.txt: Python dependencies for the Django application. -manage.py: The Django project’s command-line utility. -prepare-app.sh: Script to prepare the application environment (build images, create volumes, and networks). -start-app.sh: Script to start the application (launch containers and configure them to restart on failure). -stop-app.sh: Script to stop the containers (pause the application without resetting the state). -remove-app.sh: Script to remove all traces of the application (cleanup). -Virtual Networks and Named Volumes -Network: mic_default_san -Custom network for communication between the services. -Volume: postgres_data -A named volume used to persist PostgreSQL data so that data is not lost if the container is stopped or removed. -Container Configuration -PostgreSQL Service -Image: postgres:13 -Environment variables: -POSTGRES_DB: Database name (mydatabase) -POSTGRES_USER: Database user (myuser) -POSTGRES_PASSWORD: Database password (mypassword) -Ports: Exposes PostgreSQL on localhost:5432 -Volume: Persists data in the postgres_data volume. -Django Web Service -Build Context: The Dockerfile is used to build the web service container. -Command: Waits for PostgreSQL to be available before starting the Django development server. -Ports: Exposes the Django app on localhost:8000 -Environment Variables: -DATABASE_URL: Connection string for the database. -Instructions for Running the Application -Step 1: Prepare the Application -Run the prepare-app.sh script to build the Docker images, create named volumes, and set up networks. +├── Dockerfile # Docker configuration for Django app +├── docker-compose.yml # Docker Compose configuration (if applicable) +├── k8s/ # Kubernetes configuration files +│ ├── namespace.yaml # Namespace configuration +│ ├── statefulset.yaml # PostgreSQL StatefulSet with PV/PVC +│ ├── deployment.yaml # Django application deployment +│ ├── service.yaml # Django service (NodePort) +│ ├── migrate-job.yaml # Job for Django migrations +├── manage.py # Django management script +├── myapp/ # Django app directory +├── myproject/ # Django project directory +├── requirements.txt # Python dependencies +├── scripts/ # Utility scripts +├── wait-for-it.sh # DB connection checker +└── *.sh # Various utility scripts -bash -Copy -Edit -./prepare-app.sh -Step 2: Start the Application -Run the start-app.sh script to start the application. This will launch the containers, and you can access the web application in your browser. -bash -Copy -Edit -./start-app.sh -After running the script, you will see the following message: +Utility Scripts +prepare-app.sh: Prepares the application for deployment -arduino -Copy -Edit -The app is available at http://localhost:8000 -Step 3: Open the Web Application -Open a browser and navigate to http://localhost:8000 to view the Django application. -Step 4: Stop the Application -To stop the application without removing containers and volumes, use the stop-app.sh script. This will pause the services but retain the current state. +start-app.sh: Starts the application -bash -Copy -Edit -./stop-app.sh -Step 5: Remove the Application -If you want to completely remove all containers, networks, and volumes created by the application, use the remove-app.sh script. +stop-app.sh: Stops the application -bash -Copy -Edit -./remove-app.sh -After running the script, everything related to the application will be removed. +remove-app.sh: Removes the application -Example Workflow -Here’s an example of working with the application: +stat.sh: Shows application status -bash -Copy -Edit -# Prepare the application -./prepare-app.sh -Preparing app ... +wait-for-it.sh: Waits for database to be ready -# Run the application -./start-app.sh -Running app ... -The app is available at http://localhost:8000 -# Open the web application in a browser. -# Stop the application -./stop-app.sh -Stopping app ... +## Prerequisites -# Remove the application -./remove-app.sh -Removed app. +- Kubernetes cluster (Minikube or cloud provider) +- kubectl configured to access your cluster +- Docker installed (for building images) + +## Deployment Steps + +1. **Build the Django Docker image**: + ```bash + docker build -t django-app:latest . + + + +#Troubleshooting + +kubectl get pods -n webapp-namespace + +kubectl logs -n webapp-namespace + +kubectl get svc -n webapp-namespace + +kubectl get pv,pvc -n webapp-namespace + +#clean +kubectl delete namespace webapp-namespace + + +minikube service django-service --url -n webapp-namespace + +kubectl exec -it postgres-0 -n webapp-namespace -- psql -U myuser -d mydatabase diff --git a/z2/myproject/stat.sh b/z2/myproject/start.sh similarity index 100% rename from z2/myproject/stat.sh rename to z2/myproject/start.sh