From 79cbdf35b1188d83e64a713efa82bc7a0a72a181 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Tue, 1 Apr 2014 13:51:58 -0400 Subject: better method of starting propellor simplesh inside docker --- Propellor/SimpleSh.hs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'Propellor/SimpleSh.hs') diff --git a/Propellor/SimpleSh.hs b/Propellor/SimpleSh.hs index 25a154a9..741c1bc8 100644 --- a/Propellor/SimpleSh.hs +++ b/Propellor/SimpleSh.hs @@ -12,6 +12,8 @@ import System.Process (std_in, std_out, std_err) import System.Exit import Propellor +import Utility.FileMode +import Utility.ThreadScheduler data Cmd = Cmd String [String] deriving (Read, Show) @@ -22,7 +24,9 @@ data Resp = StdoutLine String | StderrLine String | Done ExitCode simpleSh :: FilePath -> IO () simpleSh namedpipe = do nukeFile namedpipe - createDirectoryIfMissing True (takeDirectory namedpipe) + let dir = takeDirectory namedpipe + createDirectoryIfMissing True dir + modifyFileMode dir (removeModes otherGroupModes) s <- socket AF_UNIX Stream defaultProtocol bind s (SockAddrUnix namedpipe) listen s 2 @@ -73,6 +77,20 @@ simpleShClient namedpipe cmd params handler = do resps <- catMaybes . map readish . lines <$> hGetContents h hClose h `after` handler resps +simpleShClientRetry :: Int -> FilePath -> String -> [String] -> ([Resp] -> IO a) -> IO a +simpleShClientRetry retries namedpipe cmd params handler = go retries + where + run = simpleShClient namedpipe cmd params handler + go n + | n < 1 = run + | otherwise = do + v <- tryIO run + case v of + Right r -> return r + Left _ -> do + threadDelaySeconds (Seconds 1) + go (n - 1) + getStdout :: Resp -> Maybe String getStdout (StdoutLine s) = Just s getStdout _ = Nothing -- cgit v1.2.3