summaryrefslogtreecommitdiff
path: root/Propellor/CmdLine.hs
diff options
context:
space:
mode:
authorJoey Hess2014-04-08 02:06:37 -0400
committerJoey Hess2014-04-08 02:07:11 -0400
commit634cf61c7989f5d20bccd822e2fd568ccbdef947 (patch)
tree29ff196aea04f736dcc4f3b4e3929bdefe754c71 /Propellor/CmdLine.hs
parent3068fdbe78cb86a9272b1ce5200653f5331fb173 (diff)
parent7ba62a28b51dc5826c70d3be0ab41825e31d28ac (diff)
Merge branch 'joeyconfig'
Conflicts: Propellor/Property/Docker.hs
Diffstat (limited to 'Propellor/CmdLine.hs')
-rw-r--r--Propellor/CmdLine.hs20
1 files changed, 18 insertions, 2 deletions
diff --git a/Propellor/CmdLine.hs b/Propellor/CmdLine.hs
index a9c61993..5ea982c3 100644
--- a/Propellor/CmdLine.hs
+++ b/Propellor/CmdLine.hs
@@ -8,9 +8,12 @@ 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
+import qualified Propellor.Property.Docker.Shim as DockerShim
import Utility.FileMode
import Utility.SafeCommand
@@ -53,6 +56,7 @@ processCmdLine = go =<< getArgs
defaultMain :: [HostName -> Maybe [Property]] -> IO ()
defaultMain getprops = do
+ DockerShim.cleanEnv
checkDebugMode
cmdline <- processCmdLine
debug ["command line: ", show cmdline]
@@ -69,14 +73,26 @@ 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
+ go False (Boot host) = onlyProcess $ withprops host $ boot
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 <- createFile lockfile stdFileMode
+ 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