copy-jdk-configs/copy_jdk_configs.lua

319 lines
9.0 KiB
Lua
Raw Normal View History

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