mirror of
https://github.com/rancher/os.git
synced 2025-07-04 18:46:15 +00:00
Merge pull request #681 from imikushin/hosts
Resolve localhost and current hostname
This commit is contained in:
commit
ea5a893328
@ -3,4 +3,4 @@ VERSION=v0.4.2-dev
|
||||
|
||||
DOCKER_BINARY_URL=https://github.com/rancher/docker/releases/download/v1.9.1-ros1/docker-1.9.1
|
||||
COMPILED_KERNEL_URL=https://github.com/rancher/os-kernel/releases/download/Ubuntu-4.2.0-16.19/linux-4.2.3-rancher-x86.tar.gz
|
||||
DFS_IMAGE=rancher/docker:1.9.1-1
|
||||
DFS_IMAGE=rancher/docker:1.9.1-2
|
||||
|
@ -22,6 +22,7 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
yaml "github.com/cloudfoundry-incubator/candiedyaml"
|
||||
@ -38,7 +39,6 @@ import (
|
||||
"github.com/coreos/coreos-cloudinit/pkg"
|
||||
"github.com/coreos/coreos-cloudinit/system"
|
||||
"github.com/rancher/netconf"
|
||||
"github.com/rancher/os/cmd/cloudinit/hostname"
|
||||
rancherConfig "github.com/rancher/os/config"
|
||||
)
|
||||
|
||||
@ -168,16 +168,20 @@ func fetchUserData() ([]byte, datasource.Metadata, error) {
|
||||
return userDataBytes, metadata, nil
|
||||
}
|
||||
|
||||
func SetHostname(cc *rancherConfig.CloudConfig) error {
|
||||
func SetHostname(cc *rancherConfig.CloudConfig) (string, error) {
|
||||
name, _ := os.Hostname()
|
||||
if cc.Hostname != "" {
|
||||
name = cc.Hostname
|
||||
}
|
||||
if name != "" {
|
||||
//set hostname
|
||||
if err := hostname.SetHostname(cc.Hostname); err != nil {
|
||||
log.WithFields(log.Fields{"err": err, "hostname": cc.Hostname}).Error("Error setting hostname")
|
||||
return err
|
||||
if err := syscall.Sethostname([]byte(name)); err != nil {
|
||||
log.WithFields(log.Fields{"err": err, "hostname": name}).Error("Error setting hostname")
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return name, nil
|
||||
}
|
||||
|
||||
func executeCloudConfig() error {
|
||||
@ -186,7 +190,7 @@ func executeCloudConfig() error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := SetHostname(cc); err != nil {
|
||||
if _, err := SetHostname(cc); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -1,13 +0,0 @@
|
||||
package hostname
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func SetHostname(hostname string) error {
|
||||
if err := syscall.Sethostname([]byte(hostname)); err != nil {
|
||||
return err
|
||||
}
|
||||
return ioutil.WriteFile("/etc/hostname", []byte(hostname), 0644)
|
||||
}
|
@ -1,9 +1,12 @@
|
||||
package network
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
log "github.com/Sirupsen/logrus"
|
||||
|
||||
@ -36,10 +39,34 @@ func Main() {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
cloudinit.SetHostname(cfg) // ignore error
|
||||
hostname, _ := cloudinit.SetHostname(cfg) // ignore error
|
||||
log.Infof("Network: hostname: '%s'", hostname)
|
||||
if err := netconf.ApplyNetworkConfigs(&cfg.Rancher.Network); err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
hostname, _ = cloudinit.SetHostname(cfg) // ignore error
|
||||
log.Infof("Network: hostname: '%s' (from DHCP, if not set by cloud-config)", hostname)
|
||||
if hostname != "" {
|
||||
hosts, err := os.Open("/etc/hosts")
|
||||
defer hosts.Close()
|
||||
if err != nil {
|
||||
log.Fatal(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 {
|
||||
log.Error(err)
|
||||
}
|
||||
}
|
||||
if cfg.Rancher.Network.Dns.Override {
|
||||
log.WithFields(log.Fields{"nameservers": cfg.Rancher.Network.Dns.Nameservers}).Info("Override nameservers")
|
||||
if _, err := resolvconf.Build("/etc/resolv.conf", cfg.Rancher.Network.Dns.Nameservers, cfg.Rancher.Network.Dns.Search, nil); err != nil {
|
||||
|
@ -66,7 +66,7 @@ import:
|
||||
version: 1349b37bd56f4f5ce2690b5b2c0f53f88a261c67
|
||||
|
||||
- package: github.com/rancher/docker-from-scratch
|
||||
version: 1.9.1-1
|
||||
version: 1.9.1-2
|
||||
|
||||
- package: github.com/rancher/netconf
|
||||
version: 02925e7cf5a0f0bb0aa5360ee260ef7378e5eff8
|
||||
|
@ -244,6 +244,7 @@ rancher:
|
||||
volumes:
|
||||
- /dev:/host/dev
|
||||
- /etc/docker:/etc/docker
|
||||
- /etc/hosts:/etc/hosts
|
||||
- /etc/resolv.conf:/etc/resolv.conf
|
||||
- /etc/rkt:/etc/rkt
|
||||
- /etc/ssl/certs/ca-certificates.crt:/etc/ssl/certs/ca-certificates.crt.rancher
|
||||
|
@ -174,7 +174,7 @@ if [ "$XHYVE" == "1" ] || [ "$QEMU" == "1" ]; then
|
||||
-net user,vlan=0,hostfwd=tcp::2222-:22,hostname=rancher-dev \
|
||||
-drive if=virtio,file=${HD} \
|
||||
${KVM_ENABLE} \
|
||||
-smp 4 \
|
||||
-smp 1 \
|
||||
-cdrom ${CLOUD_CONFIG_ISO} \
|
||||
-append "${KERNEL_ARGS}" \
|
||||
-nographic \
|
||||
|
@ -75,7 +75,7 @@ def test_docker_tls_args(qemu, cloud_config):
|
||||
stderr=subprocess.STDOUT, universal_newlines=True)
|
||||
|
||||
subprocess.check_call(
|
||||
ssh_command + ['docker', '--tlsverify', '-H', '127.0.0.1:2376', 'version'],
|
||||
ssh_command + ['docker', '--tlsverify', 'version'],
|
||||
stderr=subprocess.STDOUT, universal_newlines=True)
|
||||
|
||||
|
||||
|
13
vendor/github.com/rancher/docker-from-scratch/scratch.go
generated
vendored
13
vendor/github.com/rancher/docker-from-scratch/scratch.go
generated
vendored
@ -273,6 +273,19 @@ func setupNetworking(config *Config) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
hostname, err := os.Hostname();
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tryCreateFile("/etc/hosts", `127.0.0.1 localhost
|
||||
::1 localhost ip6-localhost ip6-loopback
|
||||
fe00::0 ip6-localnet
|
||||
ff00::0 ip6-mcastprefix
|
||||
ff02::1 ip6-allnodes
|
||||
ff02::2 ip6-allrouters
|
||||
|
||||
127.0.1.1 `+hostname)
|
||||
|
||||
if len(config.DnsConfig.Nameservers) != 0 {
|
||||
if _, err := resolvconf.Build("/etc/resolv.conf", config.DnsConfig.Nameservers, config.DnsConfig.Search, nil); err != nil {
|
||||
return err
|
||||
|
Loading…
Reference in New Issue
Block a user