summaryrefslogtreecommitdiff
path: root/Propellor/Property/Hostname.hs
diff options
context:
space:
mode:
authorJoey Hess2014-04-04 01:12:44 -0400
committerJoey Hess2014-04-04 01:12:44 -0400
commit8bb175d1078de6add55f5c5f689693495d141436 (patch)
tree74173c19b2b293b94d993398c41a3063d2f5b364 /Propellor/Property/Hostname.hs
parenteea8637afe78ceff25ddd11d365a3fc022cc5fa4 (diff)
parentccc82907124ccd2ad4951c2c4946ae20af007530 (diff)
Merge branch 'joeyconfig'
Conflicts: config.hs
Diffstat (limited to 'Propellor/Property/Hostname.hs')
-rw-r--r--Propellor/Property/Hostname.hs28
1 files changed, 25 insertions, 3 deletions
diff --git a/Propellor/Property/Hostname.hs b/Propellor/Property/Hostname.hs
index 25f0e1b2..26635374 100644
--- a/Propellor/Property/Hostname.hs
+++ b/Propellor/Property/Hostname.hs
@@ -3,7 +3,29 @@ module Propellor.Property.Hostname where
import Propellor
import qualified Propellor.Property.File as File
+-- | 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).
set :: HostName -> Property
-set hostname = "/etc/hostname" `File.hasContent` [hostname]
- `onChange` cmdProperty "hostname" [hostname]
- `describe` ("hostname " ++ hostname)
+set hostname = combineProperties desc go
+ `onChange` cmdProperty "hostname" [host]
+ where
+ desc = "hostname " ++ hostname
+ (host, domain) = separate (== '.') hostname
+
+ 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