network.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. if err := netconf.ApplyNetworkConfigs(&cfg.Rancher.Network); err != nil {
  43. log.Error(err)
  44. }
  45. // TODO: seems wrong to do this outside netconf
  46. userSetHostname := cfg.Hostname != ""
  47. log.Infof("Apply Network Config RunDhcp")
  48. if err := netconf.RunDhcp(&cfg.Rancher.Network, !userSetHostname, !userSetDNS); err != nil {
  49. log.Error(err)
  50. }
  51. log.Infof("Apply Network Config SyncHostname")
  52. if err := hostname.SyncHostname(); err != nil {
  53. log.Error(err)
  54. }
  55. }