module Propellor.Property.Hostname where
import Propellor
import qualified Propellor.Property.File as File
sane :: Property
sane = property ("sane hostname") (ensureProperty . setTo =<< getHostName)
setTo :: HostName -> Property
setTo hn = combineProperties desc go
where
desc = "hostname " ++ hn
(basehost, domain) = separate (== '.') hn
go = catMaybes
[ Just $ "/etc/hostname" `File.hasContent` [basehost]
, if null domain
then Nothing
else Just $ File.filePropertydesc
addhostline "/etc/hosts"
, Just $ trivial $ cmdProperty "hostname" [basehost]
]
hostip = "127.0.1.1"
hostline = hostip ++ "\t" ++ hn ++ " " ++ basehost
addhostline ls = hostline : filter (not . hashostip) ls
hashostip l = headMaybe (words l) == Just hostip