summaryrefslogtreecommitdiff
path: root/copy_jdk_configs.lua
blob: 57b259299093c22107929cd25e1a4edb072bdbc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/lua
-- rpm call
-- debug=true lua -- copy_jdk_configs.lua   --currentjvm "%{uniquesuffix %{nil}}" --jvmdir "%{_jvmdir %{nil}}" --origname "%{name}" --origjavaver "%{javaver}" --arch "%{_arch}"
--test call
-- debug=true 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" --jvmDestdir /home/jvanek/Desktop

local M = {}

if (os.getenv("debug") == "true") then
    debug = true;
else
    debug = false;
end

local function debugOneLinePrint(string)
    if (debug) then
        print(string)
    end ;
end

function getPath(str, sep)
    sep = sep or '/'
    return str:match("(.*" .. sep .. ")")
end

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

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

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


-- main function,
-- formelry main body
-- move to function resolved
-- https://bugzilla.redhat.com/show_bug.cgi?id=1892224
-- for readability not indented, todo, indent once tuned

function M.mainProgram(arg)
    debugOneLinePrint("cjc: lua debug on")
    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",
                         "jre/lib/security/unlimited/US_export_policy.jar",
                         "jre/lib/security/limited/US_export_policy.jar",
                         "jre/lib/security/policy/unlimited/US_export_policy.jar",
                         "jre/lib/security/policy/limited/US_export_policy.jar",
                         "jre/lib/security/java.policy",
                         "jre/lib/security/java.security",
                         "jre/lib/security/local_policy.jar",
                         "jre/lib/security/unlimited/local_policy.jar",
                         "jre/lib/security/limited/local_policy.jar",
                         "jre/lib/security/policy/unlimited/local_policy.jar",
                         "jre/lib/security/policy/limited/local_policy.jar",
                         "jre/lib/security/nss.cfg",
                         "jre/lib/security/cacerts",
                         "jre/lib/security/blacklisted.certs",
                         "jre/lib/security/jssecacerts",
                         "jre/lib/security/trusted.certs",
                         "jre/lib/security/trusted.jssecerts",
                         "jre/lib/security/trusted.clientcerts",
                         "jre/lib/ext",
                         "jre/lib/security/blacklist",
                         "jre/lib/security/javaws.policy",
                         "jre/lib/security/nss.fips.cfg",
                         "lib/security",
                         "conf",
                         "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

    -- 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 temp = nil;
    local dry = false;

    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 or $debug")
            print("    Enables printing out whats going on. true/false. False by default")
            print("  --temp")
            print("    optional file to save intermediate result - directory configs were copied from")
            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")
            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
        if (arg[i] == "--dry") then
            --no string, boolean, workaround
            if (arg[i + 1] == "true") then
                dry = true
            end
        end
        if (arg[i] == "--temp") then
            temp = arg[i + 1]
        end
    end

    if (jvmDestdir == nil) then
        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

    --trasnform substitute names to lua patterns
    local name = string.gsub(string.gsub(origname, "%-", "%%-"), "%.", "%%.")
    local javaver = string.gsub(origjavaver, "%.", "%%.")

    local jvms = { }

    debugOneLinePrint("started")

    foundJvms = posix.dir(jvmdir);
    if (foundJvms == nil) then
        debugOneLinePrint("no, or nothing in " .. jvmdir .. " exit")
        return
    end

    debugOneLinePrint("found " .. #foundJvms .. " jvms")

    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
            debugOneLinePrint("matched:  " .. p)
            if (currentjvm == p) then
                debugOneLinePrint("this jdk is already installed. exiting lua script")
                return
            end ;
            if (string.match(p, ".*-debug$")) then
                print(p .. " matched but seems to be debug variant. Skipping")
            else
                table.insert(jvms, p)
            end
        else
            debugOneLinePrint("NOT matched:  " .. p)
        end
    end

    if (#jvms <= 0) then
        debugOneLinePrint("no matching jdk in " .. jvmdir .. " exit")
        return
    end ;

    debugOneLinePrint("matched " .. #jvms .. " jdk in " .. jvmdir)

    --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]

    if (temp ~= nil) then
        src = jvmdir .. "/" .. latestjvm
        debugOneLinePrint("temp declared as " .. temp .. " saving used dir of " .. src)
        file = io.open(temp, "w")
        file:write(src)
        file:close()
    end

    local readlinkOutput = os.tmpname()

    for i, file in pairs(caredFiles) do
        local SOURCE = jvmdir .. "/" .. latestjvm .. "/" .. file
        local DEST = jvmDestdir .. "/" .. currentjvm .. "/" .. file
        debugOneLinePrint("going to copy " .. SOURCE)
        debugOneLinePrint("to  " .. DEST)
        local stat1 = posix.stat(SOURCE, "type");
        if (stat1 ~= nil) then
            debugOneLinePrint(SOURCE .. " exists")
            dirWithParents(DEST)
            -- Copy with -a to keep everything intact
            local exe = "cp" .. " -ar " .. SOURCE .. " " .. DEST
            local linkExe = "readlink" .. " -f " .. SOURCE .. " > " .. readlinkOutput
            debugOneLinePrint("executing " .. linkExe)
            os.remove(readlinkOutput)
            os.execute(linkExe)
            local link = trim(slurp(readlinkOutput))
            debugOneLinePrint("  ...link is " .. link)
            if (not ((link) == (SOURCE))) then
                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
            end
        else
            debugOneLinePrint(SOURCE .. " does not exists")
        end
    end

end --unindented main function

if (arg == nil) then
    debugOneLinePrint("arg variable is nil, you have to call mainProgram manually") -- this can actually not be invoked, as the debug is set via arg
else
    M.mainProgram(arg)
end

return M