github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/cmd/installer/configureNetwork.go (about)

     1  // +build linux
     2  
     3  package main
     4  
     5  import (
     6  	"bufio"
     7  	"fmt"
     8  	"io/ioutil"
     9  	"net"
    10  	"os"
    11  	"path/filepath"
    12  	"strings"
    13  
    14  	"github.com/Cloud-Foundations/Dominator/lib/fsutil"
    15  	"github.com/Cloud-Foundations/Dominator/lib/log"
    16  	libnet "github.com/Cloud-Foundations/Dominator/lib/net"
    17  	"github.com/Cloud-Foundations/Dominator/lib/net/configurator"
    18  	fm_proto "github.com/Cloud-Foundations/Dominator/proto/fleetmanager"
    19  )
    20  
    21  func addMapping(mappings map[string]string, name string) error {
    22  	filename := fmt.Sprintf("/sys/class/net/%s/device", name)
    23  	if symlink, err := os.Readlink(filename); err != nil {
    24  		return err
    25  	} else {
    26  		mappings[name] = filepath.Base(symlink)
    27  		return nil
    28  	}
    29  }
    30  
    31  func configureNetwork(machineInfo fm_proto.GetMachineInfoResponse,
    32  	interfaces map[string]net.Interface, logger log.DebugLogger) error {
    33  	hostname := strings.Split(machineInfo.Machine.Hostname, ".")[0]
    34  	err := ioutil.WriteFile(filepath.Join(*mountPoint, "etc", "hostname"),
    35  		[]byte(hostname+"\n"), fsutil.PublicFilePerms)
    36  	if err != nil {
    37  		return err
    38  	}
    39  	netconf, err := configurator.Compute(machineInfo,
    40  		markConnectedInterfaces(interfaces, logger), logger)
    41  	if err != nil {
    42  		return err
    43  	}
    44  	mappings := make(map[string]string)
    45  	for name := range interfaces {
    46  		if err := addMapping(mappings, name); err != nil {
    47  			return err
    48  		}
    49  	}
    50  	if !*dryRun {
    51  		if err := netconf.WriteDebian(*mountPoint); err != nil {
    52  			return err
    53  		}
    54  		if err := writeMappings(mappings); err != nil {
    55  			return err
    56  		}
    57  		err = configurator.WriteResolvConf(*mountPoint, netconf.DefaultSubnet)
    58  		if err != nil {
    59  			return err
    60  		}
    61  	}
    62  	return nil
    63  }
    64  
    65  // Return a new map of interfaces, marking those with a carrier as up and those
    66  // without a carrier as down.
    67  func markConnectedInterfaces(interfaces map[string]net.Interface,
    68  	logger log.DebugLogger) map[string]net.Interface {
    69  	outputInterfaces := make(map[string]net.Interface)
    70  	for name, iface := range interfaces {
    71  		if libnet.TestCarrier(name) {
    72  			iface.Flags |= net.FlagUp
    73  			logger.Debugf(1, "%s is connected\n", name)
    74  		} else {
    75  			iface.Flags &= ^net.FlagUp
    76  			run("ifconfig", "", logger, name, "down")
    77  		}
    78  		outputInterfaces[name] = iface
    79  	}
    80  	return outputInterfaces
    81  }
    82  
    83  func writeMappings(mappings map[string]string) error {
    84  	filename := filepath.Join(*mountPoint,
    85  		"etc", "udev", "rules.d", "70-persistent-net.rules")
    86  	if file, err := create(filename); err != nil {
    87  		return err
    88  	} else {
    89  		defer file.Close()
    90  		writer := bufio.NewWriter(file)
    91  		defer writer.Flush()
    92  		fmt.Fprintf(writer, "# %s -- created by SmallStack installer\n\n",
    93  			filename)
    94  		for name, kernelId := range mappings {
    95  			fmt.Fprintf(writer,
    96  				`SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{type}=="1", KERNELS=="%s", NAME="%s"`,
    97  				kernelId, name)
    98  			fmt.Fprintln(writer)
    99  		}
   100  		return writer.Flush()
   101  	}
   102  }