Blame SOURCES/copy_jdk_configs.lua

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