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