Blame SOURCES/copy_jdk_configs.lua

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