feat: 更新工具脚本

This commit is contained in:
2025-04-07 12:04:15 +08:00
parent b0d7ef1443
commit 02fe9112bd
3 changed files with 151 additions and 94 deletions
+85 -65
View File
@@ -4,6 +4,14 @@
# shell 执行参数,分别为 -e(命令执行错误则退出脚本) -u(变量未定义则报错) -x(打印实际待执行的命令行)
set -eux
# 定义错误处理函数,添加错误位置信息
error() {
local error_location="$1"
local error_message="$2"
echo "Error at $error_location: $error_message" >&2
exit 1
}
print_usage() {
echo "Usage: download_pkg <COMMAND> <PACKAGE-NAME> \"<URLS>\" [OPTIONS]"
echo ""
@@ -35,41 +43,44 @@ check_pgp() {
local name_asc=${1:?missing asc file name}
local name=${2:?missing file name}
local keys="${3:?missing key id}"
GNUPGHOME="$(mktemp -d)"
if which gpg >/dev/null 2>&1; then
for key in $keys; do
gpg --batch --keyserver ha.pool.sks-keyservers.net --recv-keys "${key}" ||
gpg --batch --keyserver pgp.mit.edu --recv-keys "${key}" ||
gpg --batch --keyserver keys.gnupg.net --recv-keys "${key}" ||
gpg --batch --keyserver keyserver.pgp.com --recv-keys "${key}";
done
gpg --batch --verify "$name_asc" "$name"
command -v gpgconf > /dev/null && gpgconf --kill all
fi
if which gpg >/dev/null 2>&1; then
local key_servers=("pgp.mit.edu" "keys.gnupg.net" "keyserver.pgp.com" "ha.pool.sks-keyservers.net")
for key in $keys; do
for server in "${key_servers[@]}"; do
if gpg --batch --keyserver "$server" --recv-keys "${key}" --timeout 10; then
break
fi
done
done
gpg --batch --verify "$name_asc" "$name" || error "PGP verification" "PGP verification failed"
command -v gpgconf > /dev/null && gpgconf --kill all
fi
}
# 获取并解析参数
ARGS=$(getopt -o g:s:h -l "checkpgp:,checksum:,help" -n "download-pkg" -- "$@")
if [ $? -ne 0 ];
then
exit 1
parsed_args=$(getopt -o g:s:h -l "checkpgp:,checksum:,help" -n "download-pkg" -- "$@")
if [ $? -ne 0 ]; then
error "Parameter parsing" "Failed to parse command line arguments."
print_usage
exit 1
fi
eval set -- "$ARGS";
eval set -- "$parsed_args";
while true; do
case "$1" in
-g|--checkpgp)
shift
if [ -n "$1" ]; then
PACKAGE_KEYS=$1
package_keys=$1
shift
fi
;;
-s|--checksum)
shift
if [ -n "$1" ]; then
PACKAGE_SHA256=$1
package_sha256=$1
shift
fi
;;
@@ -88,7 +99,7 @@ done
case "$1" in
download|install|unpack) ;;
*)
error "Unrecognized command: $1"
error "Command validation" "Unrecognized command: $1"
print_usage
exit 1
;;
@@ -96,40 +107,50 @@ esac
# 检测输入参数是否足够,需要至少提供软件包名称 及 下载路径
if [ $# -lt 3 ]; then
error "Parameter validation" "Insufficient parameters. Please provide package name and URLs."
print_usage
exit 1
fi
INSTALL_ROOT=/usr/local
CACHE_ROOT=/tmp
install_root=/usr/local
cache_root=/tmp
PACKAGE="$2"
PACKAGE_URLS=$3
package="$2"
package_urls=$3
cd $INSTALL_ROOT
echo "Downloading $PACKAGE package"
for url in $PACKAGE_URLS; do
echo "Try $url/$PACKAGE"
if wget -O "$CACHE_ROOT/$PACKAGE" "$url/$PACKAGE" && [ -s "$CACHE_ROOT/$PACKAGE" ]; then
if [ -n "${PACKAGE_KEYS:-}" ]; then
wget -O "$CACHE_ROOT/$PACKAGE.asc" "$url/$PACKAGE.asc" || wget -O "$CACHE_ROOT/$PACKAGE.asc" "$url/$PACKAGE.sign" || :
if [ ! -e "$CACHE_ROOT/$PACKAGE.asc" ]; then
exit 1
# 检查缓存目录中是否已存在该软件包
if [ -e "$cache_root/$package" ]; then
echo "Package already exists in cache: $cache_root/$package"
else
cd $install_root
echo "Downloading $package package"
for url in $package_urls; do
echo "Try $url/$package"
if wget -T 10 -O "$cache_root/$package" "$url/$package" && [ -s "$cache_root/$package" ]; then
if [ -n "${package_keys:-}" ]; then
wget -T 10 -O "$cache_root/$package.asc" "$url/$package.asc" || wget -T 10 -O "$cache_root/$package.asc" "$url/$package.sign" || :
if [ ! -e "$cache_root/$package.asc" ]; then
error "PGP signature download" "Failed to download PGP signature file."
exit 1
fi
fi
break
else
echo "Failed to download from $url/$package"
fi
fi
break
fi
done
if [ -n "${PACKAGE_SHA256:-}" ]; then
echo "Verifying package integrity"
echo "$PACKAGE_SHA256 *$CACHE_ROOT/$PACKAGE" | sha256sum -c -
done
fi
if [ -e "$CACHE_ROOT/$PACKAGE.asc" ]; then
echo "Verifying package with PGP"
check_pgp "$CACHE_ROOT/$PACKAGE.asc" "$CACHE_ROOT/$PACKAGE" "$PACKAGE_KEYS"
if [ -n "${package_sha256:-}" ]; then
echo "Verifying package integrity"
if ! echo "$package_sha256 *$cache_root/$package" | sha256sum -c -; then
error "SHA256 verification" "SHA256 verification failed"
fi
fi
if [ -e "$cache_root/$package.asc" ]; then
echo "Verifying package with PGP"
check_pgp "$cache_root/$package.asc" "$cache_root/$package" "$package_keys"
fi
# If the tarball has too many files, it can trigger a bug
@@ -137,27 +158,26 @@ fi
# to workaround it. As the overhead is too big (~40 MB), it is not added by
# default. Source: https://github.com/coreos/bugs/issues/1095
# 安装或解压软件
case "$1" in
download)
echo "Download success: $CACHE_ROOT/$PACKAGE"
;;
install)
echo "Installing $PACKAGE"
cp $CACHE_ROOT/$PACKAGE /usr/local/sbin/
;;
unpack)
if ! tar -taf $CACHE_ROOT/$PACKAGE >/dev/null 2>&1; then
echo "Invalid or corrupt '$PACKAGE' package."
exit 1
fi
echo "Unpacking $PACKAGE to $CACHE_ROOT"
cd $CACHE_ROOT
if which bsdtar >/dev/null 2>&1; then
bsdtar -xf $CACHE_ROOT/$PACKAGE
else
tar --no-same-owner -xaf $CACHE_ROOT/$PACKAGE
fi
;;
download)
echo "Download success: $cache_root/$package"
;;
install)
echo "Installing $package"
cp $cache_root/$package /usr/local/sbin/
;;
unpack)
if ! tar -taf $cache_root/$package >/dev/null 2>&1; then
error "Package integrity check" "Invalid or corrupt '$package' package."
exit 1
fi
echo "Unpacking $package to $cache_root"
cd $cache_root
if which bsdtar >/dev/null 2>&1; then
bsdtar -xf $cache_root/$package
else
tar --no-same-owner -xaf $cache_root/$package
fi
;;
esac
+40 -27
View File
@@ -1,9 +1,15 @@
#!/bin/bash
# Ver: 1.0 by Endial Fang (endial@126.com)
# Ver: 1.2 by Endial Fang (endial@126.com)
#
# shell 执行参数,分别为 -e(命令执行错误则退出脚本) -u(变量未定义则报错) -x(打印实际待执行的命令行)
set -eux
# 检查用户权限
if [ "$EUID" -ne 0 ]; then
echo "Error: This script must be run as root."
exit 1
fi
print_usage() {
echo "Usage: install_pkg <PACKAGE-NAME>"
echo ""
@@ -23,36 +29,43 @@ if [ $# -lt 1 ]; then
exit 1
fi
case "$1" in
-h|--help)
print_usage
exit 0
;;
esac
# 解析命令行参数
UPDATE=true
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
print_usage
exit 0
;;
*)
break
;;
esac
done
retry=0
max=2
export DEBIAN_FRONTEND=noninteractive &&
until [ $retry -gt $max ]; do
set +e
(
export DEBIAN_FRONTEND=noninteractive &&
apt-get update &&
apt-get upgrade -y &&
apt-get install -y --no-install-recommends "$@"
)
CODE=$?
set -e
if [ $CODE -eq 0 ]; then
break
fi
if [ $retry -eq $max ]; then
exit $CODE
fi
echo "apt failed, retrying"
retry=$(($retry + 1))
set +e
(
apt-get update &&
apt-get install -y --no-install-recommends $*
)
CODE=$?
set -e
if [ $CODE -eq 0 ]; then
break
fi
if [ $retry -eq $max ]; then
echo "Failed to install packages after $max retries."
exit $CODE
fi
echo "apt failed, retrying"
retry=$(($retry + 1))
done
apt-get purge -y --auto-remove
apt-get autoclean -y
apt-get purge -y --auto-remove && apt-get autoclean -y || :
rm -rf /var/lib/apt/lists /var/cache/apt/archives || :
echo "Installation completed successfully."
+26 -2
View File
@@ -1,7 +1,31 @@
#!/bin/bash
# Ver: 1.0 by Endial Fang (endial@126.com)
# Ver: 1.2 by Endial Fang (endial@126.com)
#
# 此脚本用于根据传入的参数选择对应的 apt 源配置文件并复制到指定目录
# shell 执行参数,分别为 -e(命令执行错误则退出脚本) -u(变量未定义则报错) -x(打印实际待执行的命令行)
set -eux
cp /etc/apt/sources/${1:-default}.sources /etc/apt/sources.list.d/debian.sources
# 检查是否有足够的权限
if [ "$EUID" -ne 0 ]; then
echo "Error: This script must be run as root."
exit 1
fi
# 获取用户传入的参数,若未传入则使用默认值 "default"
source_name=${1:-default}
# 定义源文件路径
source_file="/etc/apt/sources/${source_name}.sources"
# 检查源文件是否存在
if [ ! -f "$source_file" ]; then
echo "Error: Source file $source_file does not exist."
exit 1
fi
# 定义目标文件路径
target_file="/etc/apt/sources.list.d/debian.sources"
# 复制源文件到目标文件
cp "$source_file" "$target_file"
echo "Successfully selected apt source: $source_name"