diff options
author | Arianit K <arianit@hax.al> | 2025-06-03 00:16:37 +0000 |
---|---|---|
committer | Arianit K <arianit@hax.al> | 2025-06-03 00:16:37 +0000 |
commit | 9d7f9d6253717dc5f87d674c1cc49f53256d9485 (patch) | |
tree | 8ab3b154b462e696ce2ab34fb00af622e23365be | |
download | KISSmoDocker-9d7f9d6253717dc5f87d674c1cc49f53256d9485.tar.gz KISSmoDocker-9d7f9d6253717dc5f87d674c1cc49f53256d9485.zip |
KISSmo Docker
-rw-r--r-- | Dockerfile | 54 | ||||
-rw-r--r-- | README.md | 290 | ||||
-rw-r--r-- | entrypoint.sh | 13 | ||||
-rw-r--r-- | update_kissmo.sh | 36 |
4 files changed, 393 insertions, 0 deletions
diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..258d7f8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,54 @@ +# Use the Debian base image +FROM debian:stable-slim + +# Set environment variables to prevent interactive prompts during apt operations +ENV DEBIAN_FRONTEND=noninteractive + +# Install necessary packages: +# git: for cloning and updating the repository +# libmojolicious-perl, libdbi-perl, libfile-slurp-perl, libdbd-sqlite3-perl: Perl modules required by KISSmo Perl +# ca-certificates: essential for SSL certificate verification during git clone +# cron: for scheduling periodic updates and restarts +RUN apt update && \ + apt install -y --no-install-recommends \ + git \ + libmojolicious-perl \ + libdbi-perl \ + libfile-slurp-perl \ + libdbd-sqlite3-perl \ + ca-certificates \ + cron && \ + # Ensure CA certificates are up-to-date + update-ca-certificates && \ + # Clean up apt cache to reduce image size + rm -rf /var/lib/apt/lists/* + +# Clone the KISSmo Perl repository into the /app directory +RUN git clone https://git.hax.al/KISSmoPerl/ /app + +# Set the working directory for subsequent commands +WORKDIR /app + +# Create the SQLite database file and the 'pastes' directory +# These are required by the KISSmo Perl application for data storage +RUN touch pastes.db && mkdir pastes + +# Copy the update script into the container and make it executable +# This script will handle git pull and restarting the perl application +COPY update_kissmo.sh /usr/local/bin/update_kissmo.sh +RUN chmod +x /usr/local/bin/update_kissmo.sh + +# Add the cron job to run the update script +# The job is scheduled to run at midnight (00:00) on the 1st and 21st day of every month. +# Output from the script will be logged to /var/log/cron.log +RUN (crontab -l 2>/dev/null; echo "0 0 1,21 * * /usr/local/bin/update_kissmo.sh >> /var/log/cron.log 2>&1") | crontab - + +# Copy the entrypoint script and make it executable +COPY entrypoint.sh /usr/local/bin/entrypoint.sh +RUN chmod +x /usr/local/bin/entrypoint.sh + +# Expose port 7878, which is the port KISSmo Perl listens on +EXPOSE 7878 + +# Use the exec form for CMD, pointing to the entrypoint script +CMD ["/usr/local/bin/entrypoint.sh"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..f7ce595 --- /dev/null +++ b/README.md @@ -0,0 +1,290 @@ +# KISSmo Perl Docker Setup + +This guide provides instructions on how to build and run the KISSmo Perl pastebin application using Docker, including automatic updates of the Git repository every 20 days. + +## Table of Contents + +- [Features](#features "null") + +- [Prerequisites](#prerequisites "null") + +- [Files Needed](#files-needed "null") + +- [Building the Docker Image](#building-the-docker-image "null") + +- [Running the Docker Container](#running-the-docker-container "null") + +- [Accessing the Application](#accessing-the-application "null") + +- [Automatic Updates](#automatic-updates "null") + +- [Checking Logs](#checking-logs "null") + +- [Stopping and Removing the Container](#stopping-and-removing-the-container "null") + + +## Features + +- **Containerized Environment**: Runs KISSmo Perl in an isolated Docker container. + +- **Automatic Dependency Management**: All Perl modules and system dependencies are installed automatically during image build. + +- **Persistent Data**: The SQLite database and paste files are created inside the container. + +- **Port Exposure**: Easily accessible via a defined port on your host machine. + +- **Automatic Git Updates**: The application's source code is automatically pulled from the Git repository and the application restarted every 20 days (specifically, on the 1st and 21st of each month). + + +## Prerequisites + +Before you begin, ensure you have: + +- **Docker installed**: You can download Docker Desktop (for Windows/macOS) or install Docker Engine (for Linux) from the [official Docker website](https://docs.docker.com/get-docker/ "null"). + + +## Files Needed + +You need to create three files in a dedicated directory for your Docker project: + +1. `Dockerfile` + +2. `update_kissmo.sh` + +3. `entrypoint.sh` + + +**`Dockerfile` Content:** + +``` +# Use the Debian base image +FROM debian:stable-slim + +# Set environment variables to prevent interactive prompts during apt operations +ENV DEBIAN_FRONTEND=noninteractive + +# Install necessary packages: +# git: for cloning and updating the repository +# libmojolicious-perl, libdbi-perl, libfile-slurp-perl, libdbd-sqlite3-perl: Perl modules required by KISSmo Perl +# ca-certificates: essential for SSL certificate verification during git clone +# cron: for scheduling periodic updates and restarts +RUN apt update && \ + apt install -y --no-install-recommends \ + git \ + libmojolicious-perl \ + libdbi-perl \ + libfile-slurp-perl \ + libdbd-sqlite3-perl \ + ca-certificates \ + cron && \ + # Ensure CA certificates are up-to-date + update-ca-certificates && \ + # Clean up apt cache to reduce image size + rm -rf /var/lib/apt/lists/* + +# Clone the KISSmo Perl repository into the /app directory +RUN git clone https://git.hax.al/KISSmoPerl/ /app + +# Set the working directory for subsequent commands +WORKDIR /app + +# Create the SQLite database file and the 'pastes' directory +# These are required by the KISSmo Perl application for data storage +RUN touch pastes.db && mkdir pastes + +# Copy the update script into the container and make it executable +# This script will handle git pull and restarting the perl application +COPY update_kissmo.sh /usr/local/bin/update_kissmo.sh +RUN chmod +x /usr/local/bin/update_kissmo.sh + +# Add the cron job to run the update script +# The job is scheduled to run at midnight (00:00) on the 1st and 21st day of every month. +# Output from the script will be logged to /var/log/cron.log +RUN (crontab -l 2>/dev/null; echo "0 0 1,21 * * /usr/local/bin/update_kissmo.sh >> /var/log/cron.log 2>&1") | crontab - + +# Copy the entrypoint script and make it executable +COPY entrypoint.sh /usr/local/bin/entrypoint.sh +RUN chmod +x /usr/local/bin/entrypoint.sh + +# Expose port 7878, which is the port KISSmo Perl listens on +EXPOSE 7878 + +# Use the exec form for CMD, pointing to the entrypoint script +CMD ["/usr/local/bin/entrypoint.sh"] + +``` + +**`update_kissmo.sh` Content:** + +``` +#!/bin/bash +# Script to update the KISSmo Perl repository and restart the application + +# Navigate to the application directory. Exit if the directory is not found. +cd /app || { echo "$(date): Error: /app directory not found. Aborting update." >> /var/log/cron.log; exit 1; } + +echo "$(date): Starting KISSmo Perl update..." >> /var/log/cron.log + +# Pull the latest changes from the Git repository +git pull +if [ $? -ne 0 ]; then + echo "$(date): Git pull failed. Aborting update." >> /var/log/cron.log + exit 1 +fi +echo "$(date): Git repository updated successfully." >> /var/log/cron.log + +# Find the PID of the running KISSmo Perl process and kill it. +# We use 'pgrep -f' to match the full command line of the perl process. +PIDS=$(pgrep -f "perl paste.pl daemon -m production -l http://0.0.0.0:7878") +if [ -n "$PIDS" ]; then + echo "$(date): Found running KISSmo Perl processes: $PIDS. Killing them..." >> /var/log/cron.log + kill $PIDS + # Give the process some time to shut down gracefully + sleep 5 +else + echo "$(date): No running KISSmo Perl processes found to kill." >> /var/log/cron.log +fi + +# Start the application again in daemon mode +echo "$(date): Restarting KISSmo Perl application..." >> /var/log/cron.log +perl paste.pl daemon -m production -l http://0.0.0.0:7878 +if [ $? -ne 0 ]; then + echo "$(date): Failed to restart KISSmo Perl application." >> /var/log/cron.log + exit 1 +fi +echo "$(date): KISSmo Perl restarted successfully." >> /var/log/cron.log + +``` + +**`entrypoint.sh` Content:** + +``` +#!/bin/bash + +# Start cron +echo "$(date): Starting cron service..." +service cron start + +# Start the KISSmo Perl application in daemon mode +echo "$(date): Starting KISSmo Perl application..." +perl paste.pl daemon -m production -l http://0.0.0.0:7878 + +# Keep the container running indefinitely +echo "$(date): Tail -f /dev/null to keep container alive..." +tail -f /dev/null + +``` + +## Building the Docker Image + +1. **Navigate to your project directory**: Open your terminal or command prompt and go to the directory where you saved the three files (`Dockerfile`, `update_kissmo.sh`, `entrypoint.sh`). + + ``` + cd /path/to/your/kissmo-docker-project + + ``` + +2. **Build the Docker image**: Run the following command. This will download the base Debian image, install dependencies, clone the KISSmo Perl repository, and set up the cron job. + + ``` + docker build -t kissmoperl-auto-update . + + ``` + + - `-t kissmoperl-auto-update`: Tags the image with the name `kissmoperl-auto-update`. You can choose any name you prefer. + + - `.`: Specifies that the Dockerfile is in the current directory. + + +## Running the Docker Container + +Once the image is built, you can run a container from it: + +``` +docker run -d -p 7878:7878 --name kissmo_autoupdate_app kissmoperl-auto-update + +``` + +- `-d`: Runs the container in detached mode (in the background). + +- `-p 7878:7878`: Maps port `7878` on your host machine to port `7878` inside the container. This is the port KISSmo Perl listens on. + +- `--name kissmo_autoupdate_app`: Assigns a convenient name to your running container. + +- `kissmoperl-auto-update`: The name of the Docker image you built. + + +## Accessing the Application + +After the container starts, open your web browser and navigate to: + +``` +http://localhost:7878 + +``` + +If you are running Docker on a remote server, replace `localhost` with the actual IP address or hostname of your server. + +## Automatic Updates + +The Docker container is configured to automatically update the KISSmo Perl application from its Git repository and restart it. + +- **Schedule**: The update script (`update_kissmo.sh`) is executed by `cron` at **00:00 (midnight) on the 1st and 21st day of every month**. This provides an approximate "every 20 days" update cycle. + +- **Process**: + + 1. The script navigates to the `/app` directory. + + 2. It performs a `git pull` to fetch the latest changes. + + 3. It finds and kills any running `perl paste.pl` processes. + + 4. It restarts the `perl paste.pl` application in daemon mode. + + +## Checking Logs + +You can monitor the cron job's activity and the application's startup by checking the container logs: + +1. **View cron job logs**: + + ``` + docker exec kissmo_autoupdate_app cat /var/log/cron.log + + ``` + +2. **View container startup logs**: + + ``` + docker logs kissmo_autoupdate_app + + ``` + + +## Stopping and Removing the Container + +To stop and remove the running container: + +1. **Stop the container**: + + ``` + docker stop kissmo_autoupdate_app + + ``` + +2. **Remove the container**: + + ``` + docker rm kissmo_autoupdate_app + + ``` + + +To remove the Docker image from your system (optional, after stopping and removing all containers based on it): + +``` +docker rmi kissmoperl-auto-update + +``` + +This README provides all the necessary information to deploy and manage your KISSmo Perl application using Docker with automatic updates. diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..71d3ac1 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +# Start cron +echo "$(date): Starting cron service..." +service cron start + +# Start the KISSmo Perl application in daemon mode +echo "$(date): Starting KISSmo Perl application..." +perl paste.pl daemon -m production -l http://0.0.0.0:7878 + +# Keep the container running indefinitely +echo "$(date): Tail -f /dev/null to keep container alive..." +tail -f /dev/null diff --git a/update_kissmo.sh b/update_kissmo.sh new file mode 100644 index 0000000..f6e0195 --- /dev/null +++ b/update_kissmo.sh @@ -0,0 +1,36 @@ +#!/bin/bash +# Script to update the KISSmo Perl repository and restart the application + +# Navigate to the application directory. Exit if the directory is not found. +cd /app || { echo "$(date): Error: /app directory not found. Aborting update." >> /var/log/cron.log; exit 1; } + +echo "$(date): Starting KISSmo Perl update..." >> /var/log/cron.log + +# Pull the latest changes from the Git repository +git pull +if [ $? -ne 0 ]; then + echo "$(date): Git pull failed. Aborting update." >> /var/log/cron.log + exit 1 +fi +echo "$(date): Git repository updated successfully." >> /var/log/cron.log + +# Find the PID of the running KISSmo Perl process and kill it. +# We use 'pgrep -f' to match the full command line of the perl process. +PIDS=$(pgrep -f "perl paste.pl daemon -m production -l http://0.0.0.0:7878") +if [ -n "$PIDS" ]; then + echo "$(date): Found running KISSmo Perl processes: $PIDS. Killing them..." >> /var/log/cron.log + kill $PIDS + # Give the process some time to shut down gracefully + sleep 5 +else + echo "$(date): No running KISSmo Perl processes found to kill." >> /var/log/cron.log +fi + +# Start the application again in daemon mode +echo "$(date): Restarting KISSmo Perl application..." >> /var/log/cron.log +perl paste.pl daemon -m production -l http://0.0.0.0:7878 +if [ $? -ne 0 ]; then + echo "$(date): Failed to restart KISSmo Perl application." >> /var/log/cron.log + exit 1 +fi +echo "$(date): KISSmo Perl restarted successfully." >> /var/log/cron.log |