refactor: update help options and improve config file handling

This commit is contained in:
Daniel Akulenok
2026-04-08 22:45:43 +02:00
parent 1cb72a0ca5
commit 3554cdc44d
3 changed files with 93 additions and 142 deletions

View File

@@ -170,25 +170,19 @@ Imports GPG keys for verifying Debian and Ubuntu repository signatures. Requires
- Requires `~/.gnupg/` to exist - Requires `~/.gnupg/` to exist
- Keyserver can be customized via `GPG_KEYSERVER` in config - Keyserver can be customized via `GPG_KEYSERVER` in config
### Options ### Help
#### `-c <path>` — Specify config file #### `help`, `-h`, `--help` — Show usage
```bash ```bash
./aptly-mirror.sh -c /etc/aptly-mirror.conf update ./aptly-mirror.sh help
``` ```
Config file search order (if `-c` not specified): Config file search order:
1. `$APTLY_MIRROR_CONF` environment variable 1. `$APTLY_MIRROR_CONF` environment variable
2. `./aptly-mirror.conf` (same directory as script) 2. `./aptly-mirror.conf` (same directory as script)
3. `/etc/aptly-mirror.conf` 3. `/etc/aptly-mirror.conf`
#### `-h`, `--help` — Show usage
```bash
./aptly-mirror.sh --help
```
### Configuration ### Configuration
All settings are in `aptly-mirror.conf`. See the included example for detailed comments. All settings are in `aptly-mirror.conf`. See the included example for detailed comments.

View File

@@ -3,7 +3,7 @@
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# #
# This file is sourced by aptly-mirror.sh. It uses plain bash syntax. # This file is sourced by aptly-mirror.sh. It uses plain bash syntax.
# To override the default location, set APTLY_MIRROR_CONF or pass -c <path>. # To override the default location, set APTLY_MIRROR_CONF.
# #
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------

View File

