refactor: update help options and improve config file handling
This commit is contained in:
219
aptly-mirror.sh
219
aptly-mirror.sh
@@ -22,10 +22,9 @@ set -euo pipefail
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
# Config file search order:
|
||||
# 1. -c <path> command-line flag
|
||||
# 2. APTLY_MIRROR_CONF environment variable
|
||||
# 3. ./aptly-mirror.conf (next to the script)
|
||||
# 4. /etc/aptly-mirror.conf
|
||||
# 1. APTLY_MIRROR_CONF environment variable
|
||||
# 2. ./aptly-mirror.conf (next to the script)
|
||||
# 3. /etc/aptly-mirror.conf
|
||||
|
||||
find_config() {
|
||||
if [[ -n "${APTLY_MIRROR_CONF:-}" && -r "$APTLY_MIRROR_CONF" ]]; then
|
||||
@@ -40,27 +39,12 @@ find_config() {
|
||||
}
|
||||
|
||||
CONFIG_FILE=""
|
||||
|
||||
# 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
|
||||
CONFIG_FILE=$(find_config)
|
||||
|
||||
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 "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
|
||||
else
|
||||
# shellcheck source=aptly-mirror.conf
|
||||
@@ -155,6 +139,73 @@ get_debian_security_components() {
|
||||
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
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -166,31 +217,10 @@ create_debian_mirrors() {
|
||||
components=$(get_debian_components "$codename")
|
||||
security_components=$(get_debian_security_components "$codename")
|
||||
|
||||
log "Creating Debian ${version} (${codename}) mirrors..."
|
||||
|
||||
local name="debian-${codename}-main"
|
||||
if ! mirror_exists "$name"; then
|
||||
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
|
||||
create_release_mirrors \
|
||||
"debian" "$codename" "$version" \
|
||||
"$DEBIAN_MIRROR" "$DEBIAN_SECURITY_MIRROR" \
|
||||
"$components" "$security_components"
|
||||
done
|
||||
}
|
||||
|
||||
@@ -199,31 +229,10 @@ create_ubuntu_mirrors() {
|
||||
for codename in "${!UBUNTU_RELEASES[@]}"; do
|
||||
version="${UBUNTU_RELEASES[$codename]}"
|
||||
|
||||
log "Creating Ubuntu ${version} (${codename}) mirrors..."
|
||||
|
||||
local name="ubuntu-${codename}-main"
|
||||
if ! mirror_exists "$name"; then
|
||||
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
|
||||
create_release_mirrors \
|
||||
"ubuntu" "$codename" "$version" \
|
||||
"$UBUNTU_MIRROR" "$UBUNTU_SECURITY_MIRROR" \
|
||||
"$UBUNTU_COMPONENTS" "$UBUNTU_COMPONENTS"
|
||||
done
|
||||
}
|
||||
|
||||
@@ -257,74 +266,26 @@ update_all_mirrors() {
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
snapshot_and_publish_debian() {
|
||||
local codename version snap_main snap_updates snap_security snap_merged
|
||||
local codename version
|
||||
local pub_flags
|
||||
pub_flags=$(publish_flags)
|
||||
|
||||
for codename in "${!DEBIAN_RELEASES[@]}"; do
|
||||
version="${DEBIAN_RELEASES[$codename]}"
|
||||
|
||||
log "Snapshotting Debian ${version} (${codename})..."
|
||||
|
||||
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
|
||||
snapshot_and_publish_release "debian" "$codename" "$version" "$pub_flags"
|
||||
done
|
||||
}
|
||||
|
||||
snapshot_and_publish_ubuntu() {
|
||||
local codename version snap_main snap_updates snap_security snap_merged
|
||||
local codename version
|
||||
local pub_flags
|
||||
pub_flags=$(publish_flags)
|
||||
|
||||
for codename in "${!UBUNTU_RELEASES[@]}"; do
|
||||
version="${UBUNTU_RELEASES[$codename]}"
|
||||
|
||||
log "Snapshotting Ubuntu ${version} (${codename})..."
|
||||
|
||||
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
|
||||
snapshot_and_publish_release "ubuntu" "$codename" "$version" "$pub_flags"
|
||||
done
|
||||
}
|
||||
|
||||
@@ -454,7 +415,7 @@ do_import_keys() {
|
||||
|
||||
usage() {
|
||||
cat <<USAGE
|
||||
Usage: aptly-mirror.sh [-c <config>] <command>
|
||||
Usage: aptly-mirror.sh <command>
|
||||
|
||||
Commands:
|
||||
create Create all mirrors (first-time setup)
|
||||
@@ -464,15 +425,11 @@ Commands:
|
||||
list List all mirrors, snapshots, and published repos
|
||||
import-keys Import GPG keys for Debian and Ubuntu repositories
|
||||
|
||||
Options:
|
||||
-c <path> Path to config file (default: auto-detected)
|
||||
|
||||
Configuration:
|
||||
The config file is searched in this order:
|
||||
1. -c <path> flag
|
||||
2. \$APTLY_MIRROR_CONF environment variable
|
||||
3. ${SCRIPT_DIR}/aptly-mirror.conf
|
||||
4. /etc/aptly-mirror.conf
|
||||
1. \$APTLY_MIRROR_CONF environment variable
|
||||
2. ${SCRIPT_DIR}/aptly-mirror.conf
|
||||
3. /etc/aptly-mirror.conf
|
||||
USAGE
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user