refactor corrosion into interface service

This commit is contained in:
Pavel Sviderski
2024-09-25 23:59:56 +10:00
parent 498d35e866
commit 2f7463ebe3
3 changed files with 53 additions and 2 deletions
+38
View File
@@ -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
}
+3 -1
View File
@@ -15,6 +15,7 @@ import (
"net/netip" "net/netip"
"os" "os"
"os/user" "os/user"
"path/filepath"
"strconv" "strconv"
"uncloud/internal/machine/api/pb" "uncloud/internal/machine/api/pb"
"uncloud/internal/machine/cluster" "uncloud/internal/machine/cluster"
@@ -173,7 +174,8 @@ func (m *Machine) Run(ctx context.Context) error {
var err error var err error
slog.Info("Starting network controller.") slog.Info("Starting network controller.")
networkServer := newGRPCServer(m, m.cluster) 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 { if err != nil {
return fmt.Errorf("initialise network controller: %w", err) return fmt.Errorf("initialise network controller: %w", err)
} }
+12 -1
View File
@@ -23,12 +23,15 @@ type networkController struct {
state *State state *State
wgnet *network.WireGuardNetwork wgnet *network.WireGuardNetwork
server *grpc.Server server *grpc.Server
corrosion CorrosionService
newMachinesCh <-chan *pb.MachineInfo 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 // 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. // 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, *networkController, error,
) { ) {
slog.Info("Starting WireGuard network.") slog.Info("Starting WireGuard network.")
@@ -41,6 +44,7 @@ func newNetworkController(state *State, server *grpc.Server, newMachCh <-chan *p
state: state, state: state,
wgnet: wgnet, wgnet: wgnet,
server: server, server: server,
corrosion: corrosion,
newMachinesCh: newMachCh, newMachinesCh: newMachCh,
}, nil }, nil
} }
@@ -51,6 +55,13 @@ func (nc *networkController) Run(ctx context.Context) error {
} }
slog.Info("WireGuard network configured.") 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) errGroup, ctx := errgroup.WithContext(ctx)
// Start the network API server. Assume the management IP can't be changed when the network is running. // Start the network API server. Assume the management IP can't be changed when the network is running.