network.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package network
  2. import (
  3. "golang.org/x/net/context"
  4. "github.com/rancher/os/docker"
  5. "github.com/rancher/os/log"
  6. "github.com/docker/libnetwork/resolvconf"
  7. "github.com/rancher/os/config"
  8. "github.com/rancher/os/hostname"
  9. "github.com/rancher/os/netconf"
  10. )
  11. func Main() {
  12. log.InitLogger()
  13. cfg := config.LoadConfig()
  14. ApplyNetworkConfig(cfg)
  15. log.Infof("Restart syslog")
  16. client, err := docker.NewSystemClient()
  17. if err != nil {
  18. log.Error(err)
  19. }
  20. if err := client.ContainerRestart(context.Background(), "syslog", 10); err != nil {
  21. log.Error(err)
  22. }
  23. select {}
  24. }
  25. func ApplyNetworkConfig(cfg *config.CloudConfig) {
  26. log.Infof("Apply Network Config")
  27. nameservers := cfg.Rancher.Network.DNS.Nameservers
  28. search := cfg.Rancher.Network.DNS.Search
  29. userSetDNS := len(nameservers) > 0 || len(search) > 0
  30. if !userSetDNS {
  31. nameservers = cfg.Rancher.Defaults.Network.DNS.Nameservers
  32. search = cfg.Rancher.Defaults.Network.DNS.Search
  33. }
  34. // TODO: don't write to the file if nameservers is still empty
  35. log.Infof("Writing resolv.conf (%v) %v", nameservers, search)
  36. if _, err := resolvconf.Build("/etc/resolv.conf", nameservers, search, nil); err != nil {
  37. log.Error(err)
  38. }
  39. if err := hostname.SetHostnameFromCloudConfig(cfg); err != nil {
  40. log.Error(err)
  41. }
  42. userSetHostname := cfg.Hostname != ""
  43. if err := netconf.ApplyNetworkConfigs(&cfg.Rancher.Network, userSetHostname, userSetDNS); err != nil {
  44. log.Error(err)
  45. }
  46. log.Infof("Apply Network Config SyncHostname")
  47. if err := hostname.SyncHostname(); err != nil {
  48. log.Error(err)
  49. }
  50. }