Skip to content

Commit

Permalink
Merge pull request #527 from 3scale/bin-luarocks
Browse files Browse the repository at this point in the history
[bin] start apicast when installed with rover
  • Loading branch information
mikz authored Dec 12, 2017
2 parents 922a9fb + 8e118e0 commit d8ddb6a
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 66 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion gateway/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
66 changes: 61 additions & 5 deletions gateway/bin/apicast
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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;
60 changes: 0 additions & 60 deletions gateway/bin/apicast-lua

This file was deleted.

0 comments on commit d8ddb6a

Please sign in to comment.