summaryrefslogtreecommitdiff
path: root/Propellor/Property/Hostname.hs
blob: 0708b3ff614896b51706e46a4bcf5abeef9fa1ba (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
module Propellor.Property.Hostname where

import Propellor
import qualified Propellor.Property.File as File

-- | Ensures that the hostname is set to the HostAttr value.
-- Configures both /etc/hostname and the current hostname.
--
-- When the hostname is a FQDN, also configures /etc/hosts,
-- with an entry for 127.0.1.1, which is standard at least on Debian
-- to set the FDQN (127.0.0.1 is localhost).
sane :: Property
sane = Property ("sane hostname") (ensureProperty . setTo =<< getHostName)

setTo :: HostName -> Property
setTo hostname = combineProperties desc go
	`onChange` cmdProperty "hostname" [host]
  where
	desc = "hostname " ++ hostname
	(host, domain) = separate (== '.') hostname

	go = catMaybes
		[ Just $ "/etc/hostname" `File.hasContent` [host]
		, if null domain
			then Nothing 
			else Just $ File.fileProperty desc
				addhostline "/etc/hosts"
		]
	
	hostip = "127.0.1.1"
	hostline = hostip ++ "\t" ++ hostname ++ " " ++ host

	addhostline ls = hostline : filter (not . hashostip) ls
	hashostip l = headMaybe (words l) == Just hostip