Blame SOURCES/copy_jdk_configs.lua

0be2c1
#!/usr/bin/lua
0be2c1
-- rpm call
0be2c1
-- debug=true lua -- copy_jdk_configs.lua   --currentjvm "%{uniquesuffix %{nil}}" --jvmdir "%{_jvmdir %{nil}}" --origname "%{name}" --origjavaver "%{javaver}" --arch "%{_arch}"
0be2c1
--test call
0be2c1
-- debug=true lua -- copy_jdk_configs.lua   --currentjvm "java-1.8.0-openjdk-1.8.0.65-3.b17.fc22.x86_64" --jvmdir "/usr/lib/jvm" --origname "java-1.8.0-openjdk" --origjavaver "1.8.0" --arch "x86_64" --jvmDestdir /home/jvanek/Desktop
0be2c1
0be2c1
local M = {}
0be2c1
0be2c1
if (os.getenv("debug") == "true") then
0be2c1
    debug = true;
0be2c1
else
0be2c1
    debug = false;
0be2c1
end
0be2c1
0be2c1
local function debugOneLinePrint(string)
0be2c1
    if (debug) then
0be2c1
        print(string)
0be2c1
    end ;
0be2c1
end
0be2c1
0be2c1
function getPath(str, sep)
0be2c1
    sep = sep or '/'
0be2c1
    return str:match("(.*" .. sep .. ")")
0be2c1
end
0be2c1
0be2c1
function splitToTable(source, pattern)
0be2c1
    local i1 = string.gmatch(source, pattern)
0be2c1
    local l1 = {}
0be2c1
    for i in i1 do
0be2c1
        table.insert(l1, i)
0be2c1
    end
0be2c1
    return l1
0be2c1
end
0be2c1
0be2c1
local function slurp(path)
0be2c1
    local f = io.open(path)
0be2c1
    local s = f:read("*a")
0be2c1
    f:close()
0be2c1
    return s
0be2c1
end
0be2c1
0be2c1
function trim(s)
0be2c1
    return (s:gsub("^%s*(.-)%s*$", "%1"))
0be2c1
end
0be2c1
0be2c1
local function dirWithParents(path)
0be2c1
    local s = ""
0be2c1
    local dirs = splitToTable(path, "[^/]+")
0be2c1
    for i, d in pairs(dirs) do
