@@ -455,7 +455,7 @@ import qualified Data.ByteString.Lazy.Char8 as BSLC (unpack)
455import Data.Typeable (Typeable)
455import Data.Typeable (Typeable)
456import Data.Foldable (forM_)
456import Data.Foldable (forM_)
457import Control.Applicative ((<$>), (<*>))
457import Control.Applicative ((<$>), (<*>))
458−import Control.Monad (void, unless)
458+import Control.Monad (void)
459import Control.Monad.Reader (MonadReader, ReaderT, runReaderT, ask)
459import Control.Monad.Reader (MonadReader, ReaderT, runReaderT, ask)
460import Control.Exception
460import Control.Exception
461 ( Exception
461 ( Exception
@@ -510,6 +510,8 @@ import Control.Distributed.Process
510 , RemoteTable
510 , RemoteTable
512 , unClosure
512 , unClosure
514import Control.Distributed.Process.Serializable (Serializable)
516import Control.Distributed.Process.Serializable (Serializable)
515import qualified Control.Distributed.Process.Internal.Types as CH
517import qualified Control.Distributed.Process.Internal.Types as CH
@@ -546,7 +548,7 @@ data Backend = Backend {
546 -- | Create a new CH node and run the specified process.
548 -- | Create a new CH node and run the specified process.
547 -- The CH node will shut down when the /remote/ process exists. @spawnOnVM@
549 -- The CH node will shut down when the /remote/ process exists. @spawnOnVM@
548 -- returns as soon as the process has been spawned.
550 -- returns as soon as the process has been spawned.
549− , spawnOnVM :: VirtualMachine -> String -> RemoteProcess () -> IO ()
551+ , spawnOnVM :: VirtualMachine -> String -> RemoteProcess () -> IO ProcessId
550 } deriving (Typeable)
552 } deriving (Typeable)
552-- | Azure connection parameters
554-- | Azure connection parameters
@@ -658,7 +660,7 @@ apiSpawnOnVM :: AzureParameters
658 -> VirtualMachine
660 -> VirtualMachine
659 -> String
661 -> String
660 -> Closure (Backend -> Process ())
662 -> Closure (Backend -> Process ())
661− -> IO ()
663+ -> IO ProcessId
662apiSpawnOnVM params cloudService vm port proc =
664apiSpawnOnVM params cloudService vm port proc =
663 withSSH2 params vm $ \s -> do
665 withSSH2 params vm $ \s -> do
664 -- TODO: reduce duplication with apiCallOnVM
666 -- TODO: reduce duplication with apiCallOnVM
@@ -678,7 +680,9 @@ apiSpawnOnVM params cloudService vm port proc =
678 SSH.writeAllChannel ch paramsEnc
680 SSH.writeAllChannel ch paramsEnc
679 SSH.channelSendEOF ch
681 SSH.channelSendEOF ch
680 SSH.readAllChannel ch
682 SSH.readAllChannel ch
684+ then return (decode r)
681− unless (status == 0) $ error (BSLC.unpack r)
685+ else error (BSLC.unpack r)
683 procEnc :: BSL.ByteString
687 procEnc :: BSL.ByteString
684 procEnc = encode proc
688 procEnc = encode proc
@@ -822,15 +826,27 @@ onVmMain :: (RemoteTable -> RemoteTable) -> [String] -> IO ()
822onVmMain rtable [host, port, cloudService, bg] = do
826onVmMain rtable [host, port, cloudService, bg] = do
823 hSetBinaryMode stdin True
827 hSetBinaryMode stdin True
824 hSetBinaryMode stdout True
828 hSetBinaryMode stdout True
825− Just procEnc <- getWithLength stdin
829+ Just rprocEnc <- getWithLength stdin
826 Just paramsEnc <- getWithLength stdin
830 Just paramsEnc <- getWithLength stdin
827 backend <- initializeBackend (decode paramsEnc) cloudService
831 backend <- initializeBackend (decode paramsEnc) cloudService
828− let proc = decode procEnc
832+ let rproc = decode rprocEnc
829 lprocMVar <- newEmptyMVar :: IO (MVar CH.LocalProcess)
833 lprocMVar <- newEmptyMVar :: IO (MVar CH.LocalProcess)
830 if read bg
834 if read bg
836+ void . Posix.forkProcess $ do
837+ -- We inherit the file descriptors from the parent, so the SSH
838+ -- session will not be terminated until we close them
839+ void Posix.createSession
831− then detach $ startCH proc lprocMVar backend runProcess (\_ -> return ())
840+ startCH rproc lprocMVar backend
841+ (\node proc -> runProcess node $ do
844+ BSL.hPut stdout (encode us)
845+ mapM_ hClose [stdin, stdout, stderr]
833− startCH proc lprocMVar backend forkProcess (liftIO . remoteThrow)
849+ startCH rproc lprocMVar backend forkProcess (liftIO . remoteThrow)
834 lproc <- readMVar lprocMVar
850 lproc <- readMVar lprocMVar
835 queueFromHandle stdin (CH.processQueue lproc)
851 queueFromHandle stdin (CH.processQueue lproc)
@@ -875,11 +891,6 @@ queueFromHandle h q = do
875 enqueue q $ CH.payloadToMessage (BSL.toChunks payload)
891 enqueue q $ CH.payloadToMessage (BSL.toChunks payload)
876 queueFromHandle h q
892 queueFromHandle h q
878−detach :: IO () -> IO ()
880− mapM_ hClose [stdin, stdout, stderr]
881− void . Posix.forkProcess $ void Posix.createSession >> io
883--------------------------------------------------------------------------------
894--------------------------------------------------------------------------------
884-- SSH utilities --
895-- SSH utilities --
885--------------------------------------------------------------------------------
896--------------------------------------------------------------------------------