@@ -22,10 +22,9 @@ set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Config file search order: # Config file search order:
# 1. -c <path> command-line flag # 1. APTLY_MIRROR_CONF environment variable
# 2. APTLY_MIRROR_CONF environment variable # 2. ./aptly-mirror.conf (next to the script)
# 3. ./aptly-mirror.conf (next to the script) # 3. /etc/aptly-mirror.conf
# 4. /etc/aptly-mirror.conf
find_config() { find_config() {
if [[ -n "${APTLY_MIRROR_CONF:-}" && -r "$APTLY_MIRROR_CONF" ]]; then if [[ -n "${APTLY_MIRROR_CONF:-}" && -r "$APTLY_MIRROR_CONF" ]]; then
@@ -40,27 +39,12 @@ find_config() {
} }
CONFIG_FILE="" CONFIG_FILE=""
CONFIG_FILE=$(find_config)
# Parse flags before subcommand
while [[ "${1:-}" == -* ]]; do
case "$1" in
-c) CONFIG_FILE="$2"; shift 2 ;;
-h|--help) CONFIG_FILE="__help__"; break ;;
*) break ;;
esac
done
if [[ -z "$CONFIG_FILE" ]]; then if [[ -z "$CONFIG_FILE" ]]; then
CONFIG_FILE=$(find_config)
fi
if [[ "$CONFIG_FILE" == "__help__" ]]; then
# Defer to main() which will show usage
:
elif [[ -z "$CONFIG_FILE" ]]; then
echo "Error: No config file found." >&2 echo "Error: No config file found." >&2
echo "Searched: \$APTLY_MIRROR_CONF, ${SCRIPT_DIR}/aptly-mirror.conf, /etc/aptly-mirror.conf" >&2 echo "Searched: \$APTLY_MIRROR_CONF, ${SCRIPT_DIR}/aptly-mirror.conf, /etc/aptly-mirror.conf" >&2
echo "Create one from aptly-mirror.conf.example or pass -c <path>." >&2 echo "Create one from aptly-mirror.conf.example or set APTLY_MIRROR_CONF." >&2
exit 1 exit 1
else else
# shellcheck source=aptly-mirror.conf # shellcheck source=aptly-mirror.conf
@@ -155,6 +139,73 @@ get_debian_security_components() {
esac esac
} }
create_release_mirrors() {
local family="$1" codename="$2" version="$3"
local main_mirror="$4" security_mirror="$5"
local components="$6" security_components="$7"
local suite name suite_name suite_components source_url
log "Creating ${family^} ${version} (${codename}) mirrors..."
for suite in main updates security; do
name="${family}-${codename}-${suite}"
if mirror_exists "$name"; then
log " Mirror $name already exists, skipping."
continue
fi
case "$suite" in
main)
source_url="$main_mirror"
suite_name="$codename"
suite_components="$components"
;;
updates)
source_url="$main_mirror"
suite_name="${codename}-updates"
suite_components="$components"
;;
security)
source_url="$security_mirror"
suite_name="${codename}-security"
suite_components="$security_components"
;;
esac
run_aptly mirror create $(common_mirror_flags) \
"$name" "$source_url" "$suite_name" $suite_components
done
}
snapshot_and_publish_release() {
local family="$1" codename="$2" version="$3"
local pub_flags="$4"
local suite snap_name snap_merged
local -a snaps=()
log "Snapshotting ${family^} ${version} (${codename})..."
for suite in main updates security; do
snap_name="${family}-${codename}-${suite}-${DATE}"
run_aptly snapshot create "$snap_name" from mirror "${family}-${codename}-${suite}"
snaps+=("$snap_name")
done
snap_merged="${family}-${codename}-merged-${DATE}"
log "Merging snapshots for ${family^} ${codename}..."
run_aptly snapshot merge -latest "$snap_merged" "${snaps[@]}"
if published_exists "$codename" "$family"; then
log "Switching published ${family^} ${codename} to ${snap_merged}..."
run_aptly publish switch $pub_flags "$codename" "$family" "$snap_merged"
else
log "Publishing ${family^} ${codename} for the first time..."
run_aptly publish snapshot $pub_flags \
-distribution="$codename" -architectures="$ARCHITECTURES" \
"$snap_merged" "$family"
fi
}
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# CREATE — set up all mirrors # CREATE — set up all mirrors
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
@@ -166,31 +217,10 @@ create_debian_mirrors() {
components=$(get_debian_components "$codename") components=$(get_debian_components "$codename")
security_components=$(get_debian_security_components "$codename") security_components=$(get_debian_security_components "$codename")
log "Creating Debian ${version} (${codename}) mirrors..." create_release_mirrors \
"debian" "$codename" "$version" \
local name="debian-${codename}-main" "$DEBIAN_MIRROR" "$DEBIAN_SECURITY_MIRROR" \
if ! mirror_exists "$name"; then "$components" "$security_components"
run_aptly mirror create $(common_mirror_flags) \
"$name" "$DEBIAN_MIRROR" "$codename" $components
else
log " Mirror $name already exists, skipping."
fi
name="debian-${codename}-updates"
if ! mirror_exists "$name"; then
run_aptly mirror create $(common_mirror_flags) \
"$name" "$DEBIAN_MIRROR" "${codename}-updates" $components
else
log " Mirror $name already exists, skipping."
fi
name="debian-${codename}-security"
if ! mirror_exists "$name"; then
run_aptly mirror create $(common_mirror_flags) \
"$name" "$DEBIAN_SECURITY_MIRROR" "${codename}-security" $security_components
else
log " Mirror $name already exists, skipping."
fi
done done
} }
@@ -199,31 +229,10 @@ create_ubuntu_mirrors() {
for codename in "${!UBUNTU_RELEASES[@]}"; do for codename in "${!UBUNTU_RELEASES[@]}"; do
version="${UBUNTU_RELEASES[$codename]}" version="${UBUNTU_RELEASES[$codename]}"
log "Creating Ubuntu ${version} (${codename}) mirrors..." create_release_mirrors \
"ubuntu" "$codename" "$version" \
local name="ubuntu-${codename}-main" "$UBUNTU_MIRROR" "$UBUNTU_SECURITY_MIRROR" \
if ! mirror_exists "$name"; then "$UBUNTU_COMPONENTS" "$UBUNTU_COMPONENTS"
run_aptly mirror create $(common_mirror_flags) \
"$name" "$UBUNTU_MIRROR" "$codename" $UBUNTU_COMPONENTS
else
log " Mirror $name already exists, skipping."
fi
name="ubuntu-${codename}-updates"
if ! mirror_exists "$name"; then
run_aptly mirror create $(common_mirror_flags) \
"$name" "$UBUNTU_MIRROR" "${codename}-updates" $UBUNTU_COMPONENTS
else
log " Mirror $name already exists, skipping."
fi
name="ubuntu-${codename}-security"
if ! mirror_exists "$name"; then
run_aptly mirror create $(common_mirror_flags) \
"$name" "$UBUNTU_SECURITY_MIRROR" "${codename}-security" $UBUNTU_COMPONENTS
else
log " Mirror $name already exists, skipping."
fi
done done
} }
@@ -257,74 +266,26 @@ update_all_mirrors() {
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
snapshot_and_publish_debian() { snapshot_and_publish_debian() {
local codename version snap_main snap_updates snap_security snap_merged local codename version
local pub_flags local pub_flags
pub_flags=$(publish_flags) pub_flags=$(publish_flags)
for codename in "${!DEBIAN_RELEASES[@]}"; do for codename in "${!DEBIAN_RELEASES[@]}"; do
version="${DEBIAN_RELEASES[$codename]}" version="${DEBIAN_RELEASES[$codename]}"
log "Snapshotting Debian ${version} (${codename})..." snapshot_and_publish_release "debian" "$codename" "$version" "$pub_flags"
snap_main="debian-${codename}-main-${DATE}"
snap_updates="debian-${codename}-updates-${DATE}"
snap_security="debian-${codename}-security-${DATE}"
snap_merged="debian-${codename}-merged-${DATE}"
run_aptly snapshot create "$snap_main" from mirror "debian-${codename}-main"
run_aptly snapshot create "$snap_updates" from mirror "debian-${codename}-updates"
run_aptly snapshot create "$snap_security" from mirror "debian-${codename}-security"
log "Merging snapshots for Debian ${codename}..."
run_aptly snapshot merge -latest \
"$snap_merged" "$snap_main" "$snap_updates" "$snap_security"
local prefix="debian"
if published_exists "$codename" "$prefix"; then
log "Switching published Debian ${codename} to ${snap_merged}..."
run_aptly publish switch $pub_flags "$codename" "$prefix" "$snap_merged"
else
log "Publishing Debian ${codename} for the first time..."
run_aptly publish snapshot $pub_flags \
-distribution="$codename" -architectures="$ARCHITECTURES" \
"$snap_merged" "$prefix"
fi
done done
} }
snapshot_and_publish_ubuntu() { snapshot_and_publish_ubuntu() {
local codename version snap_main snap_updates snap_security snap_merged local codename version
local pub_flags local pub_flags
pub_flags=$(publish_flags) pub_flags=$(publish_flags)
for codename in "${!UBUNTU_RELEASES[@]}"; do for codename in "${!UBUNTU_RELEASES[@]}"; do
version="${UBUNTU_RELEASES[$codename]}" version="${UBUNTU_RELEASES[$codename]}"
log "Snapshotting Ubuntu ${version} (${codename})..." snapshot_and_publish_release "ubuntu" "$codename" "$version" "$pub_flags"
snap_main="ubuntu-${codename}-main-${DATE}"
snap_updates="ubuntu-${codename}-updates-${DATE}"
snap_security="ubuntu-${codename}-security-${DATE}"
snap_merged="ubuntu-${codename}-merged-${DATE}"
run_aptly snapshot create "$snap_main" from mirror "ubuntu-${codename}-main"
run_aptly snapshot create "$snap_updates" from mirror "ubuntu-${codename}-updates"
run_aptly snapshot create "$snap_security" from mirror "ubuntu-${codename}-security"
log "Merging snapshots for Ubuntu ${codename}..."
run_aptly snapshot merge -latest \
"$snap_merged" "$snap_main" "$snap_updates" "$snap_security"
local prefix="ubuntu"
if published_exists "$codename" "$prefix"; then
log "Switching published Ubuntu ${codename} to ${snap_merged}..."
run_aptly publish switch $pub_flags "$codename" "$prefix" "$snap_merged"
else
log "Publishing Ubuntu ${codename} for the first time..."
run_aptly publish snapshot $pub_flags \
-distribution="$codename" -architectures="$ARCHITECTURES" \
"$snap_merged" "$prefix"
fi
done done
} }
@@ -454,7 +415,7 @@ do_import_keys() {
usage() { usage() {
cat <<USAGE cat <<USAGE
Usage: aptly-mirror.sh [-c <config>] <command> Usage: aptly-mirror.sh <command>
Commands: Commands:
create Create all mirrors (first-time setup) create Create all mirrors (first-time setup)
@@ -464,15 +425,11 @@ Commands:
list List all mirrors, snapshots, and published repos list List all mirrors, snapshots, and published repos
import-keys Import GPG keys for Debian and Ubuntu repositories import-keys Import GPG keys for Debian and Ubuntu repositories
Options:
-c <path> Path to config file (default: auto-detected)
Configuration: Configuration:
The config file is searched in this order: The config file is searched in this order:
1. -c <path> flag 1. \$APTLY_MIRROR_CONF environment variable
2. \$APTLY_MIRROR_CONF environment variable 2. ${SCRIPT_DIR}/aptly-mirror.conf
3. ${SCRIPT_DIR}/aptly-mirror.conf 3. /etc/aptly-mirror.conf
4. /etc/aptly-mirror.conf
USAGE USAGE
} }