-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtusker.lua
More file actions
343 lines (288 loc) · 8.4 KB
/
tusker.lua
File metadata and controls
343 lines (288 loc) · 8.4 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#! /usr/bin/env lua
local colors = require('ansicolors')
local json = require('cjson')
local os = require('os')
local lfs = require('lfs')
local config = require('src.config')
local field = arg[1] or 'all' -- values: ngx, tnt, all
local action = arg[2] or 'restart' -- values: start, stop, restart, recreate
local key = arg[3] or 'dev' -- values: dev or prod
local tarantool_in_console = false
---------- Helpers --------------
local function say(text)
return print(colors(text))
end
local function file_exists(name)
local f=io.open(name,"r")
if f~=nil then io.close(f) return true else return false end
end
local function cmd_capture(cmd, raw)
local f = assert(io.popen(cmd, 'r'))
local s = assert(f:read('*a'))
f:close()
if raw then return s end
s = string.gsub(s, '^%s+', '')
s = string.gsub(s, '%s+$', '')
s = string.gsub(s, '[\n\r]+', ' ')
return s
end
-------- Basic commands -------------
local command = {}
command.tnt = {}
function command.tnt.start()
say("Tarantool is starting...")
os.execute('tarantool ./src/app.lua server start '..key..' '..tostring(tarantool_in_console))
if not file_exists(config.tarantool.pidfile) then
say('%{red}[FAIL]%{reset} Tarantool server have not created pid file! May be crashed. See ./planner.lua tnt log')
return nil
end
-- local plist = cmd_capture('ps aux | grep `cat '..config.tarantool.pidfile..'`')
os.execute('sleep 2')
if file_exists(config.tarantool.pidfile) then
say('%{green}[OK]%{green}')
else
say('%{red}[FAIL]%{reset} Tarantool server have not been started! See logs.')
return nil
end
end
function command.tnt.stop()
say("Tarantool is stopping...")
if(file_exists(config.tarantool.pidfile)) then
cmd_capture('kill -9 `cat '..config.tarantool.pidfile..'`')
os.remove(config.tarantool.pidfile)
else
say("%{red}[FAIL]%{reset} Cannot find pid file of tarantool process!")
end
end
function command.tnt.drop()
say("Drop Tarantool data...")
cmd_capture('rm '..config.tarantool.data_dir..'/*')
cmd_capture('rm '..config.tarantool.logfile)
end
function command.tnt.log()
if(file_exists(config.tarantool.logfile)) then
say(cmd_capture('tail -n 100 ./'..config.tarantool.logfile, true))
else
say("%{red}[FAIL]%{reset} Tnt log file is not found!")
end
end
function command.tnt.repl()
os.execute('tarantoolctl connect '..config.tarantool.user..':'..
config.tarantool.password..'@'..config.tarantool.host..':'..
config.tarantool.port..'')
end
command.migration = {}
function command.migration.make()
if(file_exists(config.tarantool.pidfile)) then
say("%{red}[FAIL]%{reset} Tarantool should be stopped before migrations.")
return false
end
os.execute('tarantool ./src/app.lua migration make '..key .. ' true')
end
function command.migration.up()
if(file_exists(config.tarantool.pidfile)) then
say("%{red}[FAIL]%{reset} Tarantool should be stopped before migrations.")
return false
end
os.execute('tarantool ./src/app.lua migration up '..key .. ' true')
end
function command.migration.uptop()
if(file_exists(config.tarantool.pidfile)) then
say("%{red}[FAIL]%{reset} Tarantool should be stopped before migrations.")
return false
end
os.execute('tarantool ./src/app.lua migration uptop '..key .. ' true')
end
function command.migration.down()
if(file_exists(config.tarantool.pidfile)) then
say("%{red}[FAIL]%{reset} Tarantool should be stopped before migrations.")
return false
end
os.execute('tarantool ./src/app.lua migration down '..key .. ' true')
end
function command.migration.clean()
os.execute('rm '..config.tarantool.data_dir..'/*')
os.execute('rm -rf ./src/migrations')
end
command.ngx = {}
function command.ngx.start()
say("Nginx is starting...")
local template_path = './nginx.conf.template'
local complied_path = './nginx.conf.compiled'
if(not file_exists(template_path)) then
say("%{red}[FAIL]%{reset} Cannot open template in path "..template_path)
return nil
end
local f = io.open(template_path, 'rb')
local template_body = f:read('*a')
f:close()
local options = {
SSL_CRT_PATH = lfs.currentdir()..'/'..config.ssl.crt_path,
SSL_KEY_PATH = lfs.currentdir()..'/'..config.ssl.key_path,
ROOT = lfs.currentdir()..'/',
NGINX_PORT = config.nginx.port,
TNT_HTTP_PORT = config.webserver.port,
SERVER_TITLE = config.nginx.title,
SERVER_NAME = config.nginx.name,
}
for i, v in pairs(options) do
template_body = template_body:gsub('{{'..i..'}}', v)
end
f = io.open(complied_path, 'w')
f:write(template_body)
f:close()
cmd_capture('sudo cp '..complied_path..' /etc/nginx/sites-enabled/'..config.nginx.conf_name)
cmd_capture('sleep 1')
cmd_capture('sudo nginx')
local url = "https://"..config.nginx.name
if(config.nginx.port ~= 443 and config.nginx.port ~= 80) then
url = url..":"..config.nginx.port
end
url = url..'/'
say("Open "..url)
end
function command.ngx.stop()
say "Nginx is stopping..."
cmd_capture('sudo nginx -s stop')
end
function command.ngx.log()
say(cmd_capture('tail -n 100 /var/log/nginx/error.log', true))
end
function command.ngx.drop()
say("Drop Nginx config at /etc/nginx/sites-enabled/"..config.nginx.conf_name.."...")
cmd_capture('sudo rm /etc/nginx/sites-enabled/'..config.nginx.conf_name)
end
--- Configuration ---
local actions = {
tnt = {
start = {
{'tnt', 'start'},
},
stop = {
{'tnt', 'stop'},
},
drop = {
{'tnt', 'stop'},
{'tnt', 'drop'},
},
restart = {
{'tnt', 'stop'},
{'tnt', 'start'},
},
recreate = {
{'tnt', 'stop'},
{'tnt', 'drop'},
{'migration', 'clean'},
{'migration', 'make'},
{'migration', 'uptop'},
{'tnt', 'start'},
},
log = {
{'tnt', 'log'},
},
repl = {
{'tnt', 'repl'},
},
},
ngx = {
start = {
{'ngx', 'start'},
},
stop = {
{'ngx', 'stop'},
},
drop = {
{'ngx', 'stop'},
{'ngx', 'drop'},
},
restart = {
{'ngx', 'stop'},
{'ngx', 'start'},
},
log = {
{'ngx', 'log'},
},
},
all = {
start = {
{'ngx', 'start'},
{'tnt', 'start'},
},
stop = {
{'tnt', 'stop'},
{'ngx', 'stop'},
},
restart = {
{'ngx', 'stop'},
{'tnt', 'stop'},
{'ngx', 'start'},
{'tnt', 'start'},
},
recreate = {
{'ngx', 'stop'},
{'tnt', 'stop'},
{'tnt', 'drop'},
{'ngx', 'start'},
{'tnt', 'start'},
},
},
migration = {
make = {
{'migration', 'make'},
},
up = {
{'migration', 'up'},
},
down = {
{'migration', 'down'},
},
clean = {
{'tnt', 'stop'},
{'migration', 'clean'},
},
}
}
------------ Executing --------------
if(key=='d') then
key = 'dev'
tarantool_in_console = true
end
if(config[key]) then
config = config[key]
else
say('%{red}[FAIL]%{reset} Cannot find config by key ' .. json.encode(key))
return false
end
local function main(_commands, _actions, _field, _action)
local keys
if not _actions[_field] then
keys = {}
for i, _ in pairs(_actions) do
table.insert(keys, i)
end
say("%{red}[FAIL]%{reset} Unknown field. List of possible fields: "..table.concat(keys, ', '))
return nil
end
if type(_actions[_field][_action]) ~= 'table' then
keys = {}
for i, _ in pairs(_actions[_field]) do
table.insert(keys, i)
end
say("%{red}[FAIL]%{reset} Unknown action. List of possible actions in ".._field..": "..table.concat(keys, ', '))
return nil
end
for _, v in pairs(_actions[_field][_action]) do
say("%{bright}[~]%{reset} "..v[1].."#"..v[2])
if not v[1] or not v[2] then
say("%{red}[FAIL]%{reset} Invalid format in command ".._field.."#".._action)
return nil
end
if not _commands[v[1]] or type(_commands[v[1]][v[2]])~='function' then
say("%{red}[FAIL]%{reset} Unknown command in ".._field.."#".._action)
return nil
end
_commands[v[1]][v[2]]()
end
end
--- Entry point ---
main(command, actions, field, action)