cmd/tailscale/cli: show online/offline status in push --file-targets

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick 2021-04-21 16:06:01 -07:00
parent dedbd483ea
commit a16eb6ac41

View File

@ -61,12 +61,13 @@ func runPush(ctx context.Context, args []string) error {
return err return err
} }
peerAPIBase, lastSeen, err := discoverPeerAPIBase(ctx, ip) peerAPIBase, lastSeen, isOffline, err := discoverPeerAPIBase(ctx, ip)
if err != nil { if err != nil {
return err return err
} }
if isOffline {
if !lastSeen.IsZero() && time.Since(lastSeen) > lastSeenOld { fmt.Fprintf(os.Stderr, "# warning: %s is offline\n", hostOrIP)
} else if !lastSeen.IsZero() && time.Since(lastSeen) > lastSeenOld {
fmt.Fprintf(os.Stderr, "# warning: %s last seen %v ago\n", hostOrIP, time.Since(lastSeen).Round(time.Minute)) fmt.Fprintf(os.Stderr, "# warning: %s last seen %v ago\n", hostOrIP, time.Since(lastSeen).Round(time.Minute))
} }
@ -126,14 +127,14 @@ func runPush(ctx context.Context, args []string) error {
return errors.New(res.Status) return errors.New(res.Status)
} }
func discoverPeerAPIBase(ctx context.Context, ipStr string) (base string, lastSeen time.Time, err error) { func discoverPeerAPIBase(ctx context.Context, ipStr string) (base string, lastSeen time.Time, isOffline bool, err error) {
ip, err := netaddr.ParseIP(ipStr) ip, err := netaddr.ParseIP(ipStr)
if err != nil { if err != nil {
return "", time.Time{}, err return "", time.Time{}, false, err
} }
fts, err := tailscale.FileTargets(ctx) fts, err := tailscale.FileTargets(ctx)
if err != nil { if err != nil {
return "", time.Time{}, err return "", time.Time{}, false, err
} }
for _, ft := range fts { for _, ft := range fts {
n := ft.Node n := ft.Node
@ -144,10 +145,11 @@ func discoverPeerAPIBase(ctx context.Context, ipStr string) (base string, lastSe
if n.LastSeen != nil { if n.LastSeen != nil {
lastSeen = *n.LastSeen lastSeen = *n.LastSeen
} }
return ft.PeerAPIURL, lastSeen, nil isOffline = n.Online != nil && !*n.Online
return ft.PeerAPIURL, lastSeen, isOffline, nil
} }
} }
return "", time.Time{}, errors.New("target seems to be running an old Tailscale version") return "", time.Time{}, false, errors.New("target seems to be running an old Tailscale version")
} }
const maxSniff = 4 << 20 const maxSniff = 4 << 20
@ -204,15 +206,22 @@ func runPushTargets(ctx context.Context, args []string) error {
} }
for _, ft := range fts { for _, ft := range fts {
n := ft.Node n := ft.Node
var ago string var detail string
if n.LastSeen == nil { if n.Online != nil {
ago = "\tnode never seen" if !*n.Online {
} else { detail = "offline"
if d := time.Since(*n.LastSeen); d > lastSeenOld {
ago = fmt.Sprintf("\tlast seen %v ago", d.Round(time.Minute))
} }
} else {
detail = "unknown-status"
} }
fmt.Printf("%s\t%s%s\n", n.Addresses[0].IP, n.ComputedName, ago) if detail != "" && n.LastSeen != nil {
d := time.Since(*n.LastSeen)
detail += fmt.Sprintf("; last seen %v ago", d.Round(time.Minute))
}
if detail != "" {
detail = "\t" + detail
}
fmt.Printf("%s\t%s%s\n", n.Addresses[0].IP, n.ComputedName, detail)
} }
return nil return nil
} }