Skip to content

Commit

Permalink
Finishing CLI script; improving Makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
subnetmarco committed Feb 18, 2015
1 parent 769a908 commit 5646148
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 116 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tmp/
nginx/
out/
dev/
.DS_Store
.vagrant/
.buildpath
Expand Down
98 changes: 34 additions & 64 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,81 +1,51 @@
PWD = `pwd`
KONG_HOME = `pwd`

# Dev environment variables
export DAEMON ?= off
export LUA_LIB ?= lua_package_path \"$(PWD)/src/?.lua\;\;\"\;
export LUA_CODE_CACHE ?= on
export KONG_PORT ?= 8000
export KONG_WEB_PORT ?= 8001
export DIR ?= $(PWD)/tmp
export KONG_CONF ?= $(DIR)/kong.conf
export SILENT ?=
export DEV_DIR ?= $(KONG_HOME)/dev
export TEST_DIR ?= $(KONG_HOME)/test
export KONG_CONF ?= $(DEV_DIR)/kong-dev.yaml
export DEV_LUA_LIB ?= lua_package_path \"$(KONG_HOME)/src/?.lua\;\;\"\;

.PHONY: build global test test-web test-all run migrate populate drop
.PHONY: install dev clean reset seed drop test test-integration test-web test-proxy test-all

global:
install:
@luarocks make kong-*.rockspec --only-server=http://rocks.moonscript.org

test:
@busted spec/unit

test-web:
@$(MAKE) build DAEMON=on
@$(MAKE) migrate SILENT=-s
@$(MAKE) run
@$(MAKE) seed SILENT=-s
@busted spec/web/ || (make stop;make drop; exit 1)
@$(MAKE) stop
@$(MAKE) drop SILENT=-s

test-proxy:
@$(MAKE) build DAEMON=on
@$(MAKE) migrate SILENT=-s
@$(MAKE) run
@$(MAKE) seed SILENT=-s
@busted spec/proxy/ || (make stop;make drop; exit 1)
@$(MAKE) stop
@$(MAKE) drop SILENT=-s

test-all:
@$(MAKE) build DAEMON=on
@$(MAKE) migrate SILENT=-s
@$(MAKE) run
@$(MAKE) seed SILENT=-s
@busted spec/ || (make stop;make drop; exit 1)
@$(MAKE) stop
@$(MAKE) drop SILENT=-s
dev:
@mkdir -p $(DEV_DIR)
@sed -e "s@lua_package_path.*;@$(DEV_LUA_LIB)@g" $(KONG_HOME)/conf/nginx.conf > $(DEV_DIR)/nginx-dev.conf
@cp $(KONG_HOME)/conf/kong.yaml $(DEV_DIR)/kong-dev.yaml

migrate:
@scripts/migrate migrate $(SILENT) --conf=$(KONG_CONF)
clean:
@rm -rf $(DEV_DIR)

reset:
@scripts/migrate reset $(SILENT) --conf=$(KONG_CONF)
@scripts/migrate reset --conf=$(KONG_CONF)

seed:
@scripts/seed seed $(SILENT) --conf=$(KONG_CONF)
@scripts/seed seed --conf=$(KONG_CONF)

drop:
@scripts/seed drop $(SILENT) --conf=$(KONG_CONF)
@scripts/seed drop --conf=$(KONG_CONF)

run:
@nginx -p $(DIR)/nginx -c nginx.conf
test:
@busted spec/unit

stop:
@nginx -p $(DIR)/nginx -c nginx.conf -s stop
test-integration:
@$(MAKE) dev DEV_DIR=$(TEST_DIR)
@bin/kong -c $(TEST_DIR)/kong-dev.yaml -n $(TEST_DIR)/nginx-dev.conf start > /dev/null
@bin/kong migrate > /dev/null
@$(MAKE) seed > /dev/null
@busted $(FOLDER) || (bin/kong stop > /dev/null;make drop > /dev/null; exit 1)
@bin/kong stop > /dev/null
@$(MAKE) drop > /dev/null
@$(MAKE) clean DEV_DIR=$(TEST_DIR)

