Blame SOURCES/copy_jdk_configs.lua

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