diff --git a/extra/dist/default/tarantool b/extra/dist/default/tarantool index b840cda22cfa56a021f1887a464366806d7a0703..81faf68920a1a7d6b61794b59c2ed71c736a532c 100644 --- a/extra/dist/default/tarantool +++ b/extra/dist/default/tarantool @@ -1,10 +1,12 @@ -# Options for Tarantool +-- Options for Tarantool +default_cfg = { + pid_file = "/var/run/tarantool", -- will become pid_file .. instance .. '.pid' + wal_dir = "/var/lib/tarantool", -- will become wal_dir/instance/ + snap_dir = "/var/lib/tarantool", -- snap_dir/instance/ + logger = "/var/log/tarantool", -- logger/instance .. '.log' + username = "tarantool", +} -SNAPS=/var/lib/tarantool -XLOGS=/var/lib/tarantool -LOGS=/var/log/tarantool -PIDS=/var/run/tarantool -USERNAME=tarantool -INSTDIR=/etc/tarantool/instances.enabled +instance_dir = "/etc/tarantool/instances.enabled" -# vim: set ft=sh : +-- vim: set ft=lua : diff --git a/extra/dist/dist.lua b/extra/dist/dist.lua index b07dbd3ae605a78f785030b93a75b431e7408b5e..94ecdcf9a01ef3e1fd389f0748effff9f363a47a 100755 --- a/extra/dist/dist.lua +++ b/extra/dist/dist.lua @@ -1,4 +1,4 @@ -#!env tarantool +#!/usr/bin/env tarantool local fio = require 'fio' local log = require 'log' @@ -7,160 +7,125 @@ local yaml = require 'yaml' local console = require 'console' local socket = require 'socket' local ffi = require 'ffi' +local os = require 'os' ffi.cdef[[ int kill(int pid, int sig); ]] -local DEFAULTS = '/etc/sysconfig/tarantool' -local DEFAULT_CFG = { - PIDS = '/var/pid/tarantool', - SNAPS = '/var/lib/tarantool', - XLOGS = '/var/lib/tarantool', - LOGS = '/var/log/tarantool', - USERNAME = 'tarantool', - INSTDIR = '/etc/tarantool/instances.enabled', -} - -if fio.stat(DEFAULTS) == nil then - DEFAULTS = '/etc/default/tarantool' +if arg[1] == nil or arg[2] == nil then + log.error("Usage: dist.lua {start|stop|logrotate} instance") + os.exit(-1) end -function read_cfg(name) - if name == nil then - return {} - end - local stat = fio.stat(name) - if stat == nil then - log.error("Can't stat file %s: %s", name, errno.strerror()) - return {} - end - - local f = fio.open(name, 'O_RDONLY') - if f == nil then - log.error("Can't open file %s: %s", name, errno.strerror()) - return {} - end +local cmd = arg[1] +local instance = fio.basename(arg[2], '.lua') - local data = f:read(32768) - if data == nil then - log.error("Can't read file %s: %s", name, errno.strerror()) - f:close() - return {} +-- shift argv to remove 'tarantoolctl' from arg[0] +for i = 0, 128 do + arg[i] = arg[i + 2] + if arg[i] == nil then + break end - f:close() - - local result = {} - repeat - local line - if string.match(data, "\n") == nil then - line = data - data = '' - else - line = string.match(data, "^(.-)\n") - data = string.sub(data, #line + 1 + 1) - end - - - local name, value = string.match(line, "^%s*(.-)%s*=%s*(.-)%s*$") - - if name ~= nil and value ~= nil and #name > 0 and #value > 0 then - if string.match(value, '^".*"$') ~= nil then - value = string.sub(value, 2, #value - 2) - elseif string.match(value, "^'.*'$") ~= nil then - value = string.sub(value, 2, #value - 2) - end - - if string.match(name, '^%s*#') == nil then - result[name] = value - end - end +end - until #data == 0 +if fio.stat('/etc/sysconfig/tarantool') then + dofile('/etc/sysconfig/tarantool') +elseif fio.stat('/etc/default/tarantool') then + dofile('/etc/default/tarantool') +end - return result +if default_cfg == nil then + default_cfg = {} end -if arg[1] == nil or arg[2] == nil then - log.error("Usage: dist.lua {start|stop|logrotate} instance") - os.exit(-1) +if instance_dir == nil then + instance_dir = '/etc/tarantool/instances.enabled' end +default_cfg.pid_file = default_cfg.pid_file and default_cfg.pid_file or "/var/run/tarantool" +default_cfg.wal_dir = default_cfg.wal_dir and default_cfg.wal_dir or "/var/lib/tarantool" +default_cfg.snap_dir = default_cfg.snap_dir and default_cfg.snap_dir or "/var/lib/tarantool" +default_cfg.logger = default_cfg.logger and default_cfg.logger or "/var/log/tarantool" +default_cfg.username = default_cfg.username and default_cfg.username or "tarantool" -local cfg = read_cfg(DEFAULTS) -for i, v in pairs(DEFAULT_CFG) do - if cfg[i] == nil then - cfg[i] = v - end -end +-- create a path to the control socket (admin console) +local console_sock = fio.pathjoin(default_cfg.pid_file, instance .. '.control') + +default_cfg.pid_file = fio.pathjoin(default_cfg.pid_file, instance .. '.pid') +default_cfg.wal_dir = fio.pathjoin(default_cfg.wal_dir, instance) +default_cfg.snap_dir = fio.pathjoin(default_cfg.snap_dir, instance) +default_cfg.logger = fio.pathjoin(default_cfg.logger, instance .. '.log') + +local instance_lua = fio.pathjoin(instance_dir, instance .. '.lua') local function mkdir(dirname) log.info("mkdir %s", dirname) - if not fio.mkdir(dirname, 0x1C0) then + if not fio.mkdir(dirname, tonumber('0755', 8)) then log.error("Can't mkdir %s: %s", dirname, errno.strerror()) os.exit(-1) end - if not fio.chown(dirname, cfg.USERNAME, cfg.USERNAME) then + if not fio.chown(dirname, default_cfg.username, default_cfg.username) then log.error("Can't chown(%s, %s, %s): %s", - cfg.USERNAME, cfg.USERNAME, dirname, errno.strerror()) + default_cfg.username, default_cfg.username, dirname, errno.strerror()) end end -local cmd = arg[1] -local instance = fio.basename(arg[2], '.lua') -local main_lua = fio.pathjoin(cfg.INSTDIR, instance .. '.lua') -for i = 0, 128 do - arg[i] = arg[i + 2] - if arg[i] == nil then - break +function mk_default_dirs(cfg) + -- create pid_dir + pid_dir = fio.dirname(cfg.pid_file) + if fio.stat(pid_dir) == nil then + mkdir(pid_dir) end -end -local force_cfg_console = fio.pathjoin(cfg.PIDS, instance .. '.control') + -- create wal_dir + if fio.stat(cfg.wal_dir) == nil then + mkdir(cfg.wal_dir) + end + + -- create snap_dir + if fio.stat(cfg.snap_dir) == nil then + mkdir(cfg.snap_dir) + end + -- create log_dir + log_dir = fio.dirname(cfg.logger) + if fio.stat(log_dir) == nil then + mkdir(log_dir) + end +end local force_cfg = { - pid_file = fio.pathjoin(cfg.PIDS, instance .. '.pid'), - wal_dir = fio.pathjoin(cfg.XLOGS, instance), - work_dir = fio.pathjoin(cfg.XLOGS, instance), - snap_dir = fio.pathjoin(cfg.SNAPS, instance), - username = cfg.USERNAME, - logger = fio.pathjoin(cfg.LOGS, instance .. '.log'), + pid_file = default_cfg.pid_file, + username = default_cfg.username, background = true, custom_proc_title = instance } - - local orig_cfg = box.cfg -box.cfg = function(cfg) +wrapper_cfg = function(cfg) + for i, v in pairs(force_cfg) do cfg[i] = v end + + for i, v in pairs(default_cfg) do + if cfg[i] == nil then + cfg[i] = v + end + end + + mk_default_dirs(cfg) local res = orig_cfg(cfg) require('fiber').name(instance) - log.info('Run console at %s', force_cfg_console) - console.listen(force_cfg_console) + log.info('Run console at %s', console_sock) + console.listen(console_sock) return res end - if cmd == 'start' then - -- create PIDDIR - if fio.stat(cfg.PIDS) == nil then - mkdir(cfg.PIDS) - end - - -- create xlogdir - if fio.stat(force_cfg.wal_dir) == nil then - mkdir(force_cfg.wal_dir) - end - - -- create snapdir - if fio.stat(force_cfg.snap_dir) == nil then - mkdir(force_cfg.snap_dir) - end - dofile(main_lua) + box.cfg = wrapper_cfg + dofile(instance_lua) elseif cmd == 'stop' then if fio.stat(force_cfg.pid_file) == nil then @@ -192,12 +157,12 @@ elseif cmd == 'stop' then os.exit(-1) elseif cmd == 'logrotate' then - if fio.stat(force_cfg.console) == nil then + if fio.stat(console_sock) == nil then -- process is not running, do nothing os.exit(0) end - local s = socket.tcp_connect('unix/', force_cfg.console) + local s = socket.tcp_connect('unix/', console_sock) if s == nil then -- socket is not opened, do nothing os.exit(0) diff --git a/src/lua/fio.lua b/src/lua/fio.lua index b0b6d3a7280cd23a4f1f54ae75067a679d702793..48687703bdfb83e2fc42e2debde74c4815949ab9 100644 --- a/src/lua/fio.lua +++ b/src/lua/fio.lua @@ -204,6 +204,25 @@ fio.basename = function(path, suffix) return path end +fio.dirname = function(path) + if path == nil then + return nil + end + path = tostring(path) + + while true do + if path == "" or string.sub(path, -1) == "/" then + break + end + path = string.sub(path, 1, -2) + end + if path == "" then + path = "." + end + + return path +end + fio.umask = function(umask) if umask == nil then diff --git a/src/say.cc b/src/say.cc index aa46db152a0242bc8bae8b84806bda9dbb555e53..974a55d973f1ecd275952576c48877cf759a6680 100644 --- a/src/say.cc +++ b/src/say.cc @@ -159,6 +159,7 @@ say_init_pipe() sigprocmask(SIG_UNBLOCK, &mask, NULL); close(pipefd[0]); log_fd = pipefd[1]; + say_info("started logging into a pipe, SIGHUP log rotation disabled"); return; error: say_syserror("Can't start logger: %s", log_path); @@ -187,6 +188,7 @@ say_logrotate(int /* signo */) int flags = fcntl(log_fd, F_GETFL, 0); fcntl(log_fd, F_SETFL, flags | O_NONBLOCK); } + say_info("log file has been reopened"); } /**