55 "context"
66 "fmt"
77 "os"
8- "os/exec"
98 "os/user"
109 "strings"
1110 "time"
@@ -18,6 +17,7 @@ import (
1817 "github.com/brevdev/brev-cli/pkg/entity"
1918 breverrors "github.com/brevdev/brev-cli/pkg/errors"
2019 "github.com/brevdev/brev-cli/pkg/externalnode"
20+ "github.com/brevdev/brev-cli/pkg/names"
2121 "github.com/brevdev/brev-cli/pkg/terminal"
2222
2323 "github.com/spf13/cobra"
@@ -41,10 +41,14 @@ func (r OSFileReader) ReadFile(path string) ([]byte, error) {
4141 return data , nil
4242}
4343
44- // NetBirdManager installs and uninstalls the NetBird network agent.
44+ // NetBirdManager installs, uninstalls, and monitors the NetBird network agent.
4545type NetBirdManager interface {
4646 Install () error
4747 Uninstall () error
48+ // EnsureRunning checks whether the NetBird service is active and
49+ // connected, starting or reconnecting it if needed. Returns nil when
50+ // the tunnel is healthy.
51+ EnsureRunning () error
4852}
4953
5054// SetupRunner runs a setup script on the local machine.
@@ -120,8 +124,8 @@ func runRegister(ctx context.Context, t *terminal.Terminal, s RegisterStore, nam
120124 return checkExistingRegistration (ctx , t , s , name , deps )
121125 }
122126
123- if name == "" {
124- return fmt . Errorf ( "please provide a name for this device \n \n Usage: brev register <name> \n Example: brev register \" my-DGX-Spark \" " )
127+ if err := names . ValidateNodeName ( name ); err != nil {
128+ return breverrors . WrapAndTrace ( err )
125129 }
126130
127131 brevUser , err := s .GetCurrentUser ()
@@ -273,7 +277,9 @@ func checkExistingRegistration(ctx context.Context, t *terminal.Terminal, s Regi
273277
274278 // Check local netbird service and start it if down.
275279 t .Vprint (" Checking local Brev tunnel..." )
276- if ensureNetbirdRunning (t ) {
280+ if err := deps .netbird .EnsureRunning (); err != nil {
281+ t .Vprintf (" %s\n " , t .Yellow (fmt .Sprintf ("Warning: %v" , err )))
282+ } else {
277283 t .Vprint (t .Green (" Brev tunnel is running." ))
278284 }
279285
@@ -282,41 +288,6 @@ func checkExistingRegistration(ctx context.Context, t *terminal.Terminal, s Regi
282288 return nil
283289}
284290
285- // ensureNetbirdRunning checks if the netbird systemd service is active and
286- // attempts to start it if it is not. It also checks the netbird peer
287- // connection status and runs "netbird up" if the peer is disconnected.
288- // Returns true if the service is running and connected after the check.
289- func ensureNetbirdRunning (t * terminal.Terminal ) bool {
290- out , err := exec .Command ("systemctl" , "is-active" , "netbird" ).Output () //nolint:gosec // fixed service name
291- if err != nil || strings .TrimSpace (string (out )) != "active" {
292- t .Vprintf (" %s\n " , t .Yellow ("Brev tunnel service is not running. Attempting to start..." ))
293- if startErr := exec .Command ("sudo" , "systemctl" , "start" , "netbird" ).Run (); startErr != nil { //nolint:gosec // fixed service name
294- t .Vprintf (" %s\n " , t .Yellow (fmt .Sprintf ("Warning: failed to start Brev tunnel service: %v" , startErr )))
295- return false
296- }
297- t .Vprint (t .Green (" Brev tunnel service started." ))
298- }
299-
300- // Service is running — now check peer connection status.
301- statusOut , err := exec .Command ("netbird" , "status" ).Output () //nolint:gosec // fixed command
302- if err != nil {
303- t .Vprintf (" %s\n " , t .Yellow (fmt .Sprintf ("Warning: could not check Brev tunnel status: %v" , err )))
304- return true // service is running, just can't confirm peer status
305- }
306-
307- if netbirdManagementConnected (string (statusOut )) {
308- return true
309- }
310-
311- t .Vprintf (" %s\n " , t .Yellow ("Brev tunnel peer is disconnected. Reconnecting..." ))
312- if upErr := exec .Command ("sudo" , "netbird" , "up" ).Run (); upErr != nil { //nolint:gosec // fixed command
313- t .Vprintf (" %s\n " , t .Yellow (fmt .Sprintf ("Warning: failed to reconnect Brev tunnel: %v" , upErr )))
314- return false
315- }
316- t .Vprint (t .Green (" Brev tunnel reconnected." ))
317- return true
318- }
319-
320291// netbirdManagementConnected parses "netbird status" output and returns true
321292// when the Management line reports "Connected".
322293func netbirdManagementConnected (statusOutput string ) bool {
0 commit comments