mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 19:13:34 +00:00
refactor daemon to use Machine to manage all components
This commit is contained in:
+10
-111
@@ -2,14 +2,9 @@ package daemon
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"golang.org/x/sync/errgroup"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/netip"
|
||||
"os"
|
||||
"strconv"
|
||||
"uncloud/internal/machine"
|
||||
"uncloud/internal/machine/api/pb"
|
||||
"uncloud/internal/machine/cluster"
|
||||
@@ -108,119 +103,23 @@ func InitCluster(dataDir, machineName string, netPrefix netip.Prefix, users []*p
|
||||
|
||||
type Daemon struct {
|
||||
machine *machine.Machine
|
||||
|
||||
state *machine.State
|
||||
cluster *cluster.Server
|
||||
}
|
||||
|
||||
func New(dataDir string) (*Daemon, error) {
|
||||
mstatePath := machine.StatePath(dataDir)
|
||||
mstate, err := machine.ParseState(mstatePath)
|
||||
config := &machine.Config{
|
||||
DataDir: dataDir,
|
||||
}
|
||||
mach, err := machine.NewMachine(config)
|
||||
if err != nil {
|
||||
if !errors.Is(err, os.ErrNotExist) {
|
||||
return nil, fmt.Errorf("load machine config: %w", err)
|
||||
}
|
||||
// Generate an empty machine config with a new key pair.
|
||||
slog.Info("Machine config not found, creating a new one.", "path", mstatePath)
|
||||
privKey, pubKey, kErr := network.NewMachineKeys()
|
||||
if kErr != nil {
|
||||
return nil, fmt.Errorf("generate machine keys: %w", kErr)
|
||||
}
|
||||
slog.Info("Generated machine key pair.", "pubkey", pubKey)
|
||||
|
||||
mstate = &machine.State{
|
||||
Network: &network.Config{
|
||||
PrivateKey: privKey,
|
||||
PublicKey: pubKey,
|
||||
},
|
||||
}
|
||||
mstate.SetPath(mstatePath)
|
||||
if err = mstate.Save(); err != nil {
|
||||
return nil, fmt.Errorf("save machine config: %w", err)
|
||||
}
|
||||
return nil, fmt.Errorf("init machine: %w", err)
|
||||
}
|
||||
|
||||
cstatePath := cluster.StatePath(dataDir)
|
||||
cstate := cluster.NewState(cstatePath)
|
||||
if err = cstate.Load(); err != nil {
|
||||
if !errors.Is(err, os.ErrNotExist) {
|
||||
return nil, fmt.Errorf("load cluster state: %w", err)
|
||||
}
|
||||
slog.Info("Cluster state not found, creating a new one.", "path", cstatePath)
|
||||
if err = cstate.Save(); err != nil {
|
||||
return nil, fmt.Errorf("save cluster state: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
d := &Daemon{
|
||||
state: mstate,
|
||||
}
|
||||
if mstate.Network.IsConfigured() {
|
||||
config := &machine.Config{
|
||||
APIAddr: net.JoinHostPort(mstate.Network.ManagementIP.String(), strconv.Itoa(machine.APIPort)),
|
||||
}
|
||||
d.machine, err = machine.NewMachine(config)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("init machine: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return d, nil
|
||||
return &Daemon{
|
||||
machine: mach,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (d *Daemon) Run(ctx context.Context) error {
|
||||
// Use an errgroup to coordinate error handling and graceful shutdown of multiple daemon components.
|
||||
errGroup, ctx := errgroup.WithContext(ctx)
|
||||
|
||||
// Start the network only if it is configured.
|
||||
if d.state.Network.IsConfigured() {
|
||||
wgnet, err := network.NewWireGuardNetwork()
|
||||
if err != nil {
|
||||
return fmt.Errorf("create WireGuard network: %w", err)
|
||||
}
|
||||
if err = wgnet.Configure(*d.state.Network); err != nil {
|
||||
return fmt.Errorf("configure WireGuard network: %w", err)
|
||||
}
|
||||
|
||||
//ctx, cancel := context.WithCancel(context.Background())
|
||||
//go wgnet.WatchEndpoints(ctx, peerEndpointChangeNotifier)
|
||||
|
||||
//addrs, err := network.ListRoutableIPs()
|
||||
//if err != nil {
|
||||
// return err
|
||||
//}
|
||||
//fmt.Println("Addresses:", addrs)
|
||||
|
||||
errGroup.Go(func() error {
|
||||
if err = wgnet.Run(ctx); err != nil {
|
||||
return fmt.Errorf("WireGuard network failed: %w", err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
} else {
|
||||
slog.Info("Waiting for network configuration to start WireGuard network.")
|
||||
}
|
||||
|
||||
if d.cluster != nil {
|
||||
errGroup.Go(func() error {
|
||||
slog.Info("Starting cluster.")
|
||||
if err := d.machine.Run(); err != nil {
|
||||
return fmt.Errorf("cluster failed: %w", err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// Shutdown goroutine.
|
||||
errGroup.Go(func() error {
|
||||
<-ctx.Done()
|
||||
if d.cluster != nil {
|
||||
slog.Info("Stopping cluster.")
|
||||
d.machine.Stop()
|
||||
slog.Info("Cluster server stopped.")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
return errGroup.Wait()
|
||||
slog.Info("Starting machine.")
|
||||
return d.machine.Run(ctx)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user