From 10c655c720d5ea2ddf9ebff58edfd264759f86d4 Mon Sep 17 00:00:00 2001 From: Sebastian Hugentobler Date: Tue, 16 Aug 2016 10:48:34 +0200 Subject: [PATCH] initial commit --- .gitignore | 3 ++ Dockerfile | 35 ++++++++++++++++ rootfs/etc/confd/conf.d/nginx.conf.toml | 3 ++ rootfs/etc/confd/conf.d/redis.conf.toml | 3 ++ rootfs/etc/confd/conf.d/redis.lua.toml | 3 ++ rootfs/etc/confd/templates/nginx.conf.tmpl | 38 +++++++++++++++++ rootfs/etc/confd/templates/redis.conf.tmpl | 49 ++++++++++++++++++++++ rootfs/etc/confd/templates/redis.lua.tmpl | 44 +++++++++++++++++++ rootfs/etc/services.d/koreader-sync/run | 5 +++ rootfs/var/lib/koreader-sync/gin.patch | 47 +++++++++++++++++++++ 10 files changed, 230 insertions(+) create mode 100755 .gitignore create mode 100644 Dockerfile create mode 100644 rootfs/etc/confd/conf.d/nginx.conf.toml create mode 100644 rootfs/etc/confd/conf.d/redis.conf.toml create mode 100644 rootfs/etc/confd/conf.d/redis.lua.toml create mode 100644 rootfs/etc/confd/templates/nginx.conf.tmpl create mode 100644 rootfs/etc/confd/templates/redis.conf.tmpl create mode 100644 rootfs/etc/confd/templates/redis.lua.tmpl create mode 100644 rootfs/etc/services.d/koreader-sync/run create mode 100644 rootfs/var/lib/koreader-sync/gin.patch diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000..af0faab --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*~ +.DS_Store +*.swp diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..061c3b7 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,35 @@ +FROM thallian/confd-env:latest + +RUN addgroup syncer +RUN adduser -h /var/lib/koreader-sync -D -G syncer syncer + +RUN apk --no-cache add tar make git openssl perl gcc musl-dev pcre pcre-dev openssl-dev lua lua-dev unzip curl +RUN apk --no-cache --repository http://dl-3.alpinelinux.org/alpine/edge/testing/ --allow-untrusted add luarocks5.1 + +RUN luarocks-5.1 install luasec +RUN luarocks-5.1 install luaposix +RUN luarocks-5.1 install redis-lua +RUN luarocks-5.1 install busted + +RUN mkdir /tmp/openresty +RUN wget -qO- https://openresty.org/download/openresty-1.9.15.1.tar.gz | tar -xz -C /tmp/openresty --strip 1 + +WORKDIR /tmp/openresty +RUN ./configure --prefix=/opt/openresty && make && make install +RUN rm -r /tmp/openresty +ENV PATH /opt/openresty/nginx/sbin:$PATH + +WORKDIR /var/lib/koreader-sync +RUN git clone https://github.com/ostinelli/gin +ADD /rootfs / + +RUN cd gin && patch -N -p1 < ../gin.patch +RUN cd gin && luarocks-5.1 make +ENV GIN_ENV production + +RUN git clone https://github.com/koreader/koreader-sync-server.git server +RUN chown -R syncer:syncer /var/lib/koreader-sync + +RUN apk del tar make git gcc musl-dev pcre-dev openssl-dev lua-dev + +EXPOSE 7200 diff --git a/rootfs/etc/confd/conf.d/nginx.conf.toml b/rootfs/etc/confd/conf.d/nginx.conf.toml new file mode 100644 index 0000000..3104b14 --- /dev/null +++ b/rootfs/etc/confd/conf.d/nginx.conf.toml @@ -0,0 +1,3 @@ +[template] +src = "nginx.conf.tmpl" +dest = "/var/lib/koreader-sync/server/config/nginx.conf" \ No newline at end of file diff --git a/rootfs/etc/confd/conf.d/redis.conf.toml b/rootfs/etc/confd/conf.d/redis.conf.toml new file mode 100644 index 0000000..71b71e6 --- /dev/null +++ b/rootfs/etc/confd/conf.d/redis.conf.toml @@ -0,0 +1,3 @@ +[template] +src = "redis.conf.tmpl" +dest = "/etc/redis.conf" \ No newline at end of file diff --git a/rootfs/etc/confd/conf.d/redis.lua.toml b/rootfs/etc/confd/conf.d/redis.lua.toml new file mode 100644 index 0000000..8cc1933 --- /dev/null +++ b/rootfs/etc/confd/conf.d/redis.lua.toml @@ -0,0 +1,3 @@ +[template] +src = "redis.lua.tmpl" +dest = "/var/lib/koreader-sync/server/db/redis.lua" diff --git a/rootfs/etc/confd/templates/nginx.conf.tmpl b/rootfs/etc/confd/templates/nginx.conf.tmpl new file mode 100644 index 0000000..22003c5 --- /dev/null +++ b/rootfs/etc/confd/templates/nginx.conf.tmpl @@ -0,0 +1,38 @@ +pid tmp/{{"{{"}}GIN_ENV{{"}}"}}-nginx.pid; + +# This number should be at maxium the number of CPU on the server +worker_processes 4; +#daemon off; + +events { + # Number of connections per worker + worker_connections 4096; +} + +http { + # use sendfile + sendfile on; + + # Gin initialization + {{"{{"}}GIN_INIT{{"}}"}} + + ssl_session_cache shared:SSL:10m; + ssl_session_timeout 10m; + + server { + # List port + listen {{"{{"}}GIN_PORT{{"}}"}}; + + keepalive_timeout 70; + + # Access log with buffer, or disable it completetely if unneeded + access_log logs/{{"{{"}}GIN_ENV{{"}}"}}-access.log combined buffer=16k; + # access_log off; + + # Error log + error_log logs/{{"{{"}}GIN_ENV{{"}}"}}-error.log debug; + + # Gin runtime + {{"{{"}}GIN_RUNTIME{{"}}"}} + } +} \ No newline at end of file diff --git a/rootfs/etc/confd/templates/redis.conf.tmpl b/rootfs/etc/confd/templates/redis.conf.tmpl new file mode 100644 index 0000000..3f11d3e --- /dev/null +++ b/rootfs/etc/confd/templates/redis.conf.tmpl @@ -0,0 +1,49 @@ +{{if (getenv "REDIS_BIND")}} +bind {{getenv "REDIS_BIND"}} +{{end}} +protected-mode no +port 6379 +tcp-backlog 511 +timeout 0 +tcp-keepalive 0 +daemonize no +supervised no +loglevel notice +logfile '' +databases 16 +save 900 1 +save 300 10 +save 60 10000 +stop-writes-on-bgsave-error yes +rdbcompression yes +rdbchecksum yes +dbfilename dump.rdb +dir /var/lib/redis/ +{{if (getenv "REDIS_PASSWORD")}} +requirepass {{getenv "REDIS_PASSWORD"}} +{{end}} +appendonly no +appendfsync everysec +no-appendfsync-on-rewrite no +auto-aof-rewrite-percentage 100 +auto-aof-rewrite-min-size 64mb +aof-load-truncated yes +lua-time-limit 5000 +slowlog-log-slower-than 10000 +slowlog-max-len 128 +latency-monitor-threshold 0 +notify-keyspace-events "" +hash-max-ziplist-entries 512 +hash-max-ziplist-value 64 +list-max-ziplist-size -2 +list-compress-depth 0 +set-max-intset-entries 512 +zset-max-ziplist-entries 128 +zset-max-ziplist-value 64 +hll-sparse-max-bytes 3000 +activerehashing yes +client-output-buffer-limit normal 0 0 0 +client-output-buffer-limit slave 256mb 64mb 60 +client-output-buffer-limit pubsub 32mb 8mb 60 +hz 10 +aof-rewrite-incremental-fsync yes diff --git a/rootfs/etc/confd/templates/redis.lua.tmpl b/rootfs/etc/confd/templates/redis.lua.tmpl new file mode 100644 index 0000000..48debbb --- /dev/null +++ b/rootfs/etc/confd/templates/redis.lua.tmpl @@ -0,0 +1,44 @@ +local Gin = require 'gin.core.gin' + +-- First, specify the environment settings for this database, for instance: +local DbSettings = { + development = { + host = "127.0.0.1", + port = 6379, + database = 1, + pool = 5 + }, + + test = { + host = "127.0.0.1", + port = 6379, + database = 2, + pool = 5 + }, + + production = { + host = "{{getenv "REDIS_HOST"}}", + port = 6379, + database = 3, + pool = 5 + } +} + +-- Then initialize and return your database: +local Redis = { + options = {}, +} + +function Redis:new() + local redis = require("resty.redis") + local option = DbSettings[Gin.env] + local red = redis:new() + red:set_timeout(1000) -- 1 sec + local ok, err = red:connect(option.host, option.port) + if ok then + red:select(option.database) + return red + end +end + +return Redis \ No newline at end of file diff --git a/rootfs/etc/services.d/koreader-sync/run b/rootfs/etc/services.d/koreader-sync/run new file mode 100644 index 0000000..ae0e558 --- /dev/null +++ b/rootfs/etc/services.d/koreader-sync/run @@ -0,0 +1,5 @@ +#!/usr/bin/with-contenv sh + +cd /var/lib/koreader-sync/server +s6-setuidgid syncer gin start +tail -f /var/lib/koreader-sync/server/logs/production-access.log diff --git a/rootfs/var/lib/koreader-sync/gin.patch b/rootfs/var/lib/koreader-sync/gin.patch new file mode 100644 index 0000000..b3f87f7 --- /dev/null +++ b/rootfs/var/lib/koreader-sync/gin.patch @@ -0,0 +1,47 @@ +diff --git a/gin-0.2.0-1.rockspec b/gin-0.2.0-1.rockspec +index b9601ec..55f4469 100644 +--- a/gin-0.2.0-1.rockspec ++++ b/gin-0.2.0-1.rockspec +@@ -18,8 +18,9 @@ dependencies = { + "busted = 2.0.rc10-0", + "lua-cjson = 2.1.0-1", + "luasocket = 3.0rc1-2", ++ "luasec = 0.5-2", + "luafilesystem = 1.6.3-1", +- "luaposix = 33.3.1-1", ++ "luaposix = 33.4.0", + "penlight = 1.3.2-2", + "luadbi = 0.5-1" + } +diff --git a/gin/spec/runners/integration.lua b/gin/spec/runners/integration.lua +index 5ef537a..21ec8f9 100644 +--- a/gin/spec/runners/integration.lua ++++ b/gin/spec/runners/integration.lua +@@ -1,5 +1,6 @@ + -- dep + local http = require 'socket.http' ++local https = require 'ssl.https' + local url = require 'socket.url' + local json = require 'cjson' + local ltn12 = require 'ltn12' +@@ -82,8 +83,9 @@ local function set_accept_header(request, api_version) + end + + local function hit_server(request) ++ local scheme = request.scheme or 'http' + local full_url = url.build({ +- scheme = 'http', ++ scheme = scheme, + host = '127.0.0.1', + port = Gin.settings.port, + path = request.path, +@@ -91,7 +93,8 @@ local function hit_server(request) + }) + + local response_body = {} +- local ok, response_status, response_headers = http.request({ ++ local http_request = (scheme == 'https') and https.request or http.request ++ local ok, response_status, response_headers = http_request({ + method = request.method, + url = full_url, + source = ltn12.source.string(request.body), \ No newline at end of file