self_update.lua
152 lines ยท 4.0 KB
eHydra Self-Update System
Copy & run
wget https://perlytiara.github.io/turtles.tips/raw/programs/perlytiara/eHydra_wip/self_update.lua
| 1 | -- eHydra Self-Update System |
| 2 | -- Auto-updates all eHydra programs from GitHub repository |
| 3 | |
| 4 | local baseUrl = "https://raw.githubusercontent.com/perlytiara/CC-Tweaked-TurtsAndComputers/refs/heads/main/programs/perlytiara/eHydra/" |
| 5 | |
| 6 | -- eHydra programs to auto-update |
| 7 | local eHydraPrograms = { |
| 8 | "autoupdater.lua", |
| 9 | "batch_updater.lua", |
| 10 | "init.lua", |
| 11 | "turtle_deployer.lua", |
| 12 | "startup.lua", |
| 13 | "self_update.lua", -- Self-update capability |
| 14 | "README.md" |
| 15 | } |
| 16 | |
| 17 | local function backupFile(filepath) |
| 18 | if fs.exists(filepath) then |
| 19 | local backupPath = filepath .. ".backup" |
| 20 | if fs.exists(backupPath) then |
| 21 | fs.delete(backupPath) |
| 22 | end |
| 23 | fs.copy(filepath, backupPath) |
| 24 | print(" ๐ฆ Backed up: " .. filepath) |
| 25 | return true |
| 26 | end |
| 27 | return false |
| 28 | end |
| 29 | |
| 30 | local function restoreFile(filepath) |
| 31 | local backupPath = filepath .. ".backup" |
| 32 | if fs.exists(backupPath) then |
| 33 | if fs.exists(filepath) then |
| 34 | fs.delete(filepath) |
| 35 | end |
| 36 | fs.copy(backupPath, filepath) |
| 37 | fs.delete(backupPath) |
| 38 | print(" ๐ Restored: " .. filepath) |
| 39 | return true |
| 40 | end |
| 41 | return false |
| 42 | end |
| 43 | |
| 44 | local function cleanupBackups() |
| 45 | print("Cleaning up backup files...") |
| 46 | for _, filename in ipairs(eHydraPrograms) do |
| 47 | local backupPath = filename .. ".backup" |
| 48 | if fs.exists(backupPath) then |
| 49 | fs.delete(backupPath) |
| 50 | print(" ๐๏ธ Removed backup: " .. backupPath) |
| 51 | end |
| 52 | end |
| 53 | end |
| 54 | |
| 55 | print("eHydra Self-Update System v1.0") |
| 56 | print("==============================") |
| 57 | print("This will update all eHydra programs from GitHub") |
| 58 | print() |
| 59 | |
| 60 | write("Proceed with update? (y/n) [y]: ") |
| 61 | local confirm = string.lower(read()) |
| 62 | if confirm == "n" then |
| 63 | print("Update cancelled.") |
| 64 | return |
| 65 | end |
| 66 | |
| 67 | print() |
| 68 | print("Starting eHydra self-update...") |
| 69 | print("Repository: " .. baseUrl) |
| 70 | print("Files to update: " .. #eHydraPrograms) |
| 71 | print() |
| 72 | |
| 73 | local success = 0 |
| 74 | local failed = 0 |
| 75 | local backups = {} |
| 76 | |
| 77 | -- Create backups first |
| 78 | print("๐ฆ Creating backups...") |
| 79 | for _, filename in ipairs(eHydraPrograms) do |
| 80 | if backupFile(filename) then |
| 81 | table.insert(backups, filename) |
| 82 | end |
| 83 | end |
| 84 | |
| 85 | print() |
| 86 | print("๐ฅ Downloading updates...") |
| 87 | |
| 88 | for i, filename in ipairs(eHydraPrograms) do |
| 89 | print("[" .. i .. "/" .. #eHydraPrograms .. "] Updating " .. filename .. "...") |
| 90 | |
| 91 | local url = baseUrl .. filename |
| 92 | local response = http.get(url) |
| 93 | |
| 94 | if response then |
| 95 | -- Delete existing file |
| 96 | if fs.exists(filename) then |
| 97 | fs.delete(filename) |
| 98 | end |
| 99 | |
| 100 | -- Write new content |
| 101 | local file = fs.open(filename, "w") |
| 102 | if file then |
| 103 | file.write(response.readAll()) |
| 104 | file.close() |
| 105 | response.close() |
| 106 | print(" โ " .. filename .. " updated successfully") |
| 107 | success = success + 1 |
| 108 | else |
| 109 | print(" โ " .. filename .. " - failed to write file") |
| 110 | -- Restore from backup |
| 111 | restoreFile(filename) |
| 112 | failed = failed + 1 |
| 113 | end |
| 114 | else |
| 115 | print(" โ " .. filename .. " - download failed") |
| 116 | -- Restore from backup |
| 117 | restoreFile(filename) |
| 118 | failed = failed + 1 |
| 119 | end |
| 120 | end |
| 121 | |
| 122 | print() |
| 123 | print("๐ Update Summary") |
| 124 | print("================") |
| 125 | print("โ Successfully updated: " .. success .. " files") |
| 126 | print("โ Failed updates: " .. failed .. " files") |
| 127 | |
| 128 | if failed == 0 then |
| 129 | print("๐ All updates completed successfully!") |
| 130 | cleanupBackups() |
| 131 | else |
| 132 | print("โ ๏ธ Some updates failed - backups preserved") |
| 133 | print(" To restore all backups, run: restore_backups") |
| 134 | end |
| 135 | |
| 136 | print() |
| 137 | if success > 0 then |
| 138 | print("๐ eHydra system has been updated!") |
| 139 | print(" Restart any running eHydra programs to use new versions") |
| 140 | |
| 141 | -- Offer to restart |
| 142 | print() |
| 143 | write("Restart eHydra startup menu? (y/n) [n]: ") |
| 144 | local restart = string.lower(read()) |
| 145 | if restart == "y" then |
| 146 | print("Restarting eHydra...") |
| 147 | shell.run("startup") |
| 148 | end |
| 149 | else |
| 150 | print("No files were updated.") |
| 151 | end |
| 152 |