Skip to content

Commit bc5bd30

Browse files
committed
Rework command runner
1 parent c984279 commit bc5bd30

File tree

9 files changed

+138
-107
lines changed

9 files changed

+138
-107
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ nginx.pid
88

99
*.sh
1010
!/tinysta.sh
11-
!/scripts/resty-runner.sh
11+
!/commands/command-runner.sh

.luacheckrc

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
return {
2-
std = 'ngx_lua',
3-
exclude_files = {
4-
'resty_modules/**',
5-
},
1+
std = 'ngx_lua'
2+
exclude_files = {
3+
'resty_modules/**',
4+
}
5+
files['commands'] = {
6+
read_globals = {
7+
'OPENRESTY_PREFIX',
8+
'TINYSTASH_DIR',
9+
'run_command',
10+
},
611
}

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ RUN while read dep; do opm --cwd get "$dep"; done < /tmp/requirements.opm
66
COPY app/ app/
77
COPY static/ static/
88
COPY templates/ templates/
9-
COPY scripts/ scripts/
9+
COPY commands/ commands/
1010
COPY tinysta.sh tinysta.sh
1111
COPY nginx.conf.tpl nginx.conf.tpl
1212

commands/command-runner.sh

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/bin/sh
2+
3+
# inspired by resty-cli -- https://github.com/openresty/resty-cli
4+
5+
die() {
6+
test ${#} -ne 0 && echo "${@}"
7+
exit 1
8+
}
9+
10+
check_is_set() {
11+
test -n "${2}" && return
12+
die "${1} is not set"
13+
}
14+
15+
check_is_set OPENRESTY_PREFIX "${OPENRESTY_PREFIX}"
16+
check_is_set TINYSTASH_DIR "${TINYSTASH_DIR}"
17+
test ${#} -ge 1 || die "usage: ${0} COMMAND [ARGS]"
18+
19+
COMMANDS_DIR="${COMMANDS_DIR:-"${TINYSTASH_DIR}/commands"}"
20+
command_name=${1}
21+
shift
22+
23+
nginx_root=$(mktemp -d)
24+
25+
cleanup() {
26+
rm -rf "${nginx_root}"
27+
trap - EXIT
28+
}
29+
30+
trap cleanup EXIT INT QUIT TERM HUP
31+
32+
mkdir "${nginx_root}/logs"
33+
nginx_conf="${nginx_root}/nginx.conf"
34+
35+
make_argv() {
36+
echo "local argv = {"
37+
while [ ${#} -ne 0 ]
38+
do
39+
echo " [===[${1}]===],"
40+
shift
41+
done
42+
echo " }"
43+
}
44+
45+
cat > "${nginx_conf}" << __EOF__
46+
daemon off;
47+
master_process off;
48+
worker_processes 1;
49+
pid logs/nginx.pid;
50+
env TINYSTASH_CONFIG_PATH;
51+
error_log stderr warn;
52+
events {
53+
worker_connections 64;
54+
}
55+
http {
56+
access_log off;
57+
resolver 8.8.8.8 ipv6=off;
58+
lua_package_path "${TINYSTASH_DIR}/?.lua;${TINYSTASH_DIR}/resty_modules/lualib/?.lua;;";
59+
lua_package_cpath "${TINYSTASH_DIR}/resty_modules/lualib/?.so;;";
60+
init_worker_by_lua_block {
61+
_G.OPENRESTY_PREFIX = [===[${OPENRESTY_PREFIX}]===]
62+
local TINYSTASH_DIR = [===[${TINYSTASH_DIR}]===]
63+
_G.TINYSTASH_DIR = TINYSTASH_DIR
64+
_G.print = function(...)
65+
io.stdout:write(table.concat({...}))
66+
io.stdout:write('\n')
67+
io.stdout:flush()
68+
end
69+
local orig_error = error
70+
_G.error = function(msg, lvl)
71+
orig_error(msg, lvl or 0)
72+
end
73+
_G.run_command = function(command_name, argv)
74+
local chunk, err = loadfile([===[${COMMANDS_DIR}]===] .. '/' .. command_name .. '.lua')
75+
if not chunk then
76+
return false, err
77+
end
78+
local ok, err = pcall(chunk, argv)
79+
if not ok then
80+
return false, err
81+
end
82+
return true
83+
end
84+
require('ngx.process').signal_graceful_exit()
85+
ngx.timer.at(0, function()
86+
local command_name = [===[${command_name}]===]
87+
$(make_argv "${@}")
88+
local ok, err = run_command(command_name, argv)
89+
if not ok then
90+
print(err)
91+
os.exit(1)
92+
end
93+
end)
94+
}
95+
}
96+
__EOF__
97+
98+
"${OPENRESTY_PREFIX}/nginx/sbin/nginx" -c "${nginx_conf}" -p "${nginx_root}"

scripts/conf.lua renamed to commands/conf.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ local compile_template = require('resty.template').compile
22

33
local config = require('app.config').nginx_conf
44

5+
6+
assert(OPENRESTY_PREFIX and OPENRESTY_PREFIX ~= '', 'OPENRESTY_PREFIX is not set')
7+
58
local NUMBER = type(1)
69
local STRING = type('')
710
local BOOLEAN = type(true)
@@ -26,7 +29,7 @@ local option = function(params)
2629
return value
2730
end
2831

29-
local fd = assert(io.open(_G.TINYSTASH_DIR .. '/nginx.conf.tpl', 'r'))
32+
local fd = assert(io.open(TINYSTASH_DIR .. '/nginx.conf.tpl', 'r'))
3033
local template = assert(fd:read('*a'))
3134
fd:close()
3235

scripts/webhook.lua renamed to commands/webhook.lua

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,8 @@ local secret = config._processed.tg_webhook_secret
99
local assume_yes = false
1010
local verbose = false
1111

12-
local die = function(...)
13-
print(...)
14-
os.exit(1)
15-
end
16-
1712
if not token then
18-
die('Bad config: set tg.token')
13+
error('Bad config: set tg.token')
1914
end
2015

2116
local verbose_print = function(...)
@@ -41,20 +36,21 @@ Options:
4136
- 'webhook_secret' (if any)
4237
- 'token' if 'webhook_secret' not set
4338
]])
44-
os.exit(0)
4539
end
4640

41+
local argv = ...
4742
local arguments = {}
48-
for _, argument in ipairs(arg) do
43+
for _, argument in ipairs(argv) do
4944
if argument:sub(1, 1) == '-' then
5045
if argument == '-h' then
5146
show_help()
47+
return
5248
elseif argument == '-y' then
5349
assume_yes = true
5450
elseif argument == '-v' then
5551
verbose = true
5652
else
57-
die('Invalid option: ', argument)
53+
error('Invalid option: ', argument)
5854
end
5955
else
6056
table.insert(arguments, argument)
@@ -68,15 +64,15 @@ local ask = function()
6864
io.stdout:write('Are you sure (yes/no)? ')
6965
io.stdout:flush()
7066
if io.stdin:read() ~= 'yes' then
71-
die('Abort')
67+
error('Abort')
7268
end
7369
end
7470

7571
local api_call = function(method, params)
7672
local httpc, res, err
7773
httpc, err = http.new()
7874
if not httpc then
79-
die('TG API request error: ', err)
75+
error('TG API request error: ', err)
8076
end
8177
httpc:set_timeout(10000)
8278
local uri = ('https://api.telegram.org/bot%s/%s'):format(token, method)
@@ -85,16 +81,16 @@ local api_call = function(method, params)
8581
ssl_verify = false,
8682
})
8783
if not res then
88-
die('TG API request error: ', err)
84+
error('TG API request error: ', err)
8985
end
9086
verbose_print('TG API response status: ', res.status)
9187
verbose_print('TG API response body: ', res.body)
9288
res, err = json.decode(res.body)
9389
if not res then
94-
die('TG API response json decode error: ', err)
90+
error('TG API response json decode error: ', err)
9591
end
9692
if not res.ok then
97-
die('TG API response error: ', res.description)
93+
error('TG API response error: ', res.description)
9894
end
9995
print('OK')
10096
if res.description then

