@@ -6,29 +6,46 @@ module Control.Distributed.Process.Backend.Azure
66 , initializeBackend
77 -- * Re-exports from Azure Service Management
88 , CloudService(..)
9+ , VirtualMachine(..)
910 ) where
1011
1112import System.Environment (getEnv)
1213import System.FilePath ((</>))
1314import System.Environment.Executable (getExecutablePath)
15+import Control.Concurrent (threadDelay)
1416import Network.Azure.ServiceManagement
15− ( CloudService )
17+ ( CloudService(..)
18+ , VirtualMachine(..)
19+ , Endpoint(..)
20+ )
1621import qualified Network.Azure.ServiceManagement as Azure
1722 ( cloudServices
1823 , AzureSetup
1924 , azureSetup
25+ , vmSshEndpoint
2026 )
27+import qualified Network.SSH.Client.LibSSH2 as SSH
28+ ( withSSH2
29+ , retryIfNeeded
30+ )
31+import qualified Network.SSH.Client.LibSSH2.Foreign as SSH
32+ ( initialize
33+ , exit
34+ , channelExecute
35+ )
2136
2237-- | Azure backend
2338data Backend = Backend {
2439 -- | Find virtual machines
2540 cloudServices :: IO [CloudService]
41+ , startOnVM :: VirtualMachine -> IO ()
2642 }
2743
2844data AzureParameters = AzureParameters {
2945 azureSubscriptionId :: String
3046 , azureAuthCertificate :: FilePath
3147 , azureAuthPrivateKey :: FilePath
48+ , azureSshUserName :: FilePath
3249 , azureSshPublicKey :: FilePath
3350 , azureSshPrivateKey :: FilePath
3451 , azureSshKnownHosts :: FilePath
@@ -41,10 +58,12 @@ defaultAzureParameters :: String -- ^ Azure subscription ID
4158 -> IO AzureParameters
4259defaultAzureParameters sid x509 pkey = do
4360 home <- getEnv "HOME"
61+ user <- getEnv "USER"
4462 return AzureParameters
4563 { azureSubscriptionId = sid
4664 , azureAuthCertificate = x509
4765 , azureAuthPrivateKey = pkey
66+ , azureSshUserName = user
4867 , azureSshPublicKey = home </> ".ssh" </> "id_rsa.pub"
4968 , azureSshPrivateKey = home </> ".ssh" </> "id_rsa"
5069 , azureSshKnownHosts = home </> ".ssh" </> "known_hosts"
@@ -60,4 +79,21 @@ initializeBackend params = do
6079 print exe
6180 return Backend {
6281 cloudServices = Azure.cloudServices setup
82+ , startOnVM = apiStartOnVM params
6383 }
84+
85+-- | Start a CH node on the given virtual machine
86+apiStartOnVM :: AzureParameters -> VirtualMachine -> IO ()
87+apiStartOnVM params vm = do
88+ _ <- SSH.initialize True
89+ let ep = Azure.vmSshEndpoint vm
90+ SSH.withSSH2 (azureSshKnownHosts params)
91+ (azureSshPublicKey params)
92+ (azureSshPrivateKey params)
93+ (azureSshUserName params)
94+ (endpointVip ep)
95+ (read $ endpointPort ep) $ \fd s ch -> do
96+ _ <- SSH.retryIfNeeded fd s $ SSH.channelExecute ch "/home/edsko/testservice"
97+ threadDelay $ 10 * 1000000 -- 10 seconds
98+ return ()
99+ SSH.exit