Skip to content

Commit

Permalink
Scripts and Makefile simplifications
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultcha committed Mar 4, 2015
1 parent f321e20 commit 292b5b7
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 39 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ bower_components/

# kong
out/
config.*/
!config.default/
kong*.yml

# luacov
luacov.*
27 changes: 10 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
KONG_HOME = `pwd`

export CONF ?= kong.yml
export SILENT_FLAG ?=
export COVERAGE_FLAG ?=

# Tests variables
TESTS_CONF ?= kong_TEST.yml
DEVELOPMENT_CONF ?= kong_DEVELOPMENT.yml

.PHONY: install dev clean migrate reset seed drop test coverage run-integration-tests test-web test-proxy test-all
.PHONY: install dev seed drop test coverage run-integration-tests test-web test-proxy test-all

install:
@if [ `uname` == "Darwin" ]; then \
Expand All @@ -23,19 +22,16 @@ dev:
@scripts/dev_rocks.sh
@scripts/config.lua -k $(KONG_HOME) -e TEST create
@scripts/config.lua -k $(KONG_HOME) -e DEVELOPMENT create
@scripts/db.lua migrate $(DEVELOPMENT_CONF)
@scripts/db.lua -c $(DEVELOPMENT_CONF) migrate

migrate:
@scripts/db.lua $(SILENT_FLAG) migrate $(CONF)

reset:
@scripts/db.lua $(SILENT_FLAG) reset $(CONF)
run:
@bin/kong -c $(DEVELOPMENT_CONF) start

seed:
@scripts/db.lua $(SILENT_FLAG) seed $(CONF)
@scripts/db.lua -c $(DEVELOPMENT_CONF) seed

drop:
@scripts/db.lua $(SILENT_FLAG) drop $(CONF)
@scripts/db.lua -c $(DEVELOPMENT_CONF) drop

test:
@busted $(COVERAGE_FLAG) spec/unit
Expand All @@ -44,20 +40,17 @@ coverage:
@rm -f luacov.*
@$(MAKE) test COVERAGE_FLAG=--coverage

clean:
@rm -f luacov.*

lint:
@luacheck kong*.rockspec

run-integration-tests:
@$(MAKE) migrate CONF=$(TESTS_CONF)
@scripts/db.lua -c $(TESTS_CONF) $(SILENT_FLAG) migrate
@bin/kong -c $(TESTS_CONF) start
@while ! [ `ps aux | grep nginx | grep -c -v grep` -gt 0 ]; do sleep 1; done # Wait until nginx starts
@$(MAKE) seed CONF=$(TESTS_CONF)
@busted $(COVERAGE_FLAG) $(FOLDER) || (bin/kong stop; make drop CONF=$(TESTS_CONF) SILENT_FLAG=$(SILENT_FLAG); exit 1)
@scripts/db.lua -c $(TESTS_CONF) $(SILENT_FLAG) seed
@busted $(COVERAGE_FLAG) $(FOLDER) || (bin/kong stop; scripts/db.lua -c $(TESTS_CONF) $(SILENT_FLAG) reset; exit 1)
@bin/kong stop
@$(MAKE) reset CONF=$(TESTS_CONF)
@scripts/db.lua -c $(TESTS_CONF) $(SILENT_FLAG) reset

test-web:
@$(MAKE) run-integration-tests FOLDER=spec/web SILENT_FLAG=-s
Expand Down
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Running Kong for development requires you to run:
make dev
```

This will install development dependencies and create your environment configuration files (`dev` and `tests`). Setup your database access for each of these enviroments (be careful about keyspaces, since Kong already uses `kong` and unit tests already use `kong_tests`).
This will install development dependencies and create your environment configuration files (`kong_TESTS.yml` and `kong_DEVELOPMENT.yml`).

- Run the tests:

Expand All @@ -54,7 +54,7 @@ make test-all
- Run it:

```
bin/kong -c config.dev/kong.yml -n config.dev/nginx.conf start
bin/kong -c kong.yml
```

#### Makefile
Expand All @@ -64,13 +64,11 @@ When developing, use the `Makefile` for doing the following operations:
| Name | Description |
| ------------ | --------------------------------------------------------------------------------------------------- |
| `install` | Install the Kong luarock globally |
| `dev` | Setup your development enviroment (install dev deps and creates `config.dev` and `config.tests`) |
| `clean` | Clean the development environment |
| `migrate` | Migrate your database schema according to the development Kong config inside `config.dev` |
| `reset` | Reset your database schema according to the development Kong config inside `config.dev` |
| `seed` | Seed your database according to the development Kong config inside `config.dev` |
| `drop` | Drop your database according to the development Kong config inside `config.dev` |
| `lint` | Lint Lua in `src/` |
| `dev` | Setup your development environment |
| `run` | Run the `DEVELOPMENT` environment (`kong_DEVELOPMENT.yml`) |
| `seed` | Seed the `DEVELOPMENT` environment (`kong_DEVELOPMENT.yml`) |
| `drop` | Drop the `DEVELOPMENT` environment (`kong_DEVELOPMENT.yml`) |
| `lint` | Lint Lua files in `src/` |
| `coverage` | Run unit tests + coverage report (only unit-tested modules) |
| `test` | Run the unit tests |
| `test-proxy` | Run the proxy integration tests |
Expand All @@ -83,12 +81,14 @@ Those scripts provide handy features while developing Kong:

##### db.lua

This script handles schema migrations, seeding and dropping of the database.

```bash
# Complete usage
scripts/db.lua --help

