summaryrefslogtreecommitdiff
path: root/hostname.go
blob: c36541a3d0e81be24a93c6d4b48ee0dca50f7484 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// figures out if your hostname is valid
// then checks if your DNS is setup correctly
package linuxstatus

import (
	"io/ioutil"
	"go.wit.com/log"

	// will try to get this hosts FQDN
	"github.com/Showmax/go-fqdn"
)

func (ls *LinuxStatus) GetDomainName() string {
	if ! me.Ready() {return ""}
	return me.domainname.Get()
}

func (ls *LinuxStatus) setDomainName() {
	if ! me.Ready() {return}

	dn := run("domainname")
	if (me.domainname.Get() != dn) {
		log.Log(CHANGE, "domainname has changed from", me.GetDomainName(), "to", dn)
		me.domainname.Set(dn)
		me.changed = true
	}
}

func (ls *LinuxStatus) GetHostname() string {
	if ! me.Ready() {return ""}
	return me.hostname.Get()
}

func (ls *LinuxStatus) ValidHostname() bool {
	if ! me.Ready() {return false}
	if me.hostnameStatus.Get() == "WORKING" {
		return true
	}
	return false
}

func (ls *LinuxStatus) setHostname(newname string) {
	if ! me.Ready() {return}
	if newname ==  me.hostname.Get() {
		return
	}
	log.Log(CHANGE, "hostname has changed from", me.GetHostname(), "to", newname)
	me.hostname.Set(newname)
	me.changed = true
}

func (ls *LinuxStatus) GetHostShort() string {
	if ! me.Ready() {return ""}
	return me.hostshort.Get()
}

func (ls *LinuxStatus) setHostShort() {
	if ! me.Ready() {return}
	hshort := run("hostname -s")
	if (me.hostshort.Get() != hshort) {
		log.Log(CHANGE, "hostname -s has changed from", me.hostshort.Get(), "to", hshort)
		me.hostshort.Set(hshort)
		me.changed = true
	}
}

func lookupHostname() {
	if ! me.Ready() {return}
	var err error
	var hostfqdn string = "broken"
	hostfqdn, err = fqdn.FqdnHostname()
	if (err != nil) {
		log.Error(err, "FQDN hostname error")
		return
	}
	log.Log(NET, "full hostname should be: ", hostfqdn)

	me.setDomainName()
	me.setHostShort()

	// these are authoritative
	// if they work wrong, your linux configuration is wrong.
	// Do not complain.
	// Fix your distro if your box is otherwise not working this way
	hshort := me.GetHostShort() // from `hostname -s`
	dn := me.GetDomainName() // from `domanname`
	hostname := me.GetHostname() // from `hostname -f`

	if hostfqdn != hostname {
		log.Log(WARN, "hostname", hostname, "does not equal fqdn.FqdnHostname()", hostfqdn)
		// TODO: figure out what is wrong
	}

	var test string
	test = hshort + "." + dn

	me.setHostname(test)

	if (hostname != test) {
		log.Log(CHANGE, "hostname", hostname, "does not equal", test)
		if (me.hostnameStatus.Get() != "BROKEN") {
			log.Log(CHANGE, "hostname", hostname, "does not equal", test)
			me.changed = true
			me.hostnameStatus.Set("BROKEN")
		}
	} else {
		if (me.hostnameStatus.Get() != "WORKING") {
			log.Log(CHANGE, "hostname", hostname, "is valid")
			me.hostnameStatus.Set("WORKING")
			me.changed = true
		}
	}
}

// returns true if the hostname is good
// check that all the OS settings are correct here
// On Linux, /etc/hosts, /etc/hostname
//      and domainname and hostname
func goodHostname() bool {
	content, err := ioutil.ReadFile("/etc/hostname")
	if err != nil {
		// this needs to be a fixWindow() error
		log.Error(err)
	}

	hostname := string(content)

	log.Log(NOW, "hostname =", hostname)

	hs := run("hostname -s")
	dn := run("domainname")
	log.Log(NOW, "hostname short =", hs, "domainname =", dn)

	tmp := hs + "." + dn
	if (hostname == tmp) {
		log.Log(NOW, "hostname seems to be good", hostname)
		return true
	}

	return false
}