2014-04-11 01:09:20 +00:00
|
|
|
{-# LANGUAGE PackageImports #-}
|
|
|
|
|
2014-06-09 05:45:58 +00:00
|
|
|
module Propellor.Info where
|
2014-04-11 01:09:20 +00:00
|
|
|
|
|
|
|
import Propellor.Types
|
|
|
|
|
|
|
|
import "mtl" Control.Monad.Reader
|
|
|
|
import qualified Data.Set as S
|
|
|
|
import qualified Data.Map as M
|
2014-04-18 21:38:21 +00:00
|
|
|
import Data.Maybe
|
2014-06-01 00:39:56 +00:00
|
|
|
import Data.Monoid
|
2014-04-13 06:28:40 +00:00
|
|
|
import Control.Applicative
|
2014-04-11 01:09:20 +00:00
|
|
|
|
2014-06-09 05:45:58 +00:00
|
|
|
pureInfoProperty :: Desc -> Info -> Property
|
2015-01-18 22:02:07 +00:00
|
|
|
pureInfoProperty desc i = Property ("has " ++ desc) (return NoChange) i mempty
|
2014-04-11 01:09:20 +00:00
|
|
|
|
2014-06-09 05:45:58 +00:00
|
|
|
askInfo :: (Info -> Val a) -> Propellor (Maybe a)
|
|
|
|
askInfo f = asks (fromVal . f . hostInfo)
|
2014-06-01 01:18:36 +00:00
|
|
|
|
2014-04-18 07:59:06 +00:00
|
|
|
os :: System -> Property
|
2014-06-09 05:45:58 +00:00
|
|
|
os system = pureInfoProperty ("Operating " ++ show system) $
|
2014-06-01 01:18:36 +00:00
|
|
|
mempty { _os = Val system }
|
2014-04-13 19:34:01 +00:00
|
|
|
|
|
|
|
getOS :: Propellor (Maybe System)
|
2014-06-09 05:45:58 +00:00
|
|
|
getOS = askInfo _os
|
2014-04-13 19:34:01 +00:00
|
|
|
|
2014-04-18 21:38:21 +00:00
|
|
|
-- | Indidate that a host has an A record in the DNS.
|
|
|
|
--
|
2015-01-01 17:57:13 +00:00
|
|
|
-- When propellor is used to deploy a DNS server for a domain,
|
|
|
|
-- the hosts in the domain are found by looking for these
|
|
|
|
-- and similar properites.
|
|
|
|
--
|
|
|
|
-- When propellor --spin is used to deploy a host, it checks
|
|
|
|
-- if the host's IP Property matches the DNS. If the DNS is missing or
|
|
|
|
-- out of date, the host will instead be contacted directly by IP address.
|
2014-04-18 21:38:21 +00:00
|
|
|
ipv4 :: String -> Property
|
2014-05-31 20:48:14 +00:00
|
|
|
ipv4 = addDNS . Address . IPv4
|
2014-04-18 21:38:21 +00:00
|
|
|
|
|
|
|
-- | Indidate that a host has an AAAA record in the DNS.
|
|
|
|
ipv6 :: String -> Property
|
2014-05-31 20:48:14 +00:00
|
|
|
ipv6 = addDNS . Address . IPv6
|
2014-04-18 21:38:21 +00:00
|
|
|
|
2014-04-19 03:41:26 +00:00
|
|
|
-- | Indicates another name for the host in the DNS.
|
2014-06-01 19:07:17 +00:00
|
|
|
--
|
|
|
|
-- When the host's ipv4/ipv6 addresses are known, the alias is set up
|
|
|
|
-- to use their address, rather than using a CNAME. This avoids various
|
|
|
|
-- problems with CNAMEs, and also means that when multiple hosts have the
|
|
|
|
-- same alias, a DNS round-robin is automatically set up.
|
2014-04-19 05:28:46 +00:00
|
|
|
alias :: Domain -> Property
|
2014-07-23 16:23:44 +00:00
|
|
|
alias d = pureInfoProperty ("alias " ++ d) $ mempty
|
|
|
|
{ _aliases = S.singleton d
|
|
|
|
-- A CNAME is added here, but the DNS setup code converts it to an
|
|
|
|
-- IP address when that makes sense.
|
|
|
|
, _dns = S.singleton $ CNAME $ AbsDomain d
|
|
|
|
}
|
2014-04-11 01:09:20 +00:00
|
|
|
|
2014-05-31 20:48:14 +00:00
|
|
|
addDNS :: Record -> Property
|
2014-07-23 16:23:44 +00:00
|
|
|
addDNS r = pureInfoProperty (rdesc r) $ mempty { _dns = S.singleton r }
|
2014-05-31 20:48:14 +00:00
|
|
|
where
|
|
|
|
rdesc (CNAME d) = unwords ["alias", ddesc d]
|
|
|
|
rdesc (Address (IPv4 addr)) = unwords ["ipv4", addr]
|
|
|
|
rdesc (Address (IPv6 addr)) = unwords ["ipv6", addr]
|
|
|
|
rdesc (MX n d) = unwords ["MX", show n, ddesc d]
|
|
|
|
rdesc (NS d) = unwords ["NS", ddesc d]
|
|
|
|
rdesc (TXT s) = unwords ["TXT", s]
|
|
|
|
rdesc (SRV x y z d) = unwords ["SRV", show x, show y, show z, ddesc d]
|
2015-01-04 23:24:18 +00:00
|
|
|
rdesc (SSHFP x y s) = unwords ["SSHFP", show x, show y, s]
|
2015-01-04 18:20:22 +00:00
|
|
|
rdesc (INCLUDE f) = unwords ["$INCLUDE", f]
|
2014-05-31 20:48:14 +00:00
|
|
|
|
|
|
|
ddesc (AbsDomain domain) = domain
|
|
|
|
ddesc (RelDomain domain) = domain
|
|
|
|
ddesc RootDomain = "@"
|
2014-04-11 01:09:20 +00:00
|
|
|
|
|
|
|
hostMap :: [Host] -> M.Map HostName Host
|
2014-06-01 00:48:23 +00:00
|
|
|
hostMap l = M.fromList $ zip (map hostName l) l
|
2014-04-11 01:09:20 +00:00
|
|
|
|
2014-07-23 16:23:44 +00:00
|
|
|
aliasMap :: [Host] -> M.Map HostName Host
|
2014-07-23 16:33:11 +00:00
|
|
|
aliasMap = M.fromList . concat .
|
|
|
|
map (\h -> map (\aka -> (aka, h)) $ S.toList $ _aliases $ hostInfo h)
|
2014-07-23 16:23:44 +00:00
|
|
|
|
2014-04-11 01:09:20 +00:00
|
|
|
findHost :: [Host] -> HostName -> Maybe Host
|
2014-10-23 16:11:07 +00:00
|
|
|
findHost l hn = maybe (findAlias l hn) Just (findHostNoAlias l hn)
|
|
|
|
|
|
|
|
findHostNoAlias :: [Host] -> HostName -> Maybe Host
|
|
|
|
findHostNoAlias l hn = M.lookup hn (hostMap l)
|
2014-07-23 16:23:44 +00:00
|
|
|
|
|
|
|
findAlias :: [Host] -> HostName -> Maybe Host
|
|
|
|
findAlias l hn = M.lookup hn (aliasMap l)
|
2014-04-13 06:28:40 +00:00
|
|
|
|
2014-06-09 05:45:58 +00:00
|
|
|
getAddresses :: Info -> [IPAddr]
|
2014-04-19 01:10:44 +00:00
|
|
|
getAddresses = mapMaybe getIPAddr . S.toList . _dns
|
|
|
|
|
|
|
|
hostAddresses :: HostName -> [Host] -> [IPAddr]
|
2014-06-09 05:45:58 +00:00
|
|
|
hostAddresses hn hosts = case hostInfo <$> findHost hosts hn of
|
2014-04-18 21:38:21 +00:00
|
|
|
Nothing -> []
|
2014-06-09 05:45:58 +00:00
|
|
|
Just info -> mapMaybe getIPAddr $ S.toList $ _dns info
|