Skip to content

Commit

Permalink
[cli] decouple apicast environment and 3scale configuration channel
Browse files Browse the repository at this point in the history
* THREESCALE_DEPLOYMENT_ENV controls the 3scale proxy configuration
channel (staging/production)
* APICAST_ENVIRONMENT controls the APIcast settings (caching, ...)
* APICAST_LOADED_ENVIRONMENTS can control always loaded APIcast environments
  • Loading branch information
mikz committed Feb 13, 2018
1 parent e2e077f commit 751ab3f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
22 changes: 20 additions & 2 deletions gateway/src/apicast/cli/command/start.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local format = string.format

local exec = require('resty.execvp')
local resty_env = require('resty.env')
local re = require('ngx.re')

local Template = require('apicast.cli.template')
local Environment = require('apicast.cli.environment')
Expand Down Expand Up @@ -109,7 +110,7 @@ local function build_env(options, config)
APICAST_CONFIGURATION = options.configuration,
APICAST_CONFIGURATION_LOADER = options.boot and 'boot' or 'lazy',
APICAST_CONFIGURATION_CACHE = options.cache,
THREESCALE_DEPLOYMENT_ENV = config.name,
THREESCALE_DEPLOYMENT_ENV = options.channel or config.name,
APICAST_POLICY_LOAD_PATH = options.policy_load_path,
}
end
Expand Down Expand Up @@ -164,12 +165,29 @@ function mt:__call(options)
return exec(openresty, cmd, env)
end

local function split_by(pattern)
return function(str)
return re.split(str or '', pattern, 'oj')
end
end

local load_env = split_by(':')

local function configure(cmd)
cmd:usage("Usage: apicast-cli start [OPTIONS]")
cmd:option("--template", "Nginx config template.", 'conf/nginx.conf.liquid')

local channel = resty_env.value('THREESCALE_DEPLOYMENT_ENV') or 'production'
local loaded_env = Environment.loaded()

insert(loaded_env, 1, channel)

cmd:option('-e --environment', "Deployment to start. Can also be a path to a Lua file.", resty_env.value('THREESCALE_DEPLOYMENT_ENV') or 'production'):count('*')
cmd:option('-3 --channel', "3scale configuration channel to use.", channel):action(function(args, name, chan)
args.environment[1] = chan
args[name] = chan
end):count('0-1')
cmd:option('-e --environment', "Deployment to start. Can also be a path to a Lua file.", resty_env.value('APICAST_ENVIRONMENT'))
:count('*'):init(loaded_env):action('concat'):convert(load_env)
cmd:flag('--dev', 'Start in development environment')

cmd:flag("-m --master", "Test the nginx config"):args('?')
Expand Down
13 changes: 9 additions & 4 deletions gateway/src/apicast/cli/environment.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,23 @@ _M.default_config = {

local mt = { __index = _M }

--- Return loaded environments defined as environment variable.
-- @treturn {string,...}
function _M.loaded()
local value = resty_env.value('APICAST_LOADED_ENVIRONMENTS')
return re.split(value or '', [[\|]], 'jo')
end

--- Load an environment from files in ENV.
-- @treturn Environment
function _M.load()
local value = resty_env.value('APICAST_LOADED_ENVIRONMENTS')
local env = _M.new()
local environments = _M.loaded()

if not value then
if not environments then
return env
end

local environments = re.split(value, '\\|', 'jo')

for i=1,#environments do
assert(env:add(environments[i]))
end
Expand Down

0 comments on commit 751ab3f

Please sign in to comment.