From 5249e45dc44fe5b0c988ca4f267ebc31db39f252 Mon Sep 17 00:00:00 2001 From: Michal Cichra Date: Mon, 11 Dec 2017 14:41:25 +0100 Subject: [PATCH 1/2] [rockspec] install environment configurations those were missing when installed from luarocks --- gateway/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gateway/Makefile b/gateway/Makefile index 55e0408cf..4d639c3b4 100644 --- a/gateway/Makefile +++ b/gateway/Makefile @@ -5,4 +5,4 @@ install: $(INST_LUADIR)/apicast @echo --- install cp -R gateway/src/* $(INST_LUADIR)/ cp gateway/bin/apicast* $(INST_BINDIR)/ - cp -r gateway/*.d gateway/conf $(INST_CONFDIR) + cp -r gateway/*.d gateway/conf{,ig} $(INST_CONFDIR) From 8e118e0ef5ea20c1b9874c46caacc3011e7e9b54 Mon Sep 17 00:00:00 2001 From: Michal Cichra Date: Mon, 11 Dec 2017 14:42:27 +0100 Subject: [PATCH 2/2] [bin] apicast can be started when installed by rover improve the launcher script to detect when it is installed by rover and start in the context of that installed version replaces the existing apicast-lua --- CHANGELOG.md | 1 + gateway/bin/apicast | 66 +++++++++++++++++++++++++++++++++++++---- gateway/bin/apicast-lua | 60 ------------------------------------- 3 files changed, 62 insertions(+), 65 deletions(-) delete mode 100755 gateway/bin/apicast-lua diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b27de2c2..5fd0b71bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Use more `command` instead of `which` to work in plain shell [PR #521](https://github.com/3scale/apicast/pull/521) - Fixed rockspec so APIcast can be installed by luarocks [PR #523](https://github.com/3scale/apicast/pull/523) - Fix loading renamed APIcast code [PR #525](https://github.com/3scale/apicast/pull/525) +- Fix `apicast` command when installed from luarocks [PR #527](https://github.com/3scale/apicast/pull/527) ## Changed diff --git a/gateway/bin/apicast b/gateway/bin/apicast index 258e2aea7..71c131f1c 100755 --- a/gateway/bin/apicast +++ b/gateway/bin/apicast @@ -2,11 +2,53 @@ use strict; use warnings FATAL => 'all'; +use File::Temp qw/ tempfile /; + use File::Basename; use Cwd qw(getcwd abs_path); -my $apicast = $ENV{APICAST_DIR} || abs_path(dirname(abs_path(__FILE__)) . '/..'); -my $bindir = $apicast . '/bin'; +sub detect_lua_version { + chomp(my $lua_version = `resty -e 'print(_VERSION:match(" (5%.[123])\$"))' 2> /dev/null` || '5.1'); + return $lua_version; +} + +my $apicast_dir = $ENV{APICAST_DIR} || abs_path(dirname(abs_path(__FILE__)) . '/..'); + + +sub detect_apicast_paths { + my $lua_modules = abs_path(dirname(abs_path(__FILE__)) . '/..'); + my $command = basename(__FILE__); + + my ($lua, $lua_file) = tempfile(); + + print $lua <<_LUA_; +local rocks_dir = assert(require('luarocks.path').rocks_dir(arg[1]), 'could not get rocks dir') +local manifest = assert(require('luarocks.manif').load_manifest(rocks_dir), 'could not load manifest') +print(rocks_dir, '/', manifest.commands[arg[2]]) +_LUA_ + + my $rock = qx{resty "$lua_file" "$lua_modules" "$command" 2>/dev/null}; + + unlink $lua_file; + chomp $rock; + + if (defined $rock && length $rock) { + return ( + $rock . '/bin', + $rock . '/conf', + $rock =~ s{/lib/luarocks/rocks/apicast/.+?/?$}[/share/lua/@{[ detect_lua_version ]}]r + ); + } else { + return ( + $apicast_dir . '/bin', + $apicast_dir, + $apicast_dir . '/src' + ) + } +} + +my ($apicast_bin, $apicast_conf, $apicast_src) = detect_apicast_paths(); + my $lua_path = $ENV{LUA_PATH}; my $cwd = getcwd(); @@ -21,12 +63,26 @@ if ($rover && !$lua_path) { $lua_path ||= ';'; } -chdir $apicast; +$ENV{APICAST_DIR} = $apicast_conf; # src/?/policy.lua allows us to require apicast.policy.apolicy -$ENV{LUA_PATH} = "$apicast/src/?.lua;$apicast/src/?/policy.lua;${lua_path}"; +$ENV{LUA_PATH} = sprintf('%1$s/?.lua;%1$s/?/policy.lua;', $apicast_src) . $lua_path; $ENV{PWD} = $cwd; -my @args = ('resty', "$bindir/cli", @ARGV); +my $bin = "$apicast_bin/cli"; + +if (! -f $bin) { + warn "$bin does not exist"; + + my ($lua, $lua_file) = tempfile(); + + print $lua <<_LUA_; +require('apicast.cli')(arg) +_LUA_ + + $bin = $lua_file; +} + +my @args = ('resty', $bin, @ARGV); exec '/usr/bin/env', @args; diff --git a/gateway/bin/apicast-lua b/gateway/bin/apicast-lua deleted file mode 100755 index 71a816718..000000000 --- a/gateway/bin/apicast-lua +++ /dev/null @@ -1,60 +0,0 @@ -#! /usr/bin/env resty --- vim: set ft=lua: - -local datafile = require("datafile") -local conf = datafile.path("conf", "r", "config") - -local ffi = require('ffi') -ffi.cdef([=[ -int setenv(const char*, const char*, int); -char *strerror(int errnum); -int execvp(const char *file, const char *argv[]); -]=]) - -local string_array_t = ffi.typeof("const char *[?]") - -local function exec(filename, arg) - local args = { filename } - for i=1, #arg do - args[i+1] = arg[i] - end - local cargv = string_array_t(#args + 1, args) - cargv[#args] = nil - ffi.C.execvp(filename, cargv) - error(ffi.string(ffi.C.strerror(ffi.errno()))) -end - -local function setenv(name, value, overwrite) - local overwrite_flag = overwrite and 1 or 0 - - if ffi.C.setenv(name, value, overwrite_flag) == -1 then - return nil, ffi.C.strerror(ffi.errno()) - else - return value - end -end - -local _, apicast = datafile.open('apicast', 'r', 'config', function(path) - local f, err = io.open(path, "r") - - if f then - local ok - - ok, err = f:read(1) - f:close() - - if ok then - return ok, path - end - end - - return nil, err -end) - -if conf then - setenv('APICAST_DIR', conf, true) - print("CONF: ", conf) -end -print("APICAST: ", apicast) - -exec(apicast or 'apicast', arg)