1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- package network
- import (
- "golang.org/x/net/context"
- "github.com/rancher/os/docker"
- "github.com/rancher/os/log"
- "io/ioutil"
- "github.com/docker/libnetwork/resolvconf"
- "github.com/rancher/os/config"
- "github.com/rancher/os/hostname"
- "github.com/rancher/os/netconf"
- )
- func Main() {
- log.InitLogger()
- cfg := config.LoadConfig()
- ApplyNetworkConfig(cfg)
- log.Infof("Restart syslog")
- client, err := docker.NewSystemClient()
- if err != nil {
- log.Error(err)
- }
- if err := client.ContainerRestart(context.Background(), "syslog", 10); err != nil {
- log.Error(err)
- }
- select {}
- }
- func ApplyNetworkConfig(cfg *config.CloudConfig) {
- log.Infof("Apply Network Config")
- userSetDNS := len(cfg.Rancher.Network.DNS.Nameservers) > 0 || len(cfg.Rancher.Network.DNS.Search) > 0
- if err := hostname.SetHostnameFromCloudConfig(cfg); err != nil {
- log.Errorf("Failed to set hostname from cloud config: %v", err)
- }
- userSetHostname := cfg.Hostname != ""
- dhcpSetDNS, err := netconf.ApplyNetworkConfigs(&cfg.Rancher.Network, userSetHostname, userSetDNS)
- if err != nil {
- log.Errorf("Failed to apply network configs(by netconf): %v", err)
- }
- if dhcpSetDNS {
- log.Infof("DNS set by DHCP")
- }
- if !userSetDNS && !dhcpSetDNS {
- // only write 8.8.8.8,8.8.4.4 as a last resort
- log.Infof("Writing default resolv.conf - no user setting, and no DHCP setting")
- if _, err := resolvconf.Build("/etc/resolv.conf",
- cfg.Rancher.Defaults.Network.DNS.Nameservers,
- cfg.Rancher.Defaults.Network.DNS.Search,
- nil); err != nil {
- log.Errorf("Failed to write resolv.conf (!userSetDNS and !dhcpSetDNS): %v", err)
- }
- }
- if userSetDNS {
- if _, err := resolvconf.Build("/etc/resolv.conf", cfg.Rancher.Network.DNS.Nameservers, cfg.Rancher.Network.DNS.Search, nil); err != nil {
- log.Errorf("Failed to write resolv.conf (userSetDNS): %v", err)
- } else {
- log.Infof("writing to /etc/resolv.conf: nameservers: %v, search: %v", cfg.Rancher.Network.DNS.Nameservers, cfg.Rancher.Network.DNS.Search)
- }
- }
- resolve, err := ioutil.ReadFile("/etc/resolv.conf")
- log.Debugf("Resolve.conf == [%s], %v", resolve, err)
- log.Infof("Apply Network Config SyncHostname")
- if err := hostname.SyncHostname(); err != nil {
- log.Errorf("Failed to sync hostname: %v", err)
- }
- }
|