build:
@mkdir -p $(DIR)/nginx/logs
@cp templates/kong.conf $(KONG_CONF)
@echo "" > $(DIR)/nginx/logs/error.log
@echo "" > $(DIR)/nginx/logs/access.log
@sed \
-e "s/{{DAEMON}}/$(DAEMON)/g" \
-e "s@{{LUA_LIB_PATH}}@$(LUA_LIB)@g" \
-e "s/{{LUA_CODE_CACHE}}/$(LUA_CODE_CACHE)/g" \
-e "s/{{PORT}}/$(KONG_PORT)/g" \
-e "s/{{WEB_PORT}}/$(KONG_WEB_PORT)/g" \
-e "s@{{KONG_CONF}}@$(KONG_CONF)@g" \
templates/nginx.conf > $(DIR)/nginx/nginx.conf;
test-web:
@$(MAKE) test-integration FOLDER=spec/web

@cp -R src/kong/web/static $(DIR)/nginx
@cp -R src/kong/web/admin $(DIR)/nginx
test-proxy:
@$(MAKE) test-integration FOLDER=spec/proxy

test-all:
@$(MAKE) test-integration FOLDER=spec/
131 changes: 95 additions & 36 deletions bin/kong
Original file line number Diff line number Diff line change
@@ -1,72 +1,127 @@
#!/bin/sh

# Setting Kong Home
SCRIPT_DIR="$( cd "$( dirname "$0" )" && pwd )"
KONG_DIR="$(echo $SCRIPT_DIR | sed -e 's/\/[^\/]*$//')"
KONG_CONF=$KONG_DIR/nginx/kong.conf
KONG_HOME="$(echo $SCRIPT_DIR | sed -e 's/\/[^\/]*$//')"

DAEMON="off"
LUA_LIB=""
LUA_CODE_CACHE="on"
KONG_PORT="8000"
KONG_ADMIN_PORT="8001"
# Properties
export KONG_CONF=$KONG_HOME/conf/kong.yaml
NGINX_CONF=$KONG_HOME/conf/nginx.conf
PID=$KONG_HOME/out/nginx.pid

DEV_LUA_LIB="lua_package_path \"$KONG_HOME/src/?.lua\;\;\"\;"

######################
# Utility function #
######################

function check_file_exists {
if [ ! -f $1 ]; then
printf "Can't find configuration file at: $1\n"
exit 1
fi
}

######################
# Operations #
######################

function show_help {
echo $"Usage: $0 {start|stop|restart}"
exit 1
printf "Usage: kong [OPTION]... {start|stop|restart|migrate}\n
\t-c specify the path to a custom Kong configuration file
\t default is: '$KONG_HOME/conf/kong.yaml'
\t-n specify the path to a custom Nginx configuration file.
\t default is: '$KONG_HOME/conf/nginx.conf'
\t-v output version information and exit
\t-h show the help
\nCommands available:\n
\tstart start Kong
\tstop stop a running Kong
\trestart restart Kong
\t it is equivalent to executing 'stop' and 'start' in succession
\tmigrate performs a database migration, execute this operation carefully
\n"
}

function show_version {
printf "Kong Version 0.1\n"
}

function start {
echo "Starting Kong"

mkdir -p $KONG_DIR/nginx/logs
touch $KONG_DIR/nginx/logs/error.log
touch $KONG_DIR/nginx/logs/access.log
cp -n $KONG_DIR/templates/kong.conf $KONG_DIR/nginx/kong.conf
sed \
-e "s/{{DAEMON}}/$DAEMON/g" \
-e "s@{{LUA_LIB_PATH}}@$LUA_LIB@g" \
-e "s/{{LUA_CODE_CACHE}}/$LUA_CODE_CACHE/g" \
-e "s/{{PORT}}/$KONG_PORT/g" \
-e "s/{{WEB_PORT}}/$KONG_ADMIN_PORT/g" \
-e "s@{{KONG_CONF}}@$KONG_CONF@g" \
$KONG_DIR/templates/nginx.conf > $KONG_DIR/nginx/nginx.conf;

nginx -p $KONG_DIR/nginx -c $KONG_DIR/nginx/nginx.conf
printf "Starting Kong"

mkdir -p $KONG_HOME/out/logs
touch $KONG_HOME/out/logs/error.log
touch $KONG_HOME/out/logs/access.log
nginx -p $KONG_HOME/out -c $NGINX_CONF

if [ $? -eq 0 ]; then
printf "$(tput setaf 2) [OK]\n$(tput sgr 0)"
else
printf "$(tput setaf 1) [ERROR]\n$(tput sgr 0)"
exit 1
fi
}

