Blame SOURCES/copy_jdk_configs.lua

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