Last active
August 29, 2015 14:26
-
-
Save subnetmarco/75d2d718ceba66169d0e to your computer and use it in GitHub Desktop.
Kong one-file installer script, with OS auto-detection.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -o errexit | |
# Force bash | |
if [ -z "$BASH_VERSION" ] | |
then | |
exec bash "$0" "$@" | |
fi | |
###################### | |
# Kong Information # | |
###################### | |
KONG_VERSION="0.4.1" | |
DEB_DEPENDENCIES="lsb-release netcat lua5.1 openssl libpcre3 dnsmasq curl sudo" | |
RPM_DEPENDENCIES="epel-release curl sudo" | |
####################### | |
# Utility functions # | |
####################### | |
is_sudo() { | |
if [ "$(id -u)" == "0" ]; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
cmd_exists() { | |
if hash $1 2>/dev/null; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
msg() { | |
str=$@ | |
printf "\033[0;32m$str\033[0m\n" 1>&3 2>&4 # Green color | |
} | |
err() { | |
str=$@ | |
printf "\033[0;31m$str\033[0m\n" 1>&3 2>&4 # Red color | |
exit 1 | |
} | |
VERBOSE=1 | |
exec 3>&1 | |
exec 4>&2 | |
if ! ((VERBOSE)); then | |
exec 1>/dev/null | |
exec 2>/dev/null | |
fi | |
####################### | |
# Main Logic # | |
####################### | |
if ! is_sudo && [ "$(uname)" != "Darwin" ]; then | |
err "You need to run the script as root" | |
elif ! is_sudo; then | |
SUDO="sudo" | |
fi | |
msg "Installing Kong $KONG_VERSION - It may take a while.." | |
if [ "$(uname)" = "Darwin" ]; then | |
if cmd_exists brew; then | |
msg "Installing with HomeBrew" | |
# Install with HomeBrew | |
brew tap mashape/kong | |
msg "Installation in progress.." | |
brew install kong | |
else | |
msg "Downloading Kong files" | |
# Install with OSX package | |
file_name="kong-${KONG_VERSION}.osx.pkg" | |
curl -L -o /tmp/${file_name} -O https://github.com/Mashape/kong/releases/download/${KONG_VERSION}/${file_name} | |
open /tmp/${file_name} | |
msg "Kong graphical installer opened. Follow the wizard to complete the installation.." | |
fi | |
elif cmd_exists yum; then | |
msg "Installing dependencies: "$RPM_DEPENDENCIES | |
$SUDO yum install -y $RPM_DEPENDENCIES | |
centos_version=`cat /etc/redhat-release | grep -oE '[0-9]+\.[0-9]+'` | |
file_name="kong-${KONG_VERSION}.el${centos_version%.*}.noarch.rpm" | |
file_url="https://github.com/Mashape/kong/releases/download/${KONG_VERSION}/${file_name}" | |
msg "Downloading Kong: ${file_url}" | |
curl -L -o /tmp/${file_name} ${file_url} | |
msg "Installation in progress.." | |
$SUDO yum install -y /tmp/${file_name} --nogpgcheck | |
# Cleaning up | |
rm /tmp/${file_name} | |
elif cmd_exists apt-get; then | |
msg "Installing dependencies: "$DEB_DEPENDENCIES | |
$SUDO apt-get update | |
$SUDO apt-get install -y $DEB_DEPENDENCIES | |
debian_version=`lsb_release -cs` | |
file_name="kong-${KONG_VERSION}.${debian_version}_all.deb" | |
file_url="https://github.com/Mashape/kong/releases/download/${KONG_VERSION}/${file_name}" | |
msg "Downloading Kong: ${file_url}" | |
curl -L -o /tmp/${file_name} ${file_url} | |
msg "Installation in progress.." | |
$SUDO dpkg -i /tmp/${file_name} | |
# Cleaning up | |
rm /tmp/${file_name} | |
else | |
err "Unsupported platform" | |
fi | |
msg "Checking installed version: "`kong version` | |
msg "Done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment