-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.lua
More file actions
546 lines (467 loc) · 19.5 KB
/
Copy pathtest.lua
File metadata and controls
546 lines (467 loc) · 19.5 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
#!/usr/bin/env lua
local REPO = "HackerOS-Linux-System/HackerOS"
local ARTIFACT_HINT = "hackeros%-v4%.9%-official" -- dopasowanie (lowercase, lua pattern)
local API_BASE = "https://api.github.com/repos/" .. REPO
local WORKDIR = (os.getenv("HOME") or "/tmp") .. "/.cache/hackeros-test"
local TOKEN = os.getenv("GITHUB_TOKEN")
math.randomseed(os.time())
---------------------------------------------------------------------
-- Pomocnicze funkcje
---------------------------------------------------------------------
local function exec_capture(cmd)
local h = io.popen(cmd .. " 2>&1")
if not h then return "", false end
local out = h:read("*a") or ""
local ok = h:close()
return out, ok and true or false
end
local function run(cmd)
-- Uruchamia polecenie "na żywo" (np. QEMU), zwraca kod wyjścia
local ok, how, code = os.execute(cmd)
if type(ok) == "number" then return ok == 0 end -- lua 5.1 kompatybilność
return ok == true
end
local function has_cmd(cmd)
local out = exec_capture("command -v " .. cmd)
return out:match("%S") ~= nil
end
local function ask(prompt)
io.write(prompt)
io.flush()
local line = io.read("*l")
return line or ""
end
local function ask_yes_no(prompt, default_no)
local ans = ask(prompt .. (default_no and " [t/N]: " or " [T/n]: "))
ans = ans:lower()
if default_no then
return ans == "t" or ans == "tak" or ans == "y" or ans == "yes"
else
return not (ans == "n" or ans == "nie" or ans == "no")
end
end
local function info(msg) print("[INFO] " .. msg) end
local function warn(msg) print("[UWAGA] " .. msg) end
local function die(msg)
io.stderr:write("[BŁĄD] " .. msg .. "\n")
os.exit(1)
end
local function mkdirp(path)
exec_capture("mkdir -p " .. string.format("%q", path))
end
---------------------------------------------------------------------
-- Sprawdzenie zależności
---------------------------------------------------------------------
local function check_dependencies()
local required = { "curl", "jq", "unzip", "tar", "find" }
local missing = {}
for _, c in ipairs(required) do
if not has_cmd(c) then table.insert(missing, c) end
end
if #missing > 0 then
die("Brakuje wymaganych narzędzi: " .. table.concat(missing, ", ") ..
". Zainstaluj je i uruchom skrypt ponownie.")
end
end
---------------------------------------------------------------------
-- Zapytania do GitHub API
---------------------------------------------------------------------
local function curl_headers()
local h = '-H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28"'
if TOKEN then
h = h .. ' -H "Authorization: Bearer ' .. TOKEN .. '"'
end
return h
end
-- Wykonuje żądanie GET do GitHub API. Jeśli odpowiedź wskazuje, że
-- potrzebny jest token (przekroczony limit zapytań, brak autoryzacji,
-- wymagane uwierzytelnienie) a token nie jest jeszcze ustawiony,
-- PYTA UŻYTKOWNIKA O TOKEN W TRAKCIE DZIAŁANIA i automatycznie ponawia
-- żądanie. Zwraca treść odpowiedzi (JSON jako string).
local function api_get(url)
local function do_request()
local cmd = string.format('curl -s -w "\\n%%{http_code}" %s "%s"', curl_headers(), url)
local out = exec_capture(cmd)
local body, code = out:match("^(.*)\n(%d+)%s*$")
if not body then body, code = out, "" end
return body, code
end
local body, code = do_request()
local needs_auth = (code == "401" or code == "403")
or body:find('"message"%s*:%s*"[^"]-[Rr]ate limit[^"]*"') ~= nil
or body:find('"message"%s*:%s*"[Bb]ad credentials"') ~= nil
or body:find('"message"%s*:%s*"[Rr]equires authentication"') ~= nil
or body:find('"message"%s*:%s*"[^"]-API rate limit exceeded[^"]*"') ~= nil
if needs_auth and not TOKEN then
warn("GitHub API zgłosił potrzebę uwierzytelnienia (limit zapytań lub brak dostępu).")
local t = ask("Podaj token GitHub (Personal Access Token), aby kontynuować: ")
if t == "" then
die("Bez tokenu nie można kontynuować - GitHub API odrzuca żądania.")
end
TOKEN = t
info("Ponawiam żądanie z podanym tokenem...")
body, code = do_request()
if code == "401" or code == "403" then
die("Podany token nie działa (GitHub zwrócił błąd autoryzacji). Sprawdź jego uprawnienia i spróbuj ponownie.")
end
end
return body
end
-- Znajduje ID najnowszego zakończonego sukcesem workflow run
local function get_latest_successful_run_id()
info("Pobieranie listy ostatnich uruchomień workflow...")
local out = api_get(API_BASE .. "/actions/runs?per_page=20")
local jq_cmd = "echo " .. string.format("%q", out) ..
" | jq -r '.workflow_runs | map(select(.status==\"completed\" and .conclusion==\"success\")) | .[0].id // empty'"
local id = exec_capture(jq_cmd):gsub("%s+$", "")
if id == "" then
die("Nie znaleziono żadnego zakończonego sukcesem uruchomienia workflow w " .. REPO)
end
return id
end
-- Pobiera listę artefaktów dla danego run_id, zwraca tabelę
-- { {id=, name=, size=, url=}, ... }
local function get_artifacts(run_id)
info("Pobieranie listy artefaktów dla uruchomienia #" .. run_id .. "...")
local out = api_get(API_BASE .. "/actions/runs/" .. run_id .. "/artifacts?per_page=100")
local jq_cmd = "echo " .. string.format("%q", out) ..
[[ | jq -r '.artifacts[] | "\(.id)\t\(.name)\t\(.size_in_bytes)\t\(.archive_download_url)"']]
local lines = exec_capture(jq_cmd)
local artifacts = {}
for line in lines:gmatch("[^\r\n]+") do
local id, name, size, url = line:match("^(%d+)\t(.-)\t(%d+)\t(.+)$")
if id then
table.insert(artifacts, { id = id, name = name, size = tonumber(size), url = url })
end
end
return artifacts
end
local function filter_matching(artifacts)
local matches = {}
for _, a in ipairs(artifacts) do
if a.name:lower():find(ARTIFACT_HINT) then
table.insert(matches, a)
end
end
return matches
end
local function human_size(bytes)
local units = { "B", "KB", "MB", "GB" }
local i = 1
local val = bytes
while val > 1024 and i < #units do
val = val / 1024
i = i + 1
end
return string.format("%.2f %s", val, units[i])
end
local function choose_artifact(matches)
if #matches == 0 then
die('Nie znaleziono żadnego artefaktu pasującego do "HackerOS-V4.9-official" w najnowszym uruchomieniu.')
elseif #matches == 1 then
info("Znaleziono jeden pasujący artefakt: " .. matches[1].name)
return matches[1]
end
print("\nZnaleziono kilka pasujących artefaktów:")
for i, a in ipairs(matches) do
print(string.format(" [%d] %s (%s)", i, a.name, human_size(a.size)))
end
local choice
repeat
local ans = ask("\nKtóry pobrać? Podaj numer: ")
choice = tonumber(ans)
until choice and matches[choice]
return matches[choice]
end
---------------------------------------------------------------------
-- Pobieranie
---------------------------------------------------------------------
-- Generuje unikalną nazwę kontenera dla danego etapu (download/qemu)
local function new_container_name(prefix)
return prefix .. "-" .. tostring(os.time()) .. "-" .. tostring(math.random(1000, 9999))
end
-- Usuwa (jeśli istnieje) kontener podman o podanej nazwie
local function remove_container(name)
info("Usuwam kontener podman: " .. name)
exec_capture('podman rm -f "' .. name .. '" >/dev/null 2>&1')
end
-- Buduje fragment nagłówka autoryzacji dla curl/aria2c - pomijany,
-- jeśli TOKEN nie jest (jeszcze) ustawiony.
local function bearer_curl_header()
if TOKEN then return '-H "Authorization: Bearer ' .. TOKEN .. '"' end
return ""
end
local function bearer_aria2_header()
if TOKEN then return '--header="Authorization: Bearer ' .. TOKEN .. '"' end
return ""
end
-- Pobieranie za pomocą aria2c uruchomionego wewnątrz kontenera podman
-- (używane gdy aria2c nie jest zainstalowany lokalnie, a użytkownik
-- zgodził się na pobieranie w kontenerze). Kontener jest usuwany zaraz
-- po zakończeniu pobierania. Zwraca true/false (nie przerywa programu).
local function download_via_container_aria2c(artifact, zip_name)
local container_name = new_container_name("hackeros-dl")
info("Pobieranie w kontenerze podman (aria2c)...")
local inner_cmd = string.format(
'apk add --no-cache aria2 >/dev/null 2>&1 || ' ..
'(apt-get update -qq && apt-get install -y -qq aria2 >/dev/null 2>&1) || ' ..
'(dnf install -y -q aria2 >/dev/null 2>&1); ' ..
'aria2c -x16 -s16 -k1M %s --header="Accept: application/vnd.github+json" ' ..
'-d /work -o "%s" "%s"',
bearer_aria2_header(), zip_name, artifact.url
)
local cmd = string.format(
'podman run --name %s -v "%s":/work:Z docker.io/library/alpine:latest sh -c %s',
container_name, WORKDIR, string.format("%q", inner_cmd)
)
local ok = run(cmd)
remove_container(container_name)
return ok
end
-- Sprawdza, czy plik jest poprawnym, niepustym archiwum zip
local function is_valid_zip(path)
local out = exec_capture(string.format('test -s "%s" && unzip -tq "%s" >/dev/null 2>&1 && echo VALID', path, path))
return out:find("VALID") ~= nil
end
-- Pobiera artefakt. Skrypt NIE wymaga tokenu z góry - próbuje pobrać
-- bez niego, a dopiero gdy wykryje, że pobieranie się nie powiodło
-- (co GitHub sygnalizuje właśnie wtedy, gdy token jest wymagany),
-- PYTA O TOKEN W TRAKCIE DZIAŁANIA i automatycznie ponawia próbę.
local function download_artifact(artifact)
mkdirp(WORKDIR)
local zip_name = artifact.id .. ".zip"
local zip_path = WORKDIR .. "/" .. zip_name
info("Pobieranie artefaktu: " .. artifact.name .. " (" .. human_size(artifact.size) .. ")")
-- Metodę pobierania wybieramy raz, niezależnie od tego, czy token
-- będzie potrzebny.
local method = "curl"
if has_cmd("aria2c") then
method = "aria2c_local"
info("Używam lokalnego aria2c (pobieranie wieloczęściowe, szybsze).")
else
warn("aria2c nie jest zainstalowany lokalnie.")
local use_container = ask_yes_no(
"Czy pobrać w kontenerze podman z zainstalowanym aria2c (szybciej niż curl)?",
true
)
if use_container and has_cmd("podman") then
method = "aria2c_container"
elseif use_container then
warn("Nie znaleziono podman - przechodzę na curl lokalnie.")
end
end
local function attempt()
os.remove(zip_path) -- usuń pozostałości po ewentualnej nieudanej próbie
if method == "aria2c_local" then
local cmd = string.format(
'aria2c -x16 -s16 -k1M %s --header="Accept: application/vnd.github+json" ' ..
'-o "%s" -d "%s" "%s"',
bearer_aria2_header(), zip_name, WORKDIR, artifact.url
)
return run(cmd)
elseif method == "aria2c_container" then
return download_via_container_aria2c(artifact, zip_name)
else
local cmd = string.format(
'curl -sL %s -H "Accept: application/vnd.github+json" -o "%s" "%s"',
bearer_curl_header(), zip_path, artifact.url
)
return run(cmd)
end
end
attempt()
if not is_valid_zip(zip_path) and not TOKEN then
warn("Pobrany plik jest nieprawidłowy - GitHub wymaga tokenu do pobrania artefaktów Actions.")
local t = ask("Podaj token GitHub (Personal Access Token z uprawnieniem 'actions:read'): ")
if t == "" then
die("Bez tokenu nie można pobrać artefaktu.")
end
TOKEN = t
info("Ponawiam pobieranie z podanym tokenem...")
attempt()
end
if not is_valid_zip(zip_path) then
die("Pobieranie nie powiodło się - sprawdź token, uprawnienia i połączenie sieciowe.")
end
info("Pobieranie zakończone: " .. zip_path)
return zip_path
end
---------------------------------------------------------------------
-- Rozpakowywanie (rekurencyjne, aż do znalezienia .iso)
---------------------------------------------------------------------
local function extract_archive(path, dest)
mkdirp(dest)
local lower = path:lower()
local cmd
if lower:match("%.zip$") then
cmd = string.format('unzip -o -q "%s" -d "%s"', path, dest)
elseif lower:match("%.tar%.gz$") or lower:match("%.tgz$") then
cmd = string.format('tar -xzf "%s" -C "%s"', path, dest)
elseif lower:match("%.tar%.xz$") or lower:match("%.txz$") then
cmd = string.format('tar -xJf "%s" -C "%s"', path, dest)
elseif lower:match("%.tar%.bz2$") then
cmd = string.format('tar -xjf "%s" -C "%s"', path, dest)
elseif lower:match("%.tar$") then
cmd = string.format('tar -xf "%s" -C "%s"', path, dest)
elseif lower:match("%.gz$") then
cmd = string.format('gzip -dk "%s" -c > "%s/%s"', path, dest, path:match("([^/]+)%.gz$"))
elseif lower:match("%.xz$") then
cmd = string.format('xz -dk "%s" -c > "%s/%s"', path, dest, path:match("([^/]+)%.xz$"))
elseif lower:match("%.7z$") then
if not has_cmd("7z") then
die("Znaleziono archiwum .7z, ale narzędzie 7z nie jest zainstalowane.")
end
cmd = string.format('7z x -o"%s" -y "%s" >/dev/null', dest, path)
else
return nil -- nie jest archiwum obsługiwanym przez tę funkcję
end
local ok = run(cmd)
if not ok then die("Rozpakowywanie nie powiodło się: " .. path) end
return dest
end
local function find_files(dir, pattern)
local out = exec_capture(string.format('find "%s" -type f -iname "%s"', dir, pattern))
local files = {}
for line in out:gmatch("[^\r\n]+") do
table.insert(files, line)
end
return files
end
-- Rozpakowuje wielopoziomowo, aż znajdzie plik(i) .iso, zwraca listę ścieżek do ISO
local function unpack_until_iso(zip_path)
local extract_dir = WORKDIR .. "/extracted"
mkdirp(extract_dir)
info("Rozpakowywanie artefaktu...")
extract_archive(zip_path, extract_dir)
-- Pętla: dopóki nie ma iso, próbuj rozpakować kolejne znalezione archiwa
for _ = 1, 5 do -- maks. 5 poziomów zagnieżdżenia, zabezpieczenie przed pętlą
local isos = find_files(extract_dir, "*.iso")
if #isos > 0 then return isos end
local archive_exts = { "*.zip", "*.tar.gz", "*.tgz", "*.tar.xz", "*.txz",
"*.tar.bz2", "*.tar", "*.gz", "*.xz", "*.7z" }
local found_any = false
for _, ext in ipairs(archive_exts) do
for _, f in ipairs(find_files(extract_dir, ext)) do
found_any = true
info("Znaleziono zagnieżdżone archiwum: " .. f .. " - rozpakowuję...")
extract_archive(f, extract_dir)
end
end
if not found_any then break end
end
local isos = find_files(extract_dir, "*.iso")
if #isos == 0 then
die("Nie znaleziono pliku .iso po rozpakowaniu artefaktu.")
end
return isos
end
local function choose_iso(isos)
if #isos == 1 then return isos[1] end
print("\nZnaleziono kilka plików ISO:")
for i, f in ipairs(isos) do
print(string.format(" [%d] %s", i, f))
end
local choice
repeat
local ans = ask("\nKtóry uruchomić? Podaj numer: ")
choice = tonumber(ans)
until choice and isos[choice]
return isos[choice]
end
---------------------------------------------------------------------
-- Uruchamianie QEMU
---------------------------------------------------------------------
local function run_qemu_locally(iso_path)
info("Uruchamiam QEMU lokalnie: " .. iso_path)
local kvm_flag = ""
if exec_capture("test -e /dev/kvm && echo ok"):find("ok") then
kvm_flag = "-enable-kvm"
else
warn("Brak /dev/kvm - emulacja będzie wolniejsza (bez akceleracji sprzętowej).")
end
local cmd = string.format(
'qemu-system-x86_64 -m 4096 %s -cdrom "%s" -boot d -vga virtio -display gtk',
kvm_flag, iso_path
)
return run(cmd)
end
-- Uruchamia QEMU wewnątrz kontenera podman (jedyne wspierane narzędzie
-- kontenerowe - świadomie NIE używamy docker). Kontener otrzymuje nazwę
-- i jest jawnie usuwany zaraz po zakończeniu działania QEMU.
local function run_qemu_in_container(iso_path)
if not has_cmd("podman") then
die("Nie znaleziono podman - nie mogę utworzyć kontenera z QEMU (obsługiwany jest wyłącznie podman).")
end
local container_name = new_container_name("hackeros-qemu")
info("Tworzę kontener podman '" .. container_name .. "' z QEMU...")
local iso_dir = iso_path:match("^(.*)/[^/]+$")
local iso_name = iso_path:match("[^/]+$")
local kvm_mount = ""
if exec_capture("test -e /dev/kvm && echo ok"):find("ok") then
kvm_mount = "--device /dev/kvm"
else
warn("Brak /dev/kvm na hoście - w kontenerze QEMU zadziała bez akceleracji sprzętowej.")
end
local inner_cmd =
"dnf install -y qemu-kvm >/dev/null 2>&1 || " ..
"(apt-get update -qq && apt-get install -y -qq qemu-system-x86 >/dev/null 2>&1); " ..
"qemu-system-x86_64 -m 4096 -cdrom /iso/" .. iso_name .. " -boot d -display gtk"
local cmd = string.format(
'podman run --name %s -it %s -v "%s":/iso:Z docker.io/library/fedora:latest bash -c %s',
container_name, kvm_mount, iso_dir, string.format("%q", inner_cmd)
)
local ok = run(cmd)
-- Kontener usuwany jawnie po zakończeniu pracy QEMU (niezależnie od wyniku)
remove_container(container_name)
return ok
end
local function launch_iso(iso_path)
if has_cmd("qemu-system-x86_64") then
return run_qemu_locally(iso_path)
end
warn("QEMU (qemu-system-x86_64) nie jest zainstalowane w systemie.")
local wants_auto = ask_yes_no(
"Czy mam automatycznie utworzyć kontener, zainstalować w nim QEMU i uruchomić obraz?",
true
)
if not wants_auto then
die("Zainstaluj QEMU ręcznie (np. 'sudo pacman -S qemu-full' lub 'sudo apt install qemu-system-x86') i uruchom skrypt ponownie.")
end
return run_qemu_in_container(iso_path)
end
---------------------------------------------------------------------
-- Sprzątanie
---------------------------------------------------------------------
local function cleanup(iso_path, zip_path)
info("Sprzątanie plików tymczasowych...")
if iso_path then
os.remove(iso_path)
end
if zip_path then
os.remove(zip_path)
end
exec_capture(string.format('rm -rf "%s/extracted"', WORKDIR))
info("Gotowe. Usunięto pobrany obraz ISO i pliki tymczasowe.")
end
---------------------------------------------------------------------
-- Główny przebieg programu
---------------------------------------------------------------------
local function main()
check_dependencies()
mkdirp(WORKDIR)
local run_id = get_latest_successful_run_id()
local artifacts = get_artifacts(run_id)
local matches = filter_matching(artifacts)
local artifact = choose_artifact(matches)
local zip_path = download_artifact(artifact)
local isos = unpack_until_iso(zip_path)
local iso_path = choose_iso(isos)
print("")
info("Uruchamianie obrazu: " .. iso_path)
local ok = launch_iso(iso_path)
if not ok then
warn("QEMU zakończyło pracę z błędem (lub zostało przerwane).")
end
cleanup(iso_path, zip_path)
end
main()