module Propellor.Property.Hostname where
import Propellor.Base
import qualified Propellor.Property.File as File
import Propellor.Property.Chroot (inChroot)
import Data.List
import Data.List.Utils
sane :: Property NoInfo
sane = sane' extractDomain
sane' :: ExtractDomain -> Property NoInfo
sane' extractdomain = property ("sane hostname") $
ensureProperty . setTo' extractdomain =<< asks hostName
setTo :: HostName -> Property NoInfo
setTo = setTo' extractDomain
setTo' :: ExtractDomain -> HostName -> Property NoInfo
setTo' extractdomain hn = combineProperties desc go
where
desc = "hostname " ++ hn
basehost = takeWhile (/= '.') hn
domain = extractdomain hn
go = catMaybes
[ Just $ "/etc/hostname" `File.hasContent` [basehost]
, if null domain
then Nothing
else Just $ trivial $ hostsline "127.0.1.1" [hn, basehost]
, Just $ trivial $ hostsline "127.0.0.1" ["localhost"]
, Just $ trivial $ check (not <$> inChroot) $
cmdProperty "hostname" [basehost]
, Just $ "/etc/mailname" `File.hasContent`
[if null domain then hn else domain]
]
hostsline ip names = File.fileProperty desc
(addhostsline ip names)
"/etc/hosts"
addhostsline ip names ls =
(ip ++ "\t" ++ (unwords names)) : filter (not . hasip ip) ls
hasip ip l = headMaybe (words l) == Just ip
searchDomain :: Property NoInfo
searchDomain = searchDomain' extractDomain
searchDomain' :: ExtractDomain -> Property NoInfo
searchDomain' extractdomain = property desc (ensureProperty . go =<< asks hostName)
where
desc = "resolv.conf search and domain configured"
go hn =
let domain = extractdomain hn
in File.fileProperty desc (use domain) "/etc/resolv.conf"
use domain ls = filter wanted $ nub (ls ++ cfgs)
where
cfgs = ["domain " ++ domain, "search " ++ domain]
wanted l
| l `elem` cfgs = True
| "domain " `isPrefixOf` l = False
| "search " `isPrefixOf` l = False
| otherwise = True
type ExtractDomain = HostName -> String
extractDomain :: ExtractDomain
extractDomain hn =
let bits = split "." hn
in intercalate "." $
if length bits > 2
then drop 1 bits
else bits