code.vegaprotocol.io/vega@v0.79.0/cmd/data-node/commands/networkhistory/list_active_peers.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package networkhistory 17 18 import ( 19 "context" 20 "fmt" 21 "os" 22 23 coreConfig "code.vegaprotocol.io/vega/core/config" 24 "code.vegaprotocol.io/vega/datanode/config" 25 vgjson "code.vegaprotocol.io/vega/libs/json" 26 "code.vegaprotocol.io/vega/logging" 27 "code.vegaprotocol.io/vega/paths" 28 v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2" 29 ) 30 31 type listActivePeers struct { 32 config.VegaHomeFlag 33 config.Config 34 coreConfig.OutputFlag 35 } 36 37 type listActivePeersOutput struct { 38 ActivePeers []string 39 } 40 41 func (o *listActivePeersOutput) printHuman() { 42 if len(o.ActivePeers) == 0 { 43 fmt.Printf("No active peers found\n") 44 return 45 } 46 fmt.Printf("Active Peers:\n\n") 47 48 for _, peer := range o.ActivePeers { 49 fmt.Printf("Active Peer: %s\n", peer) 50 } 51 } 52 53 func (cmd *listActivePeers) Execute(_ []string) error { 54 ctx, cfunc := context.WithCancel(context.Background()) 55 defer cfunc() 56 cfg := logging.NewDefaultConfig() 57 cfg.Custom.Zap.Level = logging.InfoLevel 58 cfg.Environment = "custom" 59 log := logging.NewLoggerFromConfig( 60 cfg, 61 ) 62 defer log.AtExit() 63 64 vegaPaths := paths.New(cmd.VegaHome) 65 err := fixConfig(&cmd.Config, vegaPaths) 66 if err != nil { 67 handleErr(log, 68 cmd.Output.IsJSON(), 69 "failed to fix config", 70 err) 71 } 72 73 if !datanodeLive(cmd.Config) { 74 handleErr(log, 75 cmd.Output.IsJSON(), 76 "datanode must be running for this command to work", 77 fmt.Errorf("couldn't connect to datanode on %v:%v", cmd.Config.API.IP, cmd.Config.API.Port)) 78 os.Exit(1) 79 } 80 81 client, conn, err := getDatanodeClient(cmd.Config) 82 if err != nil { 83 handleErr(log, 84 cmd.Output.IsJSON(), 85 "failed to get datanode client", 86 err) 87 os.Exit(1) 88 } 89 defer func() { _ = conn.Close() }() 90 91 resp, err := client.GetActiveNetworkHistoryPeerAddresses(ctx, &v2.GetActiveNetworkHistoryPeerAddressesRequest{}) 92 if err != nil { 93 handleErr(log, cmd.Output.IsJSON(), "failed to get active peer addresses", errorFromGrpcError("", err)) 94 os.Exit(1) 95 } 96 97 output := listActivePeersOutput{ActivePeers: resp.IpAddresses} 98 99 if cmd.Output.IsJSON() { 100 if err := vgjson.Print(&output); err != nil { 101 handleErr(log, cmd.Output.IsJSON(), "failed to marshal output", err) 102 os.Exit(1) 103 } 104 } else { 105 output.printHuman() 106 } 107 108 return nil 109 }