mirror of
https://github.com/xlionjuan/fedora-createrepo-image.git
synced 2026-11-17 04:17:30 +00:00
187 lines
4.2 KiB
Shell
Executable file
187 lines
4.2 KiB
Shell
Executable file
#!/bin/bash
|
|
#
|
|
# xlion-repo-repackage-rpm - Repackage RPM files with fpm.
|
|
#
|
|
|
|
set -euo pipefail
|
|
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m'
|
|
|
|
SOURCE_DIR=""
|
|
TARGET_DIR=""
|
|
DATE_VERSION=false
|
|
DATE_FORMAT="%Y%m%d"
|
|
COMPRESSION="xz"
|
|
REMOVE_SOURCE_DIR=false
|
|
|
|
error() {
|
|
echo -e "${RED}Error: $1${NC}" >&2
|
|
exit 1
|
|
}
|
|
|
|
info() {
|
|
echo -e "${BLUE}$1${NC}" >&2
|
|
}
|
|
|
|
success() {
|
|
echo -e "${GREEN}$1${NC}" >&2
|
|
}
|
|
|
|
show_help() {
|
|
cat << EOF
|
|
xlion-repo-repackage-rpm - Repackage RPM files with fpm.
|
|
|
|
Usage:
|
|
$(basename "$0") --source-dir DIR [options]
|
|
|
|
Options:
|
|
--source-dir DIR Directory containing source .rpm files. Required.
|
|
--target-dir DIR Output directory. Default: parent of --source-dir.
|
|
--date-version Append +DATE to the original package version.
|
|
--date-format FORMAT Date format for --date-version. Default: %Y%m%d.
|
|
--compression NAME fpm --rpm-compression value. Default: xz.
|
|
--remove-source-dir Remove --source-dir after successful repackaging.
|
|
-h, --help Show this help message.
|
|
|
|
Examples:
|
|
$(basename "$0") --source-dir wwwroot/latest/ori --remove-source-dir
|
|
|
|
$(basename "$0") --source-dir wwwroot/nightly/ori --date-version --remove-source-dir
|
|
|
|
Notes:
|
|
- Logs are printed to stderr.
|
|
- This tool only repackages files. It does not create RPM metadata or sign packages.
|
|
EOF
|
|
}
|
|
|
|
check_dependencies() {
|
|
local missing=()
|
|
local cmd
|
|
|
|
for cmd in "$@"; do
|
|
if ! command -v "$cmd" > /dev/null 2>&1; then
|
|
missing+=("$cmd")
|
|
fi
|
|
done
|
|
|
|
if [[ ${#missing[@]} -gt 0 ]]; then
|
|
error "Missing required CLI tool(s): ${missing[*]}"
|
|
fi
|
|
}
|
|
|
|
parse_args() {
|
|
while [[ $# -gt 0 ]]; do
|
|
case "$1" in
|
|
--source-dir)
|
|
[[ $# -ge 2 ]] || error "Option --source-dir requires a value."
|
|
SOURCE_DIR="$2"
|
|
shift 2
|
|
;;
|
|
--target-dir)
|
|
[[ $# -ge 2 ]] || error "Option --target-dir requires a value."
|
|
TARGET_DIR="$2"
|
|
shift 2
|
|
;;
|
|
--date-version)
|
|
DATE_VERSION=true
|
|
shift
|
|
;;
|
|
--date-format)
|
|
[[ $# -ge 2 ]] || error "Option --date-format requires a value."
|
|
DATE_FORMAT="$2"
|
|
shift 2
|
|
;;
|
|
--compression)
|
|
[[ $# -ge 2 ]] || error "Option --compression requires a value."
|
|
COMPRESSION="$2"
|
|
shift 2
|
|
;;
|
|
--remove-source-dir)
|
|
REMOVE_SOURCE_DIR=true
|
|
shift
|
|
;;
|
|
-h | --help)
|
|
show_help
|
|
exit 0
|
|
;;
|
|
*)
|
|
error "Unknown option: $1"
|
|
;;
|
|
esac
|
|
done
|
|
}
|
|
|
|
resolve_dir() {
|
|
local dir="$1"
|
|
mkdir -p "$dir" || error "Failed to create directory: $dir"
|
|
(cd "$dir" && pwd -P) || error "Failed to resolve directory: $dir"
|
|
}
|
|
|
|
validate_args() {
|
|
if [[ -z "$SOURCE_DIR" ]]; then
|
|
error "Option --source-dir is required."
|
|
fi
|
|
|
|
if [[ ! -d "$SOURCE_DIR" ]]; then
|
|
error "Source directory does not exist: $SOURCE_DIR"
|
|
fi
|
|
|
|
if [[ -z "$TARGET_DIR" ]]; then
|
|
TARGET_DIR="$(dirname "$SOURCE_DIR")"
|
|
fi
|
|
}
|
|
|
|
main() {
|
|
parse_args "$@"
|
|
validate_args
|
|
check_dependencies fpm rpm date find basename
|
|
|
|
SOURCE_DIR="$(resolve_dir "$SOURCE_DIR")"
|
|
TARGET_DIR="$(resolve_dir "$TARGET_DIR")"
|
|
|
|
mapfile -t rpm_files < <(find "$SOURCE_DIR" -maxdepth 1 -type f -name '*.rpm' | sort)
|
|
if [[ ${#rpm_files[@]} -eq 0 ]]; then
|
|
error "No .rpm files found in source directory: $SOURCE_DIR"
|
|
fi
|
|
|
|
info "Source directory: $SOURCE_DIR"
|
|
info "Target directory: $TARGET_DIR"
|
|
|
|
date_suffix="$(date "+$DATE_FORMAT")"
|
|
|
|
pids=()
|
|
|
|
for rpm_file in "${rpm_files[@]}"; do
|
|
(
|
|
info "Processing: $rpm_file"
|
|
|
|
fpm_args=(fpm -t rpm -s rpm --rpm-compression "$COMPRESSION")
|
|
|
|
if [[ "$DATE_VERSION" == true ]]; then
|
|
original_version="$(rpm -qp --qf '%{VERSION}' "$rpm_file")"
|
|
[[ -n "$original_version" ]] || error "Failed to read Version from: $rpm_file"
|
|
fpm_args+=(--version "${original_version}+${date_suffix}")
|
|
fi
|
|
|
|
fpm_args+=(-p "$TARGET_DIR/$(basename "$rpm_file")" "$rpm_file")
|
|
"${fpm_args[@]}"
|
|
) &
|
|
pids+=("$!")
|
|
done
|
|
|
|
for pid in "${pids[@]}"; do
|
|
wait "$pid"
|
|
done
|
|
|
|
if [[ "$REMOVE_SOURCE_DIR" == true ]]; then
|
|
rm -rf "$SOURCE_DIR"
|
|
info "Removed source directory: $SOURCE_DIR"
|
|
fi
|
|
|
|
success "Repackaged ${#rpm_files[@]} RPM file(s)."
|
|
}
|
|
|
|
main "$@"
|