From 065a44e8166976333f0eaa9684157e00880e3881 Mon Sep 17 00:00:00 2001 From: fukiki Date: Tue, 19 Jan 2021 16:52:23 +0800 Subject: [PATCH] fix: use a shell script to write the CLI Signed-off-by: fukiki --- Makefile | 3 +- bin/apisix | 62 ++++++++++++++++++++--------------------- bin/apisix.lua | 36 ++++++++++++++++++++++++ utils/install-apisix.sh | 2 ++ 4 files changed, 71 insertions(+), 32 deletions(-) create mode 100755 bin/apisix.lua diff --git a/Makefile b/Makefile index 55a0bb32fdc1b..de8be44ceb299 100644 --- a/Makefile +++ b/Makefile @@ -58,7 +58,7 @@ else luarocks install rockspec/apisix-master-0.rockspec --tree=deps --only-deps --local endif ifneq ($(LUA_VER),Lua 5.1) - sed -i '' -e "s|#!/usr/bin/env lua|#!/usr/bin/env $(LUAJIT_DIR)/bin/luajit|g" ./bin/apisix + sed -i '' -e "s|#!/usr/bin/env lua|#!/usr/bin/env $(LUAJIT_DIR)/bin/luajit|g" ./bin/apisix.lua endif @@ -193,6 +193,7 @@ install: default $(INSTALL) README.md $(INST_CONFDIR)/README.md $(INSTALL) bin/apisix $(INST_BINDIR)/apisix + $(INSTALL) bin/apisix.lua $(INST_BINDIR)/apisix.lua $(INSTALL) -d $(INST_LUADIR)/apisix/plugins/slslog $(INSTALL) apisix/plugins/slslog/*.lua $(INST_LUADIR)/apisix/plugins/slslog/ diff --git a/bin/apisix b/bin/apisix index c3962ac112af9..d04ec433d7afd 100755 --- a/bin/apisix +++ b/bin/apisix @@ -1,36 +1,36 @@ -#!/usr/bin/env lua +#!/bin/bash --- --- Licensed to the Apache Software Foundation (ASF) under one or more --- contributor license agreements. See the NOTICE file distributed with --- this work for additional information regarding copyright ownership. --- The ASF licenses this file to You under the Apache License, Version 2.0 --- (the "License"); you may not use this file except in compliance with --- the License. You may obtain a copy of the License at --- --- http://www.apache.org/licenses/LICENSE-2.0 --- --- Unless required by applicable law or agreed to in writing, software --- distributed under the License is distributed on an "AS IS" BASIS, --- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. --- See the License for the specific language governing permissions and --- limitations under the License. --- +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# -local pkg_cpath_org = package.cpath -local pkg_path_org = package.path +USE_LUA=true +LUA_VERSION=$(lua -v 2>&1 | grep -E -o "Lua [0-9]+.[0-9]+") +APISIX_LUA=$0.lua -local apisix_home = "/usr/local/apisix" -local pkg_cpath = apisix_home .. "/deps/lib64/lua/5.1/?.so;" - .. apisix_home .. "/deps/lib/lua/5.1/?.so;;" -local pkg_path = apisix_home .. "/deps/share/lua/5.1/?.lua;;" +if [[ "$LUA_VERSION" =~ "Lua 5.1" && $USE_LUA ]]; then + # use lua of OS + lua $APISIX_LUA $* +else + # find the openresty + OR_EXEC=$(which openresty) --- modify the load path to load our dependencies -package.cpath = pkg_cpath .. pkg_cpath_org -package.path = pkg_path .. pkg_path_org + # find the luajit binary of openresty + LUAJIT_BIN=$(${OR_EXEC} -V 2>&1 | grep prefix | grep -Eo 'prefix=(.*)/nginx\s+--' | grep -Eo '/.*/')luajit/bin/luajit --- pass path to construct the final result -local env = require("apisix.cli.env")(apisix_home, pkg_cpath_org, pkg_path_org) -local ops = require("apisix.cli.ops") - -ops.execute(env, arg) + # use the luajit of openresty + $LUAJIT_BIN $APISIX_LUA $* +fi diff --git a/bin/apisix.lua b/bin/apisix.lua new file mode 100755 index 0000000000000..c3962ac112af9 --- /dev/null +++ b/bin/apisix.lua @@ -0,0 +1,36 @@ +#!/usr/bin/env lua + +-- +-- Licensed to the Apache Software Foundation (ASF) under one or more +-- contributor license agreements. See the NOTICE file distributed with +-- this work for additional information regarding copyright ownership. +-- The ASF licenses this file to You under the Apache License, Version 2.0 +-- (the "License"); you may not use this file except in compliance with +-- the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- + +local pkg_cpath_org = package.cpath +local pkg_path_org = package.path + +local apisix_home = "/usr/local/apisix" +local pkg_cpath = apisix_home .. "/deps/lib64/lua/5.1/?.so;" + .. apisix_home .. "/deps/lib/lua/5.1/?.so;;" +local pkg_path = apisix_home .. "/deps/share/lua/5.1/?.lua;;" + +-- modify the load path to load our dependencies +package.cpath = pkg_cpath .. pkg_cpath_org +package.path = pkg_path .. pkg_path_org + +-- pass path to construct the final result +local env = require("apisix.cli.env")(apisix_home, pkg_cpath_org, pkg_path_org) +local ops = require("apisix.cli.ops") + +ops.execute(env, arg) diff --git a/utils/install-apisix.sh b/utils/install-apisix.sh index 5f5645b0ab8e5..2456e4bb8fd4d 100755 --- a/utils/install-apisix.sh +++ b/utils/install-apisix.sh @@ -51,6 +51,8 @@ do_install() { sudo rm -f /usr/bin/apisix sudo ln -s /usr/local/apisix/deps/bin/apisix /usr/bin/apisix + sudo rm -f /usr/bin/apisix.lua + sudo ln -s /usr/local/apisix/deps/bin/apisix.lua /usr/bin/apisix.lua }