propellor/src/Propellor/Property/Hostname.hs

64 lines
2.0 KiB
Haskell
Raw Normal View History

2014-03-31 03:37:54 +00:00
module Propellor.Property.Hostname where
2014-03-30 03:45:48 +00:00
2014-03-31 03:55:59 +00:00
import Propellor
2014-03-31 03:37:54 +00:00
import qualified Propellor.Property.File as File
2014-03-30 03:45:48 +00:00
2014-07-18 04:20:09 +00:00
import Data.List
-- | Ensures that the hostname is set using best practices.
--
2014-11-19 03:51:37 +00:00
-- Configures `/etc/hostname` and the current hostname.
2014-04-04 03:57:36 +00:00
--
2014-11-19 03:51:37 +00:00
-- Configures `/etc/mailname` with the domain part of the hostname.
2014-07-18 16:48:08 +00:00
--
2014-11-19 03:51:37 +00:00
-- `/etc/hosts` is also configured, with an entry for 127.0.1.1, which is
-- standard at least on Debian to set the FDQN.
--
2014-11-19 03:51:37 +00:00
-- Also, the `/etc/hosts` 127.0.0.1 line is set to localhost. Putting any
-- other hostnames there is not best practices and can lead to annoying
-- messages from eg, apache.
2014-04-10 21:46:03 +00:00
sane :: Property
2014-06-01 00:48:23 +00:00
sane = property ("sane hostname") (ensureProperty . setTo =<< asks hostName)
2014-04-10 21:46:03 +00:00
setTo :: HostName -> Property
2014-04-11 01:09:20 +00:00
setTo hn = combineProperties desc go
2014-04-04 03:35:36 +00:00
where
2014-04-11 01:09:20 +00:00
desc = "hostname " ++ hn
(basehost, domain) = separate (== '.') hn
2014-04-04 03:57:36 +00:00
go = catMaybes
2014-04-11 01:09:20 +00:00
[ Just $ "/etc/hostname" `File.hasContent` [basehost]
2014-04-04 03:57:36 +00:00
, if null domain
then Nothing
2014-07-06 16:27:05 +00:00
else Just $ trivial $ hostsline "127.0.1.1" [hn, basehost]
2014-07-06 16:26:09 +00:00
, Just $ trivial $ hostsline "127.0.0.1" ["localhost"]
2014-05-19 21:29:46 +00:00
, Just $ trivial $ cmdProperty "hostname" [basehost]
2014-07-18 16:48:08 +00:00
, Just $ "/etc/mailname" `File.hasContent`
[if null domain then hn else domain]
2014-04-04 03:57:36 +00:00
]
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
2014-07-18 04:20:09 +00:00
2014-11-19 03:51:37 +00:00
-- | Makes `/etc/resolv.conf` contain search and domain lines for
2014-07-18 04:20:09 +00:00
-- the domain that the hostname is in.
searchDomain :: Property
searchDomain = property desc (ensureProperty . go =<< asks hostName)
where
desc = "resolv.conf search and domain configured"
go hn =
let (_basehost, domain) = separate (== '.') 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