github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/cmd/hyper-control/showNetworkConfiguration.go (about)

     1  package main
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"net"
     7  	"os"
     8  	"strings"
     9  
    10  	"github.com/Cloud-Foundations/Dominator/lib/json"
    11  	"github.com/Cloud-Foundations/Dominator/lib/log"
    12  	libnet "github.com/Cloud-Foundations/Dominator/lib/net"
    13  	"github.com/Cloud-Foundations/Dominator/lib/net/configurator"
    14  	"github.com/Cloud-Foundations/Dominator/lib/srpc"
    15  	fm_proto "github.com/Cloud-Foundations/Dominator/proto/fleetmanager"
    16  )
    17  
    18  type networkInterface struct {
    19  	HardwareAddr string
    20  	Name         string
    21  	Up           bool
    22  }
    23  
    24  func showNetworkConfigurationSubcommand(args []string,
    25  	logger log.DebugLogger) error {
    26  	err := showNetworkConfiguration(logger)
    27  	if err != nil {
    28  		return fmt.Errorf("Error showing network configuration: %s", err)
    29  	}
    30  	return nil
    31  }
    32  
    33  func getInfoForhost(hostname string) (fm_proto.GetMachineInfoResponse, error) {
    34  	fmCR := srpc.NewClientResource("tcp",
    35  		fmt.Sprintf("%s:%d", *fleetManagerHostname, *fleetManagerPortNum))
    36  	defer fmCR.ScheduleClose()
    37  	if hostname != "" && hostname != "localhost" {
    38  		return getInfoForMachine(fmCR, hostname)
    39  	}
    40  	if hostname, err := os.Hostname(); err != nil {
    41  		return fm_proto.GetMachineInfoResponse{}, err
    42  	} else if info, err := getInfoForMachine(fmCR, hostname); err == nil {
    43  		return info, nil
    44  	} else if !strings.Contains(err.Error(), "unknown machine") {
    45  		return fm_proto.GetMachineInfoResponse{}, err
    46  	} else if hostname, err := getHostname(); err != nil {
    47  		return fm_proto.GetMachineInfoResponse{}, err
    48  	} else {
    49  		return getInfoForMachine(fmCR, hostname)
    50  	}
    51  }
    52  
    53  func getNetworkConfiguration(hostname string, logger log.DebugLogger) (
    54  	*configurator.NetworkConfig, error) {
    55  	info, err := getInfoForhost(hostname)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  	var interfacesMap map[string]net.Interface
    60  	if *networkInterfacesFile == "" {
    61  		if hostname != "" && hostname != "localhost" {
    62  			return nil, errors.New("no networkInterfacesFile specified")
    63  		}
    64  		_, interfacesMap, err = getUpInterfaces(logger)
    65  		if err != nil {
    66  			return nil, err
    67  		}
    68  	} else {
    69  		var networkInterfaces []networkInterface
    70  		err := json.ReadFromFile(*networkInterfacesFile, &networkInterfaces)
    71  		if err != nil {
    72  			return nil, err
    73  		}
    74  		interfacesMap = make(map[string]net.Interface, len(networkInterfaces))
    75  		for _, netInterface := range networkInterfaces {
    76  			macAddress, err := net.ParseMAC(netInterface.HardwareAddr)
    77  			if err != nil {
    78  				return nil, err
    79  			}
    80  			netIf := net.Interface{
    81  				Name:         netInterface.Name,
    82  				HardwareAddr: macAddress,
    83  			}
    84  			if netInterface.Up {
    85  				netIf.Flags = net.FlagUp
    86  			}
    87  			interfacesMap[netInterface.Name] = netIf
    88  		}
    89  	}
    90  	return configurator.Compute(info, interfacesMap, logger)
    91  }
    92  
    93  func getUpInterfaces(logger log.DebugLogger) (
    94  	[]net.Interface, map[string]net.Interface, error) {
    95  	interfaceList, interfaceMap, err := libnet.ListBroadcastInterfaces(
    96  		libnet.InterfaceTypeEtherNet, logger)
    97  	if err != nil {
    98  		return nil, nil, err
    99  	}
   100  	newList := make([]net.Interface, 0, len(interfaceList))
   101  	for _, iface := range interfaceList {
   102  		if libnet.TestCarrier(iface.Name) {
   103  			newList = append(newList, iface)
   104  			logger.Debugf(1, "will generate configuration including: %s\n",
   105  				iface.Name)
   106  		} else {
   107  			delete(interfaceMap, iface.Name)
   108  		}
   109  	}
   110  	return newList, interfaceMap, nil
   111  }
   112  
   113  func showNetworkConfiguration(logger log.DebugLogger) error {
   114  	netconf, err := getNetworkConfiguration(*hypervisorHostname, logger)
   115  	if err != nil {
   116  		return err
   117  	}
   118  	if netconf.DefaultSubnet == nil {
   119  		return errors.New("no default subnet found")
   120  	}
   121  	fmt.Println("=============================================================")
   122  	fmt.Println("Network configuration:")
   123  	if err := netconf.PrintDebian(os.Stdout); err != nil {
   124  		return err
   125  	}
   126  	fmt.Println("=============================================================")
   127  	fmt.Println("DNS configuration:")
   128  	return configurator.PrintResolvConf(os.Stdout, netconf.DefaultSubnet)
   129  }