From c20cb9cb13c2c7a6e395e32d68fa21b1eac34d4e Mon Sep 17 00:00:00 2001 From: null Date: Mon, 5 Feb 2024 10:40:37 +0100 Subject: [PATCH] add files to create Docker image --- Dockerfile | 27 +++++++++++++++++++++++++++ entrypoint.sh | 26 ++++++++++++++++++++++++++ githook.py | 21 +++++++++++++++++++++ supervisord.conf | 27 +++++++++++++++++++++++++++ 4 files changed, 101 insertions(+) create mode 100644 Dockerfile create mode 100755 entrypoint.sh create mode 100644 githook.py create mode 100644 supervisord.conf diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..043f519 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,27 @@ +# Use the official Python image as the base image +FROM python:latest + +# Install Supervisord +RUN pip install supervisor flask + +# Copy your Supervisord configuration file into the container +COPY supervisord.conf /etc/supervisor/supervisord.conf + +COPY entrypoint.sh /usr/local/bin + +# Create a directory for your Python script +WORKDIR /home/githook + +# Copy your Python script into the container +COPY githook.py . + + +# Expose any necessary ports (if your script listens on a specific port) +EXPOSE 5000 +EXPOSE 8080 + +WORKDIR /home/ + +# Define the command to start Supervisord +CMD ["entrypoint.sh"] +#CMD ["supervisord", "-c", "/etc/supervisor/supervisord.conf"] diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100755 index 0000000..5072d3b --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +# Check if the REPO_URL environment variable is set +if [ -z "$GIT_REPO" ]; then + echo "Error: REPO_URL environment variable is not set." + exit 1 +fi + +# Set the target directory +TARGET_DIR="/home/app" + +# Check if the target directory already exists +if [ ! -d "$TARGET_DIR" ]; then + # If it doesn't exist, clone the repository + git clone "$GIT_REPO" "$TARGET_DIR" +else + # If it already exists, update the repository + cd "$TARGET_DIR" || exit 1 + git pull origin master +fi + +mkdir -p /var/run/supervisor + +# Run your application or desired command +cmd="supervisord -c /etc/supervisor/supervisord.conf" +$cmd diff --git a/githook.py b/githook.py new file mode 100644 index 0000000..eb6b80c --- /dev/null +++ b/githook.py @@ -0,0 +1,21 @@ +from flask import Flask, request, jsonify +import os +import subprocess + +app = Flask(__name__) + +path="/home/app" + +@app.route('/githook', methods=['POST']) +def githook(): + try: + subprocess.run(['supervisorctl', 'stop', 'app'], check=True) + subprocess.run(['git', 'pull'], check=True, cwd=path) + subprocess.run(['supervisorctl', 'start', 'app'], check=True) + return "", 200 + except Exception as e: + return jsonify({'error': str(e)}), 500 + +if __name__ == '__main__': + app.run(host='0.0.0.0', port=5000) + diff --git a/supervisord.conf b/supervisord.conf new file mode 100644 index 0000000..2389964 --- /dev/null +++ b/supervisord.conf @@ -0,0 +1,27 @@ +[supervisord] +nodaemon=true + +[program:githook-server] +command=python githook.py +directory=/home/githook +autostart=true +autorestart=true +redirect_stderr=true +stdout_logfile=/var/log/githook.log + +[program:app] +command=python app.py +directory=/home/app +autostart=true +autorestart=false +redirect_stderr=true +stdout_logfile=/var/log/app.log + +[supervisorctl] +serverurl=unix:///var/run/supervisor/supervisor.sock + +[unix_http_server] +file=/var/run/supervisor/supervisor.sock + +[rpcinterface:supervisor] +supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface