{-# LANGUAGE LambdaCase #-}
module Propellor.Property.Hostname where
import Propellor.Base
import qualified Propellor.Property.File as File
import Propellor.Types.Container
import Utility.Split
import Data.List
sane :: Property UnixLike
sane = sane' extractDomain
sane' :: ExtractDomain -> Property UnixLike
sane' extractdomain = property' ("sane hostname") $ \w ->
ensureProperty w . setTo' extractdomain =<< asks hostName
setTo :: HostName -> Property UnixLike
setTo = setTo' extractDomain
setTo' :: ExtractDomain -> HostName -> Property UnixLike
setTo' extractdomain hn = combineProperties desc $ toProps
[ "/etc/hostname" `File.hasContent` [basehost]
, hostslines $ catMaybes
[ if null domain
then Nothing
else Just ("127.0.1.1", [hn, basehost])
, Just ("127.0.0.1", ["localhost"])
]
, check safetochange $
cmdProperty "hostname" [basehost]
`assume` NoChange
]
where
desc = "hostname " ++ hn
basehost = takeWhile (/= '.') hn
domain = extractdomain hn
safetochange = askInfo >>= return . \case
[] -> True
caps -> HostnameContained `elem` caps
hostslines ipsnames =
File.fileProperty desc (addhostslines ipsnames) "/etc/hosts"
addhostslines :: [(String, [String])] -> [String] -> [String]
addhostslines ipsnames ls =
let ips = map fst ipsnames
hasip l = maybe False (`elem` ips) (headMaybe (words l))
mkline (ip, names) = ip ++ "\t" ++ (unwords names)
in map mkline ipsnames ++ filter (not . hasip) ls
searchDomain :: Property UnixLike
searchDomain = searchDomain' extractDomain
searchDomain' :: ExtractDomain -> Property UnixLike
searchDomain' extractdomain = property' desc $ \w ->
(ensureProperty w . 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
mailname :: Property UnixLike
mailname = mailname' extractDomain
mailname' :: ExtractDomain -> Property UnixLike
mailname' extractdomain = property' ("mailname set from hostname") $ \w ->
ensureProperty w . go =<< asks hostName
where
go mn = "/etc/mailname" `File.hasContent` [if null mn' then mn else mn']
where
mn' = extractdomain mn
type ExtractDomain = HostName -> String
extractDomain :: ExtractDomain
extractDomain hn =
let bits = split "." hn
in intercalate "." $
if length bits > 2
then drop 1 bits
else bits