Toby Chui 3 роки тому
батько
коміт
073463b014
4 змінених файлів з 99 додано та 32 видалено
  1. 20 0
      fs.go
  2. BIN
      launcher.exe
  3. 35 32
      main.go
  4. 44 0
      update.go

+ 20 - 0
fs.go

@@ -7,8 +7,28 @@ import (
 	"os"
 	"path/filepath"
 	"runtime"
+
+	cp "github.com/otiai10/copy"
 )
 
+func restoreConfigs() {
+	restoreIfExists("system/bridge.json")
+	restoreIfExists("system/dev.uuid")
+	restoreIfExists("system/cron.json")
+	restoreIfExists("system/storage.json")
+	restoreIfExists("web/SystemAO/vendor/")
+}
+
+func restoreIfExists(fileRelPath string) {
+	if fileExists(filepath.Join("arozos.old", fileRelPath)) {
+		if !isDir(filepath.Join("arozos.old", fileRelPath)) {
+			copy(filepath.Join("arozos.old", fileRelPath), fileRelPath)
+		} else {
+			cp.Copy(filepath.Join("arozos.old", fileRelPath), fileRelPath)
+		}
+	}
+}
+
 //Auto detect and execute the correct binary
 func autoDetectExecutable() string {
 	if runtime.GOOS == "windows" {


+ 35 - 32
main.go

@@ -2,11 +2,10 @@ package main
 
 import (
 	"fmt"
+	"net/http"
 	"os"
 	"os/exec"
-	"path/filepath"
-
-	cp "github.com/otiai10/copy"
+	"time"
 )
 
 /*
@@ -21,6 +20,10 @@ const (
 	launcherVersion = "1.0"
 )
 
+var (
+	arozosRunning bool = false
+)
+
 func main() {
 	//Print basic information
 	fmt.Println("[LAUNCHER] ArozOS Launcher ver " + launcherVersion)
@@ -28,38 +31,38 @@ func main() {
 	fmt.Println("[LAUNCHER] Choosing binary executable: " + binaryName)
 
 	//Check if updates exists. If yes, overwrite it
-	if fileExists("./updates") && fileExists("./updates/web/") && fileExists("./updates/system") {
-		//All component exists. Update it
-		newArozBinary, err := getUpdateBinaryFilename()
-		if err != nil {
-			fmt.Println("[LAUNCHER] Unable to access update files: ", err.Error())
-		} else {
-			//Binary file got. Update it
-			//Backup the current executables and system files
-			fmt.Println("[LAUNCHER] Starting system backup process (to ./arozos.old)")
-			os.MkdirAll("./arozos.old", 0775)
-			copy(binaryName, filepath.Join("./arozos.old", filepath.Base(binaryName)))
-			cp.Copy("./system", "./arozos.old/system/")
-			cp.Copy("./web", "./arozos.old/web/")
-
-			//Success. Continue binary replacement
-			fmt.Println("[LAUNCHER] Copying updates to runtime environment")
-			copy(newArozBinary, binaryName)
-			cp.Copy("./updates/system", "./system/")
-			cp.Copy("./updates/web", "./web/")
-
-			fmt.Println("[LAUNCHER] Update Completed. Removing the update files")
-			os.RemoveAll("./updates/")
-		}
-
-	} else if fileExists("./updates") && (!fileExists("./updates/web/") || !fileExists("./updates/system")) {
-		//Update folder exists but some components is broken
-		fmt.Println("[LAUNCHER] Detected damaged / incomplete update package. Skipping update process")
-	}
+	updateIfExists(binaryName)
 
+	//Register the binary start path
 	cmd := exec.Command(binaryName, os.Args[1:]...)
 	cmd.Stdout = os.Stdout
 	cmd.Stderr = os.Stderr
-	cmd.Run()
+
+	//Register the http server to notify ArozOS there is a launcher will handle the update
+	go func() {
+		http.HandleFunc("/chk", func(w http.ResponseWriter, r *http.Request) {
+			w.Write([]byte("LauncherA v" + launcherVersion))
+			fmt.Println("[LAUNCHER] CHK RECV - DONE")
+		})
+
+		http.ListenAndServe("127.0.0.1:25576", nil)
+	}()
+
+	for {
+
+	}
+
+	//Start the cmd
+	for {
+		cmd.Run()
+		fmt.Println("[LAUNCHER] ArozOS Exited. Restarting in 3 seconds... ")
+		time.Sleep(3 * time.Second)
+		updateIfExists(binaryName)
+
+		//Rebuild the start paramters
+		cmd = exec.Command(binaryName, os.Args[1:]...)
+		cmd.Stdout = os.Stdout
+		cmd.Stderr = os.Stderr
+	}
 
 }

+ 44 - 0
update.go

@@ -0,0 +1,44 @@
+package main
+
+import (
+	"fmt"
+	"os"
+	"path/filepath"
+
+	cp "github.com/otiai10/copy"
+)
+
+func updateIfExists(binaryName string) {
+	if fileExists("./updates") && fileExists("./updates/web/") && fileExists("./updates/system") {
+		//All component exists. Update it
+		newArozBinary, err := getUpdateBinaryFilename()
+		if err != nil {
+			fmt.Println("[LAUNCHER] Unable to access update files: ", err.Error())
+		} else {
+			//Binary file got. Update it
+			//Backup the current executables and system files
+			fmt.Println("[LAUNCHER] Starting system backup process (to ./arozos.old)")
+			os.MkdirAll("./arozos.old", 0775)
+			copy(binaryName, filepath.Join("./arozos.old", filepath.Base(binaryName)))
+			cp.Copy("./system", "./arozos.old/system/")
+			cp.Copy("./web", "./arozos.old/web/")
+
+			//Success. Continue binary replacement
+			fmt.Println("[LAUNCHER] Copying updates to runtime environment")
+			copy(newArozBinary, binaryName)
+			cp.Copy("./updates/system", "./system/")
+			cp.Copy("./updates/web", "./web/")
+
+			//Restore the configs from the arozos.old
+			fmt.Println("[LAUNCHER] Restoring previous configurations")
+			restoreConfigs()
+
+			fmt.Println("[LAUNCHER] Update Completed. Removing the update files")
+			os.RemoveAll("./updates/")
+		}
+
+	} else if fileExists("./updates") && (!fileExists("./updates/web/") || !fileExists("./updates/system")) {
+		//Update folder exists but some components is broken
+		fmt.Println("[LAUNCHER] Detected damaged / incomplete update package. Skipping update process")
+	}
+}