Blame SOURCES/copy_jdk_configs.lua

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