Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions mirrors/files/linux-mint/images/confd/mintUpdateImages.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[Unit]
Description=Keeping the linux mint mirror images up to date
After=network-online.target
Wants=network-online.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/linux-mint-sync-images.sh
TimeoutStartSec=7200
User=mirrors
Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The service specifies User=mirrors, but there's no task in the Ansible playbook to create this user. Ensure the user exists on the target system, or add a task to create it before deploying these services.

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should I write the task in main instead?, because the user most exist for every mirror

10 changes: 10 additions & 0 deletions mirrors/files/linux-mint/images/confd/mintUpdateImages.timer
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[Unit]
Description=Activate service for syncing changes in the linux mint mirror

[Timer]
OnCalendar=*:0/15
AccuracySec=5m
Persistent=true

[Install]
WantedBy=timers.target
126 changes: 126 additions & 0 deletions mirrors/files/linux-mint/images/scripts/linux-mint-sync-images.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#!/bin/bash

# This script checks the status of the upstream Linux Mint server and the local mirror.
# It compares the last modification time of a reference file
# and checks the following conditions:
#
# - If the upstream mirror has new updates, the local mirror fetches them
# - If the upstream mirror was updated less than 24 hours ago, the local mirror is considered up-to-date and no action is taken.
# - If the upstream mirror was updated more than 24 hours ago it synchronizes again
#
# This script is intended to be run repeatedly by a cron job or a systemd timer.


MIRROR_DIRECTORY=/srv/linux-mint
LOCAL_MIRROR="lidsol.fi-b.unam.mx"
Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable LOCAL_MIRROR is defined but never used in the script. Consider removing it or implementing the intended functionality.

Suggested change
LOCAL_MIRROR="lidsol.fi-b.unam.mx"

Copilot uses AI. Check for mistakes.
MIRROR_DIRECTORY_SO=${MIRROR_DIRECTORY}/images
LATEST_UPDATE_FILE=latest_sync_images.meta

Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script creates directories and writes files in /srv/linux-mint while running as the mirrors user. Ensure the Ansible playbook creates the /srv/linux-mint directory with appropriate ownership and permissions for the mirrors user, otherwise the script will fail with permission errors.

Suggested change
# Ensure the main mirror directory exists and is owned by the mirrors user
if [ ! -d "${MIRROR_DIRECTORY}" ]; then
mkdir -p "${MIRROR_DIRECTORY}"
fi
chown mirrors:mirrors "${MIRROR_DIRECTORY}"
chmod 755 "${MIRROR_DIRECTORY}"

Copilot uses AI. Check for mistakes.
mkdir -p ${MIRROR_DIRECTORY_SO}

