-
Notifications
You must be signed in to change notification settings - Fork 1
/
pinnacle.go
405 lines (332 loc) · 9.75 KB
/
pinnacle.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package main
import (
"context"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"log"
"log/slog"
"os"
"path/filepath"
"github.com/alpine-client/pinnacle/sentry"
"github.com/alpine-client/pinnacle/ui"
)
type Pinnacle struct {
os OperatingSystem
arch Architecture
logger *slog.Logger
logFile *os.File
version string
branch string
}
type MetadataResponse struct {
Name string `json:"name"`
URL string `json:"url"`
Hash string `json:"sha1"`
Size uint32 `json:"size"`
}
type JavaManifest struct {
Hash string `json:"checksum"`
Size uint32 `json:"size"`
}
var (
metadataResponse MetadataResponse
errMissingJava = errors.New("missing java")
errMissingLauncher = errors.New("missing launcher")
)
func (p *Pinnacle) setup() error {
var err error
// Set Launcher Branch
branch := flag.String("branch", "production", "Launcher branch")
flag.Parse()
p.branch = *branch
// Setup Logger
err = os.MkdirAll(p.alpinePath("logs"), os.ModePerm) // note: creates .alpineclient AND .alpineclient/logs
if err != nil {
return err
}
p.logFile, err = os.OpenFile(p.alpinePath("logs", "updater.log"), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o666)
if err != nil {
return err
}
p.logger = slog.New(slog.NewTextHandler(io.MultiWriter(os.Stdout, os.Stderr, p.logFile), nil))
log.SetOutput(io.MultiWriter(os.Stdout, os.Stderr, p.logFile))
// Setup Sentry
p.StartSentry(version, p.fetchSentryDSN())
return nil
}
func (p *Pinnacle) cleanup(ctx context.Context, err error) {
if err == nil {
return
}
p.CaptureErr(ctx, ui.DisplayError(ctx, err, p.logFile))
p.CaptureErr(ctx, os.RemoveAll(p.alpinePath("launcher.jar")))
p.CaptureErr(ctx, os.RemoveAll(p.alpinePath("jre", "17")))
}
func (p *Pinnacle) download(ctx context.Context, err error) error {
switch {
case errors.Is(err, errMissingLauncher):
ui.Render()
return p.downloadLauncher(ctx)
case errors.Is(err, errMissingJava):
ui.Render()
return p.downloadJava(ctx)
}
return err
}
func (p *Pinnacle) error(_ context.Context, err error) error {
return err
}
func (p *Pinnacle) fetchMetadata(ctx context.Context, url string) (*MetadataResponse, error) {
resp, err := p.getFromURL(ctx, url)
if err != nil {
return nil, err
}
defer func() {
p.CaptureErr(ctx, resp.Body.Close())
}()
p.Breadcrumb(ctx, "decoding response from "+url)
err = json.NewDecoder(resp.Body).Decode(&metadataResponse)
if err != nil {
return nil, err
}
return &metadataResponse, nil
}
func (p *Pinnacle) fileHashMatches(ctx context.Context, hash string, path string) (bool, error) {
file, err := os.Open(path)
if err != nil {
return false, err
}
defer func() {
p.CaptureErr(ctx, file.Close())
}()
sha := sha1.New()
_, err = io.Copy(sha, file)
if err != nil {
return false, err
}
result := hex.EncodeToString(sha.Sum(nil))
if result == hash {
return true, nil
}
return false, fmt.Errorf("hash mismatch: got %s expected %s", result, hash)
}
func (p *Pinnacle) checkLauncher(ctx context.Context) error {
pt := ui.NewProgressTask("Preparing launcher...")
p.Breadcrumb(ctx, "fetching metadata from /pinnacle")
pt.UpdateProgress(0.20)
launcher, err := p.fetchMetadata(ctx, MetadataURL+"/pinnacle?branch="+p.branch)
if err != nil {
return err
}
pt.UpdateProgress(0.60)
targetPath := p.alpinePath("launcher.jar")
if !fileExists(targetPath) {
p.Breadcrumb(ctx, "missing launcher.jar")
return errMissingLauncher
}
if validHash, _ := p.fileHashMatches(ctx, launcher.Hash, targetPath); !validHash {
p.Breadcrumb(ctx, "failed checksum validation")
return errMissingLauncher
}
p.Breadcrumb(ctx, "finished checkLauncher (jar existed)")
return nil
}
func (p *Pinnacle) downloadLauncher(ctx context.Context) error {
pt := ui.NewProgressTask("Downloading launcher...")
dest := p.alpinePath("launcher.jar")
err := p.downloadFile(ctx, metadataResponse.URL, dest, pt)
if err != nil {
return err
}
var validHash bool
if validHash, err = p.fileHashMatches(ctx, metadataResponse.Hash, dest); !validHash {
p.Breadcrumb(ctx, fmt.Sprintf("hash mismatch after download (retry): %v", err), slog.LevelError)
_ = os.RemoveAll(dest)
err = p.downloadFile(ctx, metadataResponse.URL, dest, pt)
if err != nil {
return err
}
if validHash, err = p.fileHashMatches(ctx, metadataResponse.Hash, dest); !validHash {
p.Breadcrumb(ctx, fmt.Sprintf("hash mismatch after download: %v", err), slog.LevelError)
return err
}
}
p.Breadcrumb(ctx, "finished checkLauncher (jar downloaded)")
pt.UpdateProgress(0.99999, "Starting launcher...")
return nil
}
func (p *Pinnacle) checkJava(ctx context.Context) error {
pt := ui.NewProgressTask("Preparing Java runtime...")
path := p.alpinePath("jre", "17")
p.Breadcrumb(ctx, "mkdir "+path)
err := os.MkdirAll(path, os.ModePerm)
if err != nil {
return err
}
pt.UpdateProgress(0.20)
endpoint := fmt.Sprintf("%s/jre?version=17&os=%s&arch=%s", MetadataURL, p.os, p.arch)
p.Breadcrumb(ctx, "fetching manifest from "+endpoint)
jre, err := p.fetchMetadata(ctx, endpoint)
if err != nil {
return err
}
var data []byte
var manifest JavaManifest
javaPath := p.alpinePath("jre", "17", "extracted", "bin", p.os.javaExecutable())
manifestPath := p.alpinePath("jre", "17", "version.json")
pt.UpdateProgress(0.50)
if !fileExists(javaPath) {
p.Breadcrumb(ctx, "missing java executable")
return errMissingJava
}
pt.UpdateProgress(0.68)
if !fileExists(manifestPath) {
p.Breadcrumb(ctx, "missing manifest")
return errMissingJava
}
pt.UpdateProgress(0.76)
data, err = os.ReadFile(manifestPath)
if err != nil {
p.Breadcrumb(ctx, "failed to read manifest file")
return errMissingJava
}
pt.UpdateProgress(0.85)
if err = json.Unmarshal(data, &manifest); err != nil {
p.Breadcrumb(ctx, "failed to unmarshal manifest file")
return errMissingJava
}
pt.UpdateProgress(0.98)
if manifest.Hash != jre.Hash {
p.Breadcrumb(ctx, fmt.Sprintf("file checksum %s does not match expected %s", manifest.Hash, jre.Hash))
return errMissingJava
}
p.Breadcrumb(ctx, "finished checkJava (existed)")
return nil
}
func (p *Pinnacle) downloadJava(ctx context.Context) error {
archivePath := p.alpinePath("jre", "17", metadataResponse.Name)
extractedPath := p.alpinePath("jre", "17", "extracted")
manifestPath := p.alpinePath("jre", "17", "version.json")
p.CaptureErr(ctx, os.RemoveAll(archivePath))
p.CaptureErr(ctx, os.RemoveAll(extractedPath))
p.CaptureErr(ctx, os.RemoveAll(manifestPath))
pt := ui.NewProgressTask("Downloading Java...")
err := p.downloadFile(ctx, metadataResponse.URL, archivePath, pt)
if err != nil {
return err
}
pt = ui.NewProgressTask("Extracting Java...")
err = p.extractArchive(ctx, archivePath, extractedPath, pt)
if err != nil {
return err
}
_ = os.Chmod(p.alpinePath("jre", "17", "extracted", "bin", p.os.javaExecutable()), 0o755)
data, err := json.Marshal(JavaManifest{Hash: metadataResponse.Hash, Size: metadataResponse.Size})
if err != nil {
return err
}
p.Breadcrumb(ctx, "writing manifest to file "+manifestPath)
err = os.WriteFile(manifestPath, data, 0o600)
if err != nil {
return err
}
_ = os.Remove(archivePath)
p.Breadcrumb(ctx, "finished checkJava (downloaded)")
return nil
}
func (p *Pinnacle) startLauncher(ctx context.Context) error {
pt := ui.NewProgressTask("Starting launcher...")
pt.UpdateProgress(0.50)
jarPath := p.alpinePath("launcher.jar")
jrePath := p.alpinePath("jre", "17", "extracted", "bin", p.os.javaExecutable())
args := []string{
"-Xms256M",
"-Xmx256M",
}
if p.os == Mac {
args = append(args, "-XstartOnFirstThread")
}
args = append(args, "-jar", jarPath)
if version != "" {
args = append(args, "--pinnacle-version", version)
}
procAttr := &os.ProcAttr{
Dir: p.alpinePath(),
Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
}
pt.UpdateProgress(0.75)
p.Breadcrumb(ctx, fmt.Sprintf("starting launcher process: %s %s", jrePath, args))
proc, err := os.StartProcess(jrePath, args, procAttr)
if err != nil {
p.Breadcrumb(ctx, fmt.Sprintf("failed to start launcher (retrying): %v", err))
// retry with regular java.exe
proc, err = os.StartProcess(p.alpinePath("jre", "17", "extracted", "bin", "java"), args, procAttr)
if err != nil {
return err
}
}
pt.UpdateProgress(0.95)
p.Breadcrumb(ctx, "releasing launcher process")
err = proc.Release()
if err != nil {
return err
}
pt.UpdateProgress(0.99)
return nil
}
func (p *Pinnacle) StartSentry(release string, dsn string) {
err := sentry.Start(release, dsn)
if err != nil {
p.logger.Warn(err.Error())
}
}
func (p *Pinnacle) CaptureErr(ctx context.Context, err error) {
if err == nil {
return
}
p.logger.ErrorContext(ctx, err.Error())
sentry.CaptureErr(ctx, err)
}
func (p *Pinnacle) Breadcrumb(ctx context.Context, desc string, level ...slog.Level) {
var lvl slog.Level
if len(level) == 0 {
lvl = slog.LevelInfo
} else {
lvl = level[0]
}
p.logger.Log(ctx, lvl, desc)
sentry.Breadcrumb(ctx, desc, lvl)
}
// alpinePath returns the absolute path of Alpine Client's
// data directory based on the user's operating system.
//
// Optionally, pass in sub-folder/file names to add
// them to the returned path.
// - Example: p.alpinePath("jre", "17", "version.json")
//
// Windows - %AppData%\.alpineclient
// Mac - $HOME/Library/Application Support/alpineclient
// Linux - $HOME/.alpineclient
//
// note: The missing '.' for macOS is intentional.
func (p *Pinnacle) alpinePath(subs ...string) string {
var baseDir string
var dirs []string
switch p.os {
case Windows:
baseDir = os.Getenv("AppData")
dirs = append(dirs, baseDir, ".alpineclient")
case Mac:
baseDir = os.Getenv("HOME")
dirs = append(dirs, baseDir, "Library", "Application Support", "alpineclient")
case Linux:
baseDir = os.Getenv("HOME")
dirs = append(dirs, baseDir, ".alpineclient")
}
return filepath.Join(append(dirs, subs...)...)
}