network.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package network
  2. import (
  3. "flag"
  4. "os"
  5. "golang.org/x/net/context"
  6. log "github.com/Sirupsen/logrus"
  7. "github.com/docker/libnetwork/resolvconf"
  8. "github.com/rancher/netconf"
  9. "github.com/rancher/os/config"
  10. "github.com/rancher/os/docker"
  11. "github.com/rancher/os/hostname"
  12. )
  13. var (
  14. stopNetworkPre bool
  15. flags *flag.FlagSet
  16. )
  17. func init() {
  18. flags = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
  19. flags.BoolVar(&stopNetworkPre, "stop-network-pre", false, "")
  20. }
  21. func Main() {
  22. flags.Parse(os.Args[1:])
  23. log.Infof("Running network: stop-network-pre=%v", stopNetworkPre)
  24. if stopNetworkPre {
  25. client, err := docker.NewSystemClient()
  26. if err != nil {
  27. log.Error(err)
  28. }
  29. err = client.ContainerStop(context.Background(), "network-pre", 10)
  30. if err != nil {
  31. log.Error(err)
  32. }
  33. _, err = client.ContainerWait(context.Background(), "network-pre")
  34. if err != nil {
  35. log.Error(err)
  36. }
  37. }
  38. cfg := config.LoadConfig()
  39. nameservers := cfg.Rancher.Network.Dns.Nameservers
  40. search := cfg.Rancher.Network.Dns.Search
  41. userSetDns := len(nameservers) > 0 || len(search) > 0
  42. if !userSetDns {
  43. nameservers = cfg.Rancher.Defaults.Network.Dns.Nameservers
  44. search = cfg.Rancher.Defaults.Network.Dns.Search
  45. }
  46. if _, err := resolvconf.Build("/etc/resolv.conf", nameservers, search, nil); err != nil {
  47. log.Error(err)
  48. }
  49. if err := hostname.SetHostnameFromCloudConfig(cfg); err != nil {
  50. log.Error(err)
  51. }
  52. if err := netconf.ApplyNetworkConfigs(&cfg.Rancher.Network); err != nil {
  53. log.Error(err)
  54. }
  55. userSetHostname := cfg.Hostname != ""
  56. if err := netconf.RunDhcp(&cfg.Rancher.Network, !userSetHostname, !userSetDns); err != nil {
  57. log.Error(err)
  58. }
  59. if err := hostname.SyncHostname(); err != nil {
  60. log.Error(err)
  61. }
  62. select {}
  63. }