Add many functions and refactors.

This commit is contained in:
2025-09-19 21:48:47 +08:00
parent 202d873b8a
commit 89eac2841a

View File

@ -25,9 +25,10 @@
# Example: 'en_US.UTF-8', 'zh_CN.UTF-8' # Example: 'en_US.UTF-8', 'zh_CN.UTF-8'
SYSTEM_LOCALE="en_US.UTF-8" SYSTEM_LOCALE="en_US.UTF-8"
# Set the desired APT mirror URL. Scheme (http/https) is needed. # Set the APT mirror URL. Scheme (http/https) is needed.
# Example: 'http://mirrors.kernel.org' # Example: 'http://mirrors.kernel.org'
APT_MIRROR="http://mirrors.bfsu.edu.cn" # Note: AutoDL instances have default fast mirrors.
APT_MIRROR=""
APT_SOURCE_FILE="/etc/apt/sources.list" APT_SOURCE_FILE="/etc/apt/sources.list"
APT_SOURCES_LIST_D="/etc/apt/sources.list.d" APT_SOURCES_LIST_D="/etc/apt/sources.list.d"
@ -75,85 +76,14 @@ add_hook_function() {
# } # }
# # Add the function to the hook list. # # Add the function to the hook list.
# add_hook_function my_custom_setup # add_hook_function my_custom_setup
turbo_autodl_network() { disable_conda_auto_base() {
source /etc/network_turbo local condarc="${HOME}/.condarc"
} if ! grep -qF "auto_activate_base" $condarc; then
add_hook_function turbo_autodl_network echo -e "\nauto_activate_base: false" >> "$condarc"
echo "Conda auto base activation is disabled."
# Download helper, pass the URL and Destination as parameter.
download_file() {
local url=$1
local output=$2
if command -v curl &> /dev/null; then
curl -sSL -o "$output" "$url" || return 1
elif command -v wget &> /dev/null; then
wget -qO "$output" "$url" || return 1
else
echo "Error: Neither curl nor wget found. Install one to proceed." >&2
return 1
fi fi
} }
add_hook_function disable_conda_auto_base
install_uv() {
local url="${UV_DOWNLOAD_URL}"
local install_path="${UV_INSTALL_PATH}"
local shell_type=""
local shell_profile=""
if ! mkdir -p "$install_path"; then
echo "Error: failed to create install directory: $install_path" >&2
return 1
fi
# curl -sSL "$url" | tar --strip-components=1 -C "$install_path" -xzf -
if ! temp_file=$(mktemp); then
echo "Error: Failed to create temporary file" >&2
return 1
fi
if ! download_file "$url" "$temp_file"; then
rm -f "$temp_file"
return 1
fi
if ! tar --strip-components=1 -C "$install_path" -xzf "$temp_file"; then
echo "Error: Failed to extract archive" >&2
rm -f "$temp_file"
return 1
fi
rm -f "$temp_file"
echo "uv installed to $install_path"
if [ -n "$BASH_VERSION" ]; then
shell_type="bash"
shell_profile="${HOME}/.bashrc"
elif [ -n "$ZSH_VERSION" ]; then
shell_type="zsh"
shell_profile="${HOME}/.zshrc"
else
shell_type="bash"
shell_profile="${HOME}/.profile"
fi
# Ensure install path is in PATH
if ! echo "$PATH" | grep -q "$install_path"; then
echo "Adding $install_path to PATH and Shell..."
export PATH="$install_path:$PATH"
# Persist for future shells
if ! grep -q "$install_path" "$shell_profile" 2>/dev/null; then
echo "export PATH=\"$install_path:\$PATH\"" >> "$shell_profile"
fi
fi
echo "Modifying shell profile for uv..."
echo "eval \"\$(uv generate-shell-completion ${shell_type})\"" >> "$shell_profile"
echo "eval \"\$(uvx --generate-shell-completion ${shell_type})\"" >> "$shell_profile"
echo "Installation complete. Run 'uv --version' to verify."
}
add_hook_function install_uv
@ -165,16 +95,28 @@ add_hook_function install_uv
# can make some configuration so you won't need to do it later. # can make some configuration so you won't need to do it later.
############################################################### ###############################################################
# File creation helper to create parent directories automatically.
# Example:
# write_file "$HOME/my/file.txt" <<EOF
# file line 1
# file line 2
# EOF
write_file() {
local dest="$1"
mkdir -p -- "$(dirname -- "$dest")" || return 1
cat > "$dest"
}
write_file "${HOME}/.tmux.conf" <<EOF
# tmux configuration # tmux configuration
cat << EOF > "${HOME}/.tmux.conf"
set -g mouse on set -g mouse on
set -g default-terminal "screen-256color" set -g default-terminal "screen-256color"
EOF EOF
# condarc write_file "${HOME}/.config/uv/uv.toml" <<EOF
cat << EOF >> "${HOME}/.condarc" [[index]]
url = "https://mirrors.bfsu.edu.cn/pypi/web/simple"
auto_activate_base: false default = true
EOF EOF
@ -210,6 +152,30 @@ check_root() {
fi fi
} }
# Download helper, pass the URL and Destination as parameter.
download_file() {
local url=$1
local output=$2
if command -v curl &> /dev/null; then
curl -sSL -o "$output" "$url" || return 1
elif command -v wget &> /dev/null; then
wget -qO "$output" "$url" || return 1
else
echo "Error: Neither curl nor wget found. Install one to proceed." >&2
return 1
fi
}
# Set AutoDL network turbo to speed up installation.
turbo_autodl_network() {
local turbo_script="/etc/network_turbo"
if [[ -f "$turbo_script" ]]; then
echo "--- Using AutoDL Network Turbo ---"
. "$turbo_script"
fi
}
# Set the system locale. # Set the system locale.
set_locale() { set_locale() {
echo "--- Setting system locale to '$SYSTEM_LOCALE' ---" echo "--- Setting system locale to '$SYSTEM_LOCALE' ---"
@ -223,7 +189,6 @@ set_locale() {
# echo "Locale setting complete." # echo "Locale setting complete."
} }
# Function to check for required variables and distro # Function to check for required variables and distro
_check_apt_prerequisites() { _check_apt_prerequisites() {
if [[ -z "$APT_MIRROR" ]]; then if [[ -z "$APT_MIRROR" ]]; then
@ -363,6 +328,12 @@ EOF
# Replace APT mirror main function. # Replace APT mirror main function.
# Both legacy sources.list and DEB-822 format are suppported. # Both legacy sources.list and DEB-822 format are suppported.
replace_apt_mirror() { replace_apt_mirror() {
if [[ -z "$APT_MIRROR" ]]; then
echo ">>> Skipping APT mirrors setting up."
echo "To set up APT mirrors, set APT_MIRROR."
return 1
fi
echo "--- Setting up APT mirrors ---" echo "--- Setting up APT mirrors ---"
_check_apt_prerequisites || return 1 _check_apt_prerequisites || return 1
@ -392,31 +363,45 @@ replace_apt_mirror() {
if [[ $? -eq 0 ]]; then if [[ $? -eq 0 ]]; then
echo "APT source successfully updated to '$APT_MIRROR'." echo "APT source successfully updated to '$APT_MIRROR'."
echo "Running 'apt update' to refresh package lists..."
apt update
if [[ $? -eq 0 ]]; then
echo "APT update completed successfully."
else
echo "Warning: 'apt update' failed. Please check your network connection and the mirror URL."
fi
else else
echo "APT source replacement failed." echo "APT source replacement failed."
return 1 return 1
fi fi
} }
# Run apt update to fetch newest repository index.
update_apt_list() {
echo "-- Refreshing APT package lists ---"
apt update
if [[ $? -eq 0 ]]; then
echo "APT update completed successfully."
else
echo "Warning: 'apt update' failed. Please check your network connection and the mirror URL."
return 1
fi
return 0
}
# Install packages. # Install packages.
install_packages() { install_packages() {
if [[ -z "$PACKAGES_TO_INSTALL" ]]; then if [[ -z "$PACKAGES_TO_INSTALL" ]]; then
# echo "No packages specified to install. Skipping." echo ">>> Skipping APT packages installation."
echo "To install APT packages, set PACKAGES_TO_INSTALL."
return 0 return 0
fi fi
if ! update_apt_list; then
echo "!!! Failed to install APT packages."
return 1
fi
echo "--- Installing packages: $PACKAGES_TO_INSTALL ---" echo "--- Installing packages: $PACKAGES_TO_INSTALL ---"
# Use 'apt-get install -y' for non-interactive installation # Use 'apt-get install -y' for non-interactive installation
apt-get install -y $PACKAGES_TO_INSTALL apt-get install -y $PACKAGES_TO_INSTALL
echo "--- Package installation complete. ---" echo "--- Package installation complete. ---"
} }
# Add user SSH public key.
add_ssh_key() { add_ssh_key() {
# Check if the SSH_PUBLIC_KEY variable is non-empty # Check if the SSH_PUBLIC_KEY variable is non-empty
if [[ -n "$SSH_PUBLIC_KEY" ]]; then if [[ -n "$SSH_PUBLIC_KEY" ]]; then
@ -450,6 +435,75 @@ add_ssh_key() {
fi fi
} }
# Install uv, a powerful replacement of pip.
install_uv() {
if [[ -z "$UV_DOWNLOAD_URL" || -z "$UV_INSTALL_PATH" ]]; then
echo ">>> Skipping uv installation..."
echo "To install uv, set UV_DOWNLOAD_URL and UV_INSTALL_PATH."
return 1
fi
echo "--- Installing uv ---"
local url="${UV_DOWNLOAD_URL}"
local install_path="${UV_INSTALL_PATH}"
local shell_type=""
local shell_profile=""
if ! mkdir -p "$install_path"; then
echo "Error: failed to create install directory: $install_path" >&2
return 1
fi
# curl -sSL "$url" | tar --strip-components=1 -C "$install_path" -xzf -
if ! temp_file=$(mktemp); then
echo "Error: Failed to create temporary file" >&2
return 1
fi
if ! download_file "$url" "$temp_file"; then
rm -f "$temp_file"
return 1
fi
if ! tar --strip-components=1 -C "$install_path" -xzf "$temp_file"; then
echo "Error: Failed to extract archive" >&2
rm -f "$temp_file"
return 1
fi
rm -f "$temp_file"
echo "uv installed to $install_path"
if [ -n "$BASH_VERSION" ]; then
shell_type="bash"
shell_profile="${HOME}/.bashrc"
elif [ -n "$ZSH_VERSION" ]; then
shell_type="zsh"
shell_profile="${HOME}/.zshrc"
else
shell_type="bash"
shell_profile="${HOME}/.profile"
fi
# Ensure install path is in PATH
if ! echo "$PATH" | grep -q "$install_path"; then
echo "Adding $install_path to PATH and Shell..."
export PATH="$install_path:$PATH"
# Persist for future shells
if ! grep -q "$install_path" "$shell_profile" 2>/dev/null; then
echo "export PATH=\"$install_path:\$PATH\"" >> "$shell_profile"
fi
fi
if ! grep -qE "uv(x)?(\s)*(-)*generate-shell-completion" "$shell_profile"; then
echo "Adding shell completion for uv to profile..."
echo "eval \"\$(uv generate-shell-completion ${shell_type})\"" >> "$shell_profile"
echo "eval \"\$(uvx --generate-shell-completion ${shell_type})\"" >> "$shell_profile"
fi
echo "Installation complete. Run 'uv --version' to verify."
}
# Remove APT caches. # Remove APT caches.
clean_apt_cache() { clean_apt_cache() {
echo "--- Removing APT caches ---" echo "--- Removing APT caches ---"
@ -483,6 +537,9 @@ install_packages
clean_apt_cache clean_apt_cache
add_ssh_key add_ssh_key
turbo_autodl_network
install_uv
# Execute the functions in the hook list. # Execute the functions in the hook list.
execute_hooks execute_hooks