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
|
2014-06-09 05:45:58 +00:00
|
|
|
import Propellor.Types.Info
|
2014-04-11 01:09:20 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
pureInfoProperty desc = Property ("has " ++ desc) (return NoChange)
|
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.
|
|
|
|
--
|
|
|
|
-- TODO check at run time if the host really has this address.
|
|
|
|
-- (Can't change the host's address, but as a sanity check.)
|
|
|
|
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-05-31 20:48:14 +00:00
|
|
|
alias = addDNS . CNAME . AbsDomain
|
2014-04-11 01:09:20 +00:00
|
|
|
|
2014-05-31 20:48:14 +00:00
|
|
|
addDNS :: Record -> Property
|
2014-06-09 05:45:58 +00:00
|
|
|
addDNS r = pureInfoProperty (rdesc r) $
|
2014-06-01 00:39:56 +00:00
|
|
|
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]
|
|
|
|
|
|
|
|
ddesc (AbsDomain domain) = domain
|
|
|
|
ddesc (RelDomain domain) = domain
|
|
|
|
ddesc RootDomain = "@"
|
2014-04-11 01:09:20 +00:00
|
|
|
|
2014-04-18 07:59:06 +00:00
|
|
|
sshPubKey :: String -> Property
|
2014-06-09 05:45:58 +00:00
|
|
|
sshPubKey k = pureInfoProperty ("ssh pubkey known") $
|
2014-06-01 01:18:36 +00:00
|
|
|
mempty { _sshPubKey = Val k }
|
2014-04-13 06:28:40 +00:00
|
|
|
|
|
|
|
getSshPubKey :: Propellor (Maybe String)
|
2014-06-09 05:45:58 +00:00
|
|
|
getSshPubKey = askInfo _sshPubKey
|
2014-04-13 06:28:40 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
findHost :: [Host] -> HostName -> Maybe Host
|
|
|
|
findHost l hn = M.lookup hn (hostMap 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
|