nginx.conf.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ http {
4141
location /static/ {
4242
alias ./static/;
4343
try_files $uri =404;
44-
include /usr/local/openresty/nginx/conf/mime.types;
44+
include {* OPENRESTY_PREFIX *}/nginx/conf/mime.types;
4545
}
4646

4747
location = /favicon.ico {

scripts/resty-runner.sh

Lines changed: 0 additions & 74 deletions
This file was deleted.

tinysta.sh

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
#!/bin/sh
22

3-
tinystash_dir=$(dirname "$(readlink -f "${0}")")
4-
nginx_conf="${tinystash_dir}/nginx.conf"
5-
scripts_dir="${tinystash_dir}/scripts"
6-
resty_runner="${scripts_dir}/resty-runner.sh"
7-
8-
TINYSTASH_CONFIG_PATH=$(readlink -f "${TINYSTASH_CONFIG_PATH:-"${tinystash_dir}/config.lua"}")
3+
OPENRESTY_PREFIX=${OPENRESTY_PREFIX:-/usr/local/openresty}
4+
export OPENRESTY_PREFIX
5+
TINYSTASH_DIR=$(dirname "$(readlink -f "${0}")")
6+
export TINYSTASH_DIR
7+
TINYSTASH_CONFIG_PATH=$(readlink -f "${TINYSTASH_CONFIG_PATH:-"${TINYSTASH_DIR}/config.lua"}")
98
export TINYSTASH_CONFIG_PATH
109

10+
command_runner="${TINYSTASH_DIR}/commands/command-runner.sh"
11+
nginx_conf="${TINYSTASH_DIR}/nginx.conf"
12+
1113
usage() {
1214
echo "
13-
usage: ${0} COMMAND [COMMAND_ARGS ...]
15+
usage: ${0} COMMAND [ARGS]
1416
1517
commands:
1618
run
@@ -31,17 +33,18 @@ case "${command}" in
3133
conf|webhook)
3234
echo "using config: ${TINYSTASH_CONFIG_PATH}"
3335
echo
34-
exec "${resty_runner}" "${command}" "${@}"
36+
exec "${command_runner}" "${command}" "${@}"
3537
;;
3638
run)
3739
echo "using config: ${TINYSTASH_CONFIG_PATH}"
38-
if ! nginx_conf_content=$("${resty_runner}" conf); then
40+
echo
41+
if ! nginx_conf_content=$("${command_runner}" conf); then
3942
echo "error while generating nginx.conf:"
4043
echo "${nginx_conf_content}"
4144
exit 2
4245
fi
4346
echo "${nginx_conf_content}" > "${nginx_conf}"
44-
exec /usr/local/openresty/nginx/sbin/nginx -c "${nginx_conf}" -p "${tinystash_dir}"
47+
exec "${OPENRESTY_PREFIX}/nginx/sbin/nginx" -c "${nginx_conf}" -p "${TINYSTASH_DIR}"
4548
;;
4649
*)
4750
usage

0 commit comments

Comments
 (0)