# Migrate up
scripts/db.lua migrate [configuration_path] # for all commands, the default configuration_path is config.dev/kong.yml
scripts/db.lua [-c configuration_file] migrate # for all commands, the default configuration_file is kong.yml

# Revert latest migration
scripts/db.lua rollback
Expand All @@ -103,6 +103,10 @@ scripts/db.lua seed
scripts/db.lua drop
```

##### config.lua

This script handles Kong's configuration files. It is better not to directly use it, as it is mainly used through `bin/kong` and the Makefile.

[travis-url]: https://travis-ci.org/Mashape/kong
[travis-badge]: https://img.shields.io/travis/Mashape/kong.svg?style=flat
[coveralls-url]: https://coveralls.io/r/Mashape/kong?branch=master
Expand Down
3 changes: 1 addition & 2 deletions bin/kong
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ function start {
$KONG_HOME/scripts/config.lua -c $KONG_CONF -o $KONG_HOME/out/ nginx
nginx -p $KONG_HOME/out -c $KONG_HOME/out/nginx.conf


if [ $? -eq 0 ]; then
printf "$(tput setaf 2) [OK]\n$(tput sgr 0)"
else
Expand All @@ -73,7 +72,7 @@ function start {
}

function migrate {
$KONG_HOME/scripts/db.lua migrate $KONG_CONF
$KONG_HOME/scripts/db.lua -c $KONG_CONF migrate
if [ $? -eq 0 ]; then
exit 0
else
Expand Down
3 changes: 2 additions & 1 deletion scripts/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ if args.COMMAND == "create" then
["keyspace: kong"] = "keyspace: kong_development",
["lua_package_path \";;\""] = "lua_package_path \""..args.kong.."/src/?.lua;;\"",
["error_log logs/error.log info"] = "error_log logs/error.log debug",
["lua_code_cache on"] = "lua_code_cache on"
["lua_code_cache on"] = "lua_code_cache off",
["daemon on"] = "daemon off"
}
}

Expand Down
12 changes: 7 additions & 5 deletions scripts/db.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ local Migrations = require "kong.tools.migrations"

cli:set_name("db.lua")
cli:add_argument("COMMAND", "<create|migrate|rollback|reset|seed|drop>")
cli:add_option("-c, --config=CONFIG", "configuration file", "kong.yml")
cli:add_option("-n, --name=NAME", "If <create>, sets a name to the migration", "new_migration")
cli:add_flag("-r, --random", "If seeding, also seed random entities (1000 for each collection by default)")
cli:add_flag("-s, --silent", "No output")
cli:optarg("CONFIGURATION", "configuration path", "config.dev/kong.yml")

local args = cli:parse(arg)
if not args then
os.exit(1)
end

local logger = utils.logger:new(args.silent)
local configuration, dao = utils.load_configuration_and_dao(args.CONFIGURATION)
local configuration, dao = utils.load_configuration_and_dao(args.config)

local migrations = Migrations(dao)

Expand All @@ -34,7 +34,7 @@ if args.COMMAND == "create" then

elseif args.COMMAND == "migrate" then

logger:log("Migrating "..utils.yellow(dao.type))
logger:log("Migrating "..utils.yellow(dao.type).." keyspace: "..utils.yellow(dao._properties.keyspace))

migrations:migrate(function(migration, err)
if err then
Expand All @@ -48,7 +48,7 @@ elseif args.COMMAND == "migrate" then

elseif args.COMMAND == "rollback" then

logger:log("Rolling back "..utils.yellow(dao.type))
logger:log("Rolling back "..utils.yellow(dao.type).." keyspace: "..utils.yellow(dao._properties.keyspace))

migrations:rollback(function(migration, err)
if err then
Expand All @@ -62,13 +62,15 @@ elseif args.COMMAND == "rollback" then

elseif args.COMMAND == "reset" then

logger:log("Resetting "..utils.yellow(dao.type))
logger:log("Reseting "..utils.yellow(dao.type).." keyspace: "..utils.yellow(dao._properties.keyspace))

migrations:reset(function(migration, err)
if err then
logger:error(err)
elseif migration then
logger:success("Rollbacked: "..utils.yellow(migration.name))
else
logger:success("Schema reseted")
end
end)

Expand Down
4 changes: 2 additions & 2 deletions src/kong/tools/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ function logger:log(str)
end

function logger:success(str)
self:log(_M.green("")..str)
self:log(_M.green(" ")..str)
end

function logger:error(str)
self:log(_M.red("")..str)
self:log(_M.red(" ")..str)
os.exit(1)
end

Expand Down

0 comments on commit 292b5b7

Please sign in to comment.