Reboot.atEnd

This commit is contained in:
Joey Hess 2014-12-06 13:21:19 -04:00
parent 62697c7b7f
commit 29442f222e
6 changed files with 37 additions and 19 deletions

View File

@ -12,7 +12,6 @@ import qualified Propellor.Property.Cron as Cron
--import qualified Propellor.Property.Sudo as Sudo
import qualified Propellor.Property.User as User
--import qualified Propellor.Property.Hostname as Hostname
--import qualified Propellor.Property.Reboot as Reboot
--import qualified Propellor.Property.Tor as Tor
import qualified Propellor.Property.Docker as Docker

View File

@ -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.

View File

@ -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]

View File

@ -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"

View File

@ -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 = []

View File

@ -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)