function migrate {
$KONG_HOME/scripts/migrate migrate --conf=$KONG_CONF
if [ $? -eq 0 ]; then
exit 0
else
exit 1
fi
}

function stop {
echo "Stopping Kong"
kill $(cat /var/run/nginx.pid)
printf "Stopping Kong"

if [ ! -f $PID ]; then
printf "$(tput setaf 1) [NOT RUNNING]\n$(tput sgr 0)"
exit 1
else
kill $(cat $PID)
printf "$(tput setaf 2) [OK]\n$(tput sgr 0)"
fi
}

function restart {
stop
start
}

OPTIND=1 # Reset in case getopts has been used previously in the shell.
######################
# Argument parsing #
######################

OPTIND=1 # Reset in case getopts has been used previously in the shell.
cmd=""

while getopts "h?v:" opt; do
case "$opt" in
while getopts "h?vc:n:" opt; do
case "$opt" in
h|\?)
show_help
exit 0
;;
v) version=1

v) show_version
exit 0
;;
esac

c)
KONG_CONF=`realpath "$OPTARG"`
;;
n)
NGINX_CONF=`realpath "$OPTARG"`
;;
esac
done

shift $((OPTIND-1))

[ "$1" = "--" ] && shift

if [ $version ]; then
echo "Kong Version 0.1"
exit 0;
fi
check_file_exists $KONG_CONF
check_file_exists $NGINX_CONF

case "$@" in
start)
Expand All @@ -77,6 +132,10 @@ case "$@" in
stop
;;

migrate)
migrate
;;

restart)
restart
;;
Expand Down
2 changes: 1 addition & 1 deletion templates/kong.conf → conf/kong.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Kong version
version: 0.1

# Specify the dao to use
# Specify the DAO to use
database: cassandra

# Enabled plugins, in this order
Expand Down
15 changes: 9 additions & 6 deletions templates/nginx.conf → conf/nginx.conf
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
worker_processes auto;
error_log logs/error.log debug;
worker_rlimit_nofile 84280;
daemon {{DAEMON}};
daemon on;
pid nginx.pid;

env KONG_CONF;

events {
worker_connections 20480;
}

http {
{{LUA_LIB_PATH}}
lua_code_cache {{LUA_CODE_CACHE}};
lua_package_path ";;";
lua_code_cache on;

access_log logs/access.log;

Expand Down Expand Up @@ -39,10 +42,10 @@ http {
lua_max_running_timers 4096;
lua_max_pending_timers 16384;

init_by_lua "kong = require 'main'; kong.init('{{KONG_CONF}}')";
init_by_lua "kong = require 'main'; kong.init()";

server {
listen {{PORT}};
listen 8000;
resolver 8.8.8.8;
charset UTF-8;

Expand Down Expand Up @@ -88,7 +91,7 @@ http {
}

server {
listen {{WEB_PORT}};
listen 8001;

location / {
default_type application/json;
Expand Down
6 changes: 1 addition & 5 deletions scripts/seed
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@ local function print_usage()
print(" drop --conf=[conf] [-s] # Drop the database")
print("Options:")
print(" --conf=[conf] Path to configuration")
print(" -s Silent output")
print(" -s Random including random data (1000 entities by default)")
os.exit(1)
end

local function log(str)
if not opts.silent then
print(str)
end
print(str)
end

-- Command
Expand All @@ -25,7 +22,6 @@ local command = arg[1]
-- Arguments
opts = {}
opts.conf = utils.getopt(arg, "conf").conf
opts.silent = utils.getopt(arg, "s").s
opts.random = utils.getopt(arg, "r").r

-- Load configuration
Expand Down
5 changes: 3 additions & 2 deletions src/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ local function load_plugin_conf(api_id, application_id, plugin_name)
return nil
end

function _M.init(configuration_path)
function _M.init()
-- Loading configuration
configuration, dao = utils.load_configuration_and_dao(configuration_path)
print(os.getenv("KONG_CONF"))
configuration, dao = utils.load_configuration_and_dao(os.getenv("KONG_CONF"))

dao:prepare()

Expand Down

0 comments on commit 5646148

Please sign in to comment.