Procházet zdrojové kódy

Added update pending and vendor update function

Toby Chui před 3 roky
rodič
revize
2112ca24f6
4 změnil soubory, kde provedl 211 přidání a 45 odebrání
  1. 64 19
      mod/updates/handler.go
  2. 29 26
      system.info.go
  3. 29 0
      system/update.json
  4. 89 0
      web/SystemAO/updates/index.html

+ 64 - 19
mod/updates/handler.go

@@ -3,13 +3,46 @@ package updates
 import (
 	"encoding/json"
 	"fmt"
+	"io/ioutil"
+	"log"
 	"net/http"
+	"runtime"
 	"time"
 
 	"github.com/gorilla/websocket"
 	"imuslab.com/arozos/mod/common"
 )
 
+type UpdateConfig struct {
+	Vendor string `json:"vendor"`
+	Binary struct {
+		Windows struct {
+			Amd64 string `json:"amd64"`
+			Arm   string `json:"arm"`
+			Arm64 string `json:"arm64"`
+			I386  string `json:"i386"`
+		} `json:"windows"`
+		Linux struct {
+			Arm    string `json:"arm"`
+			Armv7  string `json:"armv7"`
+			Arm64  string `json:"arm64"`
+			Amd64  string `json:"amd64"`
+			Mipsle string `json:"mipsle"`
+		} `json:"linux"`
+		Darwin struct {
+			Amd64 string `json:"amd64"`
+			Arm64 string `json:"arm64"`
+		} `json:"darwin"`
+		Freebsd struct {
+			Amd64 string `json:"amd64"`
+			Arm   string `json:"arm"`
+			Arm64 string `json:"arm64"`
+			I386  string `json:"i386"`
+		} `json:"freebsd"`
+	} `json:"binary"`
+	Webpack string `json:"webpack"`
+}
+
 func HandleUpdateCheckSize(w http.ResponseWriter, r *http.Request) {
 	webpack, err := common.Mv(r, "webpack", true)
 	if err != nil {
@@ -98,34 +131,46 @@ func HandleUpdateDownloadRequest(w http.ResponseWriter, r *http.Request) {
 
 }
 
-/*
-func HandleLauncherRestart(w http.ResponseWriter, r *http.Request) {
-	//Check if there is a launcher listening to port 25576
-	resp, err := http.Get("http://127.0.0.1:25576/chk")
+//Handle getting information for vendor update
+func HandleGetUpdatePlatformInfo(w http.ResponseWriter, r *http.Request) {
+	type UpdatePackageInfo struct {
+		Config UpdateConfig
+		OS     string
+		ARCH   string
+	}
+
+	//Check if update config find. If yes, parse that
+	updateFileContent, err := ioutil.ReadFile("./system/update.json")
 	if err != nil {
-		common.SendErrorResponse(w, "No launcher found. Unable to restart")
+		common.SendErrorResponse(w, "No vendor update config found")
 		return
 	}
 
-	content, err := ioutil.ReadAll(resp.Body)
+	//Read from the update config
+	vendorUpdateConfig := UpdateConfig{}
+	err = json.Unmarshal(updateFileContent, &vendorUpdateConfig)
 	if err != nil {
-		common.SendErrorResponse(w, "Read launcher response failed")
+		log.Println("[Updates] Failed to parse update config file: ", err.Error())
+		common.SendErrorResponse(w, "Invalid or corrupted update config")
 		return
 	}
 
-	execute, _ := common.Mv(r, "exec", true)
-	if execute == "true" && r.Method == http.MethodPost {
-		//Do the update
-		log.Println("REQUESTING LAUNCHER FOR UPDATE RESTART")
+	updateinfo := UpdatePackageInfo{
+		Config: vendorUpdateConfig,
+		OS:     runtime.GOOS,
+		ARCH:   runtime.GOARCH,
+	}
 
-		common.SendOK(w)
-	} else if execute == "true" {
-		w.WriteHeader(http.StatusMethodNotAllowed)
-		w.Write([]byte("405 - Method Not Allowed"))
+	js, _ := json.Marshal(updateinfo)
+	common.SendJSONResponse(w, string(js))
+}
+
+//Handle check if there is a pending update
+func HandlePendingCheck(w http.ResponseWriter, r *http.Request) {
+	if common.FileExists("./updates/") && common.FileExists("./updates/web/") && common.FileExists("./updates/system/") {
+		//Update is pending
+		common.SendJSONResponse(w, "true")
 	} else {
-		//Return the launcher message
-		common.SendTextResponse(w, string(content))
+		common.SendJSONResponse(w, "false")
 	}
-
 }
-*/

+ 29 - 26
system.info.go

@@ -122,36 +122,39 @@ func SystemInfoInit() {
 				Group:    "Info",
 				StartDir: "SystemAO/updates/index.html",
 			})
-		}
-	}()
 
