diff --git a/Propellor/Property/Apt.hs b/Propellor/Property/Apt.hs index ac1d9a1..ff9b3de 100644 --- a/Propellor/Property/Apt.hs +++ b/Propellor/Property/Apt.hs @@ -8,6 +8,7 @@ import Control.Monad import Propellor import qualified Propellor.Property.File as File +import qualified Propellor.Property.Service as Service import Propellor.Property.File (Line) sourcesList :: FilePath @@ -149,9 +150,7 @@ autoRemove = runApt ["-y", "autoremove"] unattendedUpgrades :: RevertableProperty unattendedUpgrades = RevertableProperty enable disable where - enable = setup True - `before` installed ["cron"] - `before` serviceRunning "cron" + enable = setup True `before` Service.running "cron" disable = setup False setup enabled = (if enabled then installed else removed) ["unattended-upgrades"] @@ -176,3 +175,10 @@ reConfigure package vals = reconfigure `requires` setselections hPutStrLn h $ unwords [package, template, tmpltype, value] hClose h reconfigure = cmdProperty "dpkg-reconfigure" ["-fnone", package] + +-- | Ensures that a service is installed and running. +-- +-- Assumes that there is a 1:1 mapping between service names and apt +-- package names. +serviceInstalledRunning :: Package -> Property +serviceInstalledRunning svc = Service.running svc `requires` installed [svc] diff --git a/Propellor/Property/Cmd.hs b/Propellor/Property/Cmd.hs index f661cf8..c715fd2 100644 --- a/Propellor/Property/Cmd.hs +++ b/Propellor/Property/Cmd.hs @@ -3,16 +3,12 @@ module Propellor.Property.Cmd ( cmdProperty', scriptProperty, userScriptProperty, - serviceRunning, - serviceRestarted, ) where -import Control.Monad import Control.Applicative import Data.List import Propellor.Types -import Propellor.Engine import Utility.Monad import Utility.SafeCommand import Utility.Env @@ -47,22 +43,3 @@ userScriptProperty :: UserName -> [String] -> Property userScriptProperty user script = cmdProperty "su" ["-c", shellcmd, user] where shellcmd = intercalate " ; " ("set -e" : "cd" : script) - -type ServiceName = String - --- | Ensures that a service is running. --- --- Note that due to the general poor state of init scripts, the best --- we can do is try to start the service, and if it fails, assume --- this means it's already running. -serviceRunning :: ServiceName -> Property -serviceRunning svc = Property ("running " ++ svc) $ do - void $ ensureProperty $ - scriptProperty ["service " ++ shellEscape svc ++ " start >/dev/null 2>&1 || true"] - return NoChange - -serviceRestarted :: ServiceName -> Property -serviceRestarted svc = Property ("restarted " ++ svc) $ do - void $ ensureProperty $ - scriptProperty ["service " ++ shellEscape svc ++ " restart >/dev/null 2>&1 || true"] - return NoChange diff --git a/Propellor/Property/Cron.hs b/Propellor/Property/Cron.hs index 30bdb51..fa6019e 100644 --- a/Propellor/Property/Cron.hs +++ b/Propellor/Property/Cron.hs @@ -18,8 +18,7 @@ job desc times user cddir command = ("/etc/cron.d/" ++ desc) `File.hasContent` , "" , times ++ "\t" ++ user ++ "\t" ++ "cd " ++ cddir ++ " && " ++ command ] - `requires` Apt.installed ["cron"] - `requires` serviceRunning "cron" + `requires` Apt.serviceInstalledRunning "cron" `describe` ("cronned " ++ desc) -- | Installs a cron job, and runs it niced and ioniced. diff --git a/Propellor/Property/OpenId.hs b/Propellor/Property/OpenId.hs index 4f22bdb..c397bdb 100644 --- a/Propellor/Property/OpenId.hs +++ b/Propellor/Property/OpenId.hs @@ -3,15 +3,15 @@ module Propellor.Property.OpenId where import Propellor import qualified Propellor.Property.File as File import qualified Propellor.Property.Apt as Apt +import qualified Propellor.Property.Service as Service import Data.List providerFor :: [UserName] -> String -> Property providerFor users baseurl = propertyList desc $ - [ serviceRunning "apache2" - `requires` Apt.installed ["apache2"] + [ Apt.serviceInstalledRunning "apache2" , Apt.installed ["simpleid"] - `onChange` serviceRestarted "apache2" + `onChange` Service.restarted "apache2" , File.fileProperty desc (map setbaseurl) "/etc/simpleid/config.inc" ] ++ map identfile users diff --git a/Propellor/Property/Service.hs b/Propellor/Property/Service.hs new file mode 100644 index 0000000..2fb3e0c --- /dev/null +++ b/Propellor/Property/Service.hs @@ -0,0 +1,25 @@ +module Propellor.Property.Service where + +import Propellor +import Utility.SafeCommand + +type ServiceName = String + +-- | Ensures that a service is running. Does not ensure that +-- any package providing that service is installed. See +-- Apt.serviceInstalledRunning +-- +-- Note that due to the general poor state of init scripts, the best +-- we can do is try to start the service, and if it fails, assume +-- this means it's already running. +running :: ServiceName -> Property +running svc = Property ("running " ++ svc) $ do + void $ ensureProperty $ + scriptProperty ["service " ++ shellEscape svc ++ " start >/dev/null 2>&1 || true"] + return NoChange + +restarted :: ServiceName -> Property +restarted svc = Property ("restarted " ++ svc) $ do + void $ ensureProperty $ + scriptProperty ["service " ++ shellEscape svc ++ " restart >/dev/null 2>&1 || true"] + return NoChange diff --git a/Propellor/Property/SiteSpecific/GitAnnexBuilder.hs b/Propellor/Property/SiteSpecific/GitAnnexBuilder.hs index 149c8e6..580a52d 100644 --- a/Propellor/Property/SiteSpecific/GitAnnexBuilder.hs +++ b/Propellor/Property/SiteSpecific/GitAnnexBuilder.hs @@ -24,7 +24,7 @@ builder arch crontimes rsyncupload = combineProperties "gitannexbuilder" , Apt.buildDep ["git-annex"] , Apt.installed ["git", "rsync", "moreutils", "ca-certificates", "liblockfile-simple-perl", "cabal-install", "vim", "less"] - , serviceRunning "cron" `requires` Apt.installed ["cron"] + , Apt.serviceInstalledRunning "cron" , User.accountFor builduser , check (not <$> doesDirectoryExist gitbuilderdir) $ userScriptProperty builduser [ "git clone git://git.kitenet.net/gitannexbuilder " ++ gitbuilderdir diff --git a/config-joey.hs b/config-joey.hs index 6e58d5d..baabd8c 100644 --- a/config-joey.hs +++ b/config-joey.hs @@ -68,8 +68,7 @@ container _parenthost name [ Docker.publish "8080:80" , Docker.volume "/var/www:/var/www" , Docker.inside $ props - & serviceRunning "apache2" - `requires` Apt.installed ["apache2"] + & Apt.serviceInstalledRunning "apache2" & Apt.unattendedUpgrades ] diff --git a/config-simple.hs b/config-simple.hs index d5015ef..5e43b46 100644 --- a/config-simple.hs +++ b/config-simple.hs @@ -47,7 +47,6 @@ container _ "webserver" = Just $ Docker.containerFrom "joeyh/debian-unstable" [ Docker.publish "80:80" , Docker.volume "/var/www:/var/www" , Docker.inside $ props - & serviceRunning "apache2" - `requires` Apt.installed ["apache2"] + & Apt.serviceInstalledRunning "apache2" ] container _ _ = Nothing