1#!/usr/libexec/flua 2 3local ucl = require("ucl") 4 5-- read from environment variables 6local os_type = os.getenv("TYPE") 7local os_version = os.getenv("OSRELEASE") 8-- the raw file 9local capability_file = os.getenv("ORACLE_CAPABILITY") 10-- the platform-specific file 11local shapes_file = os.getenv("ORACLE_SHAPES") 12-- base template 13local template_file = os.getenv("ORACLE_TEMPLATE") 14local output_file = os.getenv("ORACLE_OUTPUT") 15 16if 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) 21end 22 23-- read files 24local 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 33end 34 35-- parse the template 36local template = read_file(template_file) 37local metadata = ucl.parser() 38metadata:parse_string(template) 39local data = metadata:get_object() 40 41-- update the simple fields 42data.operatingSystem = os_type 43data.operatingSystemVersion = os_version 44 45-- capability data is actually JSON, but needs to be inserted as a raw blob 46local caps = read_file(capability_file) 47-- remove all newlines and preceding spaces to match Oracle's format 48caps = caps:gsub("\n", "") 49caps = caps:gsub("%s+", "") 50-- is it still valid JSON? 51local caps_parser = ucl.parser() 52if 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) 55end 56-- insert as a raw blob 57data.imageCapabilityData = caps 58 59-- parse and insert architecture-dependent shape compatibilities data 60local shapes_data = read_file(shapes_file) 61local shapes = ucl.parser() 62shapes:parse_string(shapes_data) 63data.additionalMetadata.shapeCompatibilities = shapes:get_object() 64 65-- save the metadata file 66local dir = os.getenv("PWD") 67local out = io.open(output_file, "w") 68if not out then 69 io.stderr:write("Error: Oracle metadata script cannot create output file: " 70 .. dir .. "/" .. output_file .. "\n") 71 os.exit(1) 72end 73out:write(ucl.to_format(data, "json", {pretty = true})) 74out:close() 75