Skip to content

Commit 96ccb11

Browse files
committed
goal: remove dependency on saved machine state
1 parent fda6147 commit 96ccb11

File tree

9 files changed

+255
-688
lines changed

9 files changed

+255
-688
lines changed

cmd/device-client/main.go

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,10 @@ import (
44
"crypto/tls"
55
"crypto/x509"
66
"encoding/json"
7-
"errors"
87
"flag"
9-
"fmt"
10-
"io/fs"
118
"net/http"
129
"net/rpc"
1310
"os"
14-
"path/filepath"
15-
"sync"
1611
"time"
1712

1813
"github.com/nyiyui/qrystal/device"
@@ -42,40 +37,6 @@ type ClientConfig struct {
4237
transport *http.Transport
4338
}
4439

45-
type MachineData struct {
46-
Machines map[string]goal.Machine
47-
machinesLock sync.RWMutex
48-
path string
49-
}
50-
51-
func LoadMachineData(path string) (*MachineData, error) {
52-
var m MachineData
53-
data, err := os.ReadFile(path)
54-
if err != nil {
55-
return nil, fmt.Errorf("reading failed: %w", err)
56-
}
57-
err = json.Unmarshal(data, &m)
58-
if err != nil {
59-
return nil, fmt.Errorf("parsing failed: %w", err)
60-
}
61-
m.path = path
62-
return &m, nil
63-
}
64-
65-
func (m *MachineData) setMachine(clientName string, gm goal.Machine) {
66-
m.machinesLock.Lock()
67-
defer m.machinesLock.Unlock()
68-
m.Machines[clientName] = gm
69-
data, err := json.Marshal(m)
70-
if err != nil {
71-
panic(err)
72-
}
73-
err = os.WriteFile(m.path, data, 0600)
74-
if err != nil {
75-
zap.S().Errorf("saving machine data failed: %s. Updating WireGuard interfaces may fail.", err)
76-
}
77-
}
78-
7940
func main() {
8041
util.SetupLog()
8142

@@ -183,23 +144,10 @@ func main() {
183144
dnsClient = dns.NewDirectClient(s)
184145
}
185146

186-
path := filepath.Join(os.Getenv("STATE_DIRECTORY"), "MachineData.json")
187-
m, err := LoadMachineData(path)
188-
if err != nil {
189-
zap.S().Debugf("IsNotExist=%t", os.IsNotExist(err))
190-
if errors.Is(err, fs.ErrNotExist) {
191-
zap.S().Info("no machine data found. Creating a blank one…")
192-
m = new(MachineData)
193-
m.Machines = map[string]goal.Machine{}
194-
m.path = path
195-
} else {
196-
zap.S().Fatalf("loading machine data failed: %s", err)
197-
}
198-
}
199-
createGoroutines(m, dnsClient, config)
147+
createGoroutines(dnsClient, config)
200148
}
201149

202-
func createGoroutines(m *MachineData, dnsClient dns.Client, config Config) {
150+
func createGoroutines(dnsClient dns.Client, config Config) {
203151
util.Notify("READY=1\nSTATUS=starting…")
204152
for clientName, cc := range config.Clients {
205153
go func(clientName string, cc ClientConfig) {
@@ -212,7 +160,6 @@ func createGoroutines(m *MachineData, dnsClient dns.Client, config Config) {
212160
}
213161
c.SetCanForward(config.CanForward)
214162
c.SetAssumeProc(config.AssumeProc)
215-
c.Machine = m.Machines[clientName]
216163
c.SetDNSClient(dnsClient)
217164
continuous := new(device.ContinousClient)
218165
continuous.Client = c
@@ -229,9 +176,6 @@ func createGoroutines(m *MachineData, dnsClient dns.Client, config Config) {
229176
zap.S().Errorf("%s: %s", clientName, err)
230177
break
231178
}
232-
if updated {
233-
m.setMachine(clientName, c.Machine)
234-
}
235179
if !latest {
236180
if updated {
237181
zap.S().Infof("%s: updated but not latest; trying again…", clientName)

cmd/test-device/main.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import (
1212
)
1313

1414
type Config struct {
15-
MachineJSONPath string
16-
1715
BaseURL string
1816
Token util.Token
1917
Network string
@@ -44,19 +42,6 @@ func main() {
4442
}
4543
zap.S().Info("created client.")
4644

47-
machineData, err := os.ReadFile(config.MachineJSONPath)
48-
if err != nil {
49-
panic(err)
50-
}
51-
err = json.Unmarshal(machineData, &c.Machine)
52-
if err != nil {
53-
panic(err)
54-
}
55-
data, err := json.MarshalIndent(c.Machine, "", " ")
56-
if err != nil {
57-
panic(err)
58-
}
59-
zap.S().Infof("parsed machine data:\n%s", data)
6045
latest, err := c.ReifySpec()
6146
if err != nil {
6247
panic(err)
@@ -65,13 +50,4 @@ func main() {
6550
panic("posted but already not latest")
6651
}
6752
zap.S().Info("reified spec.")
68-
machineData, err = json.Marshal(c.Machine)
69-
if err != nil {
70-
panic(err)
71-
}
72-
err = os.WriteFile(config.MachineJSONPath, machineData, 0600)
73-
if err != nil {
74-
panic(err)
75-
}
76-
zap.S().Infof("saved machine data:\n%s", machineData)
7753
}

cmd/test-goal/main.go

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,60 +9,35 @@ import (
99

1010
"github.com/nyiyui/qrystal/goal"
1111
"github.com/nyiyui/qrystal/util"
12-
"github.com/vishvananda/netlink"
1312
"go.uber.org/zap"
14-
"golang.zx2c4.com/wireguard/wgctrl"
1513
)
1614

17-
var aMachine string
18-
var bMachine string
15+
var mPath string
1916

2017
func main() {
2118
util.SetupLog()
2219

23-
flag.StringVar(&aMachine, "a-path", "", "path to starting state")
24-
flag.StringVar(&bMachine, "b-path", "", "path to goal state")
20+
flag.StringVar(&mPath, "m-path", "", "path to goal state")
2521
flag.Parse()
2622

2723
zap.S().Info("parsing machine data…")
28-
aMachineData, err := os.ReadFile(aMachine)
29-
if err != nil {
30-
panic(err)
31-
}
32-
bMachineData, err := os.ReadFile(bMachine)
24+
raw, err := os.ReadFile(mPath)
3325
if err != nil {
3426
panic(err)
3527
}
3628

37-
var a goal.Machine
38-
var b goal.Machine
39-
err = json.Unmarshal(aMachineData, &a)
40-
if err != nil {
41-
panic(err)
42-
}
43-
err = json.Unmarshal(bMachineData, &b)
29+
var m goal.Machine
30+
err = json.Unmarshal(raw, &m)
4431
if err != nil {
4532
panic(err)
4633
}
4734
zap.S().Info("done parsing machine data.")
4835

49-
md := goal.DiffMachine(&a, &b)
50-
zap.S().Info("generated diff.")
51-
data, err := json.MarshalIndent(md, " ", " ")
36+
applier, err := goal.NewApplier(goal.ApplierOptions{})
5237
if err != nil {
5338
panic(err)
5439
}
55-
zap.S().Infof("machine diff:\n%s\n", data)
56-
client, err := wgctrl.New()
57-
if err != nil {
58-
panic(err)
59-
}
60-
handle, err := netlink.NewHandle()
61-
if err != nil {
62-
panic(err)
63-
}
64-
65-
err = goal.ApplyMachineDiff(a, b, md, client, handle, true)
40+
err = applier.ApplyMachine(m)
6641
if err != nil {
6742
panic(err)
6843
}

device/client.go

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,16 @@ import (
1818
"github.com/nyiyui/qrystal/spec"
1919
"github.com/nyiyui/qrystal/util"
2020
"go.uber.org/zap"
21-
"golang.zx2c4.com/wireguard/wgctrl"
2221
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
2322
)
2423

2524
type Client struct {
2625
client *http.Client
2726
baseURL *url.URL
28-
Machine goal.Machine
29-
wgClient *wgctrl.Client
30-
goalHandle *goal.Handle
27+
applier *goal.Applier
3128
dns dns.Client
3229
dnsLock sync.Mutex
3330
canForward bool
34-
assumeProc bool
3531

3632
spec spec.SpecCensored
3733
token util.Token
@@ -45,11 +41,7 @@ func NewClient(httpClient *http.Client, baseURL string, token util.Token, networ
4541
if err != nil {
4642
return nil, err
4743
}
48-
wgClient, err := wgctrl.New()
49-
if err != nil {
50-
return nil, err
51-
}
52-
goalHandle, err := goal.NewHandle()
44+
applier, err := goal.NewApplier(goal.ApplierOptions{})
5345
if err != nil {
5446
return nil, err
5547
}
@@ -59,8 +51,7 @@ func NewClient(httpClient *http.Client, baseURL string, token util.Token, networ
5951
return &Client{
6052
client: httpClient,
6153
baseURL: baseURL2,
62-
wgClient: wgClient,
63-
goalHandle: goalHandle,
54+
applier: applier,
6455
token: token,
6556
network: network,
6657
device: device,
@@ -72,8 +63,13 @@ func (c *Client) SetCanForward(canForward bool) {
7263
c.canForward = canForward
7364
}
7465

75-
func (c *Client) SetAssumeProc(assumeProc bool) {
76-
c.assumeProc = assumeProc
66+
func (c *Client) SetAssumeProc(assumeProc bool) error {
67+
var err error
68+
c.applier, err = goal.NewApplier(goal.ApplierOptions{Linux: goal.ApplierOptionsLinux{ReadWriteProc: !assumeProc}})
69+
if err != nil {
70+
return err
71+
}
72+
return nil
7773
}
7874

7975
func (c *Client) SetDNSClient(client dns.Client) {
@@ -147,17 +143,11 @@ func (c *Client) ReifySpec() (latest bool, err error) {
147143
gm.Interfaces[0].PrivateKey = goal.Key(c.privateKey)
148144
data, _ = json.Marshal(gm)
149145
zap.S().Debugf("compiled spec:\n%s", data)
150-
data, _ = json.Marshal(c.Machine)
151-
zap.S().Debugf("machine spec:\n%s", data)
152-
diff := goal.DiffMachine(&c.Machine, &gm)
153-
data, _ = json.Marshal(diff)
154-
zap.S().Debugf("diff:\n%s", data)
155146
zap.S().Debug("applying machine…")
156-
err = goal.ApplyMachineDiff(c.Machine, gm, diff, c.wgClient, c.goalHandle, !c.assumeProc)
147+
err = c.applier.ApplyMachine(gm)
157148
if err != nil {
158149
return false, fmt.Errorf("apply spec: %w", err)
159150
}
160-
c.Machine = gm
161151
zap.S().Debug("applied machine.")
162152

163153
// === post status ===

0 commit comments

Comments
 (0)