Blame SOURCES/copy_jdk_configs.lua

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