1
0
mirror of https://github.com/rancher/os.git synced 2025-06-22 13:07:04 +00:00
os/hostname/hostname.go

64 lines
1.1 KiB
Go
Raw Normal View History

2016-04-15 04:58:07 +00:00
package hostname
import (
"bufio"
"io/ioutil"
"os"
"strings"
"syscall"
"github.com/rancher/os/config"
2016-04-15 04:58:07 +00:00
)
func SetHostnameFromCloudConfig(cc *config.CloudConfig) error {
var hostname string
if cc.Hostname == "" {
hostname = cc.Rancher.Defaults.Hostname
2016-04-15 04:58:07 +00:00
} else {
hostname = cc.Hostname
}
if hostname == "" {
return nil
}
// set hostname
if err := syscall.Sethostname([]byte(hostname)); err != nil {
return err
}
return nil
}
func SyncHostname() error {
hostname, err := os.Hostname()
if err != nil {
return err
}
if hostname == "" {
return nil
}
hosts, err := os.Open("/etc/hosts")
defer hosts.Close()
if err != nil {
return err
}
lines := bufio.NewScanner(hosts)
hostsContent := ""
for lines.Scan() {
line := strings.TrimSpace(lines.Text())
fields := strings.Fields(line)
if len(fields) > 0 && fields[0] == "127.0.1.1" {
hostsContent += "127.0.1.1 " + hostname + "\n"
continue
}
hostsContent += line + "\n"
}
if err := ioutil.WriteFile("/etc/hosts", []byte(hostsContent), 0600); err != nil {
return err
}
return nil
}