propellor

propellor config for hosts.
git clone git://git.ricketyspace.net/propellor.git
Log | Files | Refs | LICENSE

PropAccum.hs (2678B)


      1 {-# LANGUAGE TypeFamilies #-}
      2 {-# LANGUAGE PolyKinds #-}
      3 {-# LANGUAGE FlexibleContexts #-}
      4 {-# LANGUAGE FlexibleInstances #-}
      5 {-# LANGUAGE MultiParamTypeClasses #-}
      6 {-# LANGUAGE DataKinds #-}
      7 
      8 module Propellor.PropAccum
      9 	( host
     10 	, Props(..)
     11 	, props
     12 	, (&)
     13 	, (&^)
     14 	, (!)
     15 	) where
     16 
     17 import Propellor.Types
     18 import Propellor.Types.MetaTypes
     19 import Propellor.Types.Core
     20 import Propellor.Property
     21 
     22 import GHC.TypeLits
     23 import Data.Monoid
     24 import Prelude
     25 
     26 -- | Defines a host and its properties.
     27 --
     28 -- > host "example.com" $ props
     29 -- > 	& someproperty
     30 -- > 	! oldproperty
     31 -- > 	& otherproperty
     32 host :: HostName -> Props metatypes -> Host
     33 host hn (Props ps) = Host hn ps (mconcat (map getInfoRecursive ps))
     34 
     35 -- | Start accumulating a list of properties.
     36 --
     37 -- Properties can be added to it using `(&)` etc.
     38 props :: Props UnixLike
     39 props = Props []
     40 
     41 infixl 1 &
     42 infixl 1 &^
     43 infixl 1 !
     44 
     45 type family GetMetaTypes x where
     46 	GetMetaTypes (Property (MetaTypes t)) = MetaTypes t
     47 	GetMetaTypes (RevertableProperty (MetaTypes t) undo) = MetaTypes t
     48 
     49 -- When many properties are combined, ghc error message
     50 -- can include quite a lot of code, typically starting with
     51 -- `props  and including all the properties up to and including the
     52 -- one that fails to combine. Point the user in the right direction.
     53 type family NoteFor symbol :: ErrorMessage where
     54 	NoteFor symbol =
     55 		'Text "Probably the problem is with the last property added with "
     56 			':<>: symbol
     57 			':<>: 'Text " in the code excerpt below."
     58 
     59 -- | Adds a property to a Props.
     60 --
     61 -- Can add Properties and RevertableProperties
     62 (&)
     63 	::
     64 		( IsProp p
     65 		-- -Wredundant-constraints is turned off because
     66 		-- this constraint appears redundant, but is actually
     67 		-- crucial.
     68 		, MetaTypes y ~ GetMetaTypes p
     69 		, CheckCombinableNote x y (NoteFor ('Text "&"))
     70 		)
     71 	=> Props (MetaTypes x)
     72 	-> p
     73 	-> Props (MetaTypes (Combine x y))
     74 Props c & p = Props (c ++ [toChildProperty p])
     75 
     76 -- | Adds a property before any other properties.
     77 (&^)
     78 	::
     79 		( IsProp p
     80 		-- -Wredundant-constraints is turned off because
     81 		-- this constraint appears redundant, but is actually
     82 		-- crucial.
     83 		, MetaTypes y ~ GetMetaTypes p
     84 		, CheckCombinableNote x y (NoteFor ('Text "&^"))
     85 		)
     86 	=> Props (MetaTypes x)
     87 	-> p
     88 	-> Props (MetaTypes (Combine x y))
     89 Props c &^ p = Props (toChildProperty p : c)
     90 
     91 -- | Adds a property in reverted form.
     92 (!)
     93 	-- -Wredundant-constraints is turned off because
     94 	-- this constraint appears redundant, but is actually
     95 	-- crucial.
     96 	:: CheckCombinableNote x z (NoteFor ('Text "!"))
     97 	=> Props (MetaTypes x)
     98 	-> RevertableProperty (MetaTypes y) (MetaTypes z)
     99 	-> Props (MetaTypes (Combine x z))
    100 Props c ! p = Props (c ++ [toChildProperty (revert p)])