-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_check.go
186 lines (147 loc) · 3.74 KB
/
update_check.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"os/signal"
"path/filepath"
"runtime"
"syscall"
"golang.org/x/mod/semver"
gopark "github.com/debdutdeb/gopark/pkg/utils"
"github.com/debdutdeb/node-proxy/common"
st "github.com/debdutdeb/node-proxy/state"
"github.com/debdutdeb/node-proxy/versions"
)
type releasesResponse struct {
Tag string `json:"tag_name"`
Assets []asset `json:"assets"`
}
type asset struct {
Name string `json:"name"`
Url string `json:"browser_download_url"`
}
func startCheckUpdate() (cont chan struct{}, done chan struct{}) {
cont = make(chan struct{}, 1)
done = make(chan struct{}, 1)
if versions.Version == "develop" {
log.Printf("ignoring update check for development build\n")
done<-struct{}{}
return
}
// don't run on novm call
if os.Getenv("NOVM_WAKE") != "" {
// comsume the channel
go func() {
done <- struct{}{}
}()
return cont, done
}
go func() {
defer func() {
done <- struct{}{}
}()
var (
err error
req *http.Request
resp *http.Response
release releasesResponse
tmpDownload string
state *st.State
)
state, err = st.NewState()
if err != nil {
<-cont
log.Printf("[ERROR] failed to load current state: %v", err)
return
}
if !state.ShouldCheckForUpdate() {
<-cont
return
}
err = state.IncUpdateCheck()
if err != nil {
<-cont
log.Printf("[ERROR] failed to update state: %v", err)
return
}
req, err = http.NewRequest("GET", "https://api.github.com/repos/debdutdeb/novm/releases/latest", nil)
if err != nil {
<-cont
log.Printf("[ERROR] failed to fetch latest update: %v", err)
return
}
req.Header.Add("Accept", "application/vnd.github+json")
req.Header.Add("X-GitHub-Api-Version", "2022-11-28")
resp, err = (&http.Client{}).Do(req)
if err != nil {
<-cont
log.Printf("[ERROR] failed to fetch latest update: %v", err)
return
}
if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
<-cont
log.Printf("[ERROR] failed to fetch latest update: %v", err)
return
}
if semver.Compare(versions.Version, release.Tag) == -1 {
var dir string
dir, err = gopark.MkdirTemp("", common.BIN_NAME)
if err != nil {
<-cont
log.Printf("[ERROR] failed to download latest binary: %v", err)
return
}
tmpDownload = filepath.Join(dir, common.BIN_NAME)
for _, asset := range release.Assets {
if asset.Name == common.BIN_NAME+"-"+runtime.GOOS+"-"+runtime.GOARCH {
if err := gopark.DownloadSilent(asset.Url, tmpDownload); err != nil {
<-cont
log.Printf("[ERROR] failed to download latest binary: %v", err)
return
}
break
}
}
} else {
<-cont // consume
log.Println("no new novm updates found.")
return
}
// we ignore sigint and sigterm here to not lose the binary
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGQUIT)
go func() {
for {
<-sig
log.Println("ignoring signal since novm is still updating")
}
}()
<-cont // upgrade the binary
// we expect current binary to be a symlink
log.Printf("Updating novm to %s", release.Tag)
bin, err := currentExecutable()
if err != nil {
log.Fatalf("failed to get novm binary to upgrade: %v", err)
}
if err := os.Rename(tmpDownload, bin); err != nil {
log.Fatalf("failed to move download to bin path: %v", err)
}
}()
return
}
// TODO aggregate maybe
func currentExecutable() (string, error) {
path, err1 := os.Executable()
if err1 == nil {
return filepath.EvalSymlinks(path)
}
path, err2 := exec.LookPath("node")
if err2 == nil {
return filepath.EvalSymlinks(path)
}
return "", fmt.Errorf("%w, %w", err1, err2)
}