0be2c1
        if (i == #dirs) then
0be2c1
            break
0be2c1
        end
0be2c1
        s = s .. "/" .. d
0be2c1
        local stat2 = posix.stat(s, "type");
0be2c1
        if (stat2 == nil) then
0be2c1
            debugOneLinePrint(s .. " does not exists, creating")
0be2c1
            if (not dry) then
0be2c1
                posix.mkdir(s)
0be2c1
            end
0be2c1
        else
0be2c1
            debugOneLinePrint(s .. " exists,not creating")
0be2c1
        end
0be2c1
    end
0be2c1
end
0be2c1
0be2c1
0be2c1
-- main function,
0be2c1
-- formelry main body
0be2c1
-- move to function resolved
0be2c1
-- https://bugzilla.redhat.com/show_bug.cgi?id=1892224
0be2c1
-- for readability not indented, todo, indent once tuned
0be2c1
0be2c1
function M.mainProgram(arg)
0be2c1
    debugOneLinePrint("cjc: lua debug on")
0be2c1
    local caredFiles = { "jre/lib/calendars.properties",
0be2c1
                         "jre/lib/content-types.properties",
0be2c1
                         "jre/lib/flavormap.properties",
0be2c1
                         "jre/lib/logging.properties",
0be2c1
                         "jre/lib/net.properties",
0be2c1
                         "jre/lib/psfontj2d.properties",
0be2c1
                         "jre/lib/sound.properties",
0be2c1
                         "jre/lib/deployment.properties",
0be2c1
                         "jre/lib/deployment.config",
0be2c1
                         "jre/lib/security/US_export_policy.jar",
0be2c1
                         "jre/lib/security/unlimited/US_export_policy.jar",
0be2c1
                         "jre/lib/security/limited/US_export_policy.jar",
0be2c1
                         "jre/lib/security/policy/unlimited/US_export_policy.jar",
0be2c1
                         "jre/lib/security/policy/limited/US_export_policy.jar",
0be2c1
                         "jre/lib/security/java.policy",
0be2c1
                         "jre/lib/security/java.security",
0be2c1
                         "jre/lib/security/local_policy.jar",
0be2c1
                         "jre/lib/security/unlimited/local_policy.jar",
0be2c1
                         "jre/lib/security/limited/local_policy.jar",
0be2c1
                         "jre/lib/security/policy/unlimited/local_policy.jar",
0be2c1
                         "jre/lib/security/policy/limited/local_policy.jar",
0be2c1
                         "jre/lib/security/nss.cfg",
0be2c1
                         "jre/lib/security/cacerts",
0be2c1
                         "jre/lib/security/blacklisted.certs",
0be2c1
                         "jre/lib/security/jssecacerts",
0be2c1
                         "jre/lib/security/trusted.certs",
0be2c1
                         "jre/lib/security/trusted.jssecerts",
0be2c1
                         "jre/lib/security/trusted.clientcerts",
0be2c1
                         "jre/lib/ext",
0be2c1
                         "jre/lib/security/blacklist",
0be2c1
                         "jre/lib/security/javaws.policy",
0be2c1
                         "jre/lib/security/nss.fips.cfg",
0be2c1
                         "lib/security",
0be2c1
                         "conf",
0be2c1
                         "lib/ext" }
0be2c1
0be2c1
    -- before import to allow run from spec
0be2c1
    if (arg[1] == "--list") then
0be2c1
        for i, file in pairs(caredFiles) do
0be2c1
            print(file)
0be2c1
        end
0be2c1
        return 0;
0be2c1
    end
0be2c1
0be2c1
    -- yum install lua-posix
0be2c1
    local posix = require "posix"
0be2c1
0be2c1
    -- the one we are installing
0be2c1
    local currentjvm = nil
0be2c1
    local jvmdir = nil
0be2c1
    local jvmDestdir = nil
0be2c1
    local origname = nil
0be2c1
    local origjavaver = nil
0be2c1
    local arch = nil
0be2c1
    local temp = nil;
0be2c1
    local dry = false;
0be2c1
0be2c1
    for i = 1, #arg, 2 do
0be2c1
        if (arg[i] == "--help" or arg[i] == "-h") then
0be2c1
            print("all but jvmDestdir and debug are mandatory")
0be2c1
            print("  --currentjvm")
0be2c1
            print("    NVRA of currently installed java")
0be2c1
            print("  --jvmdir")
0be2c1
            print("    Directory where to find this kind of virtual machine. Generally /usr/lib/jvm")
0be2c1
            print("  --origname")
0be2c1
            print("    convinient switch to determine jdk. Generally java-1.X.0-vendor")
0be2c1
            print("  --origjavaver")
0be2c1
            print("    convinient switch to determine jdk's version. Generally 1.X.0")
0be2c1
            print("  --arch")
0be2c1
            print("    convinient switch to determine jdk's arch")
0be2c1
            print("  --jvmDestdir")
0be2c1
            print("    Migration/testing switch. Target Mostly same as jvmdir, but you may wont to copy ouside it.")
0be2c1
            print("  --debug or $debug")
0be2c1
            print("    Enables printing out whats going on. true/false. False by default")
0be2c1
            print("  --temp")
0be2c1
            print("    optional file to save intermediate result - directory configs were copied from")
0be2c1
            print("  --dry")
0be2c1
            print("    true/fase if true, then no changes will be written to disk except one tmp file. False by default")
0be2c1
            print("  **** specil parasm ****")
0be2c1
            print("  --list")
0be2c1
            print("    if present on cmdline, list all cared files and exists")
0be2c1
            os.exit(0)
0be2c1
        end
0be2c1
        if (arg[i] == "--currentjvm") then
0be2c1
            currentjvm = arg[i + 1]
0be2c1
        end
0be2c1
        if (arg[i] == "--jvmdir") then
0be2c1
            jvmdir = arg[i + 1]
0be2c1
        end
0be2c1
        if (arg[i] == "--origname") then
0be2c1
            origname = arg[i + 1]
0be2c1
        end
0be2c1
        if (arg[i] == "--origjavaver") then
0be2c1
            origjavaver = arg[i + 1]
0be2c1
        end
0be2c1
        if (arg[i] == "--arch") then
0be2c1
            arch = arg[i + 1]
0be2c1
        end
0be2c1
        if (arg[i] == "--jvmDestdir") then
0be2c1
            jvmDestdir = arg[i + 1]
0be2c1
        end
0be2c1
        if (arg[i] == "--debug") then
0be2c1
            --no string, boolean, workaround
0be2c1
            if (arg[i + 1] == "true") then
0be2c1
                debug = true
0be2c1
            end
0be2c1
        end
0be2c1
        if (arg[i] == "--dry") then
0be2c1
            --no string, boolean, workaround
0be2c1
            if (arg[i + 1] == "true") then
0be2c1
                dry = true
0be2c1
            end
0be2c1
        end
0be2c1
        if (arg[i] == "--temp") then
0be2c1
            temp = arg[i + 1]
0be2c1
        end
0be2c1
    end
0be2c1
0be2c1
    if (jvmDestdir == nil) then
0be2c1
        jvmDestdir = jvmdir
0be2c1
    end
0be2c1
0be2c1
    if (debug) then
0be2c1
        print("--currentjvm:");
0be2c1
        print(currentjvm);
0be2c1
        print("--jvmdir:");
0be2c1
        print(jvmdir);
0be2c1
        print("--jvmDestdir:");
0be2c1
        print(jvmDestdir);
0be2c1
        print("--origname:");
0be2c1
        print(origname);
0be2c1
        print("--origjavaver:");
0be2c1
        print(origjavaver);
0be2c1
        print("--arch:");
0be2c1
        print(arch);
0be2c1
        print("--debug:");
0be2c1
        print(debug);
0be2c1
    end
0be2c1
0be2c1
    --trasnform substitute names to lua patterns
0be2c1
    local name = string.gsub(string.gsub(origname, "%-", "%%-"), "%.", "%%.")
0be2c1
    local javaver = string.gsub(origjavaver, "%.", "%%.")
0be2c1
0be2c1
    local jvms = { }
0be2c1
0be2c1
    debugOneLinePrint("started")
0be2c1
0be2c1
    foundJvms = posix.dir(jvmdir);
0be2c1
    if (foundJvms == nil) then
0be2c1
        debugOneLinePrint("no, or nothing in " .. jvmdir .. " exit")
0be2c1
        return
0be2c1
    end
0be2c1
0be2c1
    debugOneLinePrint("found " .. #foundJvms .. " jvms")
0be2c1
0be2c1
    for i, p in pairs(foundJvms) do
0be2c1
        -- regex similar to %{_jvmdir}/%{name}-%{javaver}*%{_arch} bash command
0be2c1
        if (string.find(p, name .. "%-" .. javaver .. ".*" .. arch) ~= nil) then
0be2c1
            debugOneLinePrint("matched:  " .. p)
0be2c1
            if (currentjvm == p) then
0be2c1
                debugOneLinePrint("this jdk is already installed. exiting lua script")
0be2c1
                return
0be2c1
            end ;
0be2c1
            if (string.match(p, ".*-debug$")) then
0be2c1
                print(p .. " matched but seems to be debug variant. Skipping")
0be2c1
            else
0be2c1
                table.insert(jvms, p)
0be2c1
            end
0be2c1
        else
0be2c1
            debugOneLinePrint("NOT matched:  " .. p)
0be2c1
        end
0be2c1
    end
0be2c1
0be2c1
    if (#jvms <= 0) then
0be2c1
        debugOneLinePrint("no matching jdk in " .. jvmdir .. " exit")
0be2c1
        return
0be2c1
    end ;
0be2c1
0be2c1
    debugOneLinePrint("matched " .. #jvms .. " jdk in " .. jvmdir)
0be2c1
0be2c1
    --full names are like java-1.7.0-openjdk-1.7.0.60-2.4.5.1.fc20.x86_64
0be2c1
    table.sort(jvms, function(a, b)
0be2c1
        -- version-sort
0be2c1
        -- split on non word: . -
0be2c1
        local l1 = splitToTable(a, "[^%.-]+")
0be2c1
        local l2 = splitToTable(b, "[^%.-]+")
0be2c1
        for x = 1, math.min(#l1, #l2) do
0be2c1
            local l1x = tonumber(l1[x])
0be2c1
            local l2x = tonumber(l2[x])
0be2c1
            if (l1x ~= nil and l2x ~= nil) then
0be2c1
                --if hunks are numbers, go with them
0be2c1
                if (l1x < l2x) then
0be2c1
                    return true;
0be2c1
                end
0be2c1
                if (l1x > l2x) then
0be2c1
                    return false;
0be2c1
                end
0be2c1
            else
0be2c1
                if (l1[x] < l2[x]) then
0be2c1
                    return true;
0be2c1
                end
0be2c1
                if (l1[x] > l2[x]) then
0be2c1
                    return false;
0be2c1
                end
0be2c1
            end
0be2c1
            -- if hunks are equals then move to another pair of hunks
0be2c1
        end
0be2c1
        return a < b
0be2c1
0be2c1
    end)
0be2c1
0be2c1
    if (debug) then
0be2c1
        print("sorted lsit of jvms")
0be2c1
        for i, file in pairs(jvms) do
0be2c1
            print(file)
0be2c1
        end
0be2c1
    end
0be2c1
0be2c1
    latestjvm = jvms[#jvms]
0be2c1
0be2c1
    if (temp ~= nil) then
0be2c1
        src = jvmdir .. "/" .. latestjvm
0be2c1
        debugOneLinePrint("temp declared as " .. temp .. " saving used dir of " .. src)
0be2c1
        file = io.open(temp, "w")
0be2c1
        file:write(src)
0be2c1
        file:close()
0be2c1
    end
0be2c1
0be2c1
    local readlinkOutput = os.tmpname()
0be2c1
0be2c1
    for i, file in pairs(caredFiles) do
0be2c1
        local SOURCE = jvmdir .. "/" .. latestjvm .. "/" .. file
0be2c1
        local DEST = jvmDestdir .. "/" .. currentjvm .. "/" .. file
0be2c1
        debugOneLinePrint("going to copy " .. SOURCE)
0be2c1
        debugOneLinePrint("to  " .. DEST)
0be2c1
        local stat1 = posix.stat(SOURCE, "type");
0be2c1
        if (stat1 ~= nil) then
0be2c1
            debugOneLinePrint(SOURCE .. " exists")
0be2c1
            dirWithParents(DEST)
0be2c1
            -- Copy with -a to keep everything intact
0be2c1
            local exe = "cp" .. " -ar " .. SOURCE .. " " .. DEST
0be2c1
            local linkExe = "readlink" .. " -f " .. SOURCE .. " > " .. readlinkOutput
0be2c1
            debugOneLinePrint("executing " .. linkExe)
0be2c1
            os.remove(readlinkOutput)
0be2c1
            os.execute(linkExe)
0be2c1
            local link = trim(slurp(readlinkOutput))
0be2c1
            debugOneLinePrint("  ...link is " .. link)
0be2c1
            if (not ((link) == (SOURCE))) then
0be2c1
                debugOneLinePrint("WARNING link " .. link .. " where file " .. SOURCE .. " expected!")
0be2c1
                debugOneLinePrint("Will try to copy link target rather then link itself!")
0be2c1
                --replacing  any NVRA by future NVRA (still execting to have NVRA for any multiple-installable targets
0be2c1
                -- lua stubbornly consider dash as inteval. Replacing by dot to match X-Y more correct as X.Y rather then not at all
0be2c1
                local linkDest = string.gsub(link, latestjvm:gsub("-", "."), currentjvm)
0be2c1
                debugOneLinePrint("attempting to copy " .. link .. " to " .. linkDest)
0be2c1
                if (link == linkDest) then
0be2c1
                    debugOneLinePrint("Those are identical files! Nothing to do!")
0be2c1
                else
0be2c1
                    local exe2 = "cp" .. " -ar " .. link .. " " .. linkDest
0be2c1
                    dirWithParents(linkDest)
0be2c1
                    debugOneLinePrint("executing " .. exe2)
0be2c1
                    if (not dry) then
0be2c1
                        os.execute(exe2)
0be2c1
                    end
0be2c1
                end
0be2c1
            else
0be2c1
                debugOneLinePrint("executing " .. exe)
0be2c1
                if (not dry) then
0be2c1
                    os.execute(exe)
0be2c1
                end
0be2c1
            end
0be2c1
        else
0be2c1
            debugOneLinePrint(SOURCE .. " does not exists")
0be2c1
        end
0be2c1
    end
0be2c1
0be2c1
end --unindented main function
0be2c1
0be2c1
if (arg == nil) then
0be2c1
    debugOneLinePrint("arg variable is nil, you have to call mainProgram manually") -- this can actually not be invoked, as the debug is set via arg
0be2c1
else
0be2c1
    M.mainProgram(arg)
0be2c1
end
0be2c1
0be2c1
return M