consolidate exit tasks

This commit is contained in:
Michael Yang 2023-11-21 15:32:00 -08:00
parent 06ec7f4824
commit 31488be1e3

View File

@ -8,10 +8,6 @@ status() { echo ">>> $*" >&2; }
error() { echo "ERROR $*"; exit 1; } error() { echo "ERROR $*"; exit 1; }
warning() { echo "WARNING: $*"; } warning() { echo "WARNING: $*"; }
TEMP_DIR=$(mktemp -d)
cleanup() { rm -rf "$TEMP_DIR"; }
trap cleanup EXIT
available() { command -v "$1" >/dev/null; } available() { command -v "$1" >/dev/null; }
require() { require() {
MISSING='' MISSING=''
@ -52,6 +48,31 @@ if [ -n "$NEEDS" ]; then
exit 1 exit 1
fi fi
TEMP_DIR=$(mktemp -d)
cleanup() {
EXIT_CODE=$?
rm -rf "$TEMP_DIR"
if available nvidia-smi && lsmod | grep -qv nvidia; then
status 'Reboot to complete NVIDIA CUDA driver install.'
fi
if available systemctl >/dev/null; then
$SUDO systemctl restart ollama
timeout 10 sh -c 'while :; do [ "$(curl -s http://127.0.0.1:11434)" = "Ollama is running" ] && break; sleep 0.2; done' \
&& status 'Ollama service is available at 127.0.0.1:11434' \
|| true
fi
if available ollama; then
status 'Install completed. Run "ollama --help" to get started.'
fi
exit $EXIT_CODE
}
trap cleanup EXIT
status "Downloading ollama..." status "Downloading ollama..."
curl --fail --show-error --location --progress-bar -o "$TEMP_DIR/ollama" "https://ollama.ai/download/ollama-linux-$ARCH" curl --fail --show-error --location --progress-bar -o "$TEMP_DIR/ollama" "https://ollama.ai/download/ollama-linux-$ARCH"
@ -65,12 +86,6 @@ status "Installing ollama to $BIN_DIR..."
$SUDO install -o0 -g0 -m755 -d "$BIN_DIR" $SUDO install -o0 -g0 -m755 -d "$BIN_DIR"
$SUDO install -o0 -g0 -m755 "$TEMP_DIR/ollama" "$BIN_DIR/ollama" $SUDO install -o0 -g0 -m755 "$TEMP_DIR/ollama" "$BIN_DIR/ollama"
install_success() {
status 'The Ollama API is now available at 0.0.0.0:11434.'
status 'Install complete. Run "ollama" from the command line.'
}
trap install_success EXIT
# Everything from this point onwards is optional. # Everything from this point onwards is optional.
configure_systemd() { configure_systemd() {
@ -102,9 +117,6 @@ EOF
status "Enabling and starting ollama service..." status "Enabling and starting ollama service..."
$SUDO systemctl daemon-reload $SUDO systemctl daemon-reload
$SUDO systemctl enable ollama $SUDO systemctl enable ollama
start_service() { $SUDO systemctl restart ollama; }
trap start_service EXIT
;; ;;
esac esac
} }
@ -115,7 +127,7 @@ fi
if ! available lspci && ! available lshw; then if ! available lspci && ! available lshw; then
warning "Unable to detect NVIDIA GPU. Install lspci or lshw to automatically detect and install NVIDIA CUDA drivers." warning "Unable to detect NVIDIA GPU. Install lspci or lshw to automatically detect and install NVIDIA CUDA drivers."
exit 0 exit
fi fi
check_gpu() { check_gpu() {
@ -128,13 +140,13 @@ check_gpu() {
if check_gpu nvidia-smi; then if check_gpu nvidia-smi; then
status "NVIDIA GPU installed." status "NVIDIA GPU installed."
exit 0 exit
fi fi
if ! check_gpu lspci && ! check_gpu lshw; then if ! check_gpu lspci && ! check_gpu lshw; then
install_success install_success
warning "No NVIDIA GPU detected. Ollama will run in CPU-only mode." warning "No NVIDIA GPU detected. Ollama will run with CPU."
exit 0 exit
fi fi
# ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#rhel-7-centos-7 # ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#rhel-7-centos-7
@ -242,7 +254,7 @@ if ! lsmod | grep -q nvidia; then
if lsmod | grep -q nouveau; then if lsmod | grep -q nouveau; then
status 'Reboot to complete NVIDIA CUDA driver install.' status 'Reboot to complete NVIDIA CUDA driver install.'
exit 0 exit
fi fi
$SUDO modprobe nvidia $SUDO modprobe nvidia