diff --git a/README.md b/README.md index a4c657b..968eccc 100644 --- a/README.md +++ b/README.md @@ -170,25 +170,19 @@ Imports GPG keys for verifying Debian and Ubuntu repository signatures. Requires - Requires `~/.gnupg/` to exist - Keyserver can be customized via `GPG_KEYSERVER` in config -### Options +### Help -#### `-c ` — Specify config file +#### `help`, `-h`, `--help` — Show usage ```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 2. `./aptly-mirror.conf` (same directory as script) 3. `/etc/aptly-mirror.conf` -#### `-h`, `--help` — Show usage - -```bash -./aptly-mirror.sh --help -``` - ### Configuration All settings are in `aptly-mirror.conf`. See the included example for detailed comments. diff --git a/aptly-mirror.conf b/aptly-mirror.conf index f0f5424..cca754e 100644 --- a/aptly-mirror.conf +++ b/aptly-mirror.conf @@ -3,7 +3,7 @@ # --------------------------------------------------------------------------- # # 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 . +# To override the default location, set APTLY_MIRROR_CONF. # # --------------------------------------------------------------------------- diff --git a/aptly-mirror.sh b/aptly-mirror.sh index e098ed9..7382611 100755 --- a/aptly-mirror.sh +++ b/aptly-mirror.sh @@ -22,10 +22,9 @@ set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # Config file search order: -# 1. -c 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 ." >&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: aptly-mirror.sh 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 to config file (default: auto-detected) - Configuration: The config file is searched in this order: - 1. -c 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 }