network.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. if _, err := resolvconf.Build("/etc/resolv.conf", nameservers, search, nil); err != nil {
  36. log.Error(err)
  37. }
  38. if err := hostname.SetHostnameFromCloudConfig(cfg); err != nil {
  39. log.Error(err)
  40. }
  41. if err := netconf.ApplyNetworkConfigs(&cfg.Rancher.Network); err != nil {
  42. log.Error(err)
  43. }
  44. // TODO: seems wrong to do this outside netconf
  45. userSetHostname := cfg.Hostname != ""
  46. log.Infof("Apply Network Config RunDhcp")
  47. if err := netconf.RunDhcp(&cfg.Rancher.Network, !userSetHostname, !userSetDNS); err != nil {
  48. log.Error(err)
  49. }
  50. log.Infof("Apply Network Config SyncHostname")
  51. if err := hostname.SyncHostname(); err != nil {
  52. log.Error(err)
  53. }
  54. }