forked from tarantool/cartridge-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cartridge-cli.lua
3158 lines (2647 loc) · 96 KB
/
cartridge-cli.lua
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local argparse = require('internal.argparse').parse
local digest = require('digest')
local errno = require('errno')
local ffi = require('ffi')
local fiber = require('fiber')
local fio = require('fio')
local fun = require('fun')
local log = require('log')
local socket = require('socket')
local yaml = require('yaml')
local self_name = fio.basename(arg[0])
local function VERSION()
if package.search('cartridge-cli.VERSION') then
return require('cartridge-cli.VERSION')
end
return 'unknown'
end
-- * ---------------- Utility functions ----------------
-- box.NULL, custom and cdata errors aware assert
function assert(val, message, ...) -- luacheck: no global
if not val or val == nil then
error(tostring(message), 2)
end
return val, message, ...
end
local function get_cartridgecli_dir()
local str = debug.getinfo(1, "S").source:sub(2)
return str:match("(.*/)") or '.'
end
local function get_tarantool_dir()
return fio.abspath(fio.dirname(arg[-1]))
end
local function get_template_dir()
local template_dir = fio.pathjoin(get_cartridgecli_dir(), 'templates')
if fio.path.exists(template_dir) then
return template_dir
end
error('Templates not found neither in base dir nor in .rocks')
end
local function array_contains(array, value)
if not array then
return false
end
for _, v in ipairs(array) do
if v == value then
return true
end
end
return false
end
local function array_index_of(array, value)
for i, v in ipairs(array) do
if v == value then
return i
end
end
end
local function dict_keys(dict)
local keys = {}
for key, _ in pairs(dict) do
table.insert(keys, key)
end
return keys
end
local function array_slice(array, from, to)
local result = {}
if from == nil then
from = 0
end
if to == nil then
to = #array
end
for i = from,to do
table.insert(result, array[i])
end
return result
end
local function align(addr, bytes)
return bit.band(addr + (bytes - 1), -bytes)
end
-- Pad the buffer with zeros so that its size is a multiple of 8 bytes
local function buf_pad_to_8_byte_boundary(buf)
return buf .. string.rep('\0', align(#buf, 8) - #buf)
end
local function remove_leading_dot(filename)
if string.startswith(filename, '.') then
return string.sub(filename, 2)
end
return filename
end
-- Returns a list of relative paths to files in directory `dir`
local function find_files(dir, options)
options = options or {}
local exclude = options.exclude or {}
local function find_files_rec(base_dir, subdir)
subdir = subdir or '.'
local files = fio.listdir(fio.pathjoin(base_dir, subdir))
table.sort(files)
local res = {}
for _, file in ipairs(files) do
local fullpath = fio.pathjoin(base_dir, subdir, file)
if not array_contains(exclude, file) then
if fio.path.is_dir(fullpath) then
if options.include_dirs then
table.insert(res, fio.pathjoin(subdir, file))
end
local subres = find_files_rec(base_dir, fio.pathjoin(subdir, file))
for _,v in pairs(subres) do table.insert(res, v) end
elseif fio.path.is_file(fullpath) then
table.insert(res, fio.pathjoin(subdir, file))
end
end
end
return res
end
local res = find_files_rec(dir, nil)
table.sort(res)
return res
end
local function globtopattern(g)
-- glob pattern to lua format
-- source: https://github.com/davidm/lua-glob-pattern
local p = "^" -- pattern being built
local i = 0 -- index in g
local c -- char at index i in g.
-- unescape glob char
local function unescape()
if c == '\\' then
i = i + 1; c = g:sub(i,i)
if c == '' then
p = '[^]'
return false
end
end
return true
end
-- escape pattern char
local function escape(str)
return str:match("^%w$") and str or '%' .. str
end
-- Convert tokens at end of charset.
local function charset_end()
while 1 do
if c == '' then
p = '[^]'
return false
elseif c == ']' then
p = p .. ']'
break
else
if not unescape() then break end
local c1 = c
i = i + 1; c = g:sub(i,i)
if c == '' then
p = '[^]'
return false
elseif c == '-' then
i = i + 1; c = g:sub(i,i)
if c == '' then
p = '[^]'
return false
elseif c == ']' then
p = p .. escape(c1) .. '%-]'
break
else
if not unescape() then break end
p = p .. escape(c1) .. '-' .. escape(c)
end
elseif c == ']' then
p = p .. escape(c1) .. ']'
break
else
p = p .. escape(c1)
i = i - 1 -- put back
end
end
i = i + 1; c = g:sub(i,i)
end
return true
end
-- Convert tokens in charset.
local function charset()
i = i + 1; c = g:sub(i,i)
if c == '' or c == ']' then
p = '[^]'
return false
elseif c == '^' or c == '!' then
i = i + 1; c = g:sub(i,i)
if c ~= ']' then
p = p .. '[^'
if not charset_end() then return false end
end
else
p = p .. '['
if not charset_end() then return false end
end
return true
end
-- Convert tokens.
while 1 do
i = i + 1; c = g:sub(i,i)
if c == '' then
p = p .. '$'
break
elseif c == '?' then
p = p .. '.'
elseif c == '*' then
-- if double asterisk
if i + 1 <= #g and g:sub(i + 1, i + 1) == '*' then
p = p .. '.*'
else
p = p .. '[^/]*'
end
elseif c == '[' then
if not charset() then break end
elseif c == '\\' then
i = i + 1; c = g:sub(i,i)
if c == '' then
p = p .. '\\$'
break
end
p = p .. escape(c)
else
p = p .. escape(c)
end
end
return p
end
-- * --------------------------- Color helpers ---------------------------
local RESET_TERM = '\x1B[0m'
local COLORS = {
{'magenta', '\x1B[35m'},
{'blue', '\x1B[34m'},
{'cyan', '\x1B[36m'},
{'green', '\x1B[32m'},
{'bright_magenta', '\x1B[95m'},
{'bright_cyan', '\x1B[96m'},
{'bright_blue', '\x1B[94m'},
{'bright_green', '\x1B[92m'},
}
local COLORS_ITER = fun.iter(COLORS):map(function(x) return x[2] end):cycle()
local NEXT_COLOR = 0
local function next_color_code()
NEXT_COLOR = NEXT_COLOR + 1
return COLORS_ITER:nth(NEXT_COLOR)
end
local ERROR_COLOR_CODE = '\x1B[31m' -- red
local WARN_COLOR_CODE = '\x1B[33m' -- yellow
local INFO_COLOR_CODE = '\x1B[36m' -- cyan
local DEBUG_COLOR_CODE = '\x1B[35m' -- magneta
-- Map of `log_level_letter => color_code`.
local COLOR_CODE_BY_LOG_LEVEL = fun.iter({
S_FATAL = ERROR_COLOR_CODE,
S_SYSERROR = ERROR_COLOR_CODE,
S_ERROR = ERROR_COLOR_CODE,
S_CRIT = ERROR_COLOR_CODE,
S_WARN = WARN_COLOR_CODE,
S_INFO = RESET_TERM,
S_VERBOSE = RESET_TERM,
S_DEBUG = RESET_TERM,
}):map(function(k, v) return k:sub(3, 3), v end):tomap()
local ERROR_LOG_LINE_PATTERN = ' (%u)> '
local function colored_msg(msg, color_code)
return color_code .. msg .. RESET_TERM
end
-- * ------------------------------ Messages ------------------------------
local function die(fmt, ...)
local msg = "ERROR: " .. string.format(fmt, ...)
print(colored_msg(msg, ERROR_COLOR_CODE))
os.exit(1)
end
local function warn(fmt, ...)
local msg = "WARNING: " .. string.format(fmt, ...)
print(colored_msg(msg, WARN_COLOR_CODE))
end
local function info(fmt, ...) -- luacheck: no unused
local msg = string.format(fmt, ...)
print(colored_msg(msg, INFO_COLOR_CODE))
end
local function debug(fmt, ...) -- luacheck: no unused
local msg = string.format(fmt, ...)
print(colored_msg(msg, DEBUG_COLOR_CODE))
end
local function read_file(path)
local file = fio.open(path)
if file == nil then
die('Failed to open file %s: %s', path, errno.strerror())
end
local buf = {}
while true do
local val = file:read(1024)
if val == nil then
die('Failed to read from file %s: %s', path, errno.strerror())
elseif val == '' then
break
end
table.insert(buf, val)
end
file:close()
return table.concat(buf, '')
end
local function write_file(path, data, mode)
mode = mode or tonumber(644, 8)
local file = fio.open(path, {'O_CREAT', 'O_WRONLY', 'O_TRUNC', 'O_SYNC'}, mode)
if file == nil then
die('Failed to open file %s: %s', path, errno.strerror())
end
local res = file:write(data)
if not res then
die('Failed to write to file %s: %s', path, errno.strerror())
end
file:close()
return data
end
local function file_md5_hex(filename)
local data = read_file(filename)
return digest.md5_hex(data)
end
-- expand() allows to render a text template, expanding ${statement}
-- into the calculated value of that statement.
-- Roughly based on http://lua-users.org/wiki/TextTemplate
--
-- First argument is the template string, then arbitrary number of
-- tables with mappings of variable=value
local function expand(template, ...)
assert(type(template)=='string', 'expecting string')
local searchlist = {...}
local estring,evar
local statements = {'do', 'if', 'for', 'while', 'repeat'}
function estring(str)
local b,e,i
b,i = string.find(str, '%$.')
if not b then return str end
local R, pos = {}, 1
repeat
b,e = string.find(str, '^%b{}', i)
if b then
table.insert(R, string.sub(str, pos, b-2))
table.insert(R, evar(string.sub(str, b+1, e-1)))
i = e+1
pos = i
else
b,e = string.find(str, '^%b()', i)
if b then
table.insert(R, string.sub(str, pos, b-2))
table.insert(R, evar(string.sub(str, b+1, e-1)))
i = e+1
pos = i
elseif string.find(str, '^%a', i) then
table.insert(R, string.sub(str, pos, i-2))
table.insert(R, evar(string.sub(str, i, i)))
i = i+1
pos = i
elseif string.find(str, '^%$', i) then
table.insert(R, string.sub(str, pos, i))
i = i+1
pos = i
end
end
b,i = string.find(str, '%$.', i)
until not b
table.insert(R, string.sub(str, pos))
return table.concat(R)
end
local function search(index)
for _,symt in ipairs(searchlist) do
local ts = type(symt)
local value
if ts == 'function' then value = symt(index)
elseif ts == 'table'
or ts == 'userdata' then value = symt[index]
if type(value)=='function' then value = value(symt) end
else error'search item must be a function, table or userdata' end
if value ~= nil then return value end
end
error('unknown variable: '.. index)
end
local function elist(var, v, str, sep)
local tab = search(v)
if tab then
assert(type(tab)=='table', 'expecting table from: '.. var)
local R = {}
table.insert(searchlist, 1, tab)
table.insert(searchlist, 1, false)
for _,elem in ipairs(tab) do
searchlist[1] = elem
table.insert(R, estring(str))
end
table.remove(searchlist, 1)
table.remove(searchlist, 1)
return table.concat(R, sep)
else
return ''
end
end
local function get(tab,index)
for _,symt in ipairs(searchlist) do
local ts = type(symt)
local value
if ts == 'function' then value = symt(index)
elseif ts == 'table'
or ts == 'userdata' then value = symt[index]
else error'search item must be a function, table or userdata' end
if value ~= nil then
tab[index] = value -- caches value and prevents changing elements
return value
end
end
end
function evar(var)
if string.find(var, '^[_%a][_%w]*$') then -- ${vn}
return estring(tostring(search(var)))
end
local _,e,cmd = string.find(var, '^(%a+)%s.')
if cmd == 'foreach' then -- ${foreach vn xxx} or ${foreach vn/sep/xxx}
local vn,s
_,e,vn,s = string.find(var, '^([_%a][_%w]*)([%s%p]).', e)
if vn then
if string.find(s, '%s') then
return elist(var, vn, string.sub(var, e), '')
end
local b = string.find(var, s, e, true)
if b then
return elist(var, vn, string.sub(var, b+1), string.sub(var,e,b-1))
end
end
error('syntax error in: '.. var, 2)
elseif cmd == 'when' then -- $(when vn xxx)
local vn
_,e,vn = string.find(var, '^([_%a][_%w]*)%s.', e)
if vn then
local t = search(vn)
if not t then
return ''
end
local s = string.sub(var,e)
if type(t)=='table' or type(t)=='userdata' then
table.insert(searchlist, 1, t)
s = estring(s)
table.remove(searchlist, 1)
return s
else
return estring(s)
end
end
error('syntax error in: '.. var, 2)
else
if statements[cmd] then -- do if for while repeat
var = 'local OUT="" '.. var ..' return OUT'
else -- expression
var = 'return '.. var
end
local f = assert(loadstring(var))
local t = searchlist[1]
assert(type(t)=='table' or type(t)=='userdata', 'expecting table')
setfenv(f, setmetatable({}, {__index=get, __newindex=t}))
return estring(tostring(f()))
end
end
return estring(template)
end
-- pack() allows to pack a number of values to a binary string
-- in a printf-like manner
local function pack(format, ...)
local stream = {}
local vars = {...}
local endianness = true
local i = 1
while i <= format:len() do
local opt = format:sub(i, i)
if opt == '<' then
endianness = true
elseif opt == '>' then
endianness = false
elseif opt:find('[bBhHiIlL]') then
local n = opt:find('[hH]') and 2 or opt:find('[iI]') and 4 or opt:find('[lL]') and 8 or 1
local val = tonumber(table.remove(vars, 1))
local bytes = {}
for _ = 1, n do
table.insert(bytes, string.char(val % (2 ^ 8)))
val = math.floor(val / (2 ^ 8))
end
if not endianness then
table.insert(stream, string.reverse(table.concat(bytes)))
else
table.insert(stream, table.concat(bytes))
end
elseif opt:find('[fd]') then
local val = tonumber(table.remove(vars, 1))
local sign = 0
if val < 0 then
sign = 1
val = -val
end
local mantissa, exponent = math.frexp(val)
if val == 0 then
mantissa = 0
exponent = 0
else
mantissa = (mantissa * 2 - 1) * math.ldexp(0.5, (opt == 'd') and 53 or 24)
exponent = exponent + ((opt == 'd') and 1022 or 126)
end
local bytes = {}
if opt == 'd' then
val = mantissa
for _ = 1, 6 do
table.insert(bytes, string.char(math.floor(val) % (2 ^ 8)))
val = math.floor(val / (2 ^ 8))
end
else
table.insert(bytes, string.char(math.floor(mantissa) % (2 ^ 8)))
val = math.floor(mantissa / (2 ^ 8))
table.insert(bytes, string.char(math.floor(val) % (2 ^ 8)))
val = math.floor(val / (2 ^ 8))
end
table.insert(bytes, string.char(math.floor(exponent * ((opt == 'd') and 16 or 128) + val) % (2 ^ 8)))
val = math.floor((exponent * ((opt == 'd') and 16 or 128) + val) / (2 ^ 8))
table.insert(bytes, string.char(math.floor(sign * 128 + val) % (2 ^ 8)))
if not endianness then
table.insert(stream, string.reverse(table.concat(bytes)))
else
table.insert(stream, table.concat(bytes))
end
elseif opt == 's' then
table.insert(stream, tostring(table.remove(vars, 1)))
table.insert(stream, string.char(0))
elseif opt == 'c' then
local n = format:sub(i + 1):match('%d+')
local length = tonumber(n)
if length > 0 then
local str = tostring(table.remove(vars, 1))
if length - str:len() > 0 then
str = str .. string.rep(' ', length - str:len())
end
table.insert(stream, str:sub(1, length))
end
i = i + n:len()
end
i = i + 1
end
return table.concat(stream)
end
local function prompt(text, default)
if default == nil then
io.write(string.format("%s: ", text))
elseif type(default) == 'string' then
io.write(string.format("%s [%s]: ", text, default))
end
local entry = io.read()
if string.strip(entry) == "" then
return default
end
return entry
end
local function is_executable(path)
local S_IEXEC = 64
return bit.band(fio.stat(path).mode, S_IEXEC) ~= 0
end
local function which(binary)
for _, path in ipairs(string.split(os.getenv("PATH"), ':') or {}) do
for _, file in ipairs(fio.listdir(path) or {}) do
local full_path = fio.pathjoin(path, file)
if file == binary and
fio.path.exists(full_path) and
fio.path.is_file(full_path) and
is_executable(full_path) then
return full_path
end
end
end
end
local function call(command, ...)
local cmd = string.format(command, ...)
local res, err = io.popen(string.format('(%s) && echo OK', cmd))
if res == nil then
die("Failed to execute '%s': %s", command, err)
end
local output = res:read("*all")
if output:endswith('OK\n') then
output = output:gsub('OK\n$', '')
return output
end
die("Failed to execute '%s': %s", cmd, output)
end
local function tarantool_is_enterprise()
local tarantool_dir = get_tarantool_dir()
local tnt_version = fio.pathjoin(tarantool_dir, 'VERSION')
return fio.path.exists(tnt_version)
end
-- * ---------------- Project-related functions ----------------
local function normalize_version(str)
local patterns = {
"(%d+)%.(%d+)%.(%d+)",
"(%d+)%.(%d+)",
"(%d+)"
}
for _, pattern in ipairs(patterns) do
local major, minor, patch = string.match(str, pattern)
if major ~= nil then
minor = minor or '0'
patch = patch or '0'
return {major, minor, patch}
end
end
end
local function detect_version(source_dir)
if which('git') == nil then
return nil
end
if not fio.path.exists(fio.pathjoin(source_dir, '.git')) then
return nil
end
local rc, raw_version = pcall(
call,
string.format('cd "%s" && git describe --tags --long', source_dir))
if not rc then
return nil
end
local version, release, commit = string.match(
string.strip(raw_version), "^(.*)-(%d+)-(%g+)$")
if version == nil then
return nil
end
if normalize_version(version) == nil then
warn("Detected version '%s' ignored, " ..
"because it doesn't look like proper " ..
"version (major.minor.patch)", version)
end
version = normalize_version(version)
return version, release, commit
end
local function find_rockspec(source_dir)
for _, file in ipairs(fio.listdir(source_dir) or {}) do
if string.endswith(file, '.rockspec') then
return file
end
end
end
local function detect_name(source_dir)
local rockspec = find_rockspec(source_dir)
if rockspec ~= nil then
return string.match(rockspec, '^(%g+)%-scm%-1%.rockspec$')
end
end
local function detect_name_release_version(source_dir, raw_name, raw_version)
local name = raw_name
local release
local version
if name == nil then
name = detect_name(source_dir)
if name == nil then
die("Failed to detect project name. Please pass it explicitly " ..
"via --name")
end
info("Detected project name: %s", name)
end
if raw_version then
if not normalize_version(raw_version) then
die("Passed version '%s' should be semantic (major.minor.patch)",
raw_version)
end
version = normalize_version(raw_version)
release = '0'
else
version, release = detect_version(source_dir)
if version == nil then
die("Failed to detect version from project in directory '%s'." ..
"Please pass it explicitly via --version", source_dir)
end
info("Detected project version: %s-%s",
table.concat(version, '.'), release)
end
if not fio.path.exists(fio.pathjoin(source_dir, 'init.lua')) then
die("Application must have `init.lua` in its root directory")
end
return name, release, version
end
-- * ----------- Special filenames ------------
local PREBUILD_SCRIPT_NAME = 'cartridge.pre-build'
local POSTBUILD_SCRIPT_NAME = 'cartridge.post-build'
-- deprecated files
local DEP_PREBUILD_SCRIPT_NAME = '.cartridge.pre'
local DEP_IGNORE_FILE_NAME = '.cartridge.ignore'
-- * --------------- Preinstall ---------------
local CREATE_USER_SCRIPT = [[
${groupadd} -r tarantool > /dev/null 2>&1 || :
${useradd} -M -N -g tarantool -r -d /var/lib/tarantool -s /sbin/nologin\
-c "Tarantool Server" tarantool > /dev/null 2>&1 || :
${mkdir} -p /etc/tarantool/conf.d/ --mode 755 2>&1 || :
${mkdir} -p /var/lib/tarantool/ --mode 755 2>&1 || :
${chown} tarantool:tarantool /var/lib/tarantool 2>&1 || :
${mkdir} -p /var/run/tarantool/ --mode 755 2>&1 || :
${chown} tarantool:tarantool /var/run/tarantool 2>&1 || :
]]
-- * -------------- Postinstall --------------
local SET_OWNER_SCRIPT = [[
${chown} -R root:root /usr/share/tarantool/${name}
${chown} root:root /etc/systemd/system/${name}.service
${chown} root:root /etc/systemd/system/${name}@.service
${chown} root:root /usr/lib/tmpfiles.d/${name}.conf
]]
-- * ---------------- Systemd ----------------
local SYSTEMD_UNIT_FILE = [[
[Unit]
Description=Tarantool Cartridge app ${name}.default
After=network.target
[Service]
Type=simple
ExecStartPre=${mkdir} -p ${workdir}.default
ExecStart=${bindir}/tarantool ${dir}/init.lua
Restart=on-failure
RestartSec=2
User=tarantool
Group=tarantool
Environment=TARANTOOL_WORKDIR=${workdir}.default
Environment=TARANTOOL_CFG=/etc/tarantool/conf.d/
Environment=TARANTOOL_PID_FILE=/var/run/tarantool/${name}.default.pid
Environment=TARANTOOL_CONSOLE_SOCK=/var/run/tarantool/${name}.default.control
LimitCORE=infinity
# Disable OOM killer
OOMScoreAdjust=-1000
# Increase fd limit for Vinyl
LimitNOFILE=65535
# Systemd waits until all xlogs are recovered
TimeoutStartSec=86400s
# Give a reasonable amount of time to close xlogs
TimeoutStopSec=10s
[Install]
WantedBy=multi-user.target
Alias=${name}
]]
local SYSTEMD_INSTANTIATED_UNIT_FILE = [[
[Unit]
Description=Tarantool Cartridge app ${name}@%i
After=network.target
[Service]
Type=simple
ExecStartPre=${mkdir} -p ${workdir}.%i
ExecStart=${bindir}/tarantool ${dir}/init.lua
Restart=on-failure
RestartSec=2
User=tarantool
Group=tarantool
Environment=TARANTOOL_WORKDIR=${workdir}.%i
Environment=TARANTOOL_CFG=/etc/tarantool/conf.d/
Environment=TARANTOOL_PID_FILE=/var/run/tarantool/${name}.%i.pid
Environment=TARANTOOL_CONSOLE_SOCK=/var/run/tarantool/${name}.%i.control
Environment=TARANTOOL_INSTANCE_NAME=%i
LimitCORE=infinity
# Disable OOM killer
OOMScoreAdjust=-1000
# Increase fd limit for Vinyl
LimitNOFILE=65535
# Systemd waits until all xlogs are recovered
TimeoutStartSec=86400s
# Give a reasonable amount of time to close xlogs
TimeoutStopSec=10s
[Install]
WantedBy=multi-user.target
Alias=${name}.%i
]]
-- * --------------------- Debian --------------------
local DEBIAN_CONTROL_FILE = [[
Package: ${name}
Version: ${version}
Maintainer: ${maintainer}
Architecture: ${arch}
Description: ${desc}
Depends: ${deps}
]]
-- * ------------------- Tmpfiles --------------------
local TMPFILES_CONFIG = [[
d /var/run/tarantool 0755 tarantool tarantool
]]
-- * ------------------- Dockerfile -------------------
local DOCKERFILE_FROM_DEFAULT = 'FROM centos:8'
local DOCKERFILE_TAIL_TEMPLATE = [[
SHELL ["/bin/bash", "-c"]
RUN yum install -y git gcc make cmake unzip
# create user and directories
RUN groupadd -r tarantool \
&& useradd -M -N -g tarantool -r -d /var/lib/tarantool -s /sbin/nologin \
-c "Tarantool Server" tarantool \
&& mkdir -p /var/lib/tarantool/ --mode 755 \
&& chown tarantool:tarantool /var/lib/tarantool \
&& mkdir -p /var/run/tarantool/ --mode 755 \
&& chown tarantool:tarantool /var/run/tarantool
${install_tarantool}
RUN echo 'd /var/run/tarantool 644 tarantool tarantool' > /usr/lib/tmpfiles.d/${name}.conf \
&& chmod 644 /usr/lib/tmpfiles.d/${name}.conf
# copy application source code
COPY . ${dir}
WORKDIR ${dir}
RUN : "----------- pre-build -----------" \
&& \
if [ -f ${prebuild_script_name} ]; then \
set -e && . ${prebuild_script_name} \
&& rm ${prebuild_script_name}; \
fi \
&& \
: "------------- build -------------" \
&& \
if ls *.rockspec 1> /dev/null 2>&1; then \
tarantoolctl rocks make; \
fi \
&& \
: "----------- post-build -----------" \
&& \
if [ -f ${postbuild_script_name} ]; then \
set -e && . ${postbuild_script_name} \
&& rm ${postbuild_script_name}; \
fi
USER tarantool:tarantool
CMD TARANTOOL_WORKDIR=${workdir}.${instance_name} \
TARANTOOL_PID_FILE=/var/run/tarantool/${name}.${instance_name}.pid \
TARANTOOL_CONSOLE_SOCK=/var/run/tarantool/${name}.${instance_name}.control \
tarantool ${dir}/init.lua
]]
local DOCKER_INSTALL_OPENSOURCE_TARANTOOL_TEMPLATE = [[
# install opensource Tarantool
RUN curl -s \
https://packagecloud.io/install/repositories/tarantool/${tarantool_repo_version}/script.rpm.sh | bash \
&& yum -y install tarantool tarantool-devel
]]
local DOCKER_INSTALL_ENTERPRISE_TARANTOOL_TEMPLATE = [[
ARG DOWNLOAD_TOKEN
WORKDIR /usr/share/tarantool
RUN DOWNLOAD_URL=https://tarantool:${"$"}{DOWNLOAD_TOKEN}@download.tarantool.io \
&& curl -O -L ${"$"}{DOWNLOAD_URL}/enterprise/tarantool-enterprise-bundle-${sdk_version}.tar.gz \
&& tar -xzf tarantool-enterprise-bundle-${sdk_version}.tar.gz \
&& rm -rf tarantool-enterprise-bundle-${sdk_version}.tar.gz
ENV PATH="/usr/share/tarantool/tarantool-enterprise:${"$"}{PATH}"
]]
-- * ---------- Application global state ------------
local app_state = {
-- Here will be stored some useful application info, for example,
-- flag detects if application uses deprecated packing flow
}
-- * ---------------- Generic packing ----------------