General code cleanup and refactoring

This commit is contained in:
2024-10-24 14:39:28 -04:00
parent c061c18a8e
commit b00bd9f056

View File

@@ -1,18 +1,13 @@
#!/usr/bin/env bash
#
# Builds and deploys OpenWRT images
# Copyright 2022-24 Bryan C. Roessler
#
# Build and deploy OpenWRT images
#
# Apache 2.0 License
#
# See README.md and ./profiles
#
# Set default release
: "${RELEASE:="23.05.5"}"
printHelp() {
print_help() {
debug "${FUNCNAME[0]}"
cat <<-'EOF'
@@ -51,7 +46,6 @@ printHelp() {
init() {
debug "${FUNCNAME[0]}"
declare -g ID RPM_MGR SCRIPTDIR DL_TOOL
((DEBUG)) || echo "To enable debugging output, use --debug or -d"
@@ -65,7 +59,7 @@ init() {
else
echo "/etc/os-release not found"
echo "Your OS is unsupported"
printHelp
print_help
exit 1
fi
@@ -73,27 +67,18 @@ init() {
# normalize distro ID
case "$ID" in
debian|arch)
;;
debian|arch) ;;
centos|fedora)
if hash dnf &>/dev/null; then
if command -v dnf &>/dev/null; then
RPM_MGR="dnf"
elif hash yum &>/dev/null; then
elif command -v yum &>/dev/null; then
RPM_MGR="yum"
fi
;;
rhel)
ID="centos"
;;
linuxmint|neon|*ubuntu*)
ID="ubuntu"
;;
*suse*)
ID="suse"
;;
raspbian)
ID="debian"
;;
rhel) ID="centos" ;;
linuxmint|neon|*ubuntu*) ID="ubuntu" ;;
*suse*) ID="suse" ;;
raspbian) ID="debian" ;;
*)
echo "Autodetecting distro, this may be unreliable"
if hash dnf &>/dev/null; then
@@ -116,23 +101,15 @@ init() {
# Set distro-specific functions
case "$ID" in
fedora|centos)
pkg_install(){ sudo "$RPM_MGR" install -y "$@"; }
;;
debian|ubuntu)
pkg_install(){ sudo apt-get install -y -q0 "$@"; }
;;
suse)
pkg_install(){ sudo zypper --non-interactive -q install --force --no-confirm "$@"; }
;;
arch)
pkg_install(){ sudo pacman -S --noconfirm --needed "$@"; }
;;
fedora|centos) pkg_install(){ sudo "$RPM_MGR" install -y "$@"; } ;;
debian|ubuntu) pkg_install(){ sudo apt-get install -y -q0 "$@"; } ;;
suse) pkg_install(){ sudo zypper --non-interactive -q install --force --no-confirm "$@"; } ;;
arch) pkg_install(){ sudo pacman -S --noconfirm --needed "$@"; } ;;
esac
if hash axel &>/dev/null; then
if command -v axel &>/dev/null; then
DL_TOOL="axel"
elif hash curl &>/dev/null; then
elif command -v curl &>/dev/null; then
DL_TOOL="curl"
else
echo "Downloading the Image Builder requires axel or curl"
@@ -140,53 +117,27 @@ init() {
fi
}
readInput() {
debug "${FUNCNAME[0]}"
unset RESET YES
parse_input() {
debug "${FUNCNAME[0]}" "$*"
declare -ga PROFILES
declare long_opts='release:,version:,profile:,buildroot:,source,'
local long_opts='release:,version:,profile:,buildroot:,source,'
long_opts+='ssh-upgrade:,ssh-backup:,flash:,reset,yes,debug,help'
if _input=$(getopt -o +r:v:p:b:sf:ydh -l $long_opts -- "$@"); then
eval set -- "$_input"
while true; do
case "$1" in
--release|-r|--version|-v)
shift && declare -g USER_RELEASE="$1"
;;
--profile|-p)
shift && PROFILES+=("$1")
;;
--buildroot|-b)
shift && BUILDROOT="$1"
;;
--source|-s)
FROM_SOURCE=1
;;
--ssh-upgrade)
shift && SSH_UPGRADE_PATH="$1"
;;
--ssh-backup)
shift && SSH_BACKUP_PATH="$1"
;;
--flash|-f)
shift && FLASH_DEV="$1"
;;
--reset)
RESET=1
;;
--yes|-y)
YES=1
;;
--debug|-d)
echo "Debugging on"
DEBUG=1
;;
--help|-h)
printHelp && exit 0
;;
--release|-r|--version|-v) shift; declare -g USER_RELEASE="$1" ;;
--profile|-p) shift; PROFILES+=("$1") ;;
--buildroot|-b) shift; BUILDROOT="$1" ;;
--source|-s) FROM_SOURCE=1 ;;
--ssh-upgrade) shift; SSH_UPGRADE_PATH="$1" ;;
--ssh-backup) shift; SSH_BACKUP_PATH="$1" ;;
--flash|-f) shift; FLASH_DEV="$1" ;;
--reset) RESET=1 ;;
--yes|-y) YES=1 ;;
--debug|-d) echo "Debugging on"; DEBUG=1 ;;
--help|-h) print_help; exit 0 ;;
--)
shift
break
@@ -196,16 +147,14 @@ readInput() {
done
else
echo "Incorrect options provided"
printHelp && exit 1
print_help; exit 1
fi
}
installDependencies() {
install_dependencies() {
debug "${FUNCNAME[0]}"
declare -a pkg_list
declare lock_file="$BUILDROOT/.dependencies"
local lock_file="$BUILDROOT/.dependencies"
local -a pkg_list
# TODO please contribute your platform here
if ((FROM_SOURCE)); then
@@ -243,8 +192,7 @@ installDependencies() {
"perl-JSON-PP"
"swig"
"clang" # for qosify
"llvm15-libs"
)
"llvm15-libs")
;;
debian|ubuntu)
pkg_list+=(
@@ -263,8 +211,7 @@ installDependencies() {
"unzip"
"zlib1g-dev"
"file"
"wget"
)
"wget")
;;
arch)
pkg_list+=(
@@ -305,8 +252,7 @@ installDependencies() {
"util-linux"
"wget"
"which"
"zlib"
)
"zlib")
;;
*)
debug "Skipping dependency install, your OS is unsupported"
@@ -331,8 +277,7 @@ installDependencies() {
"python3"
"python2"
"axel"
"perl-IPC-Cmd"
)
"perl-IPC-Cmd")
;;
debian|ubuntu)
pkg_list+=(
@@ -348,8 +293,7 @@ installDependencies() {
"wget"
"unzip"
"python"
"axel"
)
"axel")
;;
*)
debug "Skipping dependency install, your OS is unsupported"
@@ -365,40 +309,29 @@ installDependencies() {
}
getImageBuilder() {
debug "${FUNCNAME[0]}"
get_imagebuilder() {
debug "${FUNCNAME[0]}" "$*"
local -a ib_url_file=("$1" "$2")
local -a sha256_url_file=("$3" "$4")
local -a url_file_pairs=(
"${ib_url_file[@]}"
"${sha256_url_file[@]}")
local url="$1"
for ((i=0; i<${#url_file_pairs[@]}; i+=2)); do
local url="${url_file_pairs[i]}"
local file="${url_file_pairs[i+1]}"
if [[ -f "$IB_ARCHIVE" ]]; then
if askOk "$IB_ARCHIVE exists. Re-download?"; then
execute rm -f "$IB_ARCHIVE"
else
return 0
if [[ -f $file ]] && ask_ok "$file exists. Re-download?"; then
execute rm -f "$file"
fi
if ! [[ -f "$file" ]]; then
echo "Downloading $url to $file using $DL_TOOL"
execute "$DL_TOOL" "-o" "$file" "$url"
fi
echo "Downloading Image Builder archive using $DL_TOOL"
execute "$DL_TOOL" "-o" "$IB_ARCHIVE" "$url"
done
}
getImageBuilderChecksum() {
debug "${FUNCNAME[0]}"
if [[ -f $IB_SHA256_FILE ]]; then
if askOk "$IB_SHA256_FILE exists. Re-download?"; then
execute rm -f "$IB_SHA256_FILE"
else
return 0
fi
fi
echo "Downloading Image Builder checksum using $DL_TOOL"
"$DL_TOOL" "-o" "$IB_SHA256_FILE" "$IB_SHA256_URL"
}
addRepos() {
add_repos() {
debug "${FUNCNAME[0]}"
if [[ -v P_ARR[repo] ]]; then
@@ -409,18 +342,15 @@ addRepos() {
fi
}
sshBackup() {
ssh_backup() {
debug "${FUNCNAME[0]}"
declare date hostname backup_fname
[[ -d "$FILESDIR" ]] || mkdir -p "$FILESDIR"
local date hostname backup_fname
printf -v date '%(%Y-%m-%d-%H-%M-%S)T'
hostname=$(ssh -qt "$SSH_BACKUP_PATH" echo -n \$HOSTNAME)
backup_fname="backup-$hostname-$date.tar.gz"
[[ -d "$FILESDIR" ]] || execute mkdir -p "$FILESDIR"
# Make backup archive on remote
if ! execute "ssh -t $SSH_BACKUP_PATH sysupgrade -b /tmp/$backup_fname"; then
echo "SSH backup failed"
@@ -442,13 +372,12 @@ sshBackup() {
execute "rm $BUILDDIR/$backup_fname"
}
makeImages() {
make_images() {
debug "${FUNCNAME[0]}"
# Reuse the existing output
if [[ -d "$BINDIR" ]]; then
if askOk "$BINDIR exists. Rebuild?"; then
if ask_ok "$BINDIR exists. Rebuild?"; then
execute rm -rf "$BINDIR"
else
return 0
@@ -465,26 +394,21 @@ makeImages() {
> "$BUILDDIR/make.log"
}
verifyImages() {
verify_images() {
debug "${FUNCNAME[0]}"
declare outfile
local outfile
for outfile in "$BINDIR"/*.img.gz; do
verify "$outfile" "$IB_OUT_SHA256_FILE" || return 1
done
}
flashImage() {
flash_images() {
debug "${FUNCNAME[0]}"
declare img_gz="$1"
declare dev="$2"
declare img="${img_gz%.gz}"
declare partitions
local img_gz="$1"
local dev="$2"
local img="${img_gz%.gz}"
local partitions
if [[ ! -e "$dev" ]]; then
echo "The device specified by --flash could not be found"
@@ -508,18 +432,15 @@ flashImage() {
echo "Image flashed sucessfully!"
else
echo "dd failed!"
exit 1
return 1
fi
}
sshUpgrade() {
ssh_upgrade() {
debug "${FUNCNAME[0]}"
declare img_gz="$1"
declare ssh_path="$2"
declare img_fname="${img_gz##*/}"
local img_gz="$1"
local ssh_path="$2"
local img_fname="${img_gz##*/}"
if ! [[ -f $img_gz ]]; then
echo "$img_gz is missing, check build output"
@@ -541,14 +462,13 @@ sshUpgrade() {
ssh "$ssh_path" "sysupgrade -F /tmp/$img_fname"
}
fromSource() {
debug "${FUNCNAME[0]}"
declare src_url="https://github.com/openwrt/openwrt.git"
declare seed_file="$GITWORKTREEDIR/.config"
declare pkg config commit seed_file wt_commit description
declare -a make_opts config_opts
from_source() {
debug "${FUNCNAME[0]}" "$*"
local seed_url="$1"
local src_url="https://github.com/openwrt/openwrt.git"
local seed_file="$GITWORKTREEDIR/.config"
local pkg config commit seed_file wt_commit description
local -a make_opts config_opts
echo "Building from source is under development"
@@ -568,7 +488,7 @@ fromSource() {
[0-9][0-9].[0-9][0-9].*)
local branch="openwrt-${RELEASE%.*}"
local tag="v$RELEASE"
if askOk "Use $branch branch HEAD (y, recommended) or $tag tag (N)?"; then
if ask_ok "Use $branch branch HEAD (y, recommended) or $tag tag (N)?"; then
wt_commit="origin/$branch"
else
wt_commit="$tag"
@@ -580,7 +500,6 @@ fromSource() {
;;
esac
# TODO There's a bug in the make clean functions that seem to invoke a full make
# if [[ -d "$GITWORKTREEDIR" ]]; then
# execute git -C "$GITWORKTREEDIR" checkout "$wt_commit"
@@ -589,7 +508,6 @@ fromSource() {
# execute git -C "$GITSRCDIR" worktree add --force --detach "$GITWORKTREEDIR" "$wt_commit"
# fi
# To workaround bug, don't use make *clean, blow it away and start fresh
[[ -d "$GITWORKTREEDIR" ]] && execute rm -rf "$GITWORKTREEDIR"
execute git -C "$GITSRCDIR" worktree add --force --detach "$GITWORKTREEDIR" "$wt_commit"
@@ -617,8 +535,8 @@ fromSource() {
./scripts/feeds install -a -f
# Grab the release seed config
if ! execute curl -so "$seed_file" "$SEED_URL"; then
echo "Could not obtain $seed_file from $SEED_URL"
if ! execute "$DL_TOOL" "-o" "$seed_file" "$seed_url"; then
echo "Could not obtain $seed_file from $seed_url"
return 1
fi
@@ -698,7 +616,7 @@ fromSource() {
# Generic helpers
debug() { ((DEBUG)) && echo "Debug: $*"; }
askOk() {
ask_ok() {
((YES)) && return
local r
read -r -p "$* [y/N]: " r
@@ -706,19 +624,19 @@ askOk() {
[[ "$r" =~ ^(yes|y)$ ]]
}
extract() {
debug "${FUNCNAME[0]}"
declare archive="$1"
declare out_dir="$2"
debug "${FUNCNAME[0]}" "$*"
local archive="$1"
local out_dir="$2"
if ! execute tar -axf "$archive" -C "$out_dir" --strip-components 1; then
echo "Extraction failed"
return 1
fi
}
verify() {
debug "${FUNCNAME[0]}"
declare file_to_check="$1"
declare sumfile="$2"
declare checksum
debug "${FUNCNAME[0]}" "$*"
local file_to_check="$1"
local sumfile="$2"
local checksum
hash sha256sum &>/dev/null || return 1
[[ -f $sumfile && -f $file_to_check ]] || return 1
@@ -726,8 +644,8 @@ verify() {
echo -n "$checksum $file_to_check" | sha256sum --check --status
}
load() {
debug "${FUNCNAME[0]}"
declare source_file="$1"
debug "${FUNCNAME[0]}" "$*"
local source_file="$1"
# shellcheck disable=SC1090
[[ -f $source_file ]] && source "$source_file"
}
@@ -739,15 +657,12 @@ execute() {
fi
}
main() {
debug "${FUNCNAME[0]}"
init
load "$SCRIPTDIR/profiles"
readInput "$@"
parse_input "$@"
# Fallback to SCRIPTDIR if BUILDROOT has not been set
declare -g BUILDROOT="${BUILDROOT:=$SCRIPTDIR}"
@@ -760,21 +675,21 @@ main() {
fi
for dir in "$BUILDROOT/src" "$BUILDROOT/bin"; do
[[ -d "$dir" ]] || mkdir -p "$dir"
[[ -d "$dir" ]] || execute mkdir -p "$dir"
done
# Allow --reset without a profile
if ((RESET)) && [[ ${#PROFILES} -lt 1 ]]; then
for d in "$BUILDROOT/src" "$BUILDROOT/bin"; do
askOk "Remove $d?" && execute rm -rf "$d"
ask_ok "Remove $d?" && execute rm -rf "$d"
done
exit $?
fi
installDependencies
install_dependencies
for profile in "${PROFILES[@]}"; do
debug "Starting profile: $profile"
debug "Running profile: $profile"
if [[ ! ${!profile@a} = A ]]; then
echo "Profile '$profile' does not exist"
@@ -783,26 +698,17 @@ main() {
# Store profile in P_ARR nameref
declare -gn P_ARR="$profile"
# Load profile
declare -g FILESYSTEM="${P_ARR[filesystem]:="squashfs"}"
declare -g TARGET="${P_ARR[target]}"
declare -g DEVICE="${P_ARR[device]}"
declare -g PACKAGES="${P_ARR[packages]:-}"
# Release precedence: user input>profile>env>hardcode
declare -g RELEASE="${USER_RELEASE:=${P_ARR[release]:=$RELEASE}}"
# normalize release input
# normalize RELEASE
case "$RELEASE" in
snapshot|latest|main|master) # normalize aliases
RELEASE="snapshot"
;;
v[0-9][0-9].[0-9][0-9].*) # tag to semantic
RELEASE="${RELEASE#v}"
;;
[0-9][0-9].[0-9][0-9].*)
;;
snapshot|latest|main|master) RELEASE="snapshot" ;;
v[0-9][0-9].[0-9][0-9].*) RELEASE="${RELEASE#v}" ;;
[0-9][0-9].[0-9][0-9].*) ;;
*)
if ! ((FROM_SOURCE)); then
echo "Error: Invalid release version format"
@@ -817,16 +723,6 @@ main() {
declare -g BUILDDIR="$BUILDROOT/src/$profile/$RELEASE"
declare -g BINDIR="$BUILDROOT/bin/$profile/$RELEASE"
if (( RESET )); then
if (( FROM_SOURCE )); then
[[ -d $GITWORKTREEDIR ]] && askOk "Remove $GITWORKTREEDIR?"
execute git worktree remove --force "$GITWORKTREEDIR"
execute rm -rf "$GITWORKTREEDIR"
elif [[ -d $BUILDDIR ]] && askOk "Remove $BUILDDIR?"; then
execute rm -rf "$BUILDDIR"
fi
fi
if [[ "$RELEASE" == "snapshot" ]]; then
declare url_prefix="https://downloads.openwrt.org/snapshots/targets/$TARGET"
declare url_filename="openwrt-imagebuilder-${TARGET//\//-}.Linux-x86_64.tar.zst"
@@ -837,70 +733,53 @@ main() {
declare img_fname="openwrt-$RELEASE-${TARGET//\//-}-$DEVICE-$FILESYSTEM"
fi
declare ib_url="$url_prefix/$url_filename"
local ib_url="$url_prefix/$url_filename"
local ib_file="$BUILDDIR/$url_filename"
local ib_sha256_url="$url_prefix/sha256sums"
local ib_sha256_file="$BUILDDIR/sha256sums"
local seed_url="$url_prefix/config.buildinfo"
if ((FROM_SOURCE)); then
declare -g SYSUPGRADEIMGGZ="$BINDIR/targets/$img_fname-sysupgrade.img.gz"
declare -g SEED_URL="$url_prefix/config.buildinfo"
else
declare -g SYSUPGRADEIMGGZ="$BUILDDIR/$img_fname-sysupgrade.img.gz"
declare -g IB_ARCHIVE="$BUILDDIR/$url_filename"
declare -g IB_SHA256_URL="$url_prefix/sha256sums"
declare -g IB_SHA256_FILE="$IB_ARCHIVE.sha256sums"
declare -g IB_OUT_SHA256_FILE="$BINDIR/sha256sums"
fi
if ((RESET)); then
if ((FROM_SOURCE)); then
[[ -d $GITWORKTREEDIR ]] && ask_ok "Remove $GITWORKTREEDIR?"
execute git worktree remove --force "$GITWORKTREEDIR"
execute rm -rf "$GITWORKTREEDIR"
elif [[ -d $BUILDDIR ]] && ask_ok "Remove $BUILDDIR?"; then
execute rm -rf "$BUILDDIR"
fi
fi
if ((DEBUG)); then
echo "Profile settings:"
for x in "${!P_ARR[@]}"; do printf "%s=%s\n" "$x" "${P_ARR[$x]}"; done
echo "Build settings:"
cat <<- EOF
PROFILE, P_ARR (should match)=$profile, ${!P_ARR}
BUILDROOT=$BUILDROOT
BUILDDIR=$BUILDDIR
GITSRCDIR=$GITSRCDIR
GITWORKTREEDIR=$GITWORKTREEDIR
BINDIR=$BINDIR
TARGET=$TARGET
DEVICE=$DEVICE
RELEASE=$RELEASE
FILESYSTEM=$FILESYSTEM
SYSUPGRADEIMGGZ=$SYSUPGRADEIMGGZ
ib_url=$ib_url
EOF
echo "Environement variables:"
declare -p
fi
if ((FROM_SOURCE)); then
fromSource || return $?
from_source "$seed_url" || return $?
else
[[ -d $BUILDDIR ]] || mkdir -p "$BUILDDIR"
getImageBuilder "$ib_url" &&
getImageBuilderChecksum &&
verify "$IB_ARCHIVE" "$IB_SHA256_FILE" &&
extract "$IB_ARCHIVE" "$BUILDDIR" || return $?
addRepos
makeImages &&
verifyImages
get_imagebuilder "$ib_url" "$ib_file" "$ib_sha256_url" "$ib_sha256_file" &&
verify "$ib_file" "$ib_sha256_file" &&
extract "$ib_file" "$BUILDDIR" || return $?
add_repos
make_images &&
verify_images
#copyFiles
fi
[[ -v SSH_BACKUP_PATH ]] &&
sshBackup
[[ -v SSH_UPGRADE_PATH ]] &&
sshUpgrade "$SYSUPGRADEIMGGZ" "$SSH_UPGRADE_PATH"
[[ -v FLASH_DEV ]] &&
flashImage "$SYSUPGRADEIMGGZ" "$FLASH_DEV"
[[ -v SSH_BACKUP_PATH ]] && ssh_backup
[[ -v SSH_UPGRADE_PATH ]] && ssh_upgrade "$SYSUPGRADEIMGGZ" "$SSH_UPGRADE_PATH"
[[ -v FLASH_DEV ]] && flash_images "$SYSUPGRADEIMGGZ" "$FLASH_DEV"
done
}
main "$@"
exit
# VM setup (for testing)
# sudo sgdisk -N 0 /dev/vda &&
# sudo mkfs.ext4 /dev/vda1
# mkdir ~/mnt
# sudo mount /dev/vda1 ~/mnt
# sudo chown liveuser:liveuser -R ~/mnt
exit $?