content: #
  # Copyright 2021 The Amesh Authors
  #
  # Licensed 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.
  FROM centos:7 as proxy-build-stage
  
  # Step 1, we build OpenResty by ourselves, since we have to apply some patches to it.
  
  ADD nginx /nginx
  
  # Docker Build Arguments
  ARG APISIX_VERSION="2.15.0"
  ARG ENABLE_PROXY=false
  ARG LUAROCKS_VERSION="3.4.0"
  ARG LUAROCKS_SERVER="https://luarocks.org"
  ARG RESTY_VERSION="1.19.9.1"
  ARG RESTY_OPENSSL_VERSION="1.1.1g"
  ARG RESTY_NGINX_VERSION="1.19.9"
  ARG RESTY_OPENSSL_PATCH_VERSION="1.1.1f"
  ARG RESTY_OPENSSL_URL_BASE="https://www.openssl.org/source"
  ARG RESTY_PCRE_VERSION="8.44"
  ARG RESTY_J="1"
  ARG RESTY_CONFIG_OPTIONS="\
      --with-compat \
      --with-file-aio \
      --with-http_addition_module \
      --with-http_auth_request_module \
      --with-http_geoip_module=dynamic \
      --with-http_gunzip_module \
      --with-http_gzip_static_module \
      --with-http_random_index_module \
      --with-http_realip_module \
      --with-http_secure_link_module \
      --with-http_ssl_module \
      --with-http_stub_status_module \
      --with-http_sub_module \
      --with-http_v2_module \
      --with-http_xslt_module=dynamic \
      --with-ipv6 \
      --with-md5-asm \
      --with-pcre-jit \
      --with-sha1-asm \
      --with-stream \
      --with-stream_ssl_module \
      --with-threads \
      "
  ARG RESTY_CONFIG_OPTIONS_MORE=""
  ARG RESTY_LUAJIT_OPTIONS="--with-luajit-xcflags='-DLUAJIT_NUMMODE=2 -DLUAJIT_ENABLE_LUA52COMPAT'"
  
  ARG RESTY_ADD_PACKAGE_BUILDDEPS=""
  ARG RESTY_ADD_PACKAGE_RUNDEPS=""
  ARG RESTY_EVAL_PRE_CONFIGURE=""
  ARG RESTY_EVAL_POST_MAKE=""
  
  # These are not intended to be user-specified
  ARG _RESTY_CONFIG_DEPS="--with-pcre \
      --with-cc-opt='-DNGX_LUA_ABORT_AT_PANIC -I/usr/local/openresty/pcre/include -I/usr/local/openresty/openssl/include' \
      --with-ld-opt='-L/usr/local/openresty/pcre/lib -L/usr/local/openresty/openssl/lib -Wl,-rpath,/usr/local/openresty/pcre/lib:/usr/local/openresty/openssl/lib' \
      "
  
  LABEL resty_version="${RESTY_VERSION}"
  LABEL resty_openssl_version="${RESTY_OPENSSL_VERSION}"
  LABEL resty_openssl_patch_version="${RESTY_OPENSSL_PATCH_VERSION}"
  LABEL resty_openssl_url_base="${RESTY_OPENSSL_URL_BASE}"
  LABEL resty_pcre_version="${RESTY_PCRE_VERSION}"
  LABEL resty_config_options="${RESTY_CONFIG_OPTIONS}"
  LABEL resty_config_options_more="${RESTY_CONFIG_OPTIONS_MORE}"
  LABEL resty_config_deps="${_RESTY_CONFIG_DEPS}"
  LABEL resty_add_package_builddeps="${RESTY_ADD_PACKAGE_BUILDDEPS}"
  LABEL resty_add_package_rundeps="${RESTY_ADD_PACKAGE_RUNDEPS}"
  LABEL resty_eval_pre_configure="${RESTY_EVAL_PRE_CONFIGURE}"
  LABEL resty_eval_post_make="${RESTY_EVAL_POST_MAKE}"
  
  RUN yum install -y --disableplugin=fastestmirror  \
          make gcc \
          coreutils \
          curl \
          gd-devel \
          geoip-devel \
          libxslt-devel \
          linux-headers \
          make \
          perl-devel \
          readline \
          zlib-devel \
          ${RESTY_ADD_PACKAGE_BUILDDEPS} \
          which \
          patch
  RUN yum install -y --disableplugin=fastestmirror  \
          gd \
          geoip \
          libgcc \
          libxslt \
          zlib \
          ${RESTY_ADD_PACKAGE_RUNDEPS}
  RUN cd /tmp \
      && if [ -n "${RESTY_EVAL_PRE_CONFIGURE}" ]; then eval $(echo ${RESTY_EVAL_PRE_CONFIGURE}); fi \
      && cd /tmp \
      && curl -fSL "${RESTY_OPENSSL_URL_BASE}/openssl-${RESTY_OPENSSL_VERSION}.tar.gz" -o openssl-${RESTY_OPENSSL_VERSION}.tar.gz \
      && tar xzf openssl-${RESTY_OPENSSL_VERSION}.tar.gz \
      && cd openssl-${RESTY_OPENSSL_VERSION} \
      && if [ $(echo ${RESTY_OPENSSL_VERSION} | cut -c 1-5) = "1.1.1" ] ; then \
          echo 'patching OpenSSL 1.1.1 for OpenResty' \
          && curl -s https://raw.githubusercontent.com/openresty/openresty/master/patches/openssl-${RESTY_OPENSSL_PATCH_VERSION}-sess_set_get_cb_yield.patch | patch -p1 ; \
      fi \
      && if [ $(echo ${RESTY_OPENSSL_VERSION} | cut -c 1-5) = "1.1.0" ] ; then \
          echo 'patching OpenSSL 1.1.0 for OpenResty' \
          && curl -s https://raw.githubusercontent.com/openresty/openresty/ed328977028c3ec3033bc25873ee360056e247cd/patches/openssl-1.1.0j-parallel_build_fix.patch | patch -p1 \
          && curl -s https://raw.githubusercontent.com/openresty/openresty/master/patches/openssl-${RESTY_OPENSSL_PATCH_VERSION}-sess_set_get_cb_yield.patch | patch -p1 ; \
      fi
  RUN cd /tmp/openssl-${RESTY_OPENSSL_VERSION} \
      && ./config \
        no-threads shared zlib -g \
        enable-ssl3 enable-ssl3-method \
        --prefix=/usr/local/openresty/openssl \
        --libdir=lib \
        -Wl,-rpath,/usr/local/openresty/openssl/lib \
      && make -j${RESTY_J} \
      && make -j${RESTY_J} install_sw
  
  RUN yum install -y --disableplugin=fastestmirror  wget
  RUN cd /tmp \
      && wget --no-check-certificate https://versaweb.dl.sourceforge.net/project/pcre/pcre/${RESTY_PCRE_VERSION}/pcre-${RESTY_PCRE_VERSION}.tar.gz -O pcre-${RESTY_PCRE_VERSION}.tar.gz \
      && tar xzf pcre-${RESTY_PCRE_VERSION}.tar.gz
  RUN cd /tmp/pcre-${RESTY_PCRE_VERSION} \
      && ./configure \
          --prefix=/usr/local/openresty/pcre \
          --disable-cpp \
          --enable-jit \
          --enable-utf \
          --enable-unicode-properties \
      && make -j${RESTY_J} \
      && make -j${RESTY_J} install
  RUN cd /tmp \
      && curl -fSL https://openresty.org/download/openresty-${RESTY_VERSION}.tar.gz -o openresty-${RESTY_VERSION}.tar.gz \
      && tar xzf openresty-${RESTY_VERSION}.tar.gz
  RUN cd /tmp/openresty-${RESTY_VERSION} \
      && cd bundle/nginx-${RESTY_NGINX_VERSION} || pwd \
      && cat /nginx/patches/nginx-${RESTY_NGINX_VERSION}-connection-original-dst.patch | patch -p 1 \
      && cd ../../ \
      && eval ./configure -j${RESTY_J} ${_RESTY_CONFIG_DEPS} ${RESTY_CONFIG_OPTIONS} ${RESTY_CONFIG_OPTIONS_MORE} ${RESTY_LUAJIT_OPTIONS} \
      && make -j${RESTY_J} \
      && make -j${RESTY_J} install
  RUN cd /tmp \
      && if [ -n "${RESTY_EVAL_POST_MAKE}" ]; then eval $(echo ${RESTY_EVAL_POST_MAKE}); fi \
      && rm -rf \
          openssl-${RESTY_OPENSSL_VERSION}.tar.gz openssl-${RESTY_OPENSSL_VERSION} \
          pcre-${RESTY_PCRE_VERSION}.tar.gz pcre-${RESTY_PCRE_VERSION} \
          openresty-${RESTY_VERSION}.tar.gz openresty-${RESTY_VERSION} \
      && mkdir -p /var/run/openresty \
      && ln -sf /dev/stdout /usr/local/openresty/nginx/logs/access.log \
      && ln -sf /dev/stderr /usr/local/openresty/nginx/logs/error.log
  
  # Add additional binaries into PATH for convenience
  ENV PATH=$PATH:/usr/local/openresty/luajit/bin:/usr/local/openresty/nginx/sbin:/usr/local/openresty/bin
  
  # Step 2, building LuaRocks
  # install wget because busybox wget has several proxy bugs
  RUN cd /tmp \
      && wget https://github.com/luarocks/luarocks/archive/v${LUAROCKS_VERSION}.tar.gz
  RUN cd /tmp && tar xf v${LUAROCKS_VERSION}.tar.gz
  RUN yum install -y --disableplugin=fastestmirror \
      unzip
  RUN cd /tmp/luarocks-${LUAROCKS_VERSION} \
      && ./configure --with-lua=/usr/local/openresty/luajit \
      && make build \
      && make install
  
  # Step 3, building APISIX.
  LABEL apisix_version="${APISIX_VERSION}"
  RUN yum install -y --disableplugin=fastestmirror  \
      automake \
      autoconf \
      libtool \
      pkgconfig \
      cmake \
      unzip \
      bash \
      git \
      ldb-devel libldap openldap-devel \
      unzip
  RUN git config --global url."https://github.com/".insteadOf git://github.com/
  RUN mkdir ~/.luarocks \
      && luarocks config variables.OPENSSL_LIBDIR /usr/local/openresty/openssl/lib \
      && luarocks config variables.OPENSSL_INCDIR /usr/local/openresty/openssl/include \
      && luarocks config variables.PCRE_DIR /usr/local/openresty/pcre \
      && luarocks install https://github.com/apache/apisix/raw/master/rockspec/apisix-${APISIX_VERSION}-0.rockspec --tree=/usr/local/apisix/deps --server ${LUAROCKS_SERVER} \
      && cp -v /usr/local/apisix/deps/lib/luarocks/rocks-5.1/apisix/${APISIX_VERSION}-0/bin/apisix /usr/bin/ \
      && mv /usr/local/apisix/deps/share/lua/5.1/apisix /usr/local/apisix
  
  
  FROM centos:7
  
  RUN yum install -y --disableplugin=fastestmirror  \
      iptables \
      bash \
      libstdc++ \
      curl \
      lsof \
      patch \
      which
  #RUN apk add --no-cache --repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing/ \
  #    etcd-ctl
  
  COPY --from=proxy-build-stage /usr/local/openresty /usr/local/openresty
  COPY --from=proxy-build-stage /usr/bin/apisix /usr/bin/apisix
  COPY --from=proxy-build-stage /usr/local/apisix /usr/local/apisix
  
  # Apply patches
  #ADD apisix /apisix-patch
  #RUN patch /usr/local/apisix/apisix/core/config_etcd.lua < /apisix-patch/core/config_etcd.patch
  
  ENV PATH=$PATH:/usr/local/openresty/luajit/bin:/usr/local/openresty/nginx/sbin:/usr/local/openresty/bin
  
  WORKDIR /usr/local/apisix
  
  # Use SIGQUIT instead of default SIGTERM to cleanly drain requests
  # See https://github.com/openresty/docker-openresty/blob/master/README.md#tips--pitfalls
  STOPSIGNAL SIGQUIT
  