-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
107 lines (89 loc) · 2.35 KB
/
main.go
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
package main
import (
"fmt"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"time"
)
/*
ArozOS Launcher
For auto update and future extension purpose
Author: tobychui
*/
const (
launcherVersion = "2.011"
restoreRetryCount = 3 //The number of retry before restore old version, if not working after restoreRetryCount + 1 launcher will exit
)
var (
norestart bool = false
)
func main() {
//Print basic information
fmt.Println("[LAUNCHER] ArozOS Launcher ver " + launcherVersion)
binaryName := autoDetectExecutable()
fmt.Println("[LAUNCHER] Choosing binary executable: " + binaryName)
//Check if updates exists. If yes, overwrite it
updateIfExists(binaryName)
//Check launch paramter for norestart
for _, arg := range os.Args[1:] {
if arg == "-h" || arg == "-help" {
//help argument, do not restart
norestart = true
} else if arg == "-version" || arg == "-v" {
//version argument, no restart
norestart = true
}
}
//Register the binary start path
cmd := exec.Command(binaryName, os.Args[1:]...)
cmd.Dir = filepath.Dir(binaryName)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
//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)
}()
retryCounter := 0
//Start the cmd
for {
startTime := time.Now().Unix()
err := cmd.Run()
endTime := time.Now().Unix()
if err != nil {
panic(err)
}
if norestart {
return
}
if endTime-startTime < 3 {
//Less than 3 seconds, shd be crashed. Add to retry counter
fmt.Println("[LAUNCHER] ArozOS Crashed. Restarting in 3 seconds... ")
retryCounter++
} else {
fmt.Println("[LAUNCHER] ArozOS Exited. Restarting in 3 seconds... ")
retryCounter = 0
}
time.Sleep(3 * time.Second)
if retryCounter > restoreRetryCount+1 {
//Fail to start. Exit program
log.Fatal("Unable to start ArozOS. Exiting to OS")
return
} else if retryCounter > restoreRetryCount {
//Restore from old version of the binary
restoreOldArozOS()
} else {
updateIfExists(binaryName)
}
//Rebuild the start paramters
cmd = exec.Command(binaryName, os.Args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
}