-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
129 lines (116 loc) · 2.63 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// This code is available on the terms of the project LICENSE.md file,
// also available online at https://blueoakcouncil.org/license/1.0.0.
package main
import (
"bufio"
"context"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"golang.org/x/sync/errgroup"
)
func installurl() string {
url := "https://cdn.openbsd.org/pub/OpenBSD"
if f, err := os.Open("/etc/installurl"); err == nil {
url, err = bufio.NewReader(f).ReadString('\n')
if err != nil {
log.Fatal(err)
}
url = url[:len(url)-1] // remove newline
}
return url
}
func machine() string {
cmd := exec.Command("uname", "-m")
output, err := cmd.Output()
if err != nil {
return "amd64"
}
return string(output[:len(output)-1]) // remove newline
}
var (
mirror = flag.String("mirror", installurl(), "snapshot mirror")
arch = flag.String("arch", machine(), "CPU architecture")
rel = flag.Int("release", 68, "OpenBSD release")
dir = flag.String("d", "/home/_sysupgrade", "download directory")
pubkey = flag.String("p", "", "signify pubkey file")
)
var fetch = []string{
"SHA256.sig",
"INSTALL.ARCH",
"baseXX.tgz",
"bsd",
"bsd.mp",
"bsd.rd",
"compXX.tgz",
"gameXX.tgz",
"manXX.tgz",
"xbaseXX.tgz",
"xfontXX.tgz",
"xservXX.tgz",
"xshareXX.tgz",
}
var verify = fetch[1:]
func main() {
flag.Parse()
rel := strconv.Itoa(*rel)
arch := *arch
for i := range fetch {
fetch[i] = strings.Replace(fetch[i], "XX", rel, 1)
fetch[i] = strings.Replace(fetch[i], "ARCH", arch, 1)
}
mirror := fmt.Sprintf("%s/snapshots/%s/", *mirror, arch)
log.Printf("Downloading latest snapshot from %v to %v", mirror, *dir)
ctx := context.Background()
g, ctx := errgroup.WithContext(ctx)
for i := range fetch {
file := fetch[i]
url := mirror + file
out, err := os.Create(filepath.Join(*dir, file))
if err != nil {
log.Fatal(err)
}
r, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatal(err)
}
r = r.WithContext(ctx)
g.Go(func() error {
resp, err := http.DefaultClient.Do(r)
if resp != nil {
defer log.Printf("GET %s (%d)", file, resp.StatusCode)
}
if err != nil {
return err
}
defer resp.Body.Close()
_, err = io.Copy(out, resp.Body)
return err
})
}
err := g.Wait()
if err != nil {
log.Fatal(err)
}
pubkey := *pubkey
if pubkey == "" {
pubkey = "/etc/signify/openbsd-" + rel + "-base.pub"
}
args := []string{"-C", "-p", pubkey, "-x", "SHA256.sig"}
args = append(args, verify...)
log.Println("Verfiying")
cmd := exec.Command("signify", args...)
cmd.Dir = *dir
output, err := cmd.CombinedOutput()
os.Stderr.Write(output)
if err != nil {
log.Fatal(err)
}
}