Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/release/scripts/oracle/generate_metadata.lua
34865 views
1
#!/usr/libexec/flua
2
3
local ucl = require("ucl")
4
5
-- read from environment variables
6
local os_type = os.getenv("TYPE")
7
local os_version = os.getenv("OSRELEASE")
8
-- the raw file
9
local capability_file = os.getenv("ORACLE_CAPABILITY")
10
-- the platform-specific file
11
local shapes_file = os.getenv("ORACLE_SHAPES")
12
-- base template
13
local template_file = os.getenv("ORACLE_TEMPLATE")
14
local output_file = os.getenv("ORACLE_OUTPUT")
15
16
if not os_type or not os_version or not capability_file or
17
not shapes_file or not template_file or not output_file then
18
io.stderr:write("Error: Oracle metadata script is missing required environment variables:\n")
19
io.stderr:write("TYPE, OSRELEASE, ORACLE_CAPABILITY, ORACLE_SHAPES, ORACLE_TEMPLATE, ORACLE_OUTPUT\n")
20
os.exit(1)
21
end
22
23
-- read files
24
local function read_file(path)
25
local f = io.open(path, "r")
26
if not f then
27
io.stderr:write("Error: Oracle metadata script cannot open file: " .. path .. "\n")
28
os.exit(1)
29
end
30
local content = f:read("*a")
31
f:close()
32
return content
33
end
34
35
-- parse the template
36
local template = read_file(template_file)
37
local metadata = ucl.parser()
38
metadata:parse_string(template)
39
local data = metadata:get_object()
40
41
-- update the simple fields
42
data.operatingSystem = os_type
43
data.operatingSystemVersion = os_version
44
45
-- capability data is actually JSON, but needs to be inserted as a raw blob
46
local caps = read_file(capability_file)
47
-- remove all newlines and preceding spaces to match Oracle's format
48
caps = caps:gsub("\n", "")
49
caps = caps:gsub("%s+", "")
50
-- is it still valid JSON?
51
local caps_parser = ucl.parser()
52
if not caps_parser:parse_string(caps) then
53
io.stderr:write("Error: Oracle metadata script found invalid JSON in capability file\n")
54
os.exit(1)
55
end
56
-- insert as a raw blob
57
data.imageCapabilityData = caps
58
59
-- parse and insert architecture-dependent shape compatibilities data
60
local shapes_data = read_file(shapes_file)
61
local shapes = ucl.parser()
62
shapes:parse_string(shapes_data)
63
data.additionalMetadata.shapeCompatibilities = shapes:get_object()
64
65
-- save the metadata file
66
local dir = os.getenv("PWD")
67
local out = io.open(output_file, "w")
68
if not out then
69
io.stderr:write("Error: Oracle metadata script cannot create output file: "
70
.. dir .. "/" .. output_file .. "\n")
71
os.exit(1)
72
end
73
out:write(ucl.to_format(data, "json", {pretty = true}))
74
out:close()
75
76