-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
export.lua
352 lines (268 loc) · 7.64 KB
/
export.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
local schema_topological_sort = require "kong.db.schema.topological_sort"
local protobuf = require "kong.tools.protobuf"
local lyaml = require "lyaml"
local setmetatable = setmetatable
local assert = assert
local type = type
local pcall = pcall
local pairs = pairs
local insert = table.insert
local io_open = io.open
local null = ngx.null
local REMOVE_FIRST_LINE_PATTERN = "^[^\n]+\n(.+)$"
local GLOBAL_QUERY_OPTS = { nulls = true, workspace = null }
local function convert_nulls(tbl, from, to)
for k,v in pairs(tbl) do
if v == from then
tbl[k] = to
elseif type(v) == "table" then
tbl[k] = convert_nulls(v, from, to)
end
end
return tbl
end
local function to_yaml_string(tbl)
convert_nulls(tbl, null, lyaml.null)
local pok, yaml, err = pcall(lyaml.dump, { tbl })
if not pok then
return nil, yaml
end
if not yaml then
return nil, err
end
-- drop the multi-document "---\n" header and "\n..." trailer
return yaml:sub(5, -5)
end
local function to_yaml_file(entities, filename)
local yaml, err = to_yaml_string(entities)
if not yaml then
return nil, err
end
local fd, err = io_open(filename, "w")
if not fd then
return nil, err
end
local ok, err = fd:write(yaml)
if not ok then
return nil, err
end
fd:close()
return true
end
local function begin_transaction(db)
if db.strategy == "postgres" then
local ok, err = db.connector:connect("read")
if not ok then
return nil, err
end
ok, err = db.connector:query("BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY;", "read")
if not ok then
return nil, err
end
end
return true
end
local function end_transaction(db)
if db.strategy == "postgres" then
-- just finish up the read-only transaction,
-- either COMMIT or ROLLBACK is fine.
db.connector:query("ROLLBACK;", "read")
db.connector:setkeepalive()
end
end
local function export_from_db_impl(emitter, skip_ws, skip_disabled_entities, expand_foreigns)
local schemas = {}
local db = kong.db
for _, dao in pairs(db.daos) do
if not (skip_ws and dao.schema.name == "workspaces") then
insert(schemas, dao.schema)
end
end
local sorted_schemas, err = schema_topological_sort(schemas)
if not sorted_schemas then
return nil, err
end
local ok
ok, err = begin_transaction(db)
if not ok then
return nil, err
end
emitter:emit_toplevel({
_format_version = "3.0",
_transform = false,
})
local disabled_services = {}
local disabled_routes = {}
for i = 1, #sorted_schemas do
local schema = sorted_schemas[i]
if schema.db_export == false then
goto continue
end
local name = schema.name
local fks = {}
for field_name, field in schema:each_field() do
if field.type == "foreign" then
insert(fks, field_name)
end
end
local page_size
if db[name].pagination then
page_size = db[name].pagination.max_page_size
end
for row, err in db[name]:each_for_export(page_size, GLOBAL_QUERY_OPTS) do
if not row then
end_transaction(db)
kong.log.err(err)
return nil, err
end
-- do not export disabled services and disabled plugins when skip_disabled_entities
-- as well do not export plugins and routes of dsiabled services
if skip_disabled_entities and name == "services" and not row.enabled then
disabled_services[row.id] = true
elseif skip_disabled_entities and name == "routes" and row.service and
disabled_services[row.service ~= null and row.service.id] then
disabled_routes[row.id] = true
elseif skip_disabled_entities and name == "plugins" and not row.enabled then
goto skip_emit
else
for j = 1, #fks do
local foreign_name = fks[j]
if type(row[foreign_name]) == "table" then
local id = row[foreign_name].id
if id ~= nil then
if disabled_services[id] or disabled_routes[id] then
goto skip_emit
end
if not expand_foreigns then
row[foreign_name] = id
end
end
end
end
emitter:emit_entity(name, row)
end
::skip_emit::
end
::continue::
end
end_transaction(db)
return emitter:done()
end
local fd_emitter = {
emit_toplevel = function(self, tbl)
self.fd:write(to_yaml_string(tbl))
end,
emit_entity = function(self, entity_name, entity_data)
local yaml = to_yaml_string({ [entity_name] = { entity_data } })
if entity_name == self.current_entity then
yaml = assert(yaml:match(REMOVE_FIRST_LINE_PATTERN))
end
self.fd:write(yaml)
self.current_entity = entity_name
end,
done = function()
return true
end,
}
function fd_emitter.new(fd)
return setmetatable({ fd = fd }, { __index = fd_emitter })
end
local function export_from_db(fd, skip_ws, skip_disabled_entities)
-- not sure if this really useful for skip_ws,
-- but I want to allow skip_disabled_entities and would rather have consistent interface
if skip_ws == nil then
skip_ws = true
end
if skip_disabled_entities == nil then
skip_disabled_entities = false
end
return export_from_db_impl(fd_emitter.new(fd), skip_ws, skip_disabled_entities)
end
local table_emitter = {
emit_toplevel = function(self, tbl)
self.out = tbl
end,
emit_entity = function(self, entity_name, entity_data)
if not self.out[entity_name] then
self.out[entity_name] = { entity_data }
else
insert(self.out[entity_name], entity_data)
end
end,
done = function(self)
return self.out
end,
}
function table_emitter.new()
return setmetatable({}, { __index = table_emitter })
end
local function export_config(skip_ws, skip_disabled_entities)
-- default skip_ws=false and skip_disabled_services=true
if skip_ws == nil then
skip_ws = false
end
if skip_disabled_entities == nil then
skip_disabled_entities = true
end
return export_from_db_impl(table_emitter.new(), skip_ws, skip_disabled_entities)
end
local function remove_nulls(tbl)
for k,v in pairs(tbl) do
if v == null then
tbl[k] = nil
elseif type(v) == "table" then
tbl[k] = remove_nulls(v)
end
end
return tbl
end
local proto_emitter = {
emit_toplevel = function(self, tbl)
self.out = {
format_version = tbl._format_version,
}
end,
emit_entity = function(self, entity_name, entity_data)
if entity_name == "plugins" then
entity_data.config = protobuf.pbwrap_struct(entity_data.config)
end
if not self.out[entity_name] then
self.out[entity_name] = { entity_data }
else
insert(self.out[entity_name], entity_data)
end
end,
done = function(self)
return remove_nulls(self.out)
end,
}
function proto_emitter.new()
return setmetatable({}, { __index = proto_emitter })
end
local function export_config_proto(skip_ws, skip_disabled_entities)
-- default skip_ws=false and skip_disabled_services=true
if skip_ws == nil then
skip_ws = false
end
if skip_disabled_entities == nil then
skip_disabled_entities = true
end
return export_from_db_impl(proto_emitter.new(), skip_ws, skip_disabled_entities, true)
end
local function sanitize_output(entities)
entities.workspaces = nil
for _, s in pairs(entities) do -- set of entities
for _, e in pairs(s) do -- individual entity
e.ws_id = nil
end
end
end
return {
convert_nulls = convert_nulls,
to_yaml_string = to_yaml_string,
to_yaml_file = to_yaml_file,
export_from_db = export_from_db,
export_config = export_config,
export_config_proto = export_config_proto,
sanitize_output = sanitize_output,
}