diff --git a/README.md b/README.md index e69de29..f373e74 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,21 @@ +# PiDP 10 Services + +## Overview + +This repository combines a set of useful scripts and services, to run on the +PiDP-10 in order to make it easier to work with it and develop for it. These +services can be installed on the PiDP-10 and automate tasks such as copying +files onto the ITS operating system. These tools help automate processes, that +would require interaction with the Linux part of the PiDP-10 when developing on +another system. + +## git-monitor + +The git-monitor is a system service, that monitors a target directory on the +PiDP-10. It contains a file with a list of git repositories to monitor, one at a +time. The service will check if any of the repositories changed, and pull them. +After a successful pull a script will be called, if it exists, that allows the +newly updated repository to interact with a running PDP-10 simulation. + +The monitor consists of a script, that needs to be installed to `/usr/local/bin` +and a systemd unit-file to run it as a service. diff --git a/services/git-monitor b/services/git-monitor new file mode 100644 index 0000000..36cae64 --- /dev/null +++ b/services/git-monitor @@ -0,0 +1,89 @@ +#!/bin/bash + +# Configuration (Customize these!) +REPO_URL="ssh://gitea@git.orca-central.de:10229/jali/PDP10Sudoku.git" +REPO_LOCAL_PATH="/home/jali/Projects/PDP10Sudoku" +BRANCH_NAME="develop" +POLL_INTERVAL=60 # Seconds between checks +#LOG_FILE="/var/log/git-monitor.log" # Log file for recording actions +LOCK_FILE="/tmp/git-monitor.lock" # File to prevent multiple instances running concurrently + +# Functions +log_message() { + local level=$1 + local message=$2 + timestamp=$(date +'%Y-%m-%d %H:%M:%S') + #echo "$timestamp [$level] $message" >> "$LOG_FILE" + echo "$timestamp [$level] $message" # Also print to console (optional) +} + +check_lock() { + if [ -f "$LOCK_FILE" ]; then + log_message "WARN" "Another instance is already running. Exiting." + exit 1 + fi +} + +create_lock() { + touch "$LOCK_FILE" +} + +remove_lock() { + rm -f "$LOCK_FILE" +} + +initialize_repo() { + if [ ! -d "$REPO_LOCAL_PATH" ]; then + log_message "INFO" "Repository directory does not exist. Cloning..." + mkdir -p "$REPO_LOCAL_PATH" + git clone --depth 1 "$REPO_URL" "$REPO_LOCAL_PATH" + if [ $? -ne 0 ]; then + log_message "ERROR" "Failed to clone repository. Exiting." + exit 1 + fi + log_message "INFO" "Repository cloned successfully." + fi +} + +check_for_updates() { + local last_commit_hash + local current_commit_hash + + # Get the last known commit hash (from a file) + if [ -f "$REPO_LOCAL_PATH/.git-monitor-last-commit" ]; then + last_commit_hash=$(cat "$REPO_LOCAL_PATH/.git-monitor-last-commit") + else + last_commit_hash="" # Initialize if file doesn't exist + fi + + # Fetch the latest commit hash from the remote repository + current_commit_hash=$(git -C "$REPO_LOCAL_PATH" branch --format '%(sha)') + + if [ -z "$last_commit_hash" ] || [ "$current_commit_hash" != "$last_commit_hash" ]; then + log_message "INFO" "New commits detected. Pulling..." + git -C "$REPO_LOCAL_PATH" pull origin "$BRANCH_NAME" + if [ $? -eq 0 ]; then + log_message "INFO" "Pull successful." + + # Update the last commit hash file + echo "$current_commit_hash" > "$REPO_LOCAL_PATH/.git-monitor-last-commit" + else + log_message "ERROR" "Pull failed." + fi + fi +} + +# Main Execution +check_lock +create_lock + +log_message "INFO" "Git monitor started." + +initialize_repo + +while true; do + check_for_updates + sleep "$POLL_INTERVAL" +done + +remove_lock # This won't be reached in this infinite loop. Useful if exiting gracefully. diff --git a/units/git-monitor.service b/units/git-monitor.service new file mode 100644 index 0000000..e69de29