You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by mh...@apache.org on 2017/06/28 14:24:29 UTC

[incubator-openwhisk-apigateway] branch master updated: use opm instead of installing lua deps by hand (#235)

This is an automated email from the ASF dual-hosted git repository.

mhamann pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk-apigateway.git


The following commit(s) were added to refs/heads/master by this push:
     new d4619a8  use opm instead of installing lua deps by hand (#235)
d4619a8 is described below

commit d4619a866100ea3b3e719d5aba4ada37fe5fb0ee
Author: Taylor King <ta...@gmail.com>
AuthorDate: Wed Jun 28 10:24:28 2017 -0400

    use opm instead of installing lua deps by hand (#235)
---
 Dockerfile                          |  80 +++++------------
 scripts/lua/lib/resty/limit/req.lua | 173 ------------------------------------
 2 files changed, 24 insertions(+), 229 deletions(-)

diff --git a/Dockerfile b/Dockerfile
index 7c2e835..a30f2f2 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -36,7 +36,7 @@ RUN  echo " ... adding Openresty, NGINX, NAXSI and PCRE" \
      && tar -zxf ./openresty-${OPENRESTY_VERSION}.tar.gz \
      && tar -zxf ./pcre-${PCRE_VERSION}.tar.gz \
      && tar -zxf ./naxsi-${NAXSI_VERSION}.tar.gz \
-     && cd /tmp/api-gateway/openresty-${OPENRESTY_VERSION} \ 
+     && cd /tmp/api-gateway/openresty-${OPENRESTY_VERSION} \
 
      && echo "        - building debugging version of the api-gateway ... " \
      && ./configure \
@@ -126,64 +126,33 @@ RUN  echo " ... adding Openresty, NGINX, NAXSI and PCRE" \
     && rm -rf /var/cache/apk/* \
     && rm -rf /tmp/api-gateway
 
-ENV LUA_RESTY_HTTP_VERSION 0.07
-RUN echo " ... installing lua-resty-http..." \
-    && apk update \
-    && apk add make \
+ENV OPM_VERSION 0.0.3
+RUN echo " ... installing opm..." \
     && mkdir -p /tmp/api-gateway \
-    && curl -k -L https://github.com/pintsized/lua-resty-http/archive/v${LUA_RESTY_HTTP_VERSION}.tar.gz -o /tmp/api-gateway/lua-resty-http-${LUA_RESTY_HTTP_VERSION}.tar.gz \
-    && tar -xf /tmp/api-gateway/lua-resty-http-${LUA_RESTY_HTTP_VERSION}.tar.gz -C /tmp/api-gateway/ \
-    && cd /tmp/api-gateway/lua-resty-http-${LUA_RESTY_HTTP_VERSION} \
-    && make install \
-            LUA_LIB_DIR=${_prefix}/api-gateway/lualib \
-            INSTALL=${_prefix}/api-gateway/bin/resty-install \
-    && rm -rf /tmp/api-gateway
-
-ENV LUA_RESTY_IPUTILS_VERSION 0.2.0
-RUN echo " ... installing lua-resty-iputils..." \
-    && apk update \
-    && apk add make \
-    && mkdir -p /tmp/api-gateway \
-    && curl -k -L https://github.com/hamishforbes/lua-resty-iputils/archive/v${LUA_RESTY_IPUTILS_VERSION}.tar.gz -o /tmp/api-gateway/lua-resty-iputils-${LUA_RESTY_IPUTILS_VERSION}.tar.gz \
-    && tar -xf /tmp/api-gateway/lua-resty-iputils-${LUA_RESTY_IPUTILS_VERSION}.tar.gz -C /tmp/api-gateway/ \
-    && cd /tmp/api-gateway/lua-resty-iputils-${LUA_RESTY_IPUTILS_VERSION} \
-    && export LUA_LIB_DIR=${_prefix}/api-gateway/lualib \
-    && export INSTALL=${_prefix}/api-gateway/bin/resty-install \
-    && $INSTALL -d ${LUA_LIB_DIR}/resty \
-    && $INSTALL lib/resty/*.lua ${LUA_LIB_DIR}/resty/ \
-    && rm -rf /tmp/api-gateway
-
-
-
+    && curl -k -L https://github.com/openresty/opm/archive/v${OPM_VERSION}.tar.gz -o /tmp/api-gateway/opm-${OPM_VERSION}.tar.gz \
+    && tar -xf /tmp/api-gateway/opm-${OPM_VERSION}.tar.gz -C /tmp/api-gateway \
+    && cd /tmp/api-gateway/opm-${OPM_VERSION} \
+    && cp bin/opm ${_prefix}/api-gateway/bin \
+    && cd ${_prefix}/api-gateway \
+    && mkdir -p site/manifest site/pod \
+    && cd site \
+    && ln -s ../lualib ./ \
+    && ln -s ${_prefix}/api-gateway/bin/opm /usr/bin/opm \
+    && ln -s ${_prefix}/api-gateway/bin/resty /usr/bin/resty
+
+ENV LUA_RESTY_HTTP_VERSION 0.10
+RUN opm get pintsized/lua-resty-http=${LUA_RESTY_HTTP_VERSION}
+ENV LUA_RESTY_IPUTILS_VERSION 0.2.1
+RUN opm get hamishforbes/lua-resty-iputils=${LUA_RESTY_IPUTILS_VERSION}
 ENV LUA_RESTY_STRING_VERSION 0.09
-RUN echo " ... installing lua-resty-string..." \
-    && apk update \
-    && apk add make \
-    && mkdir -p /tmp/api-gateway \
-    && curl -k -L https://github.com/openresty/lua-resty-string/archive/v${LUA_RESTY_STRING_VERSION}.tar.gz -o /tmp/api-gateway/lua-resty-string-${LUA_RESTY_STRING_VERSION}.tar.gz \
-    && tar -xf /tmp/api-gateway/lua-resty-string-${LUA_RESTY_STRING_VERSION}.tar.gz -C /tmp/api-gateway/ \
-    && cd /tmp/api-gateway/lua-resty-string-${LUA_RESTY_STRING_VERSION} \
-    && make install \
-            LUA_LIB_DIR=${_prefix}/api-gateway/lualib \
-            INSTALL=${_prefix}/api-gateway/bin/resty-install \
-    && rm -rf /tmp/api-gateway
-
+RUN opm get openresty/lua-resty-string=${LUA_RESTY_STRING_VERSION}
 ENV LUA_RESTY_LRUCACHE_VERSION 0.04
-RUN echo " ... installing lua-resty-lrucache... " \
-    && apk update \
-    && apk add make \
-    && mkdir -p /tmp/api-gateway \
-    && curl -k -L https://github.com/openresty/lua-resty-lrucache/archive/v${LUA_RESTY_LRUCACHE_VERSION}.tar.gz -o /tmp/api-gateway/lua-resty-lrucache-${LUA_RESTY_LRUCACHE_VERSION}.tar.gz \
-    && tar -xf /tmp/api-gateway/lua-resty-lrucache-${LUA_RESTY_LRUCACHE_VERSION}.tar.gz -C /tmp/api-gateway/ \
-    && cd /tmp/api-gateway/lua-resty-lrucache-${LUA_RESTY_LRUCACHE_VERSION} \
-    && make install \
-            LUA_LIB_DIR=${_prefix}/api-gateway/lualib \
-            INSTALL=${_prefix}/api-gateway/bin/resty-install \
-    && rm -rf /tmp/api-gateway
-
-
-
+RUN opm get openresty/lua-resty-lrucache=${LUA_RESTY_LRUCACHE_VERSION}
+ENV LUA_RESTY_JWT_VERSION 0.1.10
+RUN opm get SkyLothar/lua-resty-jwt=${LUA_RESTY_JWT_VERSION}
 ENV NETURL_LUA_VERSION 0.9-1
+RUN opm get taylorking/lua-resty-rate-limit
+
 RUN echo " ... installing neturl.lua ... " \
     && mkdir -p /tmp/api-gateway \
     && curl -k -L https://github.com/golgote/neturl/archive/${NETURL_LUA_VERSION}.tar.gz -o /tmp/api-gateway/neturl.lua-${NETURL_LUA_VERSION}.tar.gz \
@@ -193,7 +162,6 @@ RUN echo " ... installing neturl.lua ... " \
     && cp lib/net/url.lua ${LUA_LIB_DIR} \
     && rm -rf /tmp/api-gateway
 
-
 RUN \
     curl -L -k -s -o /usr/local/bin/jq https://github.com/stedolan/jq/releases/download/jq-1.5/jq-linux64 \
     && apk update \
diff --git a/scripts/lua/lib/resty/limit/req.lua b/scripts/lua/lib/resty/limit/req.lua
deleted file mode 100644
index eeec626..0000000
--- a/scripts/lua/lib/resty/limit/req.lua
+++ /dev/null
@@ -1,173 +0,0 @@
---  Copyright (C) 2014 Monkey Zhang (timebug), UPYUN Inc.
---  All rights reserved.
---
---   Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
---
---   Redistributions of source code must retain the above copyright notice,
---   this list of conditions and the following disclaimer.
---
---   Redistributions in binary form must reproduce the above copyright notice,
---   this list of conditions and the following disclaimer in the documentation
---   and/or other materials provided with the distribution.
---
---   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
---   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
---   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
---   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
---   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
---   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
---   DEALINGS IN THE SOFTWARE.
-
-local redis = require 'lib/redis'
-
-local floor = math.floor
-local tonumber = tonumber
-
-
-local _M = { _VERSION = "0.01", OK = 1, BUSY = 2, FORBIDDEN = 3 }
-
-
-local redis_limit_req_script_sha
-local redis_limit_req_script = [==[
-local key = KEYS[1]
-local rate = tonumber(KEYS[2])
-local now, interval = tonumber(KEYS[3]), tonumber(KEYS[4])
-
-local excess, last, forbidden = 0, 0, 0
-
-local res = redis.pcall('GET', key)
-if type(res) == "table" and res.err then
-    return {err=res.err}
-end
-
-if res and type(res) == "string" then
-    local v = cjson.decode(res)
-    if v and #v > 2 then
-        excess, last, forbidden = v[1], v[2], v[3]
-    end
-
-    if forbidden == 1 then
-        return {3, excess} -- FORBIDDEN
-    end
-
-    local ms = math.abs(now - last)
-    excess = excess - rate * ms / 1000 + 1000
-
-    if excess < 0 then
-        excess = 0
-    end
-
-    if excess > 0 then
-        if interval > 0 then
-            local res = redis.pcall('SET', key,
-                                    cjson.encode({excess, now, 1}))
-            if type(res) == "table" and res.err then
-                return {err=res.err}
-            end
-
-            local res = redis.pcall('EXPIRE', key, interval)
-            if type(res) == "table" and res.err then
-                return {err=res.err}
-            end
-        end
-
-        return {2, excess} -- BUSY
-    end
-end
-
-local res = redis.pcall('SET', key, cjson.encode({excess, now, 0}))
-if type(res) == "table" and res.err then
-    return {err=res.err}
-end
-
-local res = redis.pcall('EXPIRE', key, 60)
-if type(res) == "table" and res.err then
-    return {err=res.err}
-end
-
-return {1, excess}
-]==]
-
-
-local function redis_lookup(conn, zone, key, rate, duration)
-    local red = conn
-
-    if not redis_limit_req_script_sha then
-        local res, err = red:script("LOAD", redis_limit_req_script)
-        if not res then
-            return nil, err
-        end
-
-        ngx.log(ngx.NOTICE, "load redis limit req script")
-
-        redis_limit_req_script_sha = res
-    end
-
-    local now = ngx.now() * 1000
-    local res, err = red:evalsha(redis_limit_req_script_sha, 4,
-                                 zone .. ":" .. key, rate, now, duration)
-    if not res then
-        redis_limit_req_script_sha = nil
-        return nil, err
-    end
-
-    -- put it into the connection pool of size 100,
-    -- with 10 seconds max idle timeout
-    local ok, err = red:set_keepalive(10000, 100)
-    if not ok then
-        ngx.log(ngx.WARN, "failed to set keepalive: ", err)
-    end
-
-    return res
-end
-
-
-function _M.limit(cfg)
-    if not cfg.conn then
-        local rds = cfg.rds or {}
-        rds.timeout = rds.timeout or 1000
-        rds.host = rds.host or "127.0.0.1"
-        rds.port = rds.port or 6379
-        rds.pass = rds.pass or nil
-
-        cfg.conn = redis.init(rds.host, rds.port, rds.pass, rds.timeout)
-    end
-
-    local conn = cfg.conn
-    local zone = cfg.zone or "limit_req"
-    local key = cfg.key or ngx.var.remote_addr
-    local rate = cfg.rate or "1r/s"
-    local interval = cfg.interval or 0
-    local log_level = cfg.log_level or ngx.NOTICE
-
-    local scale = 1
-    local len = #rate
-
-    if len > 3 and rate:sub(len - 2) == "r/s" then
-        scale = 1
-        rate = rate:sub(1, len - 3)
-    elseif len > 3 and rate:sub(len - 2) == "r/m" then
-        scale = 60
-        rate = rate:sub(1, len - 3)
-    end
-
-    rate = floor((tonumber(rate) or 1) * 1000 / scale)
-
-    local res, err = redis_lookup(conn, zone, key, rate, interval)
-    if res and (res[1] == _M.BUSY or res[1] == _M.FORBIDDEN) then
-        if res[1] == _M.BUSY then
-            ngx.log(log_level, 'limiting requests, excess ' ..
-                        res[2]/1000 .. ' by zone "' .. zone .. '"')
-        end
-        return
-    end
-
-    if not res and err then
-        ngx.log(ngx.WARN, "redis lookup err: ", err)
-    end
-
-    return _M.OK
-end
-
-
-return _M

-- 
To stop receiving notification emails like this one, please contact
['"commits@openwhisk.apache.org" <co...@openwhisk.apache.org>'].