Skip to content

Commit

Permalink
wip/feat: polishing the new Lua CLI + embedded migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultcha committed Mar 24, 2015
1 parent 833958e commit 69af92c
Show file tree
Hide file tree
Showing 17 changed files with 200 additions and 176 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
TESTING_CONF = kong_TEST.yml
DEVELOPMENT_CONF = kong_DEVELOPMENT.yml

.PHONY: install dev clean seed drop test coverage test-api test-proxy test-server test-all
.PHONY: install dev clean run seed drop lint test coverage test-api test-proxy test-server test-all

install:
@if [ `uname` == "Darwin" ]; then \
Expand Down
24 changes: 18 additions & 6 deletions bin/kong
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
#!/usr/bin/env lua

local utils = require "kong.cmd.utils"
local infos = utils.get_infos()
local help_message = string.format([[
local cutils = require "kong.cli.utils"
local infos = cutils.get_infos()
local commands = {
db = "kong.cli.db",
stop = "kong.cli.stop",
start = "kong.cli.start",
config = "kong.cli.config",
version = "kong.cli.version"
}

local help_message = string.format([[
Usage: kong <command>
where <command> is one of:
Expand All @@ -16,10 +23,15 @@ kong <command> --help print the help message of a command

local cmd = arg[1]

if not cmd or cmd == "-h" or cmd == "--help" then
if not cmd then
print("Missing <command>\n\n"..help_message)
os.exit(1)
elseif not commands[cmd] then
print("Invalid <command>: "..cmd.."\n\n"..help_message)
os.exit(1)
elseif cmd == "-h" or cmd == "--help" then
print(help_message)
os.exit(0)
end

-- TODO: test if command exists
require("kong.cmd."..cmd)
require(commands[cmd])
15 changes: 7 additions & 8 deletions kong-0.0.1beta-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ dependencies = {
"uuid ~> 0.2-1",
"lapis ~> 1.1.0-1",
"luasec ~> 0.5-2",
"lua-cjson ~> 2.1.0-1",
"yaml ~> 1.1.1-1",
"luaxml ~> 101012-1",
"cassandra ~> 0.5-4",
"lrexlib-pcre ~> 2.7.2-1",
"stringy ~> 0.2-1",
"inspect ~> 3.0-1",
"luasocket ~> 2.0.2-5",
"lua-llthreads2 ~> 0.1.3-1",
"ansicolors ~> 1.0.2-3",
"lua-path ~> 0.2.3-1",
"luatz ~> 0.3-1"
Expand All @@ -43,12 +42,12 @@ build = {

["kong.constants"] = "src/constants.lua",

["kong.cmd.utils"] = "src/cmd/utils.lua",
["kong.cmd.db"] = "src/cmd/db.lua",
["kong.cmd.start"] = "src/cmd/start.lua",
["kong.cmd.stop"] = "src/cmd/stop.lua",
["kong.cmd.config"] = "src/cmd/config.lua",
["kong.cmd.version"] = "src/cmd/version.lua",
["kong.cli.utils"] = "src/cli/utils.lua",
["kong.cli.db"] = "src/cli/db.lua",
["kong.cli.start"] = "src/cli/start.lua",
["kong.cli.stop"] = "src/cli/stop.lua",
["kong.cli.config"] = "src/cli/config.lua",
["kong.cli.version"] = "src/cli/version.lua",

["kong.tools.utils"] = "src/tools/utils.lua",
["kong.tools.migrations"] = "src/tools/migrations.lua",
Expand Down
3 changes: 1 addition & 2 deletions spec/unit/timestamp_spec.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local utils = require "kong.tools.utils"
local timestamp = require "kong.tools.timestamp"
local luatz = require "luatz"

describe("Timestamp", function()

Expand Down Expand Up @@ -45,4 +44,4 @@ describe("Timestamp", function()
assert.are.same(timestamps_one, timestamps_two)
end)

end)
end)
2 changes: 1 addition & 1 deletion src/cmd/config.lua → src/cli/config.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env lua

local cutils = require "kong.cmd.utils"
local cutils = require "kong.cli.utils"
local args = require "lapp" [[
Duplicate an existing configuration for given environment.
Expand Down
81 changes: 42 additions & 39 deletions src/cmd/db.lua → src/cli/db.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
local Faker = require "kong.tools.faker"
local Migrations = require "kong.tools.migrations"

local cutils = require "kong.cmd.utils"
local utils = require "kong.tools.utils"
local cutils = require "kong.cli.utils"
local lapp = require("lapp")
local args = lapp(string.format([[
Migrations, seeding of the DB.
Usage: kong db <command> [options]
Commands:
<command> (string) where <command> is one of:
migrate:up, migrate:down, migrate:reset, seed, drop
migrations, migrations:up, migrations:down, migrations:reset, seed, drop
Options:
-c,--config (default %s) configuration file
Expand All @@ -21,72 +22,76 @@ Options:

-- $ kong db
if args.command == "db" then
lapp.quit("Missing command.")
end

local config_path, config = cutils.get_kong_config(args.config)
-- TODO: move to config validation
local status, res = pcall(require, "kong.dao."..config.database..".factory")
if not status then
cutils.logger:error("Wrong config")
os.exit(1)
lapp.quit("Missing required <command>.")
end

local dao_factory = res(config.databases_available[config.database].properties)
local config_path = cutils.get_kong_config_path(args.config)
local _, dao_factory = cutils.load_configuration_and_dao(config_path)
local migrations = Migrations(dao_factory)

if args.command == "migrate:up" then
if args.command == "migrations" then

local migrations, err = dao_factory:get_migrations()
if err then
cutils.logger:error_exit(err)
end

cutils.logger:log(string.format(
"Executed migrations for %s on keyspace: %s:\n%s",
cutils.colors.yellow(dao_factory.type),
cutils.colors.yellow(dao_factory._properties.keyspace),
table.concat(migrations, ", ")
))

elseif args.command == "migrations:up" then

cutils.logger:log(string.format(
cutils.colors("Migrating %{yellow}%s%{reset} keyspace: %{yellow}%s%{reset}"),
dao_factory.type,
dao_factory._properties.keyspace)
"Migrating %s keyspace: %s",
cutils.colors.yellow(dao_factory.type),
cutils.colors.yellow(dao_factory._properties.keyspace))
)

migrations:migrate(function(migration, err)
if err then
cutils.logger:error(err)
os.exit(1)
cutils.logger:error_exit(err)
elseif migration then
cutils.logger:success("Migrated up to: "..cutils.colors("%{yellow}"..migration.name.."%{reset}"))
cutils.logger:success("Migrated up to: "..cutils.colors.yellow(migration.name))
else
cutils.logger:success("Schema already up to date")
end
end)

elseif args.command == "migrate:down" then
elseif args.command == "migrations:down" then

cutils.logger:log(string.format(
cutils.colors("Rolling back %{yellow}%s%{reset} keyspace: %{yellow}%s%{reset}"),
dao_factory.type,
dao_factory._properties.keyspace)
)
"Rolling back %s keyspace: %s",
cutils.colors.yellow(dao_factory.type),
cutils.colors.yellow(dao_factory._properties.keyspace)
))

migrations:rollback(function(migration, err)
if err then
cutils.logger:error(err)
os.exit(1)
cutils.logger:error_exit(err)
elseif migration then
cutils.logger:success("Rollbacked to: "..cutils.colors("%{yellow}"..migration.name.."%{reset}"))
cutils.logger:success("Rollbacked to: "..cutils.colors.yellow(migration.name))
else
cutils.logger:success("No migration to rollback")
end
end)

elseif args.command == "migrate:reset" then
elseif args.command == "migrations:reset" then

cutils.logger:log(string.format(
cutils.colors("Reseting %{yellow}%s%{reset} keyspace: %{yellow}%s%{reset}"),
dao_factory.type,
dao_factory._properties.keyspace)
"Reseting %s keyspace: %s",
cutils.colors.yellow(dao_factory.type),
cutils.colors.yellow(dao_factory._properties.keyspace))
)

migrations:reset(function(migration, err)
if err then
cutils.logger:error(err)
os.exit(1)
cutils.logger:error_exit(err)
elseif migration then
cutils.logger:success("Rollbacked: "..cutils.colors("%{yellow}"..migration.name.."%{reset}"))
cutils.logger:success("Rollbacked: "..cutils.colors.yellow(migration.name))
else
cutils.logger:success("Schema reseted")
end
Expand All @@ -97,8 +102,7 @@ elseif args.command == "seed" then
-- Drop if exists
local err = dao_factory:drop()
if err then
cutils.logger:error(err)
os.exit(1)
cutils.logger:error_exit(err)
end

local err = dao_factory:prepare()
Expand All @@ -114,8 +118,7 @@ elseif args.command == "drop" then

local err = dao_factory:drop()
if err then
cutils.logger:error(err)
os.exit(1)
cutils.logger:error_exit(err)
end

cutils.logger:success("Dropped")
Expand Down
25 changes: 22 additions & 3 deletions src/cmd/start.lua → src/cli/start.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env lua

local cutils = require "kong.cmd.utils"
local cutils = require "kong.cli.utils"
local args = require("lapp")(string.format([[
Usage: kong start [options]
Expand All @@ -15,9 +15,28 @@ if not nginx_path then
end

-- Get configuration from default or given path
local config_path, config = cutils.get_kong_config(args.config)
local config_path = cutils.get_kong_config_path(args.config)
local config, dao_factory = cutils.load_configuration_and_dao(config_path)

-- Migrate the DB if needed and possible
local keyspace, err = dao_factory:get_migrations()
if err then
cutils.logger:error_exit(err)
elseif keyspace == nil then
cutils.logger:log("Database not initialized. Running migrations...")
local migrations = require("kong.tools.migrations")(dao_factory)
migrations:migrate(function(migration, err)
if err then
cutils.logger:error_exit(err)
end
cutils.logger:success("Migrated")
end)
end

-- Prepare nginx --prefix dir
local nginx_working_dir = cutils.prepare_nginx_working_dir(config)

-- Build nginx start command
local cmd = string.format("KONG_CONF=%s %s -p %s -c %s -g 'pid %s;'",
config_path,
nginx_path,
Expand All @@ -28,5 +47,5 @@ local cmd = string.format("KONG_CONF=%s %s -p %s -c %s -g 'pid %s;'",
if os.execute(cmd) == 0 then
cutils.logger:success("Started")
else
cutils.logger:error("Could not start Kong")
cutils.logger:error_exit(err)("Could not start Kong")
end
8 changes: 4 additions & 4 deletions src/cmd/stop.lua → src/cli/stop.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env lua

local cutils = require "kong.cmd.utils"
local cutils = require "kong.cli.utils"
local args = require("lapp")(string.format([[
Usage: kong stop [options]
Expand All @@ -9,13 +9,13 @@ Options:
]], cutils.CONSTANTS.GLOBAL_KONG_CONF))

-- Get configuration from default or given path
local config_path, config = cutils.get_kong_config(args.config)
local config_path = cutils.get_kong_config_path(args.config)
local config = cutils.load_configuration_and_dao(config_path)

local pid = cutils.path:join(config.nginx_working_dir, cutils.CONSTANTS.NGINX_PID)

if not cutils.file_exists(pid) then
cutils.logger:error("Not running. Could not find pid at: "..pid)
os.exit(1)
cutils.logger:error_exit("Not running. Could not find pid at: "..pid)
end

local cmd = "kill -QUIT $(cat "..pid..")"
Expand Down
Loading

0 comments on commit 69af92c

Please sign in to comment.