@@ -18,7 +18,7 @@ import System.Environment.Executable (getExecutablePath)
1818import Data.Binary (encode, decode)
1919import Data.Digest.Pure.MD5 (md5, MD5Digest)
2020import qualified Data.ByteString.Char8 as BSSC (pack)
21−import qualified Data.ByteString.Lazy as BSL (ByteString, readFile, putStr, writeFile)
21+import qualified Data.ByteString.Lazy as BSL (ByteString, readFile, putStr)
2222import qualified Data.ByteString.Lazy.Char8 as BSLC (unpack)
2323import Data.Typeable (Typeable)
2424import Control.Applicative ((<$>))
@@ -69,7 +69,7 @@ import Control.Distributed.Process
6969 )
7070import Control.Distributed.Process.Closure
7171 ( remotable
72− , cpBind
72+ , cpComp
7373 , SerializableDict(SerializableDict)
7474 , staticConst
7575 , staticApply
@@ -104,11 +104,19 @@ data Backend = Backend {
104104 -- | Check the MD5 hash of the remote executable
105105 , checkMD5 :: VirtualMachine -> IO Bool
106106 -- | @runOnVM dict vm port p@ starts a CH node on port 'port' and runs 'p'
107− , callOnVM :: forall a. Serializable a => Static (SerializableDict a) -> VirtualMachine -> String -> Closure (Process a) -> IO a
107+ , callOnVM :: forall a. Serializable a
108+ => Static (SerializableDict a)
109+ -> VirtualMachine
110+ -> String
111+ -> Closure (Backend -> Process a)
112+ -> IO a
108113 -- | Create a new CH node and run the specified process in the background.
109114 -- The CH node will exit when the process exists.
110− , spawnOnVM :: VirtualMachine -> String -> Closure (Process ()) -> IO ()
111− }
115+ , spawnOnVM :: VirtualMachine
116+ -> String
117+ -> Closure (Backend -> Process ())
118+ -> IO ()
119+ } deriving (Typeable)
112120
113121data AzureParameters = AzureParameters {
114122 azureSetup :: AzureSetup
@@ -170,24 +178,37 @@ apiCopyToVM params vm =
170178 SSH.scpSendFile s 0o700 (azureSshLocalPath params) (azureSshRemotePath params)
171179
172180-- | Call a process on a VM
173−apiCallOnVM :: Serializable a => AzureParameters -> Static (SerializableDict a) -> VirtualMachine -> String -> Closure (Process a) -> IO a
181+apiCallOnVM :: Serializable a
182+ => AzureParameters
183+ -> Static (SerializableDict a)
184+ -> VirtualMachine
185+ -> String
186+ -> Closure (Backend -> Process a)
187+ -> IO a
174188apiCallOnVM params dict vm port proc =
175− withSSH2 params vm $ \s -> do
176− let exe = "PATH=. " ++ azureSshRemotePath params
177− ++ " onvm run "
178− ++ " --host " ++ vmIpAddress vm
179− ++ " --port " ++ port
180− ++ " 2>&1"
181− (status, r) <- SSH.withChannelBy (SSH.openChannelSession s) id $ \ch -> do
182− SSH.channelExecute ch exe
183− _cnt <- SSH.writeAllChannel ch (encode $ proc `cpBind` cpEncodeToStdout dict)
184− SSH.channelSendEOF ch
185− SSH.readAllChannel ch
186− if status == 0
187− then return $ decode r
188− else error (BSLC.unpack r)
189+ withSSH2 params vm $ \s -> do
190+ let exe = "PATH=. " ++ azureSshRemotePath params
191+ ++ " onvm run "
192+ ++ " --host " ++ vmIpAddress vm
193+ ++ " --port " ++ port
194+ ++ " 2>&1"
195+ (status, r) <- SSH.withChannelBy (SSH.openChannelSession s) id $ \ch -> do
196+ SSH.channelExecute ch exe
197+ _cnt <- SSH.writeAllChannel ch (encode proc')
198+ SSH.channelSendEOF ch
199+ SSH.readAllChannel ch
200+ if status == 0
201+ then return $ decode r
202+ else error (BSLC.unpack r)
203+ where
204+ proc' :: Closure (Backend -> Process ())
205+ proc' = proc `cpComp` cpEncodeToStdout dict
189206
190−apiSpawnOnVM :: AzureParameters -> VirtualMachine -> String -> Closure (Process ()) -> IO ()
207+apiSpawnOnVM :: AzureParameters
208+ -> VirtualMachine
209+ -> String
210+ -> Closure (Backend -> Process ())
211+ -> IO ()
191212apiSpawnOnVM params vm port proc =
192213 withSSH2 params vm $ \s -> do
193214 let exe = "PATH=. " ++ azureSshRemotePath params
@@ -196,7 +217,6 @@ apiSpawnOnVM params vm port proc =
196217 ++ " --port " ++ port
197218 ++ " --background "
198219 ++ " 2>&1"
199− BSL.writeFile "closure" (encode proc)
200220 (status, r) <- SSH.withChannelBy (SSH.openChannelSession s) id $ \ch -> do
201221 SSH.channelExecute ch exe
202222 _cnt <- SSH.writeAllChannel ch (encode proc)