Release v.1.0.25270.1 #1

Merged
jali merged 26 commits from develop into main 2025-09-27 19:36:19 +00:00
3 changed files with 80 additions and 27 deletions
Showing only changes of commit fb842558f0 - Show all commits

View File

@@ -11,7 +11,7 @@ another system.
## git-monitor ## 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 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. 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 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 ```bash
sudo systemctl enable git-monitor.service 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:
<table>
<tr>
<th>Environment Variable</th>
<th>Description</th>
<th>Default Value</th>
</tr>
<tr>
<td>GITMONITOR_URLS</td>
<td>A file that contains the list of observed repositories in a directory by name and URL.</td>
<td>/opt/src/repository.list</td>
</tr>
<tr>
<td>GITMONITOR_LOCAL_PATH</td>
<td>The path in which the clone repositories will be stored.</td>
<td>/opt/src</td>
</tr>
<tr>
<td>GITMONITOR_BRANCH_NAME</td>
<td>The name of the branch to observe.</td>
<td>develop</td>
</tr>
<tr>
<td>GITMONITOR_POLL_INTERVALL</td>
<td>The number of seconds to wait until the next poll.</td>
<td>30</td>
</tr>
<tr>
<td>GITMONITOR_LOCK_FILE</td>
<td>The lock file that is used to monitor the service. </td>
<td>/run/lock/git-monitor.lock</td>
</tr>
</table>
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.

View File

@@ -1,12 +1,12 @@
#!/bin/bash #!/bin/bash
# Configuration (Customize these!) # Configuration
REPO_URLS="/opt/src/repository.list" [[ "x${GITMONITOR_URLS}" == "x" ]] && GITMONITOR_URLS="/opt/src/repository.list"
REPO_LOCAL_PATH="/opt/src" [[ "x${GITMONITOR_LOCAL_PATH}" == "x" ]] && GITMONITOR_LOCAL_PATH="/opt/src"
BRANCH_NAME="develop" [[ "x${GITMONITOR_BRANCH_NAME}" == "x" ]] && GITMONITOR_BRANCH_NAME="develop"
POLL_INTERVAL=60 # Seconds between checks [[ "x${GITMONITOR_POLL_INTERVAL}" == "x" ]] && GITMONITOR_POLL_INTERVAL=60 # Seconds between checks
#LOG_FILE="/var/log/git-monitor.log" # Log file for recording actions [[ "x${GITMONITOR_LOCK_FILE}" == "x" ]] && GITMONITOR_LOCK_FILE="/run/lock/git-monitor.lock" # File to prevent multiple instances
LOCK_FILE="/run/lock/git-monitor.lock" # File to prevent multiple instances
IFS="|" IFS="|"
# Functions # Functions
@@ -19,14 +19,14 @@ log_message() {
} }
check_lock() { check_lock() {
if [ -f "$LOCK_FILE" ]; then if [ -f "$GITMONITOR_LOCK_FILE" ]; then
log_message "WARN" "Another instance is already running. Exiting." log_message "WARN" "Another instance is already running. Exiting."
exit 1 exit 1
fi fi
} }
create_lock() { create_lock() {
touch "$LOCK_FILE" touch "$GITMONITOR_LOCK_FILE"
} }
run_after() { run_after() {
@@ -36,17 +36,17 @@ run_after() {
initialize_repos() { initialize_repos() {
while read -r repo_name repo_url; do while read -r repo_name repo_url; do
if [ ! -d "$REPO_LOCAL_PATH/$repo_name" ]; then if [ ! -d "$GITMONITOR_LOCAL_PATH/$repo_name" ]; then
log_message "INFO" "Repository directory $REPO_LOCAL_PATH/$repo_name does not exist. Cloning..." log_message "INFO" "Repository directory $GITMONITO_LOCAL_PATH/$repo_name does not exist. Cloning..."
mkdir -p "$REPO_LOCAL_PATH/$repo_name" mkdir -p "$GITMONITOR_LOCAL_PATH/$repo_name"
git clone --depth 1 "$repo_url" "$REPO_LOCAL_PATH/$repo_name" git clone --depth 1 "$repo_url" "$GITMONITOR_LOCAL_PATH/$repo_name"
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
log_message "ERROR" "Failed to clone repository. Exiting." log_message "ERROR" "Failed to clone repository. Exiting."
exit 1 exit 1
fi fi
log_message "INFO" "Repository cloned successfully." log_message "INFO" "Repository cloned successfully."
fi fi
done <$REPO_URLS done <$GITMONITOR_URLS
} }
check_for_updates() { check_for_updates() {
@@ -55,30 +55,32 @@ check_for_updates() {
local current_commit_hash local current_commit_hash
# Get the last known commit hash (from a file) # Get the last known commit hash (from a file)
cd "$REPO_LOCAL_PATH/$repo_name" cd "$GITMONITOR
if [ -f "$REPO_LOCAL_PATH/$repo_name/.git-monitor-last-commit" ]; then _LOCAL_PATH/$repo_name"
last_commit_hash=$(cat "$REPO_LOCAL_PATH/$repo_name/.git-monitor-last-commit") 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 else
last_commit_hash="" # Initialize if file doesn't exist last_commit_hash="" # Initialize if file doesn't exist
fi fi
# Fetch the latest commit hash from the remote repository # Fetch the latest commit hash from the remote repository
git -C "$REPO_LOCAL_PATH/$repo_name" fetch origin $BRANCH_NAME git -C "$GITMONITOR_LOCAL_PATH/$repo_name" fetch origin $GITMONITOR_BRANCH_NAME
current_commit_hash=$(git -C "$REPO_LOCAL_PATH/$repo_name" rev-parse origin $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 if [ -z "$last_commit_hash" ] || [ "$current_commit_hash" != "$last_commit_hash" ]; then
log_message "INFO" "New commits detected. Pulling..." 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 if [ $? -eq 0 ]; then
log_message "INFO" "Pull successful." log_message "INFO" "Pull successful."
# run local scripts # run local scripts
if [ -f "$REPO_LOCAL_PATH/$repo_name/.git-monitor_after.sh" ]; then if [ -f "$GITMONITOR_LOCAL_PATH/$repo_name/.git-monitor_after.sh" ]; then
run_after "$REPO_LOCAL_PATH/$repo_name/.git-monitor_after.sh" run_after "$GITMONITOR_LOCAL_PATH/$repo_name/.git-monitor_after.sh"
fi fi
# Update the last commit hash file # 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 else
log_message "ERROR" "Pull failed." log_message "ERROR" "Pull failed."
fi fi
@@ -87,7 +89,7 @@ check_for_updates() {
# Main Execution # Main Execution
trap 'rm -f $LOCK_FILE' EXIT trap 'rm -f $GITMONITOR_LOCK_FILE' EXIT
check_lock check_lock
create_lock create_lock
@@ -99,7 +101,7 @@ initialize_repos
while true; do while true; do
while read -r repo_name repo_url; do while read -r repo_name repo_url; do
check_for_updates $repo_name check_for_updates $repo_name
done < $REPO_URLS done < $GITMONITOR_URLS
sleep "$POLL_INTERVAL" sleep "$GITMONITOR_POLL_INTERVAL"
done done

View File

@@ -7,6 +7,11 @@ ExecStart=/usr/local/bin/git-monitor
Restart=on-failure Restart=on-failure
User=monitor User=monitor
WorkingDirectory=/opt/src 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] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target