From dc583498af7eea792977978f45cae62bdbd606d3 Mon Sep 17 00:00:00 2001 From: Jali Date: Sat, 20 Sep 2025 22:14:49 +0200 Subject: [PATCH 01/26] Add skeleton code --- README.md | 21 +++++++++ services/git-monitor | 89 +++++++++++++++++++++++++++++++++++++++ units/git-monitor.service | 0 3 files changed, 110 insertions(+) create mode 100644 services/git-monitor create mode 100644 units/git-monitor.service 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 -- 2.51.0 From b0f1c2dc059b2a21be1d1f231e891646d7e30809 Mon Sep 17 00:00:00 2001 From: Jali Date: Sat, 20 Sep 2025 22:18:15 +0200 Subject: [PATCH 02/26] Write systemd service file --- units/git-monitor.service | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/units/git-monitor.service b/units/git-monitor.service index e69de29..ffc0b61 100644 --- a/units/git-monitor.service +++ b/units/git-monitor.service @@ -0,0 +1,12 @@ +[Unit] +Description=Get Repository Monitor +After=network.target + +[Service] +ExecStart=/usr/local/bin/git-monitor +Restart=on-failure +User=git-monitor +WorkingDirectory=/opt/src + +[Install] +WantedBy=multi-user.target -- 2.51.0 From 147c7f28d53707f9dc2ea43cd6c403bc9034c5c5 Mon Sep 17 00:00:00 2001 From: Jali Date: Sat, 20 Sep 2025 22:30:00 +0200 Subject: [PATCH 03/26] Add installation scripts and documentation --- README.md | 17 +++++++++++++++++ install.sh | 15 +++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 install.sh diff --git a/README.md b/README.md index f373e74..ec61258 100644 --- a/README.md +++ b/README.md @@ -19,3 +19,20 @@ 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. + +### Installation + +To install the git-monitor, clone this repository to the target computer, and +run the install script: + +```bash +git clone https://gitea.orca-central.de/jali/PiDP10Services.git +sudo ./install.sh +``` + +This will install the service on your system and start it. If you want the +service to be enabled on system boot, enable the service: + +```bash +sudo systemctl enable git-monitor.service +``` diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..c02a8d3 --- /dev/null +++ b/install.sh @@ -0,0 +1,15 @@ +#!/bin/sh +# Install the service on the target machine + +SERVICEDIR="/usr/local/bin" +SYSTEMDDIR="/etc/systemd/system" + +echo "- Installing the service files" +cp ./services/git-monitor $SERVICEDIR +cp ./units/git-monitor.service $SYSTEMDDIR + +echo "- Preparing the service files" +systemctl daemon-reload +systemctl start git-monitor.service + +echo "- Installation complete." -- 2.51.0 From ea1e22d3e4b8a21fdf2c177534d7b30a7df7eea8 Mon Sep 17 00:00:00 2001 From: Jali Date: Sat, 20 Sep 2025 23:34:37 +0200 Subject: [PATCH 04/26] Modify the monitor to listen to multiple repositories. --- services/git-monitor | 47 +++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/services/git-monitor b/services/git-monitor index 36cae64..71dc169 100644 --- a/services/git-monitor +++ b/services/git-monitor @@ -1,8 +1,8 @@ #!/bin/bash # Configuration (Customize these!) -REPO_URL="ssh://gitea@git.orca-central.de:10229/jali/PDP10Sudoku.git" -REPO_LOCAL_PATH="/home/jali/Projects/PDP10Sudoku" +REPO_URLS="/opt/src/repository.list" +REPO_LOCAL_PATH="/opt/src" BRANCH_NAME="develop" POLL_INTERVAL=60 # Seconds between checks #LOG_FILE="/var/log/git-monitor.log" # Log file for recording actions @@ -32,41 +32,49 @@ 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 +run_after() { + local after_pull_script=$1 + source $after_pull_script +} + +initialize_repos() { + while IFS=: read -r REPO_NAME REPO_URL; do + if [ ! -d "$REPO_LOCAL_PATH/$REPO_NAME" ]; then + log_message "INFO" "Repository directory does not exist. Cloning..." + mkdir -p "$REPO_LOCAL_PATH" + git clone --depth 1 "$REPO_URL" "$REPO_LOCAL_PATH/$REPO_NAME" + if [ $? -ne 0 ]; then + log_message "ERROR" "Failed to clone repository. Exiting." + exit 1 + fi + log_message "INFO" "Repository cloned successfully." fi - log_message "INFO" "Repository cloned successfully." - fi + done <$REPO_URLS } check_for_updates() { + local repo_name=$1 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") + if [ -f "$REPO_LOCAL_PATH/$repo_name/.git-monitor-last-commit" ]; then + last_commit_hash=$(cat "$REPO_LOCAL_PATH/$repo_name/.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)') + current_commit_hash=$(git -C "$REPO_LOCAL_PATH/$repo_name" 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" + git -C "$REPO_LOCAL_PATH/$repo_name" 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" + echo "$current_commit_hash" > "$REPO_LOCAL_PATH/$repo_name/.git-monitor-last-commit" else log_message "ERROR" "Pull failed." fi @@ -82,7 +90,10 @@ log_message "INFO" "Git monitor started." initialize_repo while true; do - check_for_updates + while IFS=: read -r $REPO_NAME $REPO_URL; do + check_for_updates $REPO_NAME + run_after "$REPO_LOCAL_PATH/$REPO_NAME/.git-monitor_after.sh" + done < $REPO_URLS sleep "$POLL_INTERVAL" done -- 2.51.0 From ce56339fde745f4f7c297f129f5b5abb1cd08b0c Mon Sep 17 00:00:00 2001 From: Jali Date: Sun, 21 Sep 2025 00:15:28 +0200 Subject: [PATCH 05/26] Change the user name to 'monitor' --- units/git-monitor.service | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/units/git-monitor.service b/units/git-monitor.service index ffc0b61..2f15469 100644 --- a/units/git-monitor.service +++ b/units/git-monitor.service @@ -5,7 +5,7 @@ After=network.target [Service] ExecStart=/usr/local/bin/git-monitor Restart=on-failure -User=git-monitor +User=monitor WorkingDirectory=/opt/src [Install] -- 2.51.0 From 0d635a00958b5359556773ca3a1ea15c1fd4e2d9 Mon Sep 17 00:00:00 2001 From: Jali Date: Sun, 21 Sep 2025 00:17:27 +0200 Subject: [PATCH 06/26] Make scripts executeable --- install.sh | 0 services/git-monitor | 0 2 files changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 install.sh mode change 100644 => 100755 services/git-monitor diff --git a/install.sh b/install.sh old mode 100644 new mode 100755 diff --git a/services/git-monitor b/services/git-monitor old mode 100644 new mode 100755 -- 2.51.0 From 2f8b01b76823abc24e955f49284d23e45462d639 Mon Sep 17 00:00:00 2001 From: Jali Date: Sun, 21 Sep 2025 00:24:04 +0200 Subject: [PATCH 07/26] Change the delimiter in the repository file from ":" to "|" Done in order to prevent problems with the ":" in URIs --- services/git-monitor | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/services/git-monitor b/services/git-monitor index 71dc169..7b37016 100755 --- a/services/git-monitor +++ b/services/git-monitor @@ -7,6 +7,7 @@ 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 +IFS="|" # Functions log_message() { @@ -38,7 +39,7 @@ run_after() { } initialize_repos() { - while IFS=: read -r REPO_NAME REPO_URL; do + while read -r REPO_NAME REPO_URL; do if [ ! -d "$REPO_LOCAL_PATH/$REPO_NAME" ]; then log_message "INFO" "Repository directory does not exist. Cloning..." mkdir -p "$REPO_LOCAL_PATH" @@ -90,7 +91,7 @@ log_message "INFO" "Git monitor started." initialize_repo while true; do - while IFS=: read -r $REPO_NAME $REPO_URL; do + while read -r $REPO_NAME $REPO_URL; do check_for_updates $REPO_NAME run_after "$REPO_LOCAL_PATH/$REPO_NAME/.git-monitor_after.sh" done < $REPO_URLS -- 2.51.0 From be116c222e13950d8fb7afef390a80633a882269 Mon Sep 17 00:00:00 2001 From: Jali Date: Sun, 21 Sep 2025 00:36:31 +0200 Subject: [PATCH 08/26] Fix bugs in service script - Call initialize_repos - only call .git-monitor_after.sh only if it exists --- services/git-monitor | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/services/git-monitor b/services/git-monitor index 7b37016..40d0047 100755 --- a/services/git-monitor +++ b/services/git-monitor @@ -41,8 +41,8 @@ run_after() { initialize_repos() { while read -r REPO_NAME REPO_URL; do if [ ! -d "$REPO_LOCAL_PATH/$REPO_NAME" ]; then - log_message "INFO" "Repository directory does not exist. Cloning..." - mkdir -p "$REPO_LOCAL_PATH" + log_message "INFO" "Repository directory $REPO_LOCAL_PATH/$REOP_NAME does not exist. Cloning..." + mkdir -p "$REPO_LOCAL_PATH/$REPO_NAME" git clone --depth 1 "$REPO_URL" "$REPO_LOCAL_PATH/$REPO_NAME" if [ $? -ne 0 ]; then log_message "ERROR" "Failed to clone repository. Exiting." @@ -88,12 +88,14 @@ create_lock log_message "INFO" "Git monitor started." -initialize_repo +initialize_repos while true; do while read -r $REPO_NAME $REPO_URL; do check_for_updates $REPO_NAME - run_after "$REPO_LOCAL_PATH/$REPO_NAME/.git-monitor_after.sh" + if [ -f "$REPO_LOCAL_PATH/$REPO_NAME/.git-monitor_after.sh" ]; then + run_after "$REPO_LOCAL_PATH/$REPO_NAME/.git-monitor_after.sh" + fi done < $REPO_URLS sleep "$POLL_INTERVAL" done -- 2.51.0 From 4fc3a849f68af053d3cd8fcf3fdbc9e6d899c262 Mon Sep 17 00:00:00 2001 From: Jali Date: Sun, 21 Sep 2025 00:52:58 +0200 Subject: [PATCH 09/26] Remove the lock file handling from the service and let systemd handle it. --- services/git-monitor | 21 --------------------- units/git-monitor.service | 2 ++ 2 files changed, 2 insertions(+), 21 deletions(-) diff --git a/services/git-monitor b/services/git-monitor index 40d0047..1d211de 100755 --- a/services/git-monitor +++ b/services/git-monitor @@ -6,7 +6,6 @@ REPO_LOCAL_PATH="/opt/src" 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 IFS="|" # Functions @@ -18,21 +17,6 @@ log_message() { 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" -} - run_after() { local after_pull_script=$1 source $after_pull_script @@ -83,9 +67,6 @@ check_for_updates() { } # Main Execution -check_lock -create_lock - log_message "INFO" "Git monitor started." initialize_repos @@ -99,5 +80,3 @@ while true; do done < $REPO_URLS 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 index 2f15469..78682ca 100644 --- a/units/git-monitor.service +++ b/units/git-monitor.service @@ -4,7 +4,9 @@ After=network.target [Service] ExecStart=/usr/local/bin/git-monitor +LockFile=/run/git-monitor-lock Restart=on-failure +RemovalPolicy=onFailure User=monitor WorkingDirectory=/opt/src -- 2.51.0 From 294997fe2702a771f05cfdee1355c4c589b14aa1 Mon Sep 17 00:00:00 2001 From: Jali Date: Sun, 21 Sep 2025 00:56:00 +0200 Subject: [PATCH 10/26] Change into the directory before pulling --- services/git-monitor | 1 + 1 file changed, 1 insertion(+) diff --git a/services/git-monitor b/services/git-monitor index 1d211de..42a0a27 100755 --- a/services/git-monitor +++ b/services/git-monitor @@ -43,6 +43,7 @@ check_for_updates() { local current_commit_hash # Get the last known commit hash (from a file) + cd "$REPO_LOCAL_PATH/$REPO_NAME" if [ -f "$REPO_LOCAL_PATH/$repo_name/.git-monitor-last-commit" ]; then last_commit_hash=$(cat "$REPO_LOCAL_PATH/$repo_name/.git-monitor-last-commit") else -- 2.51.0 From 61bd0e22a42e9a7489499013b0c0b5278c4acd08 Mon Sep 17 00:00:00 2001 From: Jali Date: Sun, 21 Sep 2025 00:57:31 +0200 Subject: [PATCH 11/26] Set the lock file path to /run/lock --- units/git-monitor.service | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/units/git-monitor.service b/units/git-monitor.service index 78682ca..e85c666 100644 --- a/units/git-monitor.service +++ b/units/git-monitor.service @@ -4,7 +4,7 @@ After=network.target [Service] ExecStart=/usr/local/bin/git-monitor -LockFile=/run/git-monitor-lock +LockFile=/run/lock/git-monitor-lock Restart=on-failure RemovalPolicy=onFailure User=monitor -- 2.51.0 From 25961b3f6fe43354d6b8fa27f5b278a288dbfe2c Mon Sep 17 00:00:00 2001 From: Jali Date: Sun, 21 Sep 2025 01:20:34 +0200 Subject: [PATCH 12/26] Move handling of lockfiles back to the script. Use trap instead. --- services/git-monitor | 24 ++++++++++++++++++++++++ units/git-monitor.service | 2 -- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/services/git-monitor b/services/git-monitor index 42a0a27..6d50270 100755 --- a/services/git-monitor +++ b/services/git-monitor @@ -6,6 +6,7 @@ REPO_LOCAL_PATH="/opt/src" BRANCH_NAME="develop" POLL_INTERVAL=60 # Seconds between checks #LOG_FILE="/var/log/git-monitor.log" # Log file for recording actions +LOCK_FILE="/run/lock/git-monitor.lock" # File to prevent multiple instances IFS="|" # Functions @@ -17,6 +18,21 @@ log_message() { 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" +} + run_after() { local after_pull_script=$1 source $after_pull_script @@ -68,6 +84,12 @@ check_for_updates() { } # Main Execution + +trap 'remove_lock' EXIT + +check_lock +create_lock + log_message "INFO" "Git monitor started." initialize_repos @@ -81,3 +103,5 @@ while true; do done < $REPO_URLS 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 index e85c666..2f15469 100644 --- a/units/git-monitor.service +++ b/units/git-monitor.service @@ -4,9 +4,7 @@ After=network.target [Service] ExecStart=/usr/local/bin/git-monitor -LockFile=/run/lock/git-monitor-lock Restart=on-failure -RemovalPolicy=onFailure User=monitor WorkingDirectory=/opt/src -- 2.51.0 From 647bfc1987cf420f49fb596637851a5ac383ed82 Mon Sep 17 00:00:00 2001 From: Jali Date: Sun, 21 Sep 2025 01:26:08 +0200 Subject: [PATCH 13/26] Check for updates, log more info. --- services/git-monitor | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/services/git-monitor b/services/git-monitor index 6d50270..abf78b0 100755 --- a/services/git-monitor +++ b/services/git-monitor @@ -29,10 +29,6 @@ create_lock() { touch "$LOCK_FILE" } -remove_lock() { - rm -f "$LOCK_FILE" -} - run_after() { local after_pull_script=$1 source $after_pull_script @@ -59,6 +55,7 @@ check_for_updates() { local current_commit_hash # Get the last known commit hash (from a file) + echo "$REPO_LOCAL_PATH/$REPO_NAME" cd "$REPO_LOCAL_PATH/$REPO_NAME" if [ -f "$REPO_LOCAL_PATH/$repo_name/.git-monitor-last-commit" ]; then last_commit_hash=$(cat "$REPO_LOCAL_PATH/$repo_name/.git-monitor-last-commit") @@ -85,7 +82,7 @@ check_for_updates() { # Main Execution -trap 'remove_lock' EXIT +trap 'rm -f $LOCKFILE' EXIT check_lock create_lock @@ -104,4 +101,3 @@ while true; do sleep "$POLL_INTERVAL" done -remove_lock # This won't be reached in this infinite loop. Useful if exiting gracefully. -- 2.51.0 From 181e6c54daf300e53fb9c6929a518d90dee6d642 Mon Sep 17 00:00:00 2001 From: Jali Date: Sun, 21 Sep 2025 01:29:21 +0200 Subject: [PATCH 14/26] Fix problems with LOCK_FILE name --- services/git-monitor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/git-monitor b/services/git-monitor index abf78b0..6c671d7 100755 --- a/services/git-monitor +++ b/services/git-monitor @@ -82,7 +82,7 @@ check_for_updates() { # Main Execution -trap 'rm -f $LOCKFILE' EXIT +trap 'rm -f $LOCK_FILE' EXIT check_lock create_lock -- 2.51.0 From 74dfbec98bb866dd5e9baad774e74f2bdf4480fc Mon Sep 17 00:00:00 2001 From: Jali Date: Sun, 21 Sep 2025 01:33:11 +0200 Subject: [PATCH 15/26] Use correct lower case style for $repo_name --- services/git-monitor | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/services/git-monitor b/services/git-monitor index 6c671d7..524d96b 100755 --- a/services/git-monitor +++ b/services/git-monitor @@ -55,8 +55,7 @@ check_for_updates() { local current_commit_hash # Get the last known commit hash (from a file) - echo "$REPO_LOCAL_PATH/$REPO_NAME" - cd "$REPO_LOCAL_PATH/$REPO_NAME" + cd "$REPO_LOCAL_PATH/$repo_name" if [ -f "$REPO_LOCAL_PATH/$repo_name/.git-monitor-last-commit" ]; then last_commit_hash=$(cat "$REPO_LOCAL_PATH/$repo_name/.git-monitor-last-commit") else -- 2.51.0 From 0a0e38e276f5d4129b1ce5519114a2a5da1dc5d4 Mon Sep 17 00:00:00 2001 From: Jali Date: Sun, 21 Sep 2025 01:35:44 +0200 Subject: [PATCH 16/26] Log the directory change --- services/git-monitor | 1 + 1 file changed, 1 insertion(+) diff --git a/services/git-monitor b/services/git-monitor index 524d96b..3028ae8 100755 --- a/services/git-monitor +++ b/services/git-monitor @@ -55,6 +55,7 @@ check_for_updates() { local current_commit_hash # Get the last known commit hash (from a file) + log_message "INFO" "Changing directory into $REPO_LOCAL_PATH/$repo_name" cd "$REPO_LOCAL_PATH/$repo_name" if [ -f "$REPO_LOCAL_PATH/$repo_name/.git-monitor-last-commit" ]; then last_commit_hash=$(cat "$REPO_LOCAL_PATH/$repo_name/.git-monitor-last-commit") -- 2.51.0 From ccfea088b9987712aacf12d8f3ff293131eb913e Mon Sep 17 00:00:00 2001 From: Jali Date: Sun, 21 Sep 2025 01:40:07 +0200 Subject: [PATCH 17/26] Change writing and remove unneeded $ --- services/git-monitor | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/services/git-monitor b/services/git-monitor index 3028ae8..9e9d3c7 100755 --- a/services/git-monitor +++ b/services/git-monitor @@ -35,11 +35,11 @@ run_after() { } initialize_repos() { - while read -r REPO_NAME REPO_URL; do - if [ ! -d "$REPO_LOCAL_PATH/$REPO_NAME" ]; then + while read -r repo_name repo_url; do + if [ ! -d "$REPO_LOCAL_PATH/$repo_name" ]; then log_message "INFO" "Repository directory $REPO_LOCAL_PATH/$REOP_NAME does not exist. Cloning..." - mkdir -p "$REPO_LOCAL_PATH/$REPO_NAME" - git clone --depth 1 "$REPO_URL" "$REPO_LOCAL_PATH/$REPO_NAME" + mkdir -p "$REPO_LOCAL_PATH/$repo_name" + git clone --depth 1 "$REPO_URL" "$REPO_LOCAL_PATH/$repo_name" if [ $? -ne 0 ]; then log_message "ERROR" "Failed to clone repository. Exiting." exit 1 @@ -92,10 +92,10 @@ log_message "INFO" "Git monitor started." initialize_repos while true; do - while read -r $REPO_NAME $REPO_URL; do - check_for_updates $REPO_NAME - if [ -f "$REPO_LOCAL_PATH/$REPO_NAME/.git-monitor_after.sh" ]; then - run_after "$REPO_LOCAL_PATH/$REPO_NAME/.git-monitor_after.sh" + while read -r repo_name repo_url; do + check_for_updates $repo_name + if [ -f "$REPO_LOCAL_PATH/$repo_name/.git-monitor_after.sh" ]; then + run_after "$REPO_LOCAL_PATH/$repo_name/.git-monitor_after.sh" fi done < $REPO_URLS sleep "$POLL_INTERVAL" -- 2.51.0 From 9fd283fff2eac4fc0bc018e510380aa88c914287 Mon Sep 17 00:00:00 2001 From: Jali Date: Sun, 21 Sep 2025 01:42:43 +0200 Subject: [PATCH 18/26] Fix lower case problem in initialize_repos --- services/git-monitor | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/git-monitor b/services/git-monitor index 9e9d3c7..faa83dd 100755 --- a/services/git-monitor +++ b/services/git-monitor @@ -37,9 +37,9 @@ run_after() { initialize_repos() { while read -r repo_name repo_url; do if [ ! -d "$REPO_LOCAL_PATH/$repo_name" ]; then - log_message "INFO" "Repository directory $REPO_LOCAL_PATH/$REOP_NAME does not exist. Cloning..." + log_message "INFO" "Repository directory $REPO_LOCAL_PATH/$repo_name does not exist. Cloning..." mkdir -p "$REPO_LOCAL_PATH/$repo_name" - git clone --depth 1 "$REPO_URL" "$REPO_LOCAL_PATH/$repo_name" + git clone --depth 1 "$repo_url" "$REPO_LOCAL_PATH/$repo_name" if [ $? -ne 0 ]; then log_message "ERROR" "Failed to clone repository. Exiting." exit 1 -- 2.51.0 From 415ef4f8656520d41904a75ae82f8de6efad7349 Mon Sep 17 00:00:00 2001 From: Jali Date: Sun, 21 Sep 2025 01:56:36 +0200 Subject: [PATCH 19/26] Use the actual commit hash to identify the commit --- services/git-monitor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/git-monitor b/services/git-monitor index faa83dd..49065f3 100755 --- a/services/git-monitor +++ b/services/git-monitor @@ -64,7 +64,7 @@ check_for_updates() { fi # Fetch the latest commit hash from the remote repository - current_commit_hash=$(git -C "$REPO_LOCAL_PATH/$repo_name" branch --format '%(sha)') + current_commit_hash=$(git -C "$REPO_LOCAL_PATH/$repo_name" rev-parse HEAD) if [ -z "$last_commit_hash" ] || [ "$current_commit_hash" != "$last_commit_hash" ]; then log_message "INFO" "New commits detected. Pulling..." -- 2.51.0 From 7278d79773ab5d190545d9fbe734fcdf9fc59cfb Mon Sep 17 00:00:00 2001 From: Jali Date: Sun, 21 Sep 2025 02:03:56 +0200 Subject: [PATCH 20/26] Remove unneccesary log message --- services/git-monitor | 1 - 1 file changed, 1 deletion(-) diff --git a/services/git-monitor b/services/git-monitor index 49065f3..6a9ca88 100755 --- a/services/git-monitor +++ b/services/git-monitor @@ -55,7 +55,6 @@ check_for_updates() { local current_commit_hash # Get the last known commit hash (from a file) - log_message "INFO" "Changing directory into $REPO_LOCAL_PATH/$repo_name" cd "$REPO_LOCAL_PATH/$repo_name" if [ -f "$REPO_LOCAL_PATH/$repo_name/.git-monitor-last-commit" ]; then last_commit_hash=$(cat "$REPO_LOCAL_PATH/$repo_name/.git-monitor-last-commit") -- 2.51.0 From ed7f4974034644b154221813fd9309fa2fdcc5ab Mon Sep 17 00:00:00 2001 From: Jali Date: Sun, 21 Sep 2025 02:33:24 +0200 Subject: [PATCH 21/26] Add a fetch step to make sure the script is on par with the git server --- services/git-monitor | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/services/git-monitor b/services/git-monitor index 6a9ca88..99c414b 100755 --- a/services/git-monitor +++ b/services/git-monitor @@ -63,11 +63,12 @@ check_for_updates() { fi # Fetch the latest commit hash from the remote repository - current_commit_hash=$(git -C "$REPO_LOCAL_PATH/$repo_name" rev-parse HEAD) + git -C "$REPO_LOCAL_PATH/$repo_name" fetch origin $BRANCH_NAME + current_commit_hash=$(git -C "$REPO_LOCAL_PATH/$repo_name" origin $BRANCH_NAME) if [ -z "$last_commit_hash" ] || [ "$current_commit_hash" != "$last_commit_hash" ]; then log_message "INFO" "New commits detected. Pulling..." - git -C "$REPO_LOCAL_PATH/$repo_name" pull origin "$BRANCH_NAME" + git -C "$REPO_LOCAL_PATH/$repo_name" checkout origin "$BRANCH_NAME" if [ $? -eq 0 ]; then log_message "INFO" "Pull successful." -- 2.51.0 From 0365b82dec9f530a3402925203c178fe95e88a40 Mon Sep 17 00:00:00 2001 From: Jali Date: Sun, 21 Sep 2025 02:36:23 +0200 Subject: [PATCH 22/26] Fix checkout --- services/git-monitor | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/git-monitor b/services/git-monitor index 99c414b..b1bf38b 100755 --- a/services/git-monitor +++ b/services/git-monitor @@ -63,12 +63,12 @@ check_for_updates() { fi # Fetch the latest commit hash from the remote repository - git -C "$REPO_LOCAL_PATH/$repo_name" fetch origin $BRANCH_NAME + git -C "$REPO_LOCAL_PATH/$" fetch origin $BRANCH_NAME current_commit_hash=$(git -C "$REPO_LOCAL_PATH/$repo_name" origin $BRANCH_NAME) if [ -z "$last_commit_hash" ] || [ "$current_commit_hash" != "$last_commit_hash" ]; then log_message "INFO" "New commits detected. Pulling..." - git -C "$REPO_LOCAL_PATH/$repo_name" checkout origin "$BRANCH_NAME" + git -C "$REPO_LOCAL_PATH/$repo_name" checkout $BRANCH_NAME if [ $? -eq 0 ]; then log_message "INFO" "Pull successful." -- 2.51.0 From 43ce6efd43993e2d3f6835e257645fd1075852de Mon Sep 17 00:00:00 2001 From: Jali Date: Sun, 21 Sep 2025 02:38:59 +0200 Subject: [PATCH 23/26] Fix nonsense created by linter --- services/git-monitor | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/git-monitor b/services/git-monitor index b1bf38b..492d6f0 100755 --- a/services/git-monitor +++ b/services/git-monitor @@ -63,8 +63,8 @@ check_for_updates() { fi # Fetch the latest commit hash from the remote repository - git -C "$REPO_LOCAL_PATH/$" fetch origin $BRANCH_NAME - current_commit_hash=$(git -C "$REPO_LOCAL_PATH/$repo_name" origin $BRANCH_NAME) + git -C "$REPO_LOCAL_PATH/$repo_name" fetch origin $BRANCH_NAME + current_commit_hash=$(git -C "$REPO_LOCAL_PATH/$repo_name" rev-parse origin $BRANCH_NAME) if [ -z "$last_commit_hash" ] || [ "$current_commit_hash" != "$last_commit_hash" ]; then log_message "INFO" "New commits detected. Pulling..." -- 2.51.0 From 474c027d6d594a17eba79d0c2b298b359cd67f7a Mon Sep 17 00:00:00 2001 From: Jali Date: Sun, 21 Sep 2025 02:40:32 +0200 Subject: [PATCH 24/26] Add missing pull --- services/git-monitor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/git-monitor b/services/git-monitor index 492d6f0..bd4855c 100755 --- a/services/git-monitor +++ b/services/git-monitor @@ -68,7 +68,7 @@ check_for_updates() { if [ -z "$last_commit_hash" ] || [ "$current_commit_hash" != "$last_commit_hash" ]; then log_message "INFO" "New commits detected. Pulling..." - git -C "$REPO_LOCAL_PATH/$repo_name" checkout $BRANCH_NAME + git -C "$REPO_LOCAL_PATH/$repo_name" pull origin $BRANCH_NAME if [ $? -eq 0 ]; then log_message "INFO" "Pull successful." -- 2.51.0 From 6a66cc0b9639d495572321f7b315ac351c45f014 Mon Sep 17 00:00:00 2001 From: Jali Date: Sun, 21 Sep 2025 02:55:04 +0200 Subject: [PATCH 25/26] Only run the run_after stage, when a successful pull happened. --- services/git-monitor | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/services/git-monitor b/services/git-monitor index bd4855c..051dee6 100755 --- a/services/git-monitor +++ b/services/git-monitor @@ -72,6 +72,11 @@ check_for_updates() { if [ $? -eq 0 ]; then log_message "INFO" "Pull successful." + # run local scripts + if [ -f "$REPO_LOCAL_PATH/$repo_name/.git-monitor_after.sh" ]; then + run_after "$REPO_LOCAL_PATH/$repo_name/.git-monitor_after.sh" + fi + # Update the last commit hash file echo "$current_commit_hash" > "$REPO_LOCAL_PATH/$repo_name/.git-monitor-last-commit" else @@ -94,9 +99,6 @@ initialize_repos while true; do while read -r repo_name repo_url; do check_for_updates $repo_name - if [ -f "$REPO_LOCAL_PATH/$repo_name/.git-monitor_after.sh" ]; then - run_after "$REPO_LOCAL_PATH/$repo_name/.git-monitor_after.sh" - fi done < $REPO_URLS sleep "$POLL_INTERVAL" done -- 2.51.0 From fb842558f0330d10fc185e0a76ff347689459633 Mon Sep 17 00:00:00 2001 From: Jali Date: Sat, 27 Sep 2025 21:32:15 +0200 Subject: [PATCH 26/26] Move configuration into the unit file --- README.md | 48 +++++++++++++++++++++++++++++++++- services/git-monitor | 54 ++++++++++++++++++++------------------- units/git-monitor.service | 5 ++++ 3 files changed, 80 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index ec61258..58522e1 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ another system. ## git-monitor -The git-monitor is a system service, that monitors a target directory on the +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 @@ -36,3 +36,49 @@ service to be enabled on system boot, enable the service: ```bash sudo systemctl enable git-monitor.service ``` + +### Configuration + +The service is usable immediately after installation. The service is configured +through a series of environment variables, that can be set to change the default +behaviour. There are five different environment variables to configure the +service with: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Environment VariableDescriptionDefault Value
GITMONITOR_URLSA file that contains the list of observed repositories in a directory by name and URL./opt/src/repository.list
GITMONITOR_LOCAL_PATHThe path in which the clone repositories will be stored./opt/src
GITMONITOR_BRANCH_NAMEThe name of the branch to observe.develop
GITMONITOR_POLL_INTERVALLThe number of seconds to wait until the next poll.30
GITMONITOR_LOCK_FILEThe lock file that is used to monitor the service. /run/lock/git-monitor.lock
+ +You can override these settings by creating an override file for the service +unit. Create a new file +`/etc/serviced/service/git-monitor.service.d/override.conf` by typing +`sudo systemctl edit git-monitor.service`. You can then uncomment the +`Environment` statements, and change the value of the variables accordingly. diff --git a/services/git-monitor b/services/git-monitor index 051dee6..3d59819 100755 --- a/services/git-monitor +++ b/services/git-monitor @@ -1,12 +1,12 @@ #!/bin/bash -# Configuration (Customize these!) -REPO_URLS="/opt/src/repository.list" -REPO_LOCAL_PATH="/opt/src" -BRANCH_NAME="develop" -POLL_INTERVAL=60 # Seconds between checks -#LOG_FILE="/var/log/git-monitor.log" # Log file for recording actions -LOCK_FILE="/run/lock/git-monitor.lock" # File to prevent multiple instances +# Configuration +[[ "x${GITMONITOR_URLS}" == "x" ]] && GITMONITOR_URLS="/opt/src/repository.list" +[[ "x${GITMONITOR_LOCAL_PATH}" == "x" ]] && GITMONITOR_LOCAL_PATH="/opt/src" +[[ "x${GITMONITOR_BRANCH_NAME}" == "x" ]] && GITMONITOR_BRANCH_NAME="develop" +[[ "x${GITMONITOR_POLL_INTERVAL}" == "x" ]] && GITMONITOR_POLL_INTERVAL=60 # Seconds between checks +[[ "x${GITMONITOR_LOCK_FILE}" == "x" ]] && GITMONITOR_LOCK_FILE="/run/lock/git-monitor.lock" # File to prevent multiple instances + IFS="|" # Functions @@ -19,14 +19,14 @@ log_message() { } check_lock() { - if [ -f "$LOCK_FILE" ]; then + if [ -f "$GITMONITOR_LOCK_FILE" ]; then log_message "WARN" "Another instance is already running. Exiting." exit 1 fi } create_lock() { - touch "$LOCK_FILE" + touch "$GITMONITOR_LOCK_FILE" } run_after() { @@ -36,17 +36,17 @@ run_after() { initialize_repos() { while read -r repo_name repo_url; do - if [ ! -d "$REPO_LOCAL_PATH/$repo_name" ]; then - log_message "INFO" "Repository directory $REPO_LOCAL_PATH/$repo_name does not exist. Cloning..." - mkdir -p "$REPO_LOCAL_PATH/$repo_name" - git clone --depth 1 "$repo_url" "$REPO_LOCAL_PATH/$repo_name" + if [ ! -d "$GITMONITOR_LOCAL_PATH/$repo_name" ]; then + log_message "INFO" "Repository directory $GITMONITO_LOCAL_PATH/$repo_name does not exist. Cloning..." + mkdir -p "$GITMONITOR_LOCAL_PATH/$repo_name" + git clone --depth 1 "$repo_url" "$GITMONITOR_LOCAL_PATH/$repo_name" if [ $? -ne 0 ]; then log_message "ERROR" "Failed to clone repository. Exiting." exit 1 fi log_message "INFO" "Repository cloned successfully." fi - done <$REPO_URLS + done <$GITMONITOR_URLS } check_for_updates() { @@ -55,30 +55,32 @@ check_for_updates() { local current_commit_hash # Get the last known commit hash (from a file) - cd "$REPO_LOCAL_PATH/$repo_name" - if [ -f "$REPO_LOCAL_PATH/$repo_name/.git-monitor-last-commit" ]; then - last_commit_hash=$(cat "$REPO_LOCAL_PATH/$repo_name/.git-monitor-last-commit") + cd "$GITMONITOR +_LOCAL_PATH/$repo_name" + if [ -f "$GITMONITOR_LOCAL_PATH/$repo_name/.git-monitor-last-commit" ]; then + last_commit_hash=$(cat "$GITMONITOR_LOCAL_PATH/$repo_name/.git-monitor-last-commit") else last_commit_hash="" # Initialize if file doesn't exist fi # Fetch the latest commit hash from the remote repository - git -C "$REPO_LOCAL_PATH/$repo_name" fetch origin $BRANCH_NAME - current_commit_hash=$(git -C "$REPO_LOCAL_PATH/$repo_name" rev-parse origin $BRANCH_NAME) + git -C "$GITMONITOR_LOCAL_PATH/$repo_name" fetch origin $GITMONITOR_BRANCH_NAME + current_commit_hash=$(git -C "$GITMONITOR_LOCAL_PATH/$repo_name" rev-parse origin $GITMONITOR_BRANCH_NAME) if [ -z "$last_commit_hash" ] || [ "$current_commit_hash" != "$last_commit_hash" ]; then log_message "INFO" "New commits detected. Pulling..." - git -C "$REPO_LOCAL_PATH/$repo_name" pull origin $BRANCH_NAME + git -C "$GITMONITOR + _LOCAL_PATH/$repo_name" pull origin $GITMONITOR_BRANCH_NAME if [ $? -eq 0 ]; then log_message "INFO" "Pull successful." # run local scripts - if [ -f "$REPO_LOCAL_PATH/$repo_name/.git-monitor_after.sh" ]; then - run_after "$REPO_LOCAL_PATH/$repo_name/.git-monitor_after.sh" + if [ -f "$GITMONITOR_LOCAL_PATH/$repo_name/.git-monitor_after.sh" ]; then + run_after "$GITMONITOR_LOCAL_PATH/$repo_name/.git-monitor_after.sh" fi # Update the last commit hash file - echo "$current_commit_hash" > "$REPO_LOCAL_PATH/$repo_name/.git-monitor-last-commit" + echo "$current_commit_hash" > "$GITMONITOR_LOCAL_PATH/$repo_name/.git-monitor-last-commit" else log_message "ERROR" "Pull failed." fi @@ -87,7 +89,7 @@ check_for_updates() { # Main Execution -trap 'rm -f $LOCK_FILE' EXIT +trap 'rm -f $GITMONITOR_LOCK_FILE' EXIT check_lock create_lock @@ -99,7 +101,7 @@ initialize_repos while true; do while read -r repo_name repo_url; do check_for_updates $repo_name - done < $REPO_URLS - sleep "$POLL_INTERVAL" + done < $GITMONITOR_URLS + sleep "$GITMONITOR_POLL_INTERVAL" done diff --git a/units/git-monitor.service b/units/git-monitor.service index 2f15469..9a26484 100644 --- a/units/git-monitor.service +++ b/units/git-monitor.service @@ -7,6 +7,11 @@ ExecStart=/usr/local/bin/git-monitor Restart=on-failure User=monitor WorkingDirectory=/opt/src +Environment="GITMONITOR_URLS=/opt/src/repository.list" +Environment="GITMONITOR_LOCAL_PATH=/opt/src" +Environment="GITMONITOR_BRANCH_NAME=develop" +Environment="GITMONITOR_POLL_INTERVALL=30" +Environment="GITMONITOR_LOCK_FILE=/run/lock/git-monitor.lock" [Install] WantedBy=multi-user.target -- 2.51.0