1
0
mirror of https://github.com/rancher/os.git synced 2025-09-19 17:38:30 +00:00
Files
os/pkg/hostname/hostname.go
Olli Janatuinen 872f1cd6da Initiate Burmilla OS project
- Use burmilla GitHub repos
- Release under burmilla Docker Hub
- GitHub action for create releases
- Updated boot image and login banner
- Use Debian as default console
- Updated system-cron to v0.5.0
- Updated services to latest versions
- Bump up kernel to 4.14.206
- Include burmilla/os-debianconsole:v1.9.0 to initrd
2021-02-18 20:07:36 +02:00

56 lines
998 B
Go

package hostname
import (
"bufio"
"io/ioutil"
"os"
"strings"
"syscall"
"github.com/burmilla/os/config"
)
func SetHostnameFromCloudConfig(cc *config.CloudConfig) error {
var hostname string
if cc.Hostname == "" {
hostname = cc.Rancher.Defaults.Hostname
} else {
hostname = cc.Hostname
}
if hostname == "" {
return nil
}
// set hostname
return syscall.Sethostname([]byte(hostname))
}
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"
}
return ioutil.WriteFile("/etc/hosts", []byte(hostsContent), 0600)
}