-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtarantoolctl.lua
More file actions
executable file
·181 lines (137 loc) · 4.02 KB
/
tarantoolctl.lua
File metadata and controls
executable file
·181 lines (137 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!./1.9/tarantool
--#!/usr/bin/env tarantool
local fio = require 'fio'
local log = require 'log'
local errno = require 'errno'
local yaml = require 'yaml'
local console = require 'console'
local socket = require 'socket'
local ffi = require 'ffi'
local os = require 'os'
local fiber = require 'fiber'
local digest = require 'digest'
local urilib = require 'uri'
ffi.cdef[[ int kill(int pid, int sig); ]]
--start_script_path = fio.pathjoin(instance_dir, start_script_name)
local function directory_exists( sPath )
local ok, err, code = os.rename(sPath, sPath)
if not ok then
if code == 13 then
-- Permission denied, but it exists
return true
end
end
return ok, err
end
local function getData(instance)
local t = dofile('./' .. instance .. '/conf.lua')
return t
end
function start(instance)
log.info("Starting " .. instance .." instance...")
local local_conf = getData(instance)
local conf = {
pid_file = './' .. instance .. '/tarantool.pid',
--logger = './' .. instance .. '/tarantool.log',
log = './' .. instance .. '/log',
wal_dir = './' .. instance,
--snap_dir = './' .. instance,
memtx_dir = './' .. instance,
vinyl_dir = './' .. instance,
background = true
}
for k,v in pairs(local_conf.box) do conf[k] = v end
box.cfg(conf)
-- Start console
if local_conf.console_url then
local console = require('console')
console.listen(local_conf.console_url)
end
local success, data = pcall(dofile, './funs.lua')
-- if load fails - show last 10 lines of the log file
if not success then
print('Start failed: ' .. data)
if fio.stat(box.cfg.logger) then
os.execute('tail -n 10 ' .. box.cfg.logger)
end
else
success1, data1 = pcall(dofile, './start.lua')
success2, data2 = pcall(dofile, './' .. instance .. '/start.lua')
-- if load fails - show last 10 lines of the log file
if not success1 or not success2 then
print('Start failed: ' .. data1)
print('Start failed: ' .. data2)
if fio.stat(box.cfg.logger) then
os.execute('tail -n 10 ' .. box.cfg.logger)
end
end
end
end
function stop(instance)
log.info("Stopping " .. instance .." instance...")
local pid_file = './' .. instance .. '/tarantool.pid'
if fio.stat(pid_file) == nil then
log.error("Process is not running (pid: %s)", pid_file)
return 0
end
local f = fio.open(pid_file, 'O_RDONLY')
if f == nil then
log.error("Can't read pid file %s: %s", pid_file, errno.strerror())
return -1
end
local str = f:read(64)
f:close()
local pid = tonumber(str)
if pid == nil or pid <= 0 then
log.error("Broken pid file %s", pid_file)
fio.unlink(pid_file)
return -1
end
if ffi.C.kill(pid, 15) < 0 then
log.error("Can't kill process %d: %s", pid, errno.strerror())
fio.unlink(pid_file)
return -1
end
return 0
end
function init(instance)
log.info("Init " .. instance .." instance...")
if directory_exists(instance) then
log.info("Node already exists")
return -1
else
os.execute('cp -R template ' .. instance)
log.info("Init " .. instance .." complete. Please edit " .. instance .. "/conf.lua")
end
return 0
end
local available_commands = {
'start',
'stop',
'restart',
'init'
}
local function usage()
print("Usage: ./tarantoolctl.lua", table.concat(available_commands, '|'), 'instance')
os.exit(1)
end
local function check_cmd(cmd)
for _, vcmd in pairs(available_commands) do
if cmd == vcmd then
return cmd
end
end
usage()
end
local cmd = check_cmd(arg[1])
if cmd == 'start' then
start(arg[2])
elseif cmd == 'stop' then
os.exit(stop(arg[2]))
elseif cmd == 'restart' then
stop(arg[2])
fiber.sleep(1)
start(arg[2])
elseif cmd == 'init' then
init(arg[2])
end