MAIN_SERVER_URL=https://pub.linuxmint.io
MAIN_SERVER_RSYNC=pub.linuxmint.com::pub
function get_upstream_time(){
dates=()
while IFS= read -r line; do
dates+=("$line")
done < <(curl -s --compressed "$MAIN_SERVER_URL" \
| grep -Eo '[0-9]{2}-[A-Za-z]{3}-[0-9]{4} [0-9]{2}:[0-9]{2}')
local latest=""
local latest_epoch=0
#In case we don't receive any date for connection problems
Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after #. Should be # In case we don't receive any date for connection problems.

Suggested change
#In case we don't receive any date for connection problems
# In case we don't receive any date for connection problems

Copilot uses AI. Check for mistakes.
if [ ${#dates[@]} -eq 0 ]; then
echo "1970-01-01 00:00:00 UTC"
return
fi

for d in "${dates[@]}"; do
converted_date=$(echo "$d" | awk '{
split($1, parts, "-")
day = parts[1]
month = parts[2]
year = parts[3]
time = $2

# Convert month name to number
if (month == "Jan") month = "01"
else if (month == "Feb") month = "02"
else if (month == "Mar") month = "03"
else if (month == "Apr") month = "04"
else if (month == "May") month = "05"
else if (month == "Jun") month = "06"
else if (month == "Jul") month = "07"
else if (month == "Aug") month = "08"
else if (month == "Sep") month = "09"
else if (month == "Oct") month = "10"
else if (month == "Nov") month = "11"
else if (month == "Dec") month = "12"

printf "%s-%s-%s %s:00", year, month, day, time
}')
epoch=$(date -d "$converted_date" +"%s" 2>/dev/null)
if [[ $? -eq 0 && $epoch -gt $latest_epoch ]]; then
latest_epoch=$epoch
latest="$converted_date"
fi
done

echo "$latest"
Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The returned date string is missing the " UTC" suffix that is expected by the rest of the script. The get_local_time function returns dates in "YYYY-MM-DD HH:MM:SS UTC" format, but this function returns only "YYYY-MM-DD HH:MM:SS". Add " UTC" to the echo statement for consistency.

Suggested change
echo "$latest"
echo "$latest UTC"

Copilot uses AI. Check for mistakes.
}

function get_local_time() {
if [ -f ${MIRROR_DIRECTORY}/${LATEST_UPDATE_FILE} ]; then
cat ${MIRROR_DIRECTORY}/${LATEST_UPDATE_FILE} | head -n 1
Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless use of cat. Replace cat ${MIRROR_DIRECTORY}/${LATEST_UPDATE_FILE} | head -n 1 with head -n 1 ${MIRROR_DIRECTORY}/${LATEST_UPDATE_FILE} to avoid unnecessary process creation.

Suggested change
cat ${MIRROR_DIRECTORY}/${LATEST_UPDATE_FILE} | head -n 1
head -n 1 ${MIRROR_DIRECTORY}/${LATEST_UPDATE_FILE}

Copilot uses AI. Check for mistakes.
else
echo "1970-01-01 00:00:00 UTC" >${MIRROR_DIRECTORY}/${LATEST_UPDATE_FILE}
echo "1970-01-01 00:00:00 UTC"
fi
}

function should_pull()
{
local_mirror_time=$1
upstream_time=$2
current_time=$3
# Log input of function to stderr
local_mirror_time_epoch=$(date -d "$local_mirror_time" +%s)
upstream_time_epoch=$(date -d "$upstream_time" +%s)
current_time_epoch=$(date -d "$current_time" +%s)


Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The comment states the local mirror is considered out of date if it's "more than 24 hours old" but the condition checks for more than 24 hours (86400 seconds). The comment matches the code, but there's an inconsistency with the repository sync script which uses 12 hours. Consider standardizing the thresholds or documenting why they differ.

Suggested change
# Note: This script uses a 24-hour (86400 seconds) threshold for image sync.
# The repository sync script uses a 12-hour (43200 seconds) threshold.
# This difference is intentional to allow less frequent image updates.

Copilot uses AI. Check for mistakes.
if [ $(($current_time_epoch - $local_mirror_time_epoch)) -gt 86400 ]; then
echo "true"
return
fi


if [ $(($current_time_epoch - $local_mirror_time_epoch)) -lt 43200 ]; then
Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition checks if less than 12 hours (43200 seconds) have passed, but the comment at the top of the file states "less than 24 hours". Either update this threshold to 86400 seconds to match the documentation, or update the file header comment to reflect the actual 12-hour threshold.

Suggested change
if [ $(($current_time_epoch - $local_mirror_time_epoch)) -lt 43200 ]; then
if [ $(($current_time_epoch - $local_mirror_time_epoch)) -lt 86400 ]; then

Copilot uses AI. Check for mistakes.

echo "false"
return
fi

# If the local mirror is older than the upstream mirror, then the local mirror
# is considered out of date
if [ $local_mirror_time_epoch -lt $upstream_time_epoch ]; then
echo "true"
else
echo "false"
fi
}

if [ $(should_pull "$(get_local_time)" "$(get_upstream_time)" "$(date -u)") == "true" ]; then
echo "Local mirror is out of date, pulling from upstream mirror. Upstream date: $(get_upstream_time), Local date: $(get_local_time) - current date: $(date -u)"
new_date="$(date -u '+%Y-%m-%d %H:%M:%S UTC')"
if rsync -av --delete --partial \
${MAIN_SERVER_RSYNC} ${MIRROR_DIRECTORY_SO}; then
sed -i "1s/.*/$new_date/" ${MIRROR_DIRECTORY}/${LATEST_UPDATE_FILE}
else
echo "Error: rsync failed"
exit 1
fi

Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove trailing whitespace at the end of line.

Suggested change

Copilot uses AI. Check for mistakes.

else
echo "Local mirror is up to date. Upstream date: $(get_upstream_time), Local date: $(get_local_time) - current date: $(date -u)"
fi
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[Unit]
Description=Keeping the linux mint mirror repository up to date
After=network-online.target
Wants=network-online.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/linux-mint-sync-repo.sh
TimeoutStartSec=1800
User=mirrors
Comment on lines +9 to +10
Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The service specifies User=mirrors, but there's no task in the Ansible playbook to create this user. Ensure the user exists on the target system, or add a task to create it before deploying these services.

Suggested change
TimeoutStartSec=1800
User=mirrors
TimeoutStartSec=1800

Copilot uses AI. Check for mistakes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[Unit]
Description=Activate service for syncing changes in the linux mint mirror

[Timer]
OnCalendar=*:0/15
AccuracySec=5m
Persistent=true

[Install]
WantedBy=timers.target
108 changes: 108 additions & 0 deletions mirrors/files/linux-mint/repository/scripts/linux-mint-sync-repo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#!/bin/bash


# This script checks the status of the upstream Linux Mint server and the local mirror.
# It compares the last modification time of a reference file
# and checks the following conditions:
#
# - If the upstream mirror has new updates, the local mirror fetches them
# - If the upstream mirror was updated less than 24 hours ago, the local mirror is considered up-to-date and no action is taken.
# - If the upstream mirror was updated more than 24 hours ago it synchronizes again
#
# This script is intended to be run repeatedly by a cron job or a systemd timer.
Comment on lines +3 to +12
Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file-level comment incorrectly describes this as an "Ansible task file" when it's actually a Bash shell script for syncing the repository. Update the comment to accurately reflect that this is a sync script, not an Ansible task file.

Copilot uses AI. Check for mistakes.


MIRROR_DIRECTORY=/srv/linux-mint
LOCAL_MIRROR="lidsol.fi-b.unam.mx"
Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable LOCAL_MIRROR is defined but never used in the script. Consider removing it or implementing the intended functionality.

Suggested change
LOCAL_MIRROR="lidsol.fi-b.unam.mx"

Copilot uses AI. Check for mistakes.
MIRROR_DIRECTORY_RP=${MIRROR_DIRECTORY}/repository
LATEST_UPDATE_FILE=latest_sync_repository.meta
Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script creates directories and writes files in /srv/linux-mint while running as the mirrors user. Ensure the Ansible playbook creates the /srv/linux-mint directory with appropriate ownership and permissions for the mirrors user, otherwise the script will fail with permission errors.

Suggested change
LATEST_UPDATE_FILE=latest_sync_repository.meta
LATEST_UPDATE_FILE=latest_sync_repository.meta
# Ensure MIRROR_DIRECTORY exists and is writable
if [ ! -d "${MIRROR_DIRECTORY}" ]; then
mkdir -p "${MIRROR_DIRECTORY}" || { echo "Error: Could not create ${MIRROR_DIRECTORY}. Permission denied."; exit 1; }
fi
if [ ! -w "${MIRROR_DIRECTORY}" ]; then
echo "Error: ${MIRROR_DIRECTORY} is not writable by user $(whoami). Please ensure correct ownership and permissions."
exit 1
fi

Copilot uses AI. Check for mistakes.
mkdir -p ${MIRROR_DIRECTORY_RP}

MIRROR_SERVER_URL=https://mirrors.seas.harvard.edu/linuxmint-packages/
MAIN_SERVER_RSYNC=rsync-packages.linuxmint.com::packages
function get_upstream_time(){
dates=()
while IFS= read -r line; do
dates+=("$line")
done < <(curl -s --compressed ${MIRROR_SERVER_URL} | grep -Eo '[0-9]{4}-(0[1-9]|1[0-2])-[0-9]{2} [0-9]{2}:[0-9]{2}')
local latest=""
local latest_epoch=0
# In case we don't receive any date for connection problems
if [ ${#dates[@]} -eq 0 ]; then
echo "1970-01-01 00:00:00 UTC"
return
fi

for d in "${dates[@]}"; do
converted_date=$d
epoch=$(date -d "$converted_date" +"%s" 2>/dev/null)
if [[ $? -eq 0 && $epoch -gt $latest_epoch ]]; then
latest_epoch=$epoch
latest="$converted_date"
fi
done

echo "${latest}:00 UTC"
}

function get_local_time() {
if [ -f ${MIRROR_DIRECTORY}/${LATEST_UPDATE_FILE} ]; then
cat ${MIRROR_DIRECTORY}/${LATEST_UPDATE_FILE} | head -n 1
Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless use of cat. Replace cat ${MIRROR_DIRECTORY}/${LATEST_UPDATE_FILE} | head -n 1 with head -n 1 ${MIRROR_DIRECTORY}/${LATEST_UPDATE_FILE} to avoid unnecessary process creation.

Suggested change
cat ${MIRROR_DIRECTORY}/${LATEST_UPDATE_FILE} | head -n 1
head -n 1 ${MIRROR_DIRECTORY}/${LATEST_UPDATE_FILE}

Copilot uses AI. Check for mistakes.
else
echo "1970-01-01 00:00:00 UTC" >${MIRROR_DIRECTORY}/${LATEST_UPDATE_FILE}
echo "1970-01-01 00:00:00 UTC"
fi
}

function should_pull()
{
local_mirror_time=$1
upstream_time=$2
current_time=$3
# Log input of function to stderr
local_mirror_time_epoch=$(date -d "$local_mirror_time" +%s)
upstream_time_epoch=$(date -d "$upstream_time" +%s)
current_time_epoch=$(date -d "$current_time" +%s)

# If the local mirror is more than 12 hours old, then the mirror is considered out of date
if [ $(($current_time_epoch - $local_mirror_time_epoch)) -gt 43200 ]; then
Comment on lines +67 to +68
Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment states "more than 24 hours" but the condition checks for more than 12 hours (43200 seconds). Either update the comment to say "more than 12 hours" or adjust the threshold to 86400 seconds for 24 hours.

Copilot uses AI. Check for mistakes.
echo "true"
return
fi

# If it has been less than 12 hours since the upstream mirror started updating,
# then the mirror is considered up-to-date
if [ $(($current_time_epoch - $local_mirror_time_epoch)) -lt 21600 ]; then
Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment states "less than 12 hours" but the condition checks for less than 6 hours (21600 seconds). Either update the comment to say "less than 6 hours" or adjust the threshold to 43200 seconds for 12 hours.

Suggested change
if [ $(($current_time_epoch - $local_mirror_time_epoch)) -lt 21600 ]; then
if [ $(($current_time_epoch - $local_mirror_time_epoch)) -lt 43200 ]; then

Copilot uses AI. Check for mistakes.
# Log the operation above to stderr
echo "false"
return
fi

# If the local mirror is older than the upstream mirror, then the local mirror
# is considered out of date
if [ $local_mirror_time_epoch -lt $upstream_time_epoch ]; then
echo "true"
else
echo "false"
fi
}

if [ $(should_pull "$(get_local_time)" "$(get_upstream_time)" "$(date -u)") == "true" ]; then
echo "Local mirror is out of date, pulling from upstream mirror. Upstream date: $(get_upstream_time), Local date: $(get_local_time) - current date: $(date -u)"
new_date="$(date -u '+%Y-%m-%d %H:%M:%S UTC')"
if rsync -av --delete --partial \
${MAIN_SERVER_RSYNC} ${MIRROR_DIRECTORY_RP}; then
sed -i "1s/.*/$new_date/" ${MIRROR_DIRECTORY}/${LATEST_UPDATE_FILE}
else
echo "Error: rsync failed"
exit 1
fi

Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove trailing whitespace at the end of line.

Suggested change

Copilot uses AI. Check for mistakes.
else
echo "Local mirror is up to date. Upstream date: $(get_upstream_time), Local date: $(get_local_time) - current date: $(date -u)"
fi





2 changes: 2 additions & 0 deletions mirrors/hosts.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[mirror-server]
lidsol.fi-b.unam.mx
9 changes: 9 additions & 0 deletions mirrors/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
- name: Setup mirrors server for LIDSoL
hosts: mirror-server
become: true

tasks:
- name: Linux Mint mirrors
ansible.builtin.import_tasks: tasks/linux-mint.yaml

Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove trailing whitespace at the end of the file.

Copilot uses AI. Check for mistakes.
51 changes: 51 additions & 0 deletions mirrors/tasks/linux-mint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Ansible task file for setting up Linux Mint mirrors
# This playbook copies necessary service and script files to the target system
# and ensures that the corresponding systemd services and timers are enabled and started.
---
- name: Copy required systemd service and timer files for linux mint images mirror
ansible.builtin.copy:
src: files/linux-mint/images/confd/{{ item }}
dest: /etc/systemd/system/{{ item }}
owner: root
group: root
mode: '0644'
loop:
- "mintUpdateImages.service"
- "mintUpdateImages.timer"

- name: Copy linux mint sync script for images mirror
ansible.builtin.copy:
src: files/linux-mint/images/scripts/linux-mint-sync-images.sh
Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra space after src:. Remove the extra space for consistency with other file copy tasks in the playbook.

Copilot uses AI. Check for mistakes.
dest: /usr/local/bin/linux-mint-sync-images.sh
owner: root
group: root
mode: '0755'

- name: Copy required systemd service and timer files for linux mint repository mirror
ansible.builtin.copy:
src: files/linux-mint/repository/confd/{{ item }}
dest: /etc/systemd/system/{{ item }}
owner: root
group: root
mode: '0644'
loop:
- "mintUpdateRepository.service"
- "mintUpdateRepository.timer"

- name: Copy linux mint sync script for repository mirror
ansible.builtin.copy:
src: files/linux-mint/repository/scripts/linux-mint-sync-repo.sh
Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra space after src:. Remove the extra space for consistency with other file copy tasks in the playbook.

Suggested change
src: files/linux-mint/repository/scripts/linux-mint-sync-repo.sh
src: files/linux-mint/repository/scripts/linux-mint-sync-repo.sh

Copilot uses AI. Check for mistakes.
dest: /usr/local/bin/linux-mint-sync-repo.sh
owner: root
group: root
mode: '0755'

- name: Start timer service for linux mint mirrors
ansible.builtin.systemd_service:
name: "{{ item }}"
enabled: true
state: started
daemon_reload: true
loop:
- "mintUpdateImages.timer"
- "mintUpdateRepository.timer"