Blame SOURCES/copy_jdk_configs.lua

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