Skip to content

Instantly share code, notes, and snippets.

@YourFriendCaspian
Forked from schollz/install_caddy.sh
Created November 14, 2018 16:54
Show Gist options
  • Save YourFriendCaspian/648f38053ce37be84e7d8425686a8d34 to your computer and use it in GitHub Desktop.
Save YourFriendCaspian/648f38053ce37be84e7d8425686a8d34 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#
# Caddy Installer Script
#
# Homepage: https://caddyserver.com
# Issues: https://github.com/caddyserver/caddyserver.com/issues
# Requires: curl or wget, tar or unzip
#
# Hello! This is an experimental script that installs Caddy
# into your PATH (which may require password authorization).
# Use it like this:
#
# $ curl https://getcaddy.com | bash
# or
# $ wget -qO- https://getcaddy.com | bash
#
# In automated environments, you may want to run as root.
# If using curl, we recommend using the -fsSL flags.
#
# If you want to get Caddy with extra features, use -s with a
# comma-separated list of directives, like this:
#
# $ curl https://getcaddy.com | bash -s git,mailout
#
# This should work on Mac, Linux, and BSD systems, and
# hopefully Windows with Cygwin. Please open an issue if
# you notice any bugs.
#
set -e
install_caddy()
{
CADDY_OS="unsupported"
CADDY_ARCH="unknown"
CADDY_ARM=""
CADDY_FEATURES="$1"
# Not every platform has or needs sudo (see issue #40)
[[ $EUID -ne 0 ]] && SUDO_CMD="sudo"
#########################
# Which OS and version? #
#########################
CADDY_BIN="caddy"
CADDY_DL_EXT=".tar.gz"
# NOTE `uname -m` is more accurate than `arch`
# See: https://en.wikipedia.org/wiki/Uname
if [ -n "$(uname -m | grep 64)" ]; then
CADDY_ARCH="amd64"
elif [ -n "$(uname -m | grep 86)" ]; then
CADDY_ARCH="386"
elif [ -n "$(uname -m | grep armv5)" ]; then
CADDY_ARCH="arm"
CADDY_ARM="5"
elif [ -n "$(uname -m | grep armv6l)" ]; then
CADDY_ARCH="arm"
CADDY_ARM="6"
elif [ -n "$(uname -m | grep armv7l)" ]; then
CADDY_ARCH="arm"
CADDY_ARM="7"
else
echo "unsupported or unknown architecture"
exit 1
fi
if [ "$(uname | grep -i 'Darwin')" ]; then
CADDY_OS='darwin'
CADDY_DL_EXT='.zip'
OSX_VER="$(sw_vers | grep ProductVersion | cut -d':' -f2 | cut -f2)"
OSX_MAJOR="$(echo ${OSX_VER} | cut -d'.' -f1)"
OSX_MINOR="$(echo ${OSX_VER} | cut -d'.' -f2)"
OSX_PATCH="$(echo ${OSX_VER} | cut -d'.' -f3)"
# Major
if [ "$OSX_MAJOR" -lt 10 ]; then
echo "unsupported OS X version (9-)"
exit 2
fi
if [ "$OSX_MAJOR" -gt 10 ]; then
echo "unsupported OS X version (11+)"
exit 2
fi
# Minor
if [ "$OSX_MINOR" -le 5 ]; then
echo "unsupported OS X version (10.5-)"
exit 2
fi
elif [ "$(uname | grep -i 'Linux')" ]; then
CADDY_OS='linux'
elif [ "$(uname | grep -i 'FreeBSD')" ]; then
CADDY_OS='freebsd'
elif [ "$(uname | grep -i 'OpenBSD')" ]; then
CADDY_OS='openbsd'
elif [ "$(uname | grep -i 'WIN')" ]; then
# Should catch cygwin
CADDY_OS='windows'
CADDY_DL_EXT='.zip'
CADDY_BIN=$CADDY_BIN.exe
else
echo "unsupported or unknown os"
exit 3
fi
# Back up existing caddy, if any
CADDY_CUR_VER="$(caddy --version 2>/dev/null | cut -d ' ' -f2)"
if [ -n "${CADDY_CUR_VER}" ]; then
# caddy of some version is already installed
CADDY_PATH="$(which caddy)"
CADDY_BACKUP="${CADDY_PATH}_${CADDY_CUR_VER}"
echo "Backing up ${CADDY_PATH} to ${CADDY_BACKUP}"
echo "(Password may be required.)"
$SUDO_CMD mv "${CADDY_PATH}" "${CADDY_BACKUP}"
fi
########################
# Download and extract #
########################
echo "Downloading Caddy for ${CADDY_OS}/${CADDY_ARCH}..."
CADDY_FILE="caddy_${CADDY_OS}_${CADDY_ARCH}${CADDY_ARM}_custom${CADDY_DL_EXT}"
CADDY_URL="https://caddyserver.com/download/build?os=${CADDY_OS}&arch=${CADDY_ARCH}&arm=${CADDY_ARM}&features=${CADDY_FEATURES}"
echo "$CADDY_URL"
rm -rf "/tmp/${CADDY_FILE}"
if [ -n "$(which curl)" ]; then
curl -fsSL "$CADDY_URL" \
-o /tmp/${CADDY_FILE}
elif [ -n "$(which wget)" ]; then
wget --quiet "$CADDY_URL" \
-O /tmp/${CADDY_FILE}
else
echo "could not find curl or wget"
exit 4
fi
echo "Extracting..."
case "$CADDY_FILE" in
*.zip) unzip -o "/tmp/$CADDY_FILE" "$CADDY_BIN" -d /tmp/ ;;
*.tar.gz) tar -xzf "/tmp/$CADDY_FILE" -C /tmp/ "$CADDY_BIN" ;;
esac
chmod +x /tmp/$CADDY_BIN
echo "Putting caddy in /usr/local/bin (may require password)"
$SUDO_CMD mv /tmp/$CADDY_BIN /usr/local/bin/$CADDY_BIN
$SUDO_CMD rm /tmp/$CADDY_FILE
$CADDY_BIN --version
echo "Successfully installed"
exit 0
}
install_caddy "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment