This commit is contained in:
Joey Hess 2014-03-31 10:36:45 -04:00
parent f9536060e8
commit 9b65d96504
Failed to extract signature
4 changed files with 31 additions and 25 deletions

View File

@ -17,7 +17,7 @@
-- > getProperties "example.com" = Just -- > getProperties "example.com" = Just
-- > [ Apt.installed ["mydaemon"] -- > [ Apt.installed ["mydaemon"]
-- > , "/etc/mydaemon.conf" `File.containsLine` "secure=1" -- > , "/etc/mydaemon.conf" `File.containsLine` "secure=1"
-- > `onChange` cmdProperty "service" ["mydaemon", "restart"]] -- > `onChange` cmdProperty "service" ["mydaemon", "restart"]
-- > ] -- > ]
-- > getProperties _ = Nothing -- > getProperties _ = Nothing
-- --
@ -57,3 +57,4 @@ import Data.Maybe as X
import Data.Either as X import Data.Either as X
import Control.Applicative as X import Control.Applicative as X
import Control.Monad as X import Control.Monad as X
import Data.Monoid as X

View File

@ -3,6 +3,7 @@ module Propellor.Engine where
import System.Console.ANSI import System.Console.ANSI
import System.Exit import System.Exit
import System.IO import System.IO
import Data.Monoid
import Propellor.Types import Propellor.Types
import Utility.Exception import Utility.Exception
@ -42,7 +43,7 @@ ensureProperties' ps = ensure ps NoChange
putStrLn "done" putStrLn "done"
setSGR [] setSGR []
hFlush stdout hFlush stdout
ensure ls (combineResult r rs) ensure ls (r <> rs)
warningMessage :: String -> IO () warningMessage :: String -> IO ()
warningMessage s = do warningMessage s = do

View File

@ -2,6 +2,7 @@ module Propellor.Property where
import System.Directory import System.Directory
import Control.Monad import Control.Monad
import Data.Monoid
import Propellor.Types import Propellor.Types
import Propellor.Engine import Propellor.Engine
@ -13,16 +14,15 @@ makeChange a = a >> return MadeChange
noChange :: IO Result noChange :: IO Result
noChange = return NoChange noChange = return NoChange
{- | Combines a list of properties, resulting in a single property -- | Combines a list of properties, resulting in a single property
- that when run will run each property in the list in turn, -- that when run will run each property in the list in turn,
- and print out the description of each as it's run. Does not stop -- and print out the description of each as it's run. Does not stop
- on failure; does propigate overall success/failure. -- on failure; does propigate overall success/failure.
-}
propertyList :: Desc -> [Property] -> Property propertyList :: Desc -> [Property] -> Property
propertyList desc ps = Property desc $ ensureProperties' ps propertyList desc ps = Property desc $ ensureProperties' ps
{- | Combines a list of properties, resulting in one property that -- | Combines a list of properties, resulting in one property that
- ensures each in turn, stopping on failure. -} -- ensures each in turn, stopping on failure.
combineProperties :: [Property] -> Property combineProperties :: [Property] -> Property
combineProperties ps = Property desc $ go ps NoChange combineProperties ps = Property desc $ go ps NoChange
where where
@ -31,14 +31,14 @@ combineProperties ps = Property desc $ go ps NoChange
r <- ensureProperty l r <- ensureProperty l
case r of case r of
FailedChange -> return FailedChange FailedChange -> return FailedChange
_ -> go ls (combineResult r rs) _ -> go ls (r <> rs)
desc = case ps of desc = case ps of
(p:_) -> propertyDesc p (p:_) -> propertyDesc p
_ -> "(empty)" _ -> "(empty)"
{- | Makes a perhaps non-idempotent Property be idempotent by using a flag -- | Makes a perhaps non-idempotent Property be idempotent by using a flag
- file to indicate whether it has run before. -- file to indicate whether it has run before.
- Use with caution. -} -- Use with caution.
flagFile :: Property -> FilePath -> Property flagFile :: Property -> FilePath -> Property
flagFile property flagfile = Property (propertyDesc property) $ flagFile property flagfile = Property (propertyDesc property) $
go =<< doesFileExist flagfile go =<< doesFileExist flagfile
@ -50,19 +50,19 @@ flagFile property flagfile = Property (propertyDesc property) $
writeFile flagfile "" writeFile flagfile ""
return r return r
{- | Whenever a change has to be made for a Property, causes a hook --- | Whenever a change has to be made for a Property, causes a hook
- Property to also be run, but not otherwise. -} -- Property to also be run, but not otherwise.
onChange :: Property -> Property -> Property onChange :: Property -> Property -> Property
property `onChange` hook = Property (propertyDesc property) $ do property `onChange` hook = Property (propertyDesc property) $ do
r <- ensureProperty property r <- ensureProperty property
case r of case r of
MadeChange -> do MadeChange -> do
r' <- ensureProperty hook r' <- ensureProperty hook
return $ combineResult r r' return $ r <> r'
_ -> return r _ -> return r
{- | Indicates that the first property can only be satisfied once -- | Indicates that the first property can only be satisfied once
- the second is. -} -- the second is.
requires :: Property -> Property -> Property requires :: Property -> Property -> Property
x `requires` y = combineProperties [y, x] `describe` propertyDesc x x `requires` y = combineProperties [y, x] `describe` propertyDesc x
@ -73,7 +73,7 @@ describe p d = p { propertyDesc = d }
(==>) = flip describe (==>) = flip describe
infixl 1 ==> infixl 1 ==>
{- | Makes a Property only be performed when a test succeeds. -} -- | Makes a Property only be performed when a test succeeds.
check :: IO Bool -> Property -> Property check :: IO Bool -> Property -> Property
check c property = Property (propertyDesc property) $ ifM c check c property = Property (propertyDesc property) $ ifM c
( ensureProperty property ( ensureProperty property

View File

@ -1,5 +1,7 @@
module Propellor.Types where module Propellor.Types where
import Data.Monoid
type HostName = String type HostName = String
type UserName = String type UserName = String
@ -14,9 +16,11 @@ type Desc = String
data Result = NoChange | MadeChange | FailedChange data Result = NoChange | MadeChange | FailedChange
deriving (Show, Eq) deriving (Show, Eq)
combineResult :: Result -> Result -> Result instance Monoid Result where
combineResult FailedChange _ = FailedChange mempty = NoChange
combineResult _ FailedChange = FailedChange
combineResult MadeChange _ = MadeChange mappend FailedChange _ = FailedChange
combineResult _ MadeChange = MadeChange mappend _ FailedChange = FailedChange
combineResult NoChange NoChange = NoChange mappend MadeChange _ = MadeChange
mappend _ MadeChange = MadeChange
mappend NoChange NoChange = NoChange