2014-04-11 01:09:20 +00:00
|
|
|
{-# LANGUAGE PackageImports #-}
|
|
|
|
|
|
|
|
module Propellor.Attr where
|
|
|
|
|
|
|
|
import Propellor.Types
|
|
|
|
import Propellor.Types.Attr
|
|
|
|
|
|
|
|
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-04-13 06:28:40 +00:00
|
|
|
import Control.Applicative
|
2014-04-11 01:09:20 +00:00
|
|
|
|
2014-04-18 08:48:49 +00:00
|
|
|
pureAttrProperty :: Desc -> SetAttr -> Property
|
2014-04-18 07:59:06 +00:00
|
|
|
pureAttrProperty desc = Property ("has " ++ desc) (return NoChange)
|
2014-04-11 01:09:20 +00:00
|
|
|
|
2014-04-18 07:59:06 +00:00
|
|
|
hostname :: HostName -> Property
|
2014-04-11 01:09:20 +00:00
|
|
|
hostname name = pureAttrProperty ("hostname " ++ name) $
|
|
|
|
\d -> d { _hostname = name }
|
|
|
|
|
|
|
|
getHostName :: Propellor HostName
|
|
|
|
getHostName = asks _hostname
|
|
|
|
|
2014-04-18 07:59:06 +00:00
|
|
|
os :: System -> Property
|
2014-04-13 19:54:25 +00:00
|
|
|
os system = pureAttrProperty ("Operating " ++ show system) $
|
2014-04-13 19:34:01 +00:00
|
|
|
\d -> d { _os = Just system }
|
|
|
|
|
|
|
|
getOS :: Propellor (Maybe System)
|
|
|
|
getOS = asks _os
|
|
|
|
|
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
|
|
|
|
ipv4 addr = pureAttrProperty ("ipv4 " ++ addr)
|
|
|
|
(addDNS $ Address $ IPv4 addr)
|
|
|
|
|
|
|
|
-- | Indidate that a host has an AAAA record in the DNS.
|
|
|
|
ipv6 :: String -> Property
|
|
|
|
ipv6 addr = pureAttrProperty ("ipv6 " ++ addr)
|
|
|
|
(addDNS $ Address $ IPv6 addr)
|
|
|
|
|
2014-04-19 03:41:26 +00:00
|
|
|
-- | Indicates another name for the host in the DNS.
|
2014-04-19 05:28:46 +00:00
|
|
|
alias :: Domain -> Property
|
2014-04-19 15:06:28 +00:00
|
|
|
alias domain = pureAttrProperty ("alias " ++ domain)
|
2014-04-18 21:19:28 +00:00
|
|
|
(addDNS $ CNAME $ AbsDomain domain)
|
2014-04-11 01:09:20 +00:00
|
|
|
|
2014-04-18 21:19:28 +00:00
|
|
|
addDNS :: Record -> SetAttr
|
|
|
|
addDNS record d = d { _dns = S.insert record (_dns d) }
|
2014-04-11 01:09:20 +00:00
|
|
|
|
2014-04-19 05:42:19 +00:00
|
|
|
-- | Adds a DNS NamedConf stanza.
|
|
|
|
--
|
|
|
|
-- Note that adding a Master stanza for a domain always overrides an
|
|
|
|
-- existing Secondary stanza, while a Secondary stanza is only added
|
|
|
|
-- when there is no existing Master stanza.
|
2014-04-19 05:26:38 +00:00
|
|
|
addNamedConf :: NamedConf -> SetAttr
|
2014-04-19 05:42:19 +00:00
|
|
|
addNamedConf conf d = d { _namedconf = new }
|
|
|
|
where
|
|
|
|
m = _namedconf d
|
|
|
|
domain = confDomain conf
|
2014-04-23 19:04:35 +00:00
|
|
|
new = case (confDnsServerType conf, confDnsServerType <$> M.lookup domain m) of
|
2014-04-19 05:42:19 +00:00
|
|
|
(Secondary, Just Master) -> m
|
|
|
|
_ -> M.insert domain conf m
|
|
|
|
|
|
|
|
getNamedConf :: Propellor (M.Map Domain NamedConf)
|
2014-04-19 05:26:38 +00:00
|
|
|
getNamedConf = asks _namedconf
|
|
|
|
|
2014-04-18 07:59:06 +00:00
|
|
|
sshPubKey :: String -> Property
|
2014-04-13 06:28:40 +00:00
|
|
|
sshPubKey k = pureAttrProperty ("ssh pubkey known") $
|
|
|
|
\d -> d { _sshPubKey = Just k }
|
|
|
|
|
|
|
|
getSshPubKey :: Propellor (Maybe String)
|
|
|
|
getSshPubKey = asks _sshPubKey
|
|
|
|
|
2014-04-11 01:09:20 +00:00
|
|
|
hostnameless :: Attr
|
|
|
|
hostnameless = newAttr (error "hostname Attr not specified")
|
|
|
|
|
|
|
|
hostAttr :: Host -> Attr
|
|
|
|
hostAttr (Host _ mkattrs) = mkattrs hostnameless
|
|
|
|
|
|
|
|
hostProperties :: Host -> [Property]
|
|
|
|
hostProperties (Host ps _) = ps
|
|
|
|
|
|
|
|
hostMap :: [Host] -> M.Map HostName Host
|
|
|
|
hostMap l = M.fromList $ zip (map (_hostname . hostAttr) l) l
|
|
|
|
|
2014-04-19 01:10:44 +00:00
|
|
|
hostAttrMap :: [Host] -> M.Map HostName Attr
|
|
|
|
hostAttrMap l = M.fromList $ zip (map _hostname attrs) attrs
|
|
|
|
where
|
|
|
|
attrs = map hostAttr 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-04-19 01:10:44 +00:00
|
|
|
getAddresses :: Attr -> [IPAddr]
|
|
|
|
getAddresses = mapMaybe getIPAddr . S.toList . _dns
|
|
|
|
|
|
|
|
hostAddresses :: HostName -> [Host] -> [IPAddr]
|
|
|
|
hostAddresses hn hosts = case hostAttr <$> findHost hosts hn of
|
2014-04-18 21:38:21 +00:00
|
|
|
Nothing -> []
|
|
|
|
Just attr -> mapMaybe getIPAddr $ S.toList $ _dns attr
|
|
|
|
|
2014-04-13 06:28:40 +00:00
|
|
|
-- | Lifts an action into a different host.
|
|
|
|
--
|
|
|
|
-- For example, `fromHost hosts "otherhost" getSshPubKey`
|
|
|
|
fromHost :: [Host] -> HostName -> Propellor a -> Propellor (Maybe a)
|
|
|
|
fromHost l hn getter = case findHost l hn of
|
|
|
|
Nothing -> return Nothing
|
|
|
|
Just h -> liftIO $ Just <$>
|
|
|
|
runReaderT (runWithAttr getter) (hostAttr h)
|