From 2f7463ebe318b13eabf5e31a5535e97f04014001 Mon Sep 17 00:00:00 2001 From: Pavel Sviderski Date: Wed, 25 Sep 2024 23:59:56 +1000 Subject: [PATCH] refactor corrosion into interface service --- internal/machine/corrosion.go | 38 +++++++++++++++++++++++++++++++++++ internal/machine/machine.go | 4 +++- internal/machine/network.go | 13 +++++++++++- 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 internal/machine/corrosion.go diff --git a/internal/machine/corrosion.go b/internal/machine/corrosion.go new file mode 100644 index 00000000..d0d4b273 --- /dev/null +++ b/internal/machine/corrosion.go @@ -0,0 +1,38 @@ +package machine + +import ( + "fmt" + "log/slog" + "os/exec" +) + +const ( + CorrosionSystemdUnit = "uncloud-corrosion.service" +) + +type CorrosionService interface { + Configure() error + Start() error +} + +type CorrosionSystemdService struct { + Unit string + DataDir string +} + +func (s *CorrosionSystemdService) Configure() error { + return nil +} + +func (s *CorrosionSystemdService) Start() error { + unit := s.Unit + if unit == "" { + unit = CorrosionSystemdUnit + } + + if _, err := exec.Command("systemctl", "start", unit).Output(); err != nil { + return fmt.Errorf("start %s: %w", unit, err) + } + slog.Info("Corrosion systemd service started.", "unit", unit) + return nil +} diff --git a/internal/machine/machine.go b/internal/machine/machine.go index 2fbd941b..30933369 100644 --- a/internal/machine/machine.go +++ b/internal/machine/machine.go @@ -15,6 +15,7 @@ import ( "net/netip" "os" "os/user" + "path/filepath" "strconv" "uncloud/internal/machine/api/pb" "uncloud/internal/machine/cluster" @@ -173,7 +174,8 @@ func (m *Machine) Run(ctx context.Context) error { var err error slog.Info("Starting network controller.") networkServer := newGRPCServer(m, m.cluster) - ctrl, err = newNetworkController(m.state, networkServer, m.newMachinesCh) + corrosion := &CorrosionSystemdService{DataDir: filepath.Join(m.config.DataDir, "corrosion")} + ctrl, err = newNetworkController(m.state, networkServer, corrosion, m.newMachinesCh) if err != nil { return fmt.Errorf("initialise network controller: %w", err) } diff --git a/internal/machine/network.go b/internal/machine/network.go index 64e96ff5..5d9ff879 100644 --- a/internal/machine/network.go +++ b/internal/machine/network.go @@ -23,12 +23,15 @@ type networkController struct { state *State wgnet *network.WireGuardNetwork server *grpc.Server + corrosion CorrosionService newMachinesCh <-chan *pb.MachineInfo // TODO: DNS server/resolver listening on the machine IP, e.g. 10.210.0.1:53. It can't listen on 127.0.X.X // like resolved does because it needs to be reachable from both the host and the containers. } -func newNetworkController(state *State, server *grpc.Server, newMachCh <-chan *pb.MachineInfo) ( +func newNetworkController( + state *State, server *grpc.Server, corrosion CorrosionService, newMachCh <-chan *pb.MachineInfo, +) ( *networkController, error, ) { slog.Info("Starting WireGuard network.") @@ -41,6 +44,7 @@ func newNetworkController(state *State, server *grpc.Server, newMachCh <-chan *p state: state, wgnet: wgnet, server: server, + corrosion: corrosion, newMachinesCh: newMachCh, }, nil } @@ -51,6 +55,13 @@ func (nc *networkController) Run(ctx context.Context) error { } slog.Info("WireGuard network configured.") + if err := nc.corrosion.Configure(); err != nil { + return err + } + if err := nc.corrosion.Start(); err != nil { + return err + } + errGroup, ctx := errgroup.WithContext(ctx) // Start the network API server. Assume the management IP can't be changed when the network is running.