network.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package network
  2. import (
  3. "github.com/rancher/os/log"
  4. "github.com/docker/libnetwork/resolvconf"
  5. "github.com/rancher/os/config"
  6. "github.com/rancher/os/hostname"
  7. "github.com/rancher/os/netconf"
  8. )
  9. func Main() {
  10. log.InitLogger()
  11. log.Infof("Running network")
  12. cfg := config.LoadConfig()
  13. ApplyNetworkConfig(cfg)
  14. select {}
  15. }
  16. func ApplyNetworkConfig(cfg *config.CloudConfig) {
  17. nameservers := cfg.Rancher.Network.DNS.Nameservers
  18. search := cfg.Rancher.Network.DNS.Search
  19. userSetDNS := len(nameservers) > 0 || len(search) > 0
  20. if !userSetDNS {
  21. nameservers = cfg.Rancher.Defaults.Network.DNS.Nameservers
  22. search = cfg.Rancher.Defaults.Network.DNS.Search
  23. }
  24. if _, err := resolvconf.Build("/etc/resolv.conf", nameservers, search, nil); err != nil {
  25. log.Error(err)
  26. }
  27. if err := hostname.SetHostnameFromCloudConfig(cfg); err != nil {
  28. log.Error(err)
  29. }
  30. if err := netconf.ApplyNetworkConfigs(&cfg.Rancher.Network); err != nil {
  31. log.Error(err)
  32. }
  33. userSetHostname := cfg.Hostname != ""
  34. if err := netconf.RunDhcp(&cfg.Rancher.Network, !userSetHostname, !userSetDNS); err != nil {
  35. log.Error(err)
  36. }
  37. if err := hostname.SyncHostname(); err != nil {
  38. log.Error(err)
  39. }
  40. }