|
#!/bin/bash |
|
|
|
# Written by Nijiko Yonskai <2015> |
|
# Copyright Mashape Inc |
|
# License MIT |
|
|
|
set -e |
|
|
|
# Variables |
|
VERSION=0.1.0 |
|
LUA_VERSION=5.1.5 |
|
LUAROCKS_VERSION=2.2.1 |
|
OPENRESTY_VERSION=1.7.10.1 |
|
KONG_VERSION=${KONG_VERSION:-0.1.1beta-2} |
|
PCRE_VERSION=8.36 |
|
SCRATCH=/tmp/$$.scratch |
|
OPENRESTY_INSTALL_LOCATION=/usr/local/openresty |
|
HAZ_BREW=false |
|
OVERWRITE_CONFIG=true |
|
|
|
# Functions |
|
function help() { |
|
echo " " |
|
echo "Kong OS X Installer version: ${VERSION}" |
|
echo " " |
|
echo "usage: kong-install [options]" |
|
echo " " |
|
echo " options:" |
|
echo " " |
|
echo " -x disable configuration overwrite (true by default)" |
|
echo " -v <version> specific version of kong to install (${KONG_VERSION} by default)" |
|
exit 0 |
|
} |
|
|
|
function clean_up() { |
|
if [[ -d "$SCRATCH" ]]; then |
|
rm -rf "$SCRATCH" |
|
fi |
|
} |
|
|
|
# Arguments |
|
if ( ! getopts ":xv:h" opt); then |
|
help |
|
fi |
|
|
|
while getopts ":xv:h" opt; do |
|
case $opt in |
|
x) |
|
OVERWRITE_CONFIG=false |
|
;; |
|
v) |
|
KONG_VERSION=$OPTARG |
|
;; |
|
h) |
|
help |
|
;; |
|
\?) |
|
echo "Invalid option: -$OPTARG" >&2 |
|
exit 1 |
|
;; |
|
:) |
|
echo "It's dangerous to go alone! Take this." |
|
help |
|
exit 1 |
|
;; |
|
esac |
|
done |
|
|
|
shift $((OPTIND - 1)) |
|
|
|
# Header |
|
printf " |
|
/\ ____ |
|
<> ( oo ) |
|
<>_| ^^ |_ |
|
<> @ \\ Mashape Kong OS X installation script |
|
/~~\ . . _ | |
|
/~~~~\ | | Nijiko Yonskai (2015 - MIT) |
|
/~~~~~~\/ _| | |
|
|[][][]/ / [m] |
|
|[][][[m] |
|
|[][][]| Dependencies (to be checked & installed) |
|
|[][][]| |
|
|[][][]| - Lua v${LUA_VERSION} |
|
|[][][]| - LuaRocks v${LUAROCKS_VERSION} |
|
|[][][]| - OpenResty v${OPENRESTY_VERSION} |
|
|[][][]| - PCRE v${PCRE_VERSION} |
|
|[][][]| |
|
|[][][]| Optional Dependencies (for compiling) |
|
|[|--|]| |
|
|[| |]| - Brew (for Lua) |
|
======== |
|
========== |
|
|[[ ]]| |
|
========== Installing Kong v${KONG_VERSION}... |
|
" |
|
|
|
# Cleanup trapper |
|
trap clean_up EXIT |
|
mkdir $SCRATCH |
|
|
|
# Dependencies |
|
printf "\n# Checking Dependencies\n\n" |
|
|
|
printf "checking for brew ... " |
|
if hash brew 2>/dev/null; then |
|
HAZ_BREW=true |
|
printf "found\n" |
|
printf " + updating brew\n" |
|
brew update |
|
else |
|
printf "not found\n" |
|
printf " - will compile dependencies from source\n" |
|
fi |
|
|
|
printf "checking for pcre ... " |
|
if hash pcretest 2>/dev/null; then |
|
printf "found\n" |
|
else |
|
printf "not found\n\n" |
|
printf " + installing PCRE v${PCRE_VERSION}\n" |
|
|
|
if HAZ_BREW; then |
|
brew install pcre |
|
else |
|
cd $SCRATCH |
|
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-$PCRE_VERSION.tar.gz |
|
tar -xzvf pcre-$PCRE_VERSION.tar.gz |
|
cd pcre-$PCRE_VERSION |
|
./configure --prefix=/usr/local/pcre-$PCRE_VERSION |
|
make |
|
sudo make install |
|
sudo ln -s pcre-8.36 /usr/local/pcre |
|
#echo 'export PATH=/usr/local/pcre/bin:$PATH' >> ~/.bash_profile |
|
#source ~/.bash_profile |
|
fi |
|
fi |
|
|
|
printf "checking for Lua v${LUA_VERSION} ... " |
|
if hash lua5.1 2>/dev/null; then |
|
printf "found\n" |
|
else |
|
printf "not found\n\n" |
|
printf " + installing\n" |
|
|
|
if HAZ_BREW; then |
|
brew install lua51 |
|
else |
|
cd $SCRATCH |
|
wget http://www.lua.org/ftp/lua-$LUA_VERSION.tar.gz |
|
tar xzf lua-$LUA_VERSION.tar.gz |
|
cd lua-$LUA_VERSION |
|
make macosx |
|
make install |
|
fi |
|
fi |
|
|
|
printf "checking for LuaRocks v${LUAROCKS_VERSION} ... " |
|
if luarocks --version | grep -q "2.2.1"; then |
|
printf "found\n" |
|
else |
|
printf "not found\n\n" |
|
printf " + installing\n" |
|
|
|
cd $SCRATCH |
|
wget http://luarocks.org/releases/luarocks-$LUAROCKS_VERSION.tar.gz |
|
tar xzf luarocks-$LUAROCKS_VERSION.tar.gz |
|
cd luarocks-$LUAROCKS_VERSION |
|
./configure |
|
make build |
|
sudo make install |
|
fi |
|
|
|
printf "checking for OpenResty v${OPENRESTY_VERSION} ... " |
|
if [[ -d "$OPENRESTY_INSTALL_LOCATION" ]]; then |
|
printf "found\n" |
|
else |
|
printf "not found\n\n" |
|
printf " + installing\n" |
|
|
|
cd $SCRATCH |
|
wget http://openresty.org/download/ngx_openresty-$OPENRESTY_VERSION.tar.gz |
|
tar xzf ngx_openresty-$OPENRESTY_VERSION.tar.gz |
|
cd ngx_openresty-$OPENRESTY_VERSION |
|
./configure --with-pcre-jit --with-ipv6 --with-http_realip_module --with-http_ssl_module --with-http_stub_status_module --with-cc-opt="-I/usr/local/include" --with-ld-opt="-L/usr/local/lib" |
|
make |
|
sudo make install |
|
fi |
|
|
|
# Install Kong |
|
printf "\n\n# Installing Kong v${KONG_VERSION}\n\n" |
|
sudo luarocks install kong $KONG_VERSION |
|
|
|
if hash kong 2>/dev/null; then |
|
# Copy Configuration File |
|
if OVERWRITE_CONFIG; then |
|
printf "\n# Copying Kong configuration file to: /etc/kong/kong.yml\n" |
|
sudo mkdir -p /etc/kong |
|
sudo cp /usr/local/lib/luarocks/rocks/kong/$KONG_VERSION/conf/kong.yml /etc/kong/kong.yml |
|
fi |
|
|
|
# Installation failed |
|
printf "\n[\e[92mSUCCESS\e[0m] Kong has been installed.\n" |
|
else |
|
# Installation failed |
|
printf "\n[\e[91mFAILURE\e[0m] Kong has failed to install.\n" |
|
exit 1 |
|
fi |
|
|
|
# Success |
|
trap - EXIT |
|
clean_up |
|
exit 0 |