Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

template nginx configuration with liquid #449

Merged
merged 3 commits into from
Nov 6, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
environment:
S2I_VERSION: "1.1.7-226afa1"
DOCKER_COMPOSE_VERSION: "1.16.1"
OPENRESTY_VERSION: "1.11.2.5-1-rover"
OPENRESTY_VERSION: "1.11.2.5-1-rover2"
steps:
- run: apk update && apk add wget make bash curl py-pip git openssh-client
- run: |
Expand Down Expand Up @@ -37,7 +37,7 @@ jobs:
- run: make prove-docker
build:
docker:
- image: quay.io/3scale/s2i-openresty-centos7:1.11.2.5-1-rover
- image: quay.io/3scale/s2i-openresty-centos7:1.11.2.5-1-rover2
environment:
TEST_NGINX_BINARY: openresty
LUA_BIN_PATH: /opt/app-root/bin
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- `/status/info` endpoint to the Management API [PR #290](https://github.com/3scale/apicast/pull/290)
- `/_threescale/healthz` endpoint returns a success status code, this is used for health checking in kubernetes environments [PR #285](https://github.com/3scale/apicast/pull/285)
- Usage limit errors are now configurable to distinguish them from other authorization errors [PR #453](https://github.com/3scale/apicast/pull/453).
- Templating nginx configuration with liquid. [PR #449](https://github.com/3scale/apicast/pull/449)

## Changed

Expand All @@ -22,6 +23,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Make OAuth tokens TTL configurable [PR #448](https://github.com/3scale/apicast/pull/448)
- Detect when being executed in Test::Nginx and use default backend accordingly [PR #458](https://github.com/3scale/apicast/pull/458)
- Update the s2i-openresty image to have the same path (`/opt/app-root/src`) in all images [PR #460](https://github.com/3scale/apicast/pull/460)
- Launcher scripts are now Perl + Lua instead of Shell [PR #449](https://github.com/3scale/apicast/pull/449)

### Fixed

Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ test-builder-image: export IMAGE_NAME = apicast-test
test-builder-image: builder-image clean-containers ## Smoke test the builder image. Pass any docker image in IMAGE_NAME parameter.
$(DOCKER_COMPOSE) --version
@echo -e $(SEPARATOR)
$(DOCKER_COMPOSE) run --rm --user 100001 gateway openresty -c /opt/app-root/src/conf/nginx.conf -g 'error_log stderr info; pid /tmp/nginx.pid;' -t
$(DOCKER_COMPOSE) run --rm --user 100001 gateway bin/apicast --test
@echo -e $(SEPARATOR)
$(DOCKER_COMPOSE) run --rm --user 100001 gateway openresty -c /opt/app-root/src/conf/nginx.conf -g 'error_log stderr info; pid /tmp/nginx.pid;'
$(DOCKER_COMPOSE) run --rm --user 100001 gateway bin/apicast --daemon
@echo -e $(SEPARATOR)
$(DOCKER_COMPOSE) run --rm test bash -c 'for i in {1..5}; do curl --fail http://gateway:8090/status/live && break || sleep 1; done'
$(DOCKER_COMPOSE) logs gateway
Expand Down
43 changes: 23 additions & 20 deletions apicast/Roverfile.lock
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
busted 2.0.rc12-1
dkjson 2.5-2
inspect 3.1.0-1
ldoc 1.4.6-2
lua-resty-env 0.4.0-1
lua-resty-http 0.10-0
lua-resty-iputils 0.3.0-1
lua-resty-jwt 0.1.10-1
lua-resty-repl 0.0.6-0
lua-resty-url 0.2.0-1
lua-term 0.7-1
lua_cliargs 3.0-1
luafilesystem 1.7.0-2
luassert 1.7.10-0
luasystem 0.2.1-0
markdown 0.33-1
mediator_lua 1.1.2-0
penlight 1.5.4-1
router 2.1-0
say 1.3-1
argparse 0.5.0-1|
busted 2.0.rc12-1|
dkjson 2.5-2|
inspect 3.1.0-1|
ldoc 1.4.6-2|
liquid scm-1|811a73e38fdd9fdea116be4baf310ca326b96c77
lua-resty-env 0.4.0-1|
lua-resty-execvp 0.1.0-1|
lua-resty-http 0.11-0|
lua-resty-iputils 0.3.0-1|
lua-resty-jwt 0.1.11-0|
lua-resty-repl 0.0.6-0|3878f41b7e8f97b1c96919db19dbee9496569dda
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the hash in this one? It has a version.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but version can still point to a scm to get the source. Depends if it is a rock or a rockspec.

rock has a source packaged in the bundle but rockspec has to download it from somewhere
in this case repl has only rockspec and that points to a git tag https://luarocks.org/modules/saksmlz/lua-resty-repl/0.0.6-0

lua-resty-url 0.2.0-1|
lua-term 0.7-1|
lua_cliargs 3.0-1|
luafilesystem 1.7.0-2|
luassert 1.7.10-0|
luasystem 0.2.1-0|
markdown 0.33-1|
mediator_lua 1.1.2-0|
penlight 1.5.4-1|
router 2.1-0|
say 1.3-1|
4 changes: 4 additions & 0 deletions apicast/apicast-0.1-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ dependencies = {
'lua-resty-jwt',
'lua-resty-url',
'lua-resty-env',
'lua-resty-execvp',
'liquid',
'argparse',
'penlight',
}
build = {
type = "builtin",
Expand Down
139 changes: 18 additions & 121 deletions apicast/bin/apicast
Original file line number Diff line number Diff line change
@@ -1,130 +1,27 @@
#!/bin/bash
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';

set -euo pipefail
IFS=$'\n\t'
use File::Basename;
use Cwd qw(abs_path);

script=${BASH_SOURCE[0]}
if (readlink -f "${script}" > /dev/null 2>&1); then
path=$(readlink -f "${script}")
elif (readlink "${script}" > /dev/null 2>&1); then
path="$(dirname "${script}")/$(readlink "${script}")"
else
path="${script}"
fi
my $apicast = $ENV{APICAST_DIR} || abs_path(dirname(abs_path(__FILE__)) . '/..');
my $bindir = $apicast . '/bin';
my $lua_path = $ENV{LUA_PATH};

bin_dir=$(dirname "${path}")
apicast_dir=${APICAST_DIR:-"$( cd "${bin_dir}/.." && pwd )"}
chomp(my $rover = `which rover`);
if ($rover) { $rover = abs_path($rover) }

pick_openresty() {
for cmd in "$@"
do
if (${cmd} -V > /dev/null 2>&1); then
echo "${cmd}"
exit 0
fi
done

(>&2 echo "ERROR: Could not find openresty executable in your PATH.")
(>&2 echo "Make sure you have one of: $(printf "%s " "$@")")
exit 1
}

openresty_binary=${APICAST_OPENRESTY_BINARY:-$(pick_openresty openresty-debug openresty nginx)}
log_level=${APICAST_LOG_LEVEL:-warn}
log_file=${APICAST_LOG_FILE:-stderr}
log_levels=(emerg alert crit error warn notice info debug)
((max_log_level=${#log_levels[@]}-1))

for ((i=0; i < ${#log_levels[@]}; i++)); do
ll=${log_levels[i]}
declare -r "log_level_${ll}=$i"
done

log="log_level_${log_level}"
log_level="${!log}"

daemon=off
worker_processes=${APICAST_WORKERS:-1}

usage () {
cat <<-USAGE
Usage $0
-h Show this help
-c <file> Path to custom config file (JSON).
-d Daemonize
-v Increase verbosity (can be repeated)
-i Cache configuration for N seconds. Using 0 will reload on every request (not for production).
-w <workers> Number of worker processes to start.
-m <on|off> Whether to start worker processes. Only for development.
-s <signal> Send signal to a master process: stop, quit, reopen, reload
-p <pid> Path to the PID file.
-b Load configuration on boot.
-e Deployment environment. Can be staging or production.
USAGE
if ($rover && !$lua_path) {
exec '/usr/bin/env', $rover, 'exec', $0, @ARGV
} else {
$lua_path ||= ';';
}

main=("")
args=("")

while getopts ":dc:hvbqi:rw:m:s:p:e:" opt; do
case "${opt}" in
d)
daemon="on"
;;
c)
export APICAST_CONFIGURATION="$OPTARG"
;;
b)
export APICAST_CONFIGURATION_LOADER="boot"
;;
v)
log_level=$((log_level == max_log_level ? max_log_level : log_level+1))
;;
q)
log_level=$((log_level == 0 ? 0 : log_level-1))
;;
i)
export APICAST_CONFIGURATION_CACHE="${OPTARG}"
;;
e)
export THREESCALE_DEPLOYMENT_ENV="${OPTARG}"
;;
w)
worker_processes=${OPTARG}
;;
m)
main+=("master_process ${OPTARG};")
;;
p)
main+=("pid ${PWD}/${OPTARG};")
;;
s)
args+=("-s" "${OPTARG}")
;;
h)
usage
exit 0
;;
\?)
echo "Invalid option: -${OPTARG}" >&2
echo
usage
exit 1
;;
esac
done

export THREESCALE_DEPLOYMENT_ENV=${THREESCALE_DEPLOYMENT_ENV-production}

main+=("daemon ${daemon};")
main+=("worker_processes ${worker_processes};")
main+=$(printenv | awk '$1 ~ /^(APICAST|THREESCALE)_/ {split($0,env,"="); print "env", env[1] ";"}')
chdir $apicast;

function join_by { local IFS="$1"; shift; echo "$*"; }
args=$(join_by '' "${args[@]}")
main=$(join_by '' "${main[@]}")
$ENV{LUA_PATH} = "$apicast/src/?.lua;${lua_path}";

cd "${apicast_dir}"
my @args = ('resty', "$bindir/cli", @ARGV);

# shellcheck disable=SC2086
exec "${openresty_binary}" -c "${apicast_dir}/conf/nginx.conf" ${args} -g "${main} error_log ${log_file} ${log_levels[log_level]};"
exec '/usr/bin/env', @args;
11 changes: 11 additions & 0 deletions apicast/bin/cli
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env resty

local ok, setup = pcall(require, 'rover.setup')

if ok then
setup()
else
package.path = './src/?.lua;' .. package.path
end

require('apicast.cli')(arg)
78 changes: 0 additions & 78 deletions apicast/conf/nginx.conf

This file was deleted.

Loading