update.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. cp "github.com/otiai10/copy"
  7. )
  8. func updateIfExists(binaryName string) {
  9. if fileExists("./updates") && fileExists("./updates/web/") && fileExists("./updates/system") {
  10. //All component exists. Update it
  11. newArozBinary, err := getUpdateBinaryFilename()
  12. if err != nil {
  13. fmt.Println("[LAUNCHER] Unable to access update files: ", err.Error())
  14. } else {
  15. //Binary file got. Update it
  16. //Backup the current executables and system files
  17. fmt.Println("[LAUNCHER] Starting system backup process (to ./arozos.old)")
  18. os.MkdirAll("./arozos.old", 0775)
  19. copy(binaryName, filepath.Join("./arozos.old", filepath.Base(binaryName)))
  20. //Backup the start.sh script and start.bat script if exists
  21. if fileExists("./start.sh") {
  22. copy("./start.sh", "./arozos.old/start.sh")
  23. }
  24. if fileExists("./start.bat") {
  25. copy("./start.bat", "./arozos.old/start.bat")
  26. }
  27. //Backup the important system files
  28. fmt.Println("[LAUNCHER] Backing up system files")
  29. cp.Copy("./system", "./arozos.old/system/")
  30. fmt.Println("[LAUNCHER] Backing up web server")
  31. cp.Copy("./web", "./arozos.old/web/")
  32. //Success. Continue binary replacement
  33. fmt.Println("[LAUNCHER] Copying updates to runtime environment")
  34. copy(newArozBinary, binaryName)
  35. cp.Copy("./updates/system", "./system/")
  36. cp.Copy("./updates/web", "./web/")
  37. //Restore the configs from the arozos.old
  38. fmt.Println("[LAUNCHER] Restoring previous configurations")
  39. restoreConfigs()
  40. fmt.Println("[LAUNCHER] Update Completed. Removing the update files")
  41. os.RemoveAll("./updates/")
  42. }
  43. } else if fileExists("./updates") && (!fileExists("./updates/web/") || !fileExists("./updates/system")) {
  44. //Update folder exists but some components is broken
  45. fmt.Println("[LAUNCHER] Detected damaged / incomplete update package. Skipping update process")
  46. }
  47. }