From 29442f222ec2342c6dea7036e80e15eefa4158c0 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Sat, 6 Dec 2014 13:21:19 -0400 Subject: Reboot.atEnd --- src/Propellor/Engine.hs | 12 ++++++------ src/Propellor/Property.hs | 3 +-- src/Propellor/Property/OS.hs | 8 +++----- src/Propellor/Property/Reboot.hs | 28 +++++++++++++++++++++++++--- src/Propellor/Types.hs | 4 ++-- 5 files changed, 37 insertions(+), 18 deletions(-) (limited to 'src/Propellor') diff --git a/src/Propellor/Engine.hs b/src/Propellor/Engine.hs index 310f4c84..44b10cab 100644 --- a/src/Propellor/Engine.hs +++ b/src/Propellor/Engine.hs @@ -43,13 +43,13 @@ mainProperties host = do -- are then also run. runPropellor :: Host -> Propellor Result -> IO Result runPropellor host a = do - (ret, _s, endactions) <- runRWST (runWithHost a) host () - endrets <- mapM (runEndAction host) endactions - return $ mconcat (ret:endrets) + (res, _s, endactions) <- runRWST (runWithHost a) host () + endres <- mapM (runEndAction host res) endactions + return $ mconcat (res:endres) -runEndAction :: Host -> EndAction -> IO Result -runEndAction host (EndAction desc a) = actionMessageOn (hostName host) desc $ do - (ret, _s, _) <- runRWST (runWithHost (catchPropellor a)) host () +runEndAction :: Host -> Result -> EndAction -> IO Result +runEndAction host res (EndAction desc a) = actionMessageOn (hostName host) desc $ do + (ret, _s, _) <- runRWST (runWithHost (catchPropellor (a res))) host () return ret -- | Ensures a list of Properties, with a display of each as it runs. diff --git a/src/Propellor/Property.hs b/src/Propellor/Property.hs index 1533471e..6371cc09 100644 --- a/src/Propellor/Property.hs +++ b/src/Propellor/Property.hs @@ -149,6 +149,5 @@ noChange :: Propellor Result noChange = return NoChange -- | Registers an action that should be run at the very end, --- and only when all configured Properties of the host succeed. -endAction :: Desc -> Propellor Result -> Propellor () +endAction :: Desc -> (Result -> Propellor Result) -> Propellor () endAction desc a = tell [EndAction desc a] diff --git a/src/Propellor/Property/OS.hs b/src/Propellor/Property/OS.hs index e84490fd..2185471d 100644 --- a/src/Propellor/Property/OS.hs +++ b/src/Propellor/Property/OS.hs @@ -11,6 +11,7 @@ import Propellor import qualified Propellor.Property.Debootstrap as Debootstrap import qualified Propellor.Property.Ssh as Ssh import qualified Propellor.Property.File as File +import qualified Propellor.Property.Reboot as Reboot import Propellor.Property.Mount import Propellor.Property.Chroot.Util (stdPATH) import Utility.SafeCommand @@ -67,6 +68,8 @@ cleanInstallOnce confirmation = check (not <$> doesFileExist flagfile) $ go = finalized `requires` + Reboot.atEnd True (/= FailedChange) + `requires` propellorbootstrapped `requires` flipped @@ -137,11 +140,6 @@ cleanInstallOnce confirmation = check (not <$> doesFileExist flagfile) $ finalized = property "clean OS installed" $ do liftIO $ writeFile flagfile "" - endAction "rebooting into new OS" $ liftIO $ - ifM (boolSystem "reboot" [ Param "--force" ]) - ( return MadeChange - , return FailedChange - ) return MadeChange flagfile = "/etc/propellor-cleaninstall" diff --git a/src/Propellor/Property/Reboot.hs b/src/Propellor/Property/Reboot.hs index 25e53159..3a725838 100644 --- a/src/Propellor/Property/Reboot.hs +++ b/src/Propellor/Property/Reboot.hs @@ -1,7 +1,29 @@ module Propellor.Property.Reboot where import Propellor +import Utility.SafeCommand -now :: Property -now = cmdProperty "reboot" [] - `describe` "reboot now" +-- | Schedules a reboot at the end of the current propellor run. +-- +-- The Result code of the endire propellor run can be checked; +-- the reboot proceeds only if the function returns True. +-- +-- The reboot can be forced to run, which bypasses the init system. Useful +-- if the init system might not be running for some reason. +atEnd :: Bool -> (Result -> Bool) -> Property +atEnd force resultok = property "scheduled reboot at end of propellor run" $ do + endAction "rebooting" atend + return NoChange + where + atend r + | resultok r = liftIO $ + ifM (boolSystem "reboot" rebootparams) + ( return MadeChange + , return FailedChange + ) + | otherwise = do + warningMessage "Not rebooting, due to status of propellor run." + return FailedChange + rebootparams + | force = [Param "--force"] + | otherwise = [] diff --git a/src/Propellor/Types.hs b/src/Propellor/Types.hs index 64cb5fbb..f349a29a 100644 --- a/src/Propellor/Types.hs +++ b/src/Propellor/Types.hs @@ -204,5 +204,5 @@ fromVal NoVal = Nothing type RunLog = [EndAction] -- | An action that Propellor runs at the end, after trying to satisfy all --- properties. -data EndAction = EndAction Desc (Propellor Result) +-- properties. It's passed the combined Result of the entire Propellor run. +data EndAction = EndAction Desc (Result -> Propellor Result) -- cgit v1.2.3