github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/libnetwork/cmd/networkdb-test/dbserver/ndbServer.go (about) 1 package dbserver 2 3 import ( 4 "context" 5 "errors" 6 "fmt" 7 "net" 8 "net/http" 9 "os" 10 "strconv" 11 12 "github.com/Prakhar-Agarwal-byte/moby/libnetwork/cmd/networkdb-test/dummyclient" 13 "github.com/Prakhar-Agarwal-byte/moby/libnetwork/diagnostic" 14 "github.com/Prakhar-Agarwal-byte/moby/libnetwork/networkdb" 15 "github.com/containerd/log" 16 ) 17 18 var ( 19 nDB *networkdb.NetworkDB 20 server *diagnostic.Server 21 ipAddr string 22 ) 23 24 var testerPaths2Func = map[string]diagnostic.HTTPHandlerFunc{ 25 "/myip": ipaddress, 26 } 27 28 func ipaddress(ctx interface{}, w http.ResponseWriter, r *http.Request) { 29 fmt.Fprintf(w, "%s\n", ipAddr) 30 } 31 32 // Server starts the server 33 func Server(args []string) { 34 log.G(context.TODO()).Infof("[SERVER] Starting with arguments %v", args) 35 if len(args) < 1 { 36 log.G(context.TODO()).Fatal("Port number is a mandatory argument, aborting...") 37 } 38 port, _ := strconv.Atoi(args[0]) 39 var localNodeName string 40 var ok bool 41 if localNodeName, ok = os.LookupEnv("TASK_ID"); !ok { 42 log.G(context.TODO()).Fatal("TASK_ID environment variable not set, aborting...") 43 } 44 log.G(context.TODO()).Infof("[SERVER] Starting node %s on port %d", localNodeName, port) 45 46 ip, err := getIPInterface("eth0") 47 if err != nil { 48 log.G(context.TODO()).Errorf("%s There was a problem with the IP %s\n", localNodeName, err) 49 return 50 } 51 ipAddr = ip 52 log.G(context.TODO()).Infof("%s uses IP %s\n", localNodeName, ipAddr) 53 54 server = diagnostic.New() 55 server.Init() 56 conf := networkdb.DefaultConfig() 57 conf.Hostname = localNodeName 58 conf.AdvertiseAddr = ipAddr 59 conf.BindAddr = ipAddr 60 nDB, err = networkdb.New(conf) 61 if err != nil { 62 log.G(context.TODO()).Infof("%s error in the DB init %s\n", localNodeName, err) 63 return 64 } 65 66 // Register network db handlers 67 server.RegisterHandler(nDB, networkdb.NetDbPaths2Func) 68 server.RegisterHandler(nil, testerPaths2Func) 69 server.RegisterHandler(nDB, dummyclient.DummyClientPaths2Func) 70 server.EnableDiagnostic("", port) 71 // block here 72 select {} 73 } 74 75 func getIPInterface(name string) (string, error) { 76 ifaces, err := net.Interfaces() 77 if err != nil { 78 return "", err 79 } 80 for _, iface := range ifaces { 81 if iface.Name != name { 82 continue // not the name specified 83 } 84 85 if iface.Flags&net.FlagUp == 0 { 86 return "", errors.New("Interfaces is down") 87 } 88 89 addrs, err := iface.Addrs() 90 if err != nil { 91 return "", err 92 } 93 for _, addr := range addrs { 94 var ip net.IP 95 switch v := addr.(type) { 96 case *net.IPNet: 97 ip = v.IP 98 case *net.IPAddr: 99 ip = v.IP 100 } 101 if ip == nil || ip.IsLoopback() { 102 continue 103 } 104 ip = ip.To4() 105 if ip == nil { 106 continue 107 } 108 return ip.String(), nil 109 } 110 return "", errors.New("Interfaces does not have a valid IPv4") 111 } 112 return "", errors.New("Interface not found") 113 }