Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit 13c155d

Browse files
authored
[GH-78] Create the initial launcher version. (#84)
* [GH-78] Create the initial launcher version. * [GH-78] Fix the wallet args. * [GH-78] Add something to run.
1 parent d345e2a commit 13c155d

File tree

7 files changed

+119
-37
lines changed

7 files changed

+119
-37
lines changed

app/Launcher/Main.hs

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
{-# LANGUAGE LambdaCase #-}
1+
{-# LANGUAGE LambdaCase #-}
2+
{-# LANGUAGE RankNTypes #-}
3+
{-# LANGUAGE RecordWildCards #-}
24

35
module Main where
46

@@ -8,12 +10,17 @@ import qualified Prelude
810
import System.Directory (createDirectoryIfMissing)
911
import System.FilePath ((</>))
1012

11-
import Formatting (build, bprint, formatToString)
13+
import qualified System.Process as Process
14+
import Turtle (system)
15+
16+
import Formatting (bprint, build, formatToString)
1217
import Formatting.Buildable (Buildable (..))
1318

1419
import Control.Exception.Safe (throwM)
1520

16-
import Cardano.Shell.Configuration.Types (LauncherConfig (..))
21+
import Cardano.Shell.Configuration.Types (LauncherConfig (..),
22+
WalletArguments (..),
23+
WalletPath (..))
1724

1825
import Cardano.X509.Configuration (ConfigurationKey (..),
1926
DirConfiguration (..), certChecks,
@@ -39,11 +46,19 @@ main = do
3946
, lcfgSeed = Nothing
4047
}
4148

49+
-- Really no clue what to put there and how will the wallet work.
50+
let walletPath :: WalletPath
51+
walletPath = WalletPath "stack"
52+
53+
let walletArgs :: WalletArguments
54+
walletArgs = WalletArguments ["exec", "cardano-shell-exe"]
55+
4256
-- | Yes, this is something we probably need to replace with actual loggging.
4357
let externalDependencies :: ExternalDependencies
4458
externalDependencies = ExternalDependencies
4559
{ logInfo = putTextLn
4660
, logError = putTextLn
61+
, logNotice = putTextLn
4762
}
4863

4964
-- | If we need to, we first check if there are certificates so we don't have
@@ -54,13 +69,63 @@ main = do
5469
launcherConfig
5570
(TLSPath "./configuration/") -- where to generate the certificates
5671

72+
-- With the exit code
73+
_ <- runWallet externalDependencies walletPath walletArgs
74+
75+
pure ()
76+
77+
-- | Launching the wallet.
78+
-- For now, this is really light since we don't know whether we will reuse the
79+
-- older configuration and if so, which parts of it.
80+
-- We passed in the bare minimum and if we require anything else, we will add it.
81+
runWallet
82+
:: ExternalDependencies
83+
-> WalletPath
84+
-> WalletArguments
85+
-> IO ExitCode
86+
runWallet ed@ExternalDependencies{..} walletPath walletArguments = do
87+
logNotice "Starting the wallet"
88+
89+
-- create the wallet process
90+
walletExitStatus <- system (createProc Process.Inherit walletPath walletArguments) mempty
91+
92+
case walletExitStatus of
93+
ExitFailure 21 -> do
94+
logNotice "The wallet has exited with code 21"
95+
--logInfo "Switching Configuration to safe mode"
96+
--saveSafeMode lo True
97+
runWallet ed walletPath walletArguments
98+
99+
ExitFailure 22 -> do
100+
logNotice "The wallet has exited with code 22"
101+
--logInfo "Switching Configuration to normal mode"
102+
--saveSafeMode lo False
103+
runWallet ed walletPath walletArguments
104+
105+
-- Otherwise, return the exit status.
106+
_ -> pure walletExitStatus
107+
where
108+
-- | The creation of the process.
109+
createProc
110+
:: Process.StdStream
111+
-> WalletPath
112+
-> WalletArguments
113+
-> Process.CreateProcess
114+
createProc stdStream (WalletPath commandPath) (WalletArguments commandArguments) =
115+
(Process.proc (strConv Lenient commandPath) (map (strConv Lenient) commandArguments))
116+
{ Process.std_in = Process.CreatePipe
117+
, Process.std_out = stdStream
118+
, Process.std_err = stdStream
119+
}
120+
57121
--------------------------------------------------------------------------------
58122
-- Types
59123
--------------------------------------------------------------------------------
60124

61125
data ExternalDependencies = ExternalDependencies
62-
{ logInfo :: Text -> IO ()
63-
, logError :: Text -> IO ()
126+
{ logInfo :: Text -> IO ()
127+
, logError :: Text -> IO ()
128+
, logNotice :: Text -> IO ()
64129
}
65130

66131
newtype X509ToolPath = X509ToolPath { getX509ToolPath :: FilePath }

cardano-shell.cabal

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,12 @@ executable cardano-launcher
145145
, cardano-shell
146146
, cardano-prelude
147147
, cardano-sl-x509
148+
-- conccurency
149+
, async
148150
-- process managment
149151
, process
150152
, unix
153+
, turtle
151154
-- directory
152155
, directory
153156
, filepath

cardano-shell.nix

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
{ mkDerivation, aeson, base, binary, bytestring, Cabal
1+
{ mkDerivation, aeson, async, base, binary, bytestring, Cabal
22
, cardano-prelude, cardano-sl-x509, concurrency, containers
33
, contravariant, dejafu, dhall, directory, ekg-core, filepath
44
, formatting, hspec, hspec-contrib, hunit-dejafu, iohk-monitoring
55
, process, QuickCheck, safe-exceptions, stdenv, text, transformers
6-
, unix
6+
, turtle, unix
77
}:
88
mkDerivation {
99
pname = "cardano-shell";
@@ -18,8 +18,8 @@ mkDerivation {
1818
transformers unix
1919
];
2020
executableHaskellDepends = [
21-
base cardano-prelude cardano-sl-x509 directory filepath formatting
22-
iohk-monitoring process safe-exceptions unix
21+
async base cardano-prelude cardano-sl-x509 directory filepath
22+
formatting iohk-monitoring process safe-exceptions turtle unix
2323
];
2424
testHaskellDepends = [
2525
aeson base cardano-prelude concurrency dejafu dhall hspec

dhall/features/wallet.dhall

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
, walletRelays = { getWalletRelays = topology.wallet.wtcfgRelays}
1111
, walletValency = { getWalletValency = topology.wallet.wtcfgValency}
1212
, walletFallback = { getWalletFallback = topology.wallet.wtcfgFallbacks}
13-
}
13+
}

src/Cardano/Shell/Configuration/Lib.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ mkNetworkConfig os cluster = input auto networkPath
7878
<> "(" <> toPath "launcher" <> " " <> clusterPath cluster <> " " <>
7979
"(" <> osPath os <> " " <> clusterPath cluster <> ")" <>")"
8080

81+
-- TODO(KS): This is a bit complicated.
8182
mkWalletConfig :: OS -> Cluster -> IO WalletConfig
8283
mkWalletConfig os cluster = input auto walletPath
8384
where

src/Cardano/Shell/Configuration/Types.hs

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ module Cardano.Shell.Configuration.Types
4444
, Tlsca(..)
4545
, Tlscert(..)
4646
, Tlskey(..)
47+
, WalletArguments(..)
4748
, WalletDbPath(..)
4849
, WalletPath(..)
4950
, WalletLogging(..)
@@ -59,9 +60,9 @@ import Cardano.Prelude hiding (evalState, maybe)
5960
import Control.Monad.Trans.State.Strict (evalState)
6061
import Data.Functor.Contravariant (contramap)
6162
import qualified Data.Text as T
62-
import Dhall (GenericInject, Inject (..), InputType (..), Interpret (..),
63-
InterpretOptions (..), auto, field, genericInjectWith,
64-
record)
63+
import Dhall (GenericInject, Inject (..), InputType (..),
64+
Interpret (..), InterpretOptions (..), auto, field,
65+
genericInjectWith, record)
6566
import GHC.Generics (from)
6667
import Test.QuickCheck (Arbitrary (..), Gen, arbitraryASCIIChar,
6768
elements, listOf, listOf1)
@@ -629,6 +630,17 @@ instance Inject Tlskey
629630
instance Arbitrary Tlskey where
630631
arbitrary = Tlskey <$> genSafeText
631632

633+
newtype WalletArguments = WalletArguments
634+
{ getWalletArguments :: [Text]
635+
} deriving (Eq, Show, Generic)
636+
637+
-- Not sure if we will use it in Dhall
638+
--instance Interpret WalletArguments
639+
--instance Inject WalletArguments
640+
641+
instance Arbitrary WalletArguments where
642+
arbitrary = WalletArguments <$> listOf1 genSafeText
643+
632644
newtype WalletDbPath = WalletDbPath {
633645
getDbPath :: Text
634646
} deriving (Eq, Show, Generic)
@@ -816,39 +828,39 @@ instance Arbitrary NetworkConfig where
816828
}
817829

818830
data WalletConfig = WalletConfig
819-
{ walletDbPath :: !WalletDbPath
820-
, walletPath :: !WalletPath
821-
, walletLogging :: !WalletLogging
822-
, walletPort :: !WalletPort
823-
, walletAddress :: !WalletAddress
824-
, walletRelays :: !WalletRelays
825-
, walletFallback :: !WalletFallback
826-
, walletValency :: !WalletValency
831+
{ walletDbPath :: !WalletDbPath
832+
, walletPath :: !WalletPath
833+
, walletLogging :: !WalletLogging
834+
, walletPort :: !WalletPort
835+
, walletAddress :: !WalletAddress
836+
, walletRelays :: !WalletRelays
837+
, walletFallback :: !WalletFallback
838+
, walletValency :: !WalletValency
827839
} deriving (Eq, Generic, Show)
828840

829841
instance Interpret WalletConfig
830842
instance Inject WalletConfig
831843

832844
instance Arbitrary WalletConfig where
833845
arbitrary = do
834-
dbpath <- arbitrary
835-
path <- arbitrary
836-
logging <- arbitrary
837-
port <- arbitrary
838-
address <- arbitrary
839-
relays <- arbitrary
840-
fallback <- arbitrary
841-
valency <- arbitrary
846+
dbpath <- arbitrary
847+
path <- arbitrary
848+
logging <- arbitrary
849+
port <- arbitrary
850+
address <- arbitrary
851+
relays <- arbitrary
852+
fallback <- arbitrary
853+
valency <- arbitrary
842854

843855
pure $ WalletConfig
844-
{ walletDbPath = dbpath
845-
, walletPath = path
846-
, walletLogging = logging
847-
, walletPort = port
848-
, walletAddress = address
849-
, walletRelays = relays
850-
, walletFallback = fallback
851-
, walletValency = valency
856+
{ walletDbPath = dbpath
857+
, walletPath = path
858+
, walletLogging = logging
859+
, walletPort = port
860+
, walletAddress = address
861+
, walletRelays = relays
862+
, walletFallback = fallback
863+
, walletValency = valency
852864
}
853865

854866
--------------------------------------------------------------------------------

test/DhallConfigSpec.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,4 @@ mkConfigSpec = describe "Cardano configurations" $ do
124124
\(os :: OS) (cluster :: Cluster) -> monadicIO $ do
125125
eWalletConfig <- run $ tryAny $ mkWalletConfig os cluster
126126
assert $ isRight eWalletConfig
127+

0 commit comments

Comments
 (0)