func setupNetworking(config *Config) error { if config == nil { return nil } if len(config.DnsConfig.Nameservers) != 0 { if err := resolvconf.Build("/etc/resolv.conf", config.DnsConfig.Nameservers, config.DnsConfig.Search); err != nil { return err } } if config.BridgeName != "" { log.Debugf("Creating bridge %s (%s)", config.BridgeName, config.BridgeAddress) if err := netconf.ApplyNetworkConfigs(&netconf.NetworkConfig{ Interfaces: map[string]netconf.InterfaceConfig{ config.BridgeName: { Address: config.BridgeAddress, MTU: config.BridgeMtu, Bridge: true, }, }, }); err != nil { return err } } return nil }
func enableDoLinkLocal() { err := netconf.ApplyNetworkConfigs(&netconf.NetworkConfig{ Interfaces: map[string]netconf.InterfaceConfig{ "eth0": { IPV4LL: true, }, }, }) if err != nil { log.Errorf("Failed to apply link local on eth0: %v", err) } }
func Main() { args := os.Args if len(args) > 1 { fmt.Println("call " + args[0] + " to load network config from cloud-config.yml") return } os.Remove(NETWORK_DONE) // ignore error cfg, err := config.LoadConfig() if err != nil { log.Fatal(err) } hostname, _ := cloudinit.SetHostname(cfg) // ignore error log.Infof("Network: hostname: '%s'", hostname) if err := netconf.ApplyNetworkConfigs(&cfg.Rancher.Network); err != nil { log.Error(err) } hostname, _ = cloudinit.SetHostname(cfg) // ignore error log.Infof("Network: hostname: '%s' (from DHCP, if not set by cloud-config)", hostname) if hostname != "" { hosts, err := os.Open("/etc/hosts") defer hosts.Close() if err != nil { log.Fatal(err) } lines := bufio.NewScanner(hosts) hostsContent := "" for lines.Scan() { line := strings.TrimSpace(lines.Text()) fields := strings.Fields(line) if len(fields) > 0 && fields[0] == "127.0.1.1" { hostsContent += "127.0.1.1 " + hostname + "\n" continue } hostsContent += line + "\n" } if err := ioutil.WriteFile("/etc/hosts", []byte(hostsContent), 0600); err != nil { log.Error(err) } } if cfg.Rancher.Network.Dns.Override { log.WithFields(log.Fields{"nameservers": cfg.Rancher.Network.Dns.Nameservers}).Info("Override nameservers") if _, err := resolvconf.Build("/etc/resolv.conf", cfg.Rancher.Network.Dns.Nameservers, cfg.Rancher.Network.Dns.Search, nil); err != nil { log.Error(err) } } if f, err := os.Create(NETWORK_DONE); err != nil { log.Error(err) } else { f.Close() } sendTerm(WAIT_FOR_NETWORK) select {} }
func Main() { flags.Parse(os.Args[1:]) log.Infof("Running network: daemon=%v", daemon) os.Remove(NETWORK_DONE) // ignore error cfg, err := config.LoadConfig() if err != nil { log.Fatal(err) } nameservers := cfg.Rancher.Network.Dns.Nameservers search := cfg.Rancher.Network.Dns.Search userSetDns := len(nameservers) > 0 || len(search) > 0 if !userSetDns { nameservers = cfg.Rancher.DefaultNetwork.Dns.Nameservers search = cfg.Rancher.DefaultNetwork.Dns.Search } if _, err := resolvconf.Build("/etc/resolv.conf", nameservers, search, nil); err != nil { log.Error(err) } if err := hostname.SetHostnameFromCloudConfig(cfg); err != nil { log.Error(err) } if err := netconf.ApplyNetworkConfigs(&cfg.Rancher.Network); err != nil { log.Error(err) } userSetHostname := cfg.Hostname != "" if err := netconf.RunDhcp(&cfg.Rancher.Network, !userSetHostname, !userSetDns); err != nil { log.Error(err) } if err := hostname.SyncHostname(); err != nil { log.Error(err) } if f, err := os.Create(NETWORK_DONE); err != nil { log.Error(err) } else { f.Close() } sendTerm(WAIT_FOR_NETWORK) if daemon { select {} } }
func Main() { args := os.Args if len(args) > 1 { fmt.Println("call " + args[0] + " to load network config from cloud-config.yml") return } cfg, err := config.LoadConfig() if err != nil { log.Fatal(err) } if err := netconf.ApplyNetworkConfigs(&cfg.Rancher.Network); err != nil { log.Fatal(err) } }
func Main() { args := os.Args if len(args) > 1 { fmt.Println("call " + args[0] + " to load network config from cloud-config.yml") return } cfg, err := config.LoadConfig() if err != nil { log.Fatal(err) } // Purposely ignore error cloudinit.SetHostname(cfg) if err := netconf.ApplyNetworkConfigs(&cfg.Rancher.Network); err != nil { log.Fatal(err) } }
func setupNetworking(config *Config) error { if config == nil { return nil } hostname, err := os.Hostname() if err != nil { return err } tryCreateFile("/etc/hosts", `127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters 127.0.1.1 `+hostname) if len(config.DnsConfig.Nameservers) != 0 { if _, err := resolvconf.Build("/etc/resolv.conf", config.DnsConfig.Nameservers, config.DnsConfig.Search, nil); err != nil { return err } } if config.BridgeName != "" { log.Debugf("Creating bridge %s (%s)", config.BridgeName, config.BridgeAddress) if err := netconf.ApplyNetworkConfigs(&netconf.NetworkConfig{ Interfaces: map[string]netconf.InterfaceConfig{ config.BridgeName: { Address: config.BridgeAddress, MTU: config.BridgeMtu, Bridge: true, }, }, }); err != nil { return err } } return nil }
func Main() { args := os.Args if len(args) > 1 { fmt.Println("call " + args[0] + " to load network config from cloud-config.yml") return } os.Remove(NETWORK_DONE) // ignore error cfg, err := config.LoadConfig() if err != nil { log.Fatal(err) } cloudinit.SetHostname(cfg) // ignore error if err := netconf.ApplyNetworkConfigs(&cfg.Rancher.Network); err != nil { log.Fatal(err) } if _, err := os.Create(NETWORK_DONE); err != nil { log.Error(err) } sendTerm(WAIT_FOR_NETWORK) select {} }
func run() error { logrus.SetLevel(logrus.DebugLevel) var input io.Reader = os.Stdin if len(os.Args) > 1 { var err error input, err = os.Open(os.Args[1]) if err != nil { return err } } content, err := ioutil.ReadAll(input) if err != nil { return err } var netCfg netconf.NetworkConfig err = yaml.Unmarshal(content, &netCfg) if err != nil { return err } return netconf.ApplyNetworkConfigs(&netCfg) }
func enablePacketNetwork(cfg *rancherConfig.RancherConfig) { bootStrapped := false for _, v := range cfg.Network.Interfaces { if v.Address != "" { if err := netconf.ApplyNetworkConfigs(&cfg.Network); err != nil { logrus.Errorf("Failed to bootstrap network: %v", err) return } bootStrapped = true break } } if !bootStrapped { return } c := metadata.NewClient(http.DefaultClient) m, err := c.Metadata.Get() if err != nil { logrus.Errorf("Failed to get Packet metadata: %v", err) return } bondCfg := netconf.InterfaceConfig{ Addresses: []string{}, BondOpts: map[string]string{ "lacp-rate": "1", "xmit_hash_policy": "layer3+4", "downdelay": "200", "updelay": "200", "miimon": "100", "mode": "4", }, } netCfg := netconf.NetworkConfig{ Interfaces: map[string]netconf.InterfaceConfig{}, } for _, iface := range m.Network.Interfaces { netCfg.Interfaces["mac="+iface.Mac] = netconf.InterfaceConfig{ Bond: "bond0", } } for _, addr := range m.Network.Addresses { bondCfg.Addresses = append(bondCfg.Addresses, fmt.Sprintf("%s/%d", addr.Address, addr.Cidr)) if addr.Gateway != "" { if addr.AddressFamily == 4 { if addr.Public { bondCfg.Gateway = addr.Gateway } } else { bondCfg.GatewayIpv6 = addr.Gateway } } if addr.AddressFamily == 4 && strings.HasPrefix(addr.Gateway, "10.") { bondCfg.PostUp = append(bondCfg.PostUp, "ip route add 10.0.0.0/8 via "+addr.Gateway) } } netCfg.Interfaces["bond0"] = bondCfg bytes, _ := yaml.Marshal(netCfg) logrus.Debugf("Generated network config: %s", string(bytes)) cc := rancherConfig.CloudConfig{ Rancher: rancherConfig.RancherConfig{ Network: netCfg, }, } if err := rancherConfig.WriteToFile(cc, rancherConfig.CloudConfigNetworkFile); err != nil { logrus.Errorf("Failed to save config file %s: %v", rancherConfig.CloudConfigNetworkFile, err) } }