Blame SOURCES/copy_jdk_configs.lua

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