diff --git a/autodl-initializer.sh b/autodl-initializer.sh index 5c20450..83635ac 100644 --- a/autodl-initializer.sh +++ b/autodl-initializer.sh @@ -25,9 +25,10 @@ # Example: 'en_US.UTF-8', 'zh_CN.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' -APT_MIRROR="http://mirrors.bfsu.edu.cn" +# Note: AutoDL instances have default fast mirrors. +APT_MIRROR="" APT_SOURCE_FILE="/etc/apt/sources.list" APT_SOURCES_LIST_D="/etc/apt/sources.list.d" @@ -75,85 +76,14 @@ add_hook_function() { # } # # Add the function to the hook list. # add_hook_function my_custom_setup -turbo_autodl_network() { - source /etc/network_turbo -} -add_hook_function turbo_autodl_network - -# 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 +disable_conda_auto_base() { + local condarc="${HOME}/.condarc" + if ! grep -qF "auto_activate_base" $condarc; then + echo -e "\nauto_activate_base: false" >> "$condarc" + echo "Conda auto base activation is disabled." fi } - -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 +add_hook_function disable_conda_auto_base @@ -165,16 +95,28 @@ add_hook_function install_uv # 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" < "$dest" +} + +write_file "${HOME}/.tmux.conf" < "${HOME}/.tmux.conf" set -g mouse on set -g default-terminal "screen-256color" EOF -# condarc -cat << EOF >> "${HOME}/.condarc" - -auto_activate_base: false +write_file "${HOME}/.config/uv/uv.toml" < /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_locale() { echo "--- Setting system locale to '$SYSTEM_LOCALE' ---" @@ -223,7 +189,6 @@ set_locale() { # echo "Locale setting complete." } - # Function to check for required variables and distro _check_apt_prerequisites() { if [[ -z "$APT_MIRROR" ]]; then @@ -250,8 +215,8 @@ _get_source_style() { if [[ -d "$APT_SOURCES_LIST_D" ]]; then # Check if there are any .sources files in the sources.list.d directory if find "$APT_SOURCES_LIST_D" -name "*.sources" -print -quit | grep -q '.*'; then - echo "deb-822" - return + echo "deb-822" + return fi fi echo "legacy" @@ -363,6 +328,12 @@ EOF # Replace APT mirror main function. # Both legacy sources.list and DEB-822 format are suppported. 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 ---" _check_apt_prerequisites || return 1 @@ -392,31 +363,45 @@ replace_apt_mirror() { if [[ $? -eq 0 ]]; then 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 echo "APT source replacement failed." return 1 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() { 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 fi + + if ! update_apt_list; then + echo "!!! Failed to install APT packages." + return 1 + fi + echo "--- Installing packages: $PACKAGES_TO_INSTALL ---" # Use 'apt-get install -y' for non-interactive installation apt-get install -y $PACKAGES_TO_INSTALL echo "--- Package installation complete. ---" } +# Add user SSH public key. add_ssh_key() { # Check if the SSH_PUBLIC_KEY variable is non-empty if [[ -n "$SSH_PUBLIC_KEY" ]]; then @@ -450,6 +435,75 @@ add_ssh_key() { 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. clean_apt_cache() { echo "--- Removing APT caches ---" @@ -483,6 +537,9 @@ install_packages clean_apt_cache add_ssh_key +turbo_autodl_network +install_uv + # Execute the functions in the hook list. execute_hooks