propellor/Propellor/Property/Hostname.hs

32 lines
926 B
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-04-04 03:57:36 +00:00
-- | Sets the hostname. Configures both /etc/hostname and the current
-- hostname.
--
-- When provided with 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).
2014-03-30 03:45:48 +00:00
set :: HostName -> Property
2014-04-04 04:11:24 +00:00
set hostname = combineProperties desc go
2014-04-04 03:57:36 +00:00
`onChange` cmdProperty "hostname" [host]
2014-04-04 03:35:36 +00:00
where
2014-04-04 03:57:36 +00:00
desc = "hostname " ++ hostname
2014-04-04 03:35:36 +00:00
(host, domain) = separate (== '.') hostname
2014-04-04 03:57:36 +00:00
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