Skip to content

Commit

Permalink
bootstrap: add device to repo only there is a peer
Browse files Browse the repository at this point in the history
Signed-off-by: Date Huang <tjjh89017@hotmail.com>
  • Loading branch information
tjjh89017 committed Sep 30, 2024
1 parent 15613cd commit 38cf66d
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions internal/ctrl/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,11 @@ func (ctrl *BootstrapController) registerDevice(ctx context.Context, deviceName
return err
}

deviceEntity := entity.NewDevice(
entity.DeviceId(device.Name),
device.ListenPort,
device.PrivateKey[:],
)

ctrl.devices.Save(ctx, deviceEntity)

peerCount := 0
for _, p := range device.Peers {
base64PublicKey := base64.StdEncoding.EncodeToString(p.PublicKey[:])
if name, ok := containsPeer(peers, base64PublicKey); ok {
peerCount += 1
ctrl.logger.Info().Str("device", deviceName).Str("peer", name).Str("publicKey", base64PublicKey).Msg("Register Peer")
peer := entity.NewPeer(
entity.NewPeerId(device.PublicKey[:], p.PublicKey[:]),
Expand All @@ -69,6 +63,17 @@ func (ctrl *BootstrapController) registerDevice(ctx context.Context, deviceName
}
}

if peerCount > 0 {
ctrl.logger.Info().Str("device", deviceName).Msg("Register Device")
deviceEntity := entity.NewDevice(
entity.DeviceId(device.Name),
device.ListenPort,
device.PrivateKey[:],
)

ctrl.devices.Save(ctx, deviceEntity)
}

return nil
}

Expand Down

0 comments on commit 38cf66d

Please sign in to comment.