-
Notifications
You must be signed in to change notification settings - Fork 212
/
Launch.hs
273 lines (257 loc) · 7.38 KB
/
Launch.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# OPTIONS_GHC -Wno-redundant-constraints #-}
{-
Control the launching of a cluster of nodes for testing purposes.
The cluster will run on the testnet network.
-}
module Test.Integration.Framework.Cluster.Launch
( withLocalCluster
) where
import Prelude
import Cardano.Launcher
( withBackendCreateProcess
)
import Cardano.Launcher.Node
( CardanoNodeConn
, cardanoNodeConn
, nodeSocketPath
)
import Cardano.Ledger.Shelley.API
( ShelleyGenesis (sgNetworkMagic)
)
import Cardano.Wallet.Faucet.Yaml
( saveFunds
)
import Cardano.Wallet.Launch.Cluster
( ClusterEra
, ClusterLog (..)
, FaucetFunds
, RunningNode
, clusterEraToString
)
import Cardano.Wallet.Launch.Cluster.CommandLine
( CommandLineOptions (..)
, Monitoring (..)
, renderControl
)
import Cardano.Wallet.Launch.Cluster.Config
( Config (..)
)
import Cardano.Wallet.Launch.Cluster.FileOf
( AbsFileOf
)
import Cardano.Wallet.Launch.Cluster.Monitoring.Http.Client
( Query (..)
, RunQuery (..)
)
import Cardano.Wallet.Launch.Cluster.Monitoring.Monitor
( withHttpMonitoring
)
import Cardano.Wallet.Launch.Cluster.Node.RunningNode
( RunningNode (..)
)
import Control.Monad
( void
)
import Control.Monad.Cont
( ContT (..)
, evalContT
)
import Control.Monad.Trans
( MonadIO (..)
, lift
)
import Control.Retry
( RetryPolicyM
, capDelay
, exponentialBackoff
, retrying
)
import Control.Tracer
( Tracer
)
import Data.Aeson
( FromJSON
)
import Data.Functor.Contravariant
( (>$<)
)
import GHC.Stack
( HasCallStack
)
import Ouroboros.Network.Magic
( NetworkMagic (..)
)
import Ouroboros.Network.NodeToClient
( NodeToClientVersionData (..)
)
import Path
( fromAbsDir
, fromAbsFile
, parent
, parseAbsFile
, relfile
, (</>)
)
import Path.IO
( createDirIfMissing
)
import System.Environment
( getEnvironment
)
import System.IO.Extra
( IOMode (..)
, withTempFile
)
import System.Process.Extra
( CreateProcess (..)
, StdStream (..)
, proc
)
import UnliftIO
( Handle
, SomeException
, finally
, hClose
, openFile
, try
)
import qualified Data.Aeson as Aeson
import qualified Data.ByteString as BS
-- | The withFile from base intercept exceptions and rethrows them after messing
-- up the exception informations. :(
withFile :: FilePath -> IOMode -> (Handle -> IO a) -> IO a
withFile fp mode f = do
h <- openFile fp mode
f h `finally` hClose h
withLocalClusterProcess
:: HasCallStack
=> CommandLineOptions
-> Tracer IO ClusterLog
-> ClusterEra
-> ContT r IO ()
withLocalClusterProcess CommandLineOptions{..} cfgTracer era = do
myEnv <- lift getEnvironment
let envs =
[ ("LOCAL_CLUSTER_ERA", clusterEraToString era)
]
output <- case clusterLogs of
Nothing -> pure Inherit
Just logFile -> do
createDirIfMissing True (parent logFile)
fmap UseHandle
$ ContT
$ withFile (fromAbsFile logFile) WriteMode
void
$ ContT
$ withBackendCreateProcess
(MsgLauncher "local-cluster" >$< cfgTracer)
$ (proc "local-cluster" args)
{ env = Just $ myEnv ++ envs
, std_out = output
, std_err = output
}
where
args =
renderControl clusterControl
<> [ "--cluster-configs"
, fromAbsDir clusterConfigsDir
, "--faucet-funds"
, fromAbsFile faucetFundsFile
, "--socket-path"
, fromAbsFile nodeToClientSocket
]
<> case clusterDir of
Nothing -> []
Just clusterDir' ->
[ "--cluster"
, fromAbsDir clusterDir'
]
<> case monitoring of
Nothing -> []
Just (Monitoring port) ->
[ "--monitoring-port"
, show port
]
withFaucetFunds
:: HasCallStack
=> FaucetFunds
-> ContT r IO (AbsFileOf s)
withFaucetFunds faucetFunds = ContT $ \action ->
withTempFile $ \faucetFile -> do
faucetFilePath <- parseAbsFile faucetFile
saveFunds faucetFilePath faucetFunds
action faucetFilePath
withSocketPath
:: HasCallStack
=> AbsFileOf s
-> ContT r m CardanoNodeConn
withSocketPath socketFilePath = ContT $ \f ->
case cardanoNodeConn $ nodeSocketPath $ fromAbsFile socketFilePath of
Left err -> error $ "Failed to get socket path: " ++ err
Right socketPath -> f socketPath
withGenesisData :: FromJSON a => AbsFileOf "genesis-data" -> ContT r IO a
withGenesisData shelleyGenesis = ContT $ \f -> do
genesisContent <- BS.readFile (fromAbsFile shelleyGenesis)
eGenesisData <- try $ Aeson.throwDecodeStrict genesisContent
case eGenesisData of
Left (e :: SomeException) ->
error $ "Failed to decode genesis data: " ++ show e
Right genesisData -> f genesisData
withLocalClusterReady :: (Query Bool -> IO Bool) -> IO ()
withLocalClusterReady queryMonitor = do
void $ liftIO $ retrying policy (const $ pure . not) $ \_ -> do
queryMonitor ReadyQ
where
policy :: RetryPolicyM IO
policy = capDelay (120 * oneSecond) $ exponentialBackoff oneSecond
oneSecond = 1_000_000 :: Int
-- | Run an action against a node socket, backed by a local cluster process
withLocalCluster
:: HasCallStack
=> Config
-- ^ Configuration for the cluster.
-> FaucetFunds
-- ^ Initial faucet funds.
-> (RunQuery IO -> RunningNode -> IO a)
-- ^ Action to run once when all pools have started.
-> IO a
withLocalCluster
Config{..}
faucetFunds
action = do
let
clusterConfigsDir = cfgClusterConfigs
shelleyGenesis = cfgClusterDir </> [relfile|shelley-genesis.json|]
clusterDir = Just cfgClusterDir
clusterLogs = cfgClusterLogFile
clusterControl = Nothing
nodeToClientSocket = cfgNodeToClientSocket
evalContT $ do
(monitoring, rq@(RunQuery queryMonitor)) <-
withHttpMonitoring $ MsgHttpMonitoring >$< cfgTracer
faucetFundsFile <- withFaucetFunds faucetFunds
socketPath <- withSocketPath nodeToClientSocket
withLocalClusterProcess
CommandLineOptions{monitoring = Just monitoring, ..}
cfgTracer
cfgLastHardFork
liftIO $ withLocalClusterReady queryMonitor
genesisData <- withGenesisData shelleyGenesis
lift
$ action rq
$ RunningNode
{ runningNodeSocketPath = socketPath
, runningNodeShelleyGenesis = genesisData
, runningNodeVersionData =
NodeToClientVersionData
{ networkMagic =
NetworkMagic
$ sgNetworkMagic genesisData
, query = False
}
}