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
package install
import (
"cmp"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"regexp"
"sort"
"strings"
"github.com/johnandersen777/socialweb-computer-kcp/internal/bootstrap/table"
"github.com/johnandersen777/socialweb-computer-kcp/internal/buildstamp"
"github.com/johnandersen777/socialweb-computer-kcp/internal/hostcontract"
"github.com/johnandersen777/socialweb-computer-kcp/internal/hostsvc"
"github.com/johnandersen777/socialweb-computer-kcp/internal/kit/sha256sum"
)
var (
ErrManifestMismatch = errors.New("upgrade: the binary does not match the sha256 it was published with")
ErrSelfCheck = errors.New("upgrade: the new binary's self-check failed")
ErrNoPrevious = errors.New("rollback: bin/previous names no binary")
ErrNoCoreUnit = errors.New("no enabled core unit")
ErrDevBinary = errors.New("the binary is a dev build (version says \"dev build, not reproducible\"); install and upgrade take a release build unless --allow-dev-binary is given (dev and test runs only)")
digestName = regexp.MustCompile(`^[0-9a-f]{64}$`)
)
type UpgradeOptions struct {
StateDir string
Home string
Account string
Root string
RuntimeDir string
Proc string
Binary string
SHA256 string
AllowDev bool
Out io.Writer
DevStamp func(ctx context.Context, exe string) (*buildstamp.Info, error)
SelfCheck func(ctx context.Context, exe string, role hostcontract.Role, stateDir string) error
Restart func(ctx context.Context, units []string) error
}
func (o UpgradeOptions) out() io.Writer {
if o.Out == nil {
return io.Discard
}
return o.Out
}
func (o UpgradeOptions) role() hostcontract.Role {
if o.Account != "" {
if rec, err := ReadRecord(cmp.Or(o.Root, "/"), o.Account); err == nil && rec.Role != "" && rec.StateDir == o.StateDir {
return rec.Role
}
}
return InstalledRole(o.Home)
}
func (o UpgradeOptions) bin() string { return filepath.Join(o.StateDir, "bin") }
func isGatewayCore(name string) bool {
return strings.HasPrefix(name, RuntimePrefix) && strings.HasSuffix(name, "-"+table.GatewayCore)
}
func isBidderCore(name string) bool {
return strings.HasPrefix(name, RuntimePrefix) && strings.Contains(name, "-"+table.BidderCore) && strings.HasSuffix(name, hostsvc.ServiceSuffix)
}
func isRequesterCore(name string) bool {
return strings.HasPrefix(name, RuntimePrefix) && strings.Contains(name, "-"+table.RequesterCore) && strings.HasSuffix(name, hostsvc.ServiceSuffix)
}
func InstalledRole(home string) hostcontract.Role {
entries, _ := os.ReadDir(filepath.Join(home, UserUnitDir))
for _, e := range entries {
if isGatewayCore(e.Name()) {
return hostcontract.RoleGateway
}
}
for _, e := range entries {
if isRequesterCore(e.Name()) {
return hostcontract.RoleRequester
}
}
return hostcontract.RoleBidder
}
func EnabledCoreUnits(home string) []string {
entries, _ := os.ReadDir(filepath.Join(home, UserUnitDir, WantsDir))
var out []string
for _, e := range entries {
if isGatewayCore(e.Name()) || isBidderCore(e.Name()) || isRequesterCore(e.Name()) {
out = append(out, e.Name())
}
}
sort.Strings(out)
return out
}
func readTarget(link string) string {
t, err := os.Readlink(link)
if err != nil || !digestName.MatchString(t) {
return ""
}
return t
}
func Upgrade(ctx context.Context, o UpgradeOptions) error {
if !digestName.MatchString(o.SHA256) {
return fmt.Errorf("upgrade: --sha256 must name the published 64-hex digest of the binary, got %q", o.SHA256)
}
sum, err := sha256sum.File(o.Binary)
if err != nil {
return err
}
if sum != o.SHA256 {
return fmt.Errorf("%w: %s is %s, published %s", ErrManifestMismatch, o.Binary, sum, o.SHA256)
}
if !o.AllowDev && o.DevStamp != nil {
stamp, err := o.DevStamp(ctx, o.Binary)
if err != nil {
return fmt.Errorf("%w: %s version --json: %v", ErrSelfCheck, o.Binary, err)
}
if stamp != nil {
return fmt.Errorf("upgrade: %w: %s (commit %s, built %s)", ErrDevBinary, o.Binary, stamp.Commit, stamp.BuiltUTC)
}
}
current := readTarget(filepath.Join(o.bin(), "current"))
if current == sum {
fmt.Fprintf(o.out(), "upgrade: bin/current is already %s; nothing to switch, and a running core is left "+
"running. If its units are not up, restart them: systemctl --user restart <unit>\n", sum)
return nil
}
dir := filepath.Join(o.bin(), sum)
dst := filepath.Join(dir, CoreName)
if err := os.MkdirAll(dir, 0o755); err != nil {
return err
}
if have, _ := sha256sum.File(dst); have != sum {
if err := copyFile(o.Binary, dst, 0o755); err != nil {
return err
}
}
if have, err := sha256sum.File(dst); err != nil || have != sum {
return fmt.Errorf("%w: the copy at %s is %s", ErrManifestMismatch, dst, have)
}
role := o.role()
if err := o.SelfCheck(ctx, dst, role, o.StateDir); err != nil {
return fmt.Errorf("%w: %s: %v; bin/current still names %s", ErrSelfCheck, dst, err, current)
}
if current != "" {
if err := retarget(filepath.Join(o.bin(), "previous"), current); err != nil {
return err
}
}
if err := retarget(filepath.Join(o.bin(), "current"), sum); err != nil {
return err
}
fmt.Fprintf(o.out(), "upgrade: bin/current -> %s (previous %s)\n", sum, current)
if err := o.restart(ctx); err != nil {
return err
}
o.collect()
return nil
}
func (o UpgradeOptions) restart(ctx context.Context) error {
units := EnabledCoreUnits(o.Home)
if len(units) == 0 {
return fmt.Errorf("%w under %s: bin/current is switched but nothing was restarted; restart the core unit by hand",
ErrNoCoreUnit, filepath.Join(o.Home, UserUnitDir, WantsDir))
}
fmt.Fprintf(o.out(), "restarting %s\n", strings.Join(units, " "))
return o.Restart(ctx, units)
}
func Rollback(ctx context.Context, o UpgradeOptions) error {
previous := readTarget(filepath.Join(o.bin(), "previous"))
if previous == "" {
return ErrNoPrevious
}
if _, err := os.Stat(filepath.Join(o.bin(), previous, CoreName)); err != nil {
return fmt.Errorf("%w: %v", ErrNoPrevious, err)
}
current := readTarget(filepath.Join(o.bin(), "current"))
if err := retarget(filepath.Join(o.bin(), "current"), previous); err != nil {
return err
}
if current != "" {
if err := retarget(filepath.Join(o.bin(), "previous"), current); err != nil {
return err
}
}
fmt.Fprintf(o.out(), "rollback: bin/current -> %s (previous %s)\n", previous, current)
if err := o.restart(ctx); err != nil {
return err
}
o.collect()
return nil
}
func DevStamp(ctx context.Context, exe string) (*buildstamp.Info, error) {
b, err := exec.CommandContext(ctx, exe, "version", "--json").Output()
if err != nil {
return nil, err
}
var v struct {
Dev *buildstamp.Info `json:"dev"`
}
if err := json.Unmarshal(b, &v); err != nil {
return nil, err
}
return v.Dev, nil
}