From da3976b9e2f9abf0f2d37300b70bc8884e4ab2f8 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Fri, 4 Apr 2014 16:20:20 -0400 Subject: propellor spin --- Propellor/CmdLine.hs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'Propellor/CmdLine.hs') diff --git a/Propellor/CmdLine.hs b/Propellor/CmdLine.hs index d10211f0..3974f1d5 100644 --- a/Propellor/CmdLine.hs +++ b/Propellor/CmdLine.hs @@ -8,6 +8,8 @@ import System.Log.Formatter import System.Log.Handler (setFormatter, LogHandler) import System.Log.Handler.Simple import System.PosixCompat +import Control.Exception (bracket) +import System.Posix.IO import Propellor import qualified Propellor.Property.Docker as Docker @@ -71,7 +73,7 @@ defaultMain getprops = do go True cmdline = updateFirst cmdline $ go False cmdline go False (Spin host) = withprops host $ const $ spin host go False (Run host) = ifM ((==) 0 <$> getRealUserID) - ( withprops host ensureProperties + ( onlyProcess $ withprops host ensureProperties , go True (Spin host) ) go False (Boot host) = withprops host $ boot @@ -79,6 +81,18 @@ defaultMain getprops = do withprops host a = maybe (unknownhost host) a $ headMaybe $ catMaybes $ map (\get -> get host) getprops +onlyProcess :: IO a -> IO a +onlyProcess a = bracket lock unlock (const a) + where + lock = do + l <- openFd lockfile ReadWrite Nothing defaultFileFlags + setLock l (WriteLock, AbsoluteSeek, 0, 0) + `catchIO` (const alreadyrunning) + return l + unlock = closeFd + alreadyrunning = error "Propellor is already running on this host!" + lockfile = localdir ".lock" + unknownhost :: HostName -> IO a unknownhost h = errorMessage $ unlines [ "Unknown host: " ++ h -- cgit v1.2.3