-	//Handle updates
-	adminRouter.HandleFunc("/system/update/download", updates.HandleUpdateDownloadRequest)
-	adminRouter.HandleFunc("/system/update/checksize", updates.HandleUpdateCheckSize)
+			//Register Update Functions
+			adminRouter.HandleFunc("/system/update/download", updates.HandleUpdateDownloadRequest)
+			adminRouter.HandleFunc("/system/update/checksize", updates.HandleUpdateCheckSize)
+			adminRouter.HandleFunc("/system/update/checkpending", updates.HandlePendingCheck)
+			adminRouter.HandleFunc("/system/update/platform", updates.HandleGetUpdatePlatformInfo)
+
+			//Special function for handling launcher restart, must be in this scope
+			adminRouter.HandleFunc("/system/update/restart", func(w http.ResponseWriter, r *http.Request) {
+				launcherVersion, err := updates.GetLauncherVersion()
+				if err != nil {
+					sendErrorResponse(w, err.Error())
+					return
+				}
+				execute, _ := common.Mv(r, "exec", true)
+				if execute == "true" && r.Method == http.MethodPost {
+					//Do the update
+					log.Println("REQUESTING LAUNCHER FOR UPDATE RESTART")
+					executeShutdownSequence()
+					common.SendOK(w)
+				} else if execute == "true" {
+					//Prevent redirection attack
+					w.WriteHeader(http.StatusMethodNotAllowed)
+					w.Write([]byte("405 - Method Not Allowed"))
+				} else {
+					//Return the launcher message
+					common.SendTextResponse(w, string(launcherVersion))
+				}
 
-	//Special function for handling launcher restart, must be in this scope
-	adminRouter.HandleFunc("/system/update/restart", func(w http.ResponseWriter, r *http.Request) {
-		launcherVersion, err := updates.GetLauncherVersion()
-		if err != nil {
-			sendErrorResponse(w, err.Error())
-			return
-		}
-		execute, _ := common.Mv(r, "exec", true)
-		if execute == "true" && r.Method == http.MethodPost {
-			//Do the update
-			log.Println("REQUESTING LAUNCHER FOR UPDATE RESTART")
-			executeShutdownSequence()
-			common.SendOK(w)
-		} else if execute == "true" {
-			//Prevent redirection attack
-			w.WriteHeader(http.StatusMethodNotAllowed)
-			w.Write([]byte("405 - Method Not Allowed"))
-		} else {
-			//Return the launcher message
-			common.SendTextResponse(w, string(launcherVersion))
+			})
 		}
+	}()
 
-	})
 }
 
 func InfoHandleGetRuntimeInfo(w http.ResponseWriter, r *http.Request) {

+ 29 - 0
system/update.json

@@ -0,0 +1,29 @@
+{
+	"Vendor":"ArozOS Github",
+	"binary":{
+		"windows": {
+			"amd64":"https://github.com/tobychui/arozos/releases/latest/download/arozos_windows_amd64.exe",
+			"arm":"",
+			"arm64":"",
+			"i386":""
+		},
+		"linux":{
+			"arm":"https://github.com/tobychui/arozos/releases/latest/download/arozos_linux_arm",
+			"armv7":"https://github.com/tobychui/arozos/releases/latest/download/arozos_linux_armv7",
+			"arm64":"https://github.com/tobychui/arozos/releases/latest/download/arozos_linux_arm64",
+			"amd64":"https://github.com/tobychui/arozos/releases/latest/download/arozos_linux_amd64",
+			"mipsle":""
+		},
+		"darwin":{
+			"amd64":"https://github.com/tobychui/arozos/releases/latest/download/arozos_darwin_amd64",
+			"arm64":""
+		},
+		"freebsd":{
+			"amd64":"",
+			"arm":"",
+			"arm64":"",
+			"i386":""
+		}
+	},
+	"webpack":"https://github.com/tobychui/arozos/releases/latest/download/web.tar.gz"
+}

+ 89 - 0
web/SystemAO/updates/index.html

@@ -77,6 +77,15 @@
                     <p>Restart ArozOS using the launcher or apply manual update files overwrite to finish the update process.</p>
                 </div>
             </div>
+            <div id="pending" class="ui icon green message" style="display:none;">
+                <i class="green clock icon"></i>
+                <div class="content">
+                    <div class="header" id="downloadStatusText">
+                        Update Pending
+                    </div>
+                    <p>Restart ArozOS using the launcher or apply manual update files overwrite to finish the update process.</p>
+                </div>
+            </div>
             <div id="failed" class="ui icon red message" style="display:none;">
                 <i class="red remove icon"></i>
                 <div class="content">
@@ -96,6 +105,12 @@
                 <button class="ui button" onclick="restartLater();">Restart Later</button>
             </div>
             <!-- End of Status Messages -->
+            <div class="vendorupdate">
+                <h4>Update from <span class="vendorName">Vendor</span> (<i class="yellow star icon"></i> Recommended)</h4>
+                <p>Update from <span class="vendorName">your vendor</span> based on the system update configuration that ship with this machine</p>
+                <button class="ui blue button updateBtn" onclick="updateViaVendor();"><i class="cloud upload icon"></i>  Update from Vendor</button>
+                <div class="ui divider"></div>
+            </div>
             <h4>Update via Download</h4>
             <p>Binary Executable Download URL</p>
             <div class="ui fluid input">
@@ -156,7 +171,74 @@
         </div>
     </div>
     <script>
+        var useVendorUpdate = false;
+        var vendorUpdateBinaryURL = "";
+        var vendorUpdateWebpackURL = "";
+
         $(".accordion").accordion();
+        initVendorUpdateInfo();
+        checkPendingUpdates();
+
+        function checkPendingUpdates(){
+            $.get("../../system/update/checkpending", function(data){
+                if (data == true){
+                    //There is a pending update.
+                    hideAllStatus();
+                    $("#pending").slideDown('fast');
+                    $.get("/system/update/restart", function(data){
+                        if (data.error !== undefined){
+                            //No launcher
+                        }else{
+                            $("#launcherName").text(data);
+                            $("#restartPanel").show();
+                        }
+                        console.log("Launcher check: ", data);
+                    })
+                }
+            })
+        }
+
+        function initVendorUpdateInfo(){
+            $.get("../../system/update/platform", function(data){
+                console.log(data);
+                if (data.error !== undefined){
+                    //No vendor update modes
+                    $(".vendorupdate").hide();
+                }else{
+                    if (data.Config.binary[data.OS] && data.Config.binary[data.OS][data.ARCH]){
+                        //This update target exists
+                        vendorUpdateBinaryURL = data.Config.binary[data.OS][data.ARCH];
+                    }
+                    vendorUpdateWebpackURL = data.Config.webpack;
+                    $(".vendorName").text(data.Config.vendor);
+                }
+            })
+        }
+
+        function updateViaVendor(){
+            let binaryDownloadURL = vendorUpdateBinaryURL;
+            let webpackDownloadURL = vendorUpdateWebpackURL;
+            if (binaryDownloadURL == "" || webpackDownloadURL == ""){
+                return
+            }
+            //Check space need
+            $("#checking").slideDown("fast");
+            $("#warning").slideUp("fast");
+            useVendorUpdate = true;
+            $.get(`/system/update/checksize?webpack=${webpackDownloadURL}&binary=${binaryDownloadURL}`, function(data){
+                if (data.error != undefined){
+                    cancelUpdateStatus();
+                    alert("Update failed: " + data.error)
+                }else{
+                    let totalDownloadBytes = data[0] + data[1];
+                    $("#spaceEst").text(ao_module_utils.formatBytes(totalDownloadBytes, 2));
+                    $("#confirmDownload").slideDown("fast");
+                    $("#checking").slideUp("fast");
+                    console.log(data);
+                }
+               
+            })
+        }
 
         function updateViaURL(){
             let binaryDownloadURL = $("#burl").val().trim();
@@ -186,6 +268,13 @@
         function confirmURLUpdate(){
             let binaryDownloadURL = $("#burl").val().trim();
             let webpackDownloadURL = $("#wurl").val().trim();
+
+            if (useVendorUpdate){
+                //Use vendor link for update. Replace the download target with vendor update links
+                useVendorUpdate = false;
+                binaryDownloadURL = vendorUpdateBinaryURL;
+                webpackDownloadURL = vendorUpdateWebpackURL;
+            }
             if (binaryDownloadURL == "" || webpackDownloadURL == ""){
                 alert("Invalid or Empty URL given");
                 return