shellcheck

This commit is contained in:
Michael Yang 2023-11-21 15:29:52 -08:00
parent 37d95157df
commit 06ec7f4824

View File

@ -9,19 +9,19 @@ error() { echo "ERROR $*"; exit 1; }
warning() { echo "WARNING: $*"; } warning() { echo "WARNING: $*"; }
TEMP_DIR=$(mktemp -d) TEMP_DIR=$(mktemp -d)
cleanup() { rm -rf $TEMP_DIR; } cleanup() { rm -rf "$TEMP_DIR"; }
trap cleanup EXIT trap cleanup EXIT
available() { command -v $1 >/dev/null; } available() { command -v "$1" >/dev/null; }
require() { require() {
local MISSING='' MISSING=''
for TOOL in $*; do for TOOL in "$@"; do
if ! available $TOOL; then if ! available "$TOOL"; then
MISSING="$MISSING $TOOL" MISSING="$MISSING $TOOL"
fi fi
done done
echo $MISSING echo "$MISSING"
} }
[ "$(uname -s)" = "Linux" ] || error 'This script is intended to run on Linux only.' [ "$(uname -s)" = "Linux" ] || error 'This script is intended to run on Linux only.'
@ -53,15 +53,17 @@ if [ -n "$NEEDS" ]; then
fi fi
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"
for BINDIR in /usr/local/bin /usr/bin /bin; do for BIN_DIR in /usr/local/bin /usr/bin /bin; do
echo $PATH | grep -q $BINDIR && break || continue if echo "$PATH" | grep -q $BIN_DIR; then
break
fi
done done
status "Installing ollama to $BINDIR..." status "Installing ollama to $BIN_DIR..."
$SUDO install -o0 -g0 -m755 -d $BINDIR $SUDO install -o0 -g0 -m755 -d "$BIN_DIR"
$SUDO install -o0 -g0 -m755 $TEMP_DIR/ollama $BINDIR/ollama $SUDO install -o0 -g0 -m755 "$TEMP_DIR/ollama" "$BIN_DIR/ollama"
install_success() { install_success() {
status 'The Ollama API is now available at 0.0.0.0:11434.' status 'The Ollama API is now available at 0.0.0.0:11434.'
@ -77,9 +79,6 @@ configure_systemd() {
$SUDO useradd -r -s /bin/false -m -d /usr/share/ollama ollama $SUDO useradd -r -s /bin/false -m -d /usr/share/ollama ollama
fi fi
status "Adding current user to ollama group..."
$SUDO usermod -a -G ollama $(whoami)
status "Creating ollama systemd service..." status "Creating ollama systemd service..."
cat <<EOF | $SUDO tee /etc/systemd/system/ollama.service >/dev/null cat <<EOF | $SUDO tee /etc/systemd/system/ollama.service >/dev/null
[Unit] [Unit]
@ -87,7 +86,7 @@ Description=Ollama Service
After=network-online.target After=network-online.target
[Service] [Service]
ExecStart=$BINDIR/ollama serve ExecStart=$BIN_DIR/ollama serve
User=ollama User=ollama
Group=ollama Group=ollama
Restart=always Restart=always
@ -147,10 +146,10 @@ install_cuda_driver_yum() {
case $PACKAGE_MANAGER in case $PACKAGE_MANAGER in
yum) yum)
$SUDO $PACKAGE_MANAGER -y install yum-utils $SUDO $PACKAGE_MANAGER -y install yum-utils
$SUDO $PACKAGE_MANAGER-config-manager --add-repo https://developer.download.nvidia.com/compute/cuda/repos/$1$2/$(uname -m)/cuda-$1$2.repo $SUDO $PACKAGE_MANAGER-config-manager --add-repo "https://developer.download.nvidia.com/compute/cuda/repos/$1$2/$(uname -m)/cuda-$1$2.repo"
;; ;;
dnf) dnf)
$SUDO $PACKAGE_MANAGER config-manager --add-repo https://developer.download.nvidia.com/compute/cuda/repos/$1$2/$(uname -m)/cuda-$1$2.repo $SUDO $PACKAGE_MANAGER config-manager --add-repo "https://developer.download.nvidia.com/compute/cuda/repos/$1$2/$(uname -m)/cuda-$1$2.repo"
;; ;;
esac esac
@ -158,7 +157,7 @@ install_cuda_driver_yum() {
rhel) rhel)
status 'Installing EPEL repository...' status 'Installing EPEL repository...'
# EPEL is required for third-party dependencies such as dkms and libvdpau # EPEL is required for third-party dependencies such as dkms and libvdpau
$SUDO $PACKAGE_MANAGER -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-$2.noarch.rpm || true $SUDO $PACKAGE_MANAGER -y install "https://dl.fedoraproject.org/pub/epel/epel-release-latest-$2.noarch.rpm" || true
;; ;;
esac esac
@ -175,7 +174,7 @@ install_cuda_driver_yum() {
# ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#debian # ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#debian
install_cuda_driver_apt() { install_cuda_driver_apt() {
status 'Installing NVIDIA repository...' status 'Installing NVIDIA repository...'
curl -fsSL -o $TEMP_DIR/cuda-keyring.deb https://developer.download.nvidia.com/compute/cuda/repos/$1$2/$(uname -m)/cuda-keyring_1.1-1_all.deb curl -fsSL -o "$TEMP_DIR/cuda-keyring.deb" "https://developer.download.nvidia.com/compute/cuda/repos/$1$2/$(uname -m)/cuda-keyring_1.1-1_all.deb"
case $1 in case $1 in
debian) debian)
@ -188,7 +187,7 @@ install_cuda_driver_apt() {
esac esac
status 'Installing CUDA driver...' status 'Installing CUDA driver...'
$SUDO dpkg -i $TEMP_DIR/cuda-keyring.deb $SUDO dpkg -i "$TEMP_DIR/cuda-keyring.deb"
$SUDO apt-get update $SUDO apt-get update
[ -n "$SUDO" ] && SUDO_E="$SUDO -E" || SUDO_E= [ -n "$SUDO" ] && SUDO_E="$SUDO -E" || SUDO_E=
@ -215,14 +214,14 @@ if [ -z "$PACKAGE_MANAGER" ]; then
error "Unknown package manager. Skipping CUDA installation." error "Unknown package manager. Skipping CUDA installation."
fi fi
if ! check_gpu nvidia-smi || [ -z "$(nvidia-smi | grep -o "CUDA Version: [0-9]*\.[0-9]*")" ]; then if ! check_gpu nvidia-smi || nvidia-smi | grep -qo "CUDA Version: [0-9]*\.[0-9]*"; then
case $OS_NAME in case $OS_NAME in
centos|rhel) install_cuda_driver_yum 'rhel' $OS_VERSION ;; centos|rhel) install_cuda_driver_yum 'rhel' "$OS_VERSION" ;;
rocky) install_cuda_driver_yum 'rhel' $(echo $OS_VERSION | cut -c1) ;; rocky) install_cuda_driver_yum 'rhel' "$(echo "$OS_VERSION" | cut -c1)" ;;
fedora) install_cuda_driver_yum $OS_NAME $OS_VERSION ;; fedora) install_cuda_driver_yum "$OS_NAME" "$OS_VERSION" ;;
amzn) install_cuda_driver_yum 'fedora' '35' ;; amzn) install_cuda_driver_yum 'fedora' '35' ;;
debian) install_cuda_driver_apt $OS_NAME $OS_VERSION ;; debian) install_cuda_driver_apt "$OS_NAME" "$OS_VERSION" ;;
ubuntu) install_cuda_driver_apt $OS_NAME $(echo $OS_VERSION | sed 's/\.//') ;; ubuntu) install_cuda_driver_apt "$OS_NAME" "$(echo "$OS_VERSION" | sed 's/\.//')" ;;
*) exit ;; *) exit ;;
esac esac
fi fi
@ -230,15 +229,15 @@ fi
if ! lsmod | grep -q nvidia; then if ! lsmod | grep -q nvidia; then
KERNEL_RELEASE="$(uname -r)" KERNEL_RELEASE="$(uname -r)"
case $OS_NAME in case $OS_NAME in
centos|rhel|rocky|amzn) $SUDO $PACKAGE_MANAGER -y install kernel-devel-$KERNEL_RELEASE kernel-headers-$KERNEL_RELEASE ;; centos|rhel|rocky|amzn) $SUDO $PACKAGE_MANAGER -y install "kernel-devel-$KERNEL_RELEASE" "kernel-headers-$KERNEL_RELEASE" ;;
fedora) $SUDO $PACKAGE_MANAGER -y install kernel-devel-$KERNEL_RELEASE ;; fedora) $SUDO $PACKAGE_MANAGER -y install "kernel-devel-$KERNEL_RELEASE" ;;
debian|ubuntu) $SUDO apt-get -y install linux-headers-$KERNEL_RELEASE ;; debian|ubuntu) $SUDO apt-get -y install "linux-headers-$KERNEL_RELEASE" ;;
*) exit ;; *) exit ;;
esac esac
NVIDIA_CUDA_VERSION=$($SUDO dkms status | awk -F: '/added/ { print $1 }') NVIDIA_CUDA_VERSION=$($SUDO dkms status | awk -F: '/added/ { print $1 }')
if [ -n "$NVIDIA_CUDA_VERSION" ]; then if [ -n "$NVIDIA_CUDA_VERSION" ]; then
$SUDO dkms install $NVIDIA_CUDA_VERSION $SUDO dkms install "$NVIDIA_CUDA_VERSION"
fi fi
if lsmod | grep -q nouveau; then if lsmod | grep -q nouveau; then