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

[GH-313] Test together launcher/updater. #331

Merged
merged 3 commits into from
Oct 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 13 additions & 24 deletions cardano-launcher/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ import Cardano.Shell.Configuration (ConfigurationOptions (..),
getWPath, getWargs,
setWorkingDirectory)
import Cardano.Shell.Launcher (LoggingDependencies (..), TLSError,
TLSPath (..), WalletMode (..),
generateTlsCertificates,
runWalletProcess, walletRunnerProcess)
import Cardano.Shell.Update.Lib (RemoveArchiveAfterInstall (..),
UpdaterData (..),
runDefaultUpdateProcess, runUpdater)
TLSPath (..), generateTlsCertificates,
runLauncher, walletRunnerProcess)
import Cardano.Shell.Update.Lib (UpdaterData (..),
runDefaultUpdateProcess)
import Control.Exception.Safe (throwM)
import Data.Text.Lazy.Builder (fromString, fromText)

Expand Down Expand Up @@ -77,8 +75,7 @@ main = silence $ do
Trace.logError baseTrace $ "Working directory does not exist: " <> toS workingDir
throwM . WorkingDirectoryDoesNotExist $ workingDir

-- Really no clue what to put there and how will the wallet work.
-- These will be refactored in the future
-- Configuration from the launcher options.
let configurationOptions :: ConfigurationOptions
configurationOptions = loConfiguration launcherOptions

Expand All @@ -91,11 +88,12 @@ main = silence $ do
let updaterData :: UpdaterData
updaterData = getUpdaterData launcherOptions


-- where to generate the certificates
let mTlsPath :: Maybe TLSPath
mTlsPath = TLSPath <$> loTlsPath launcherOptions


-- If the path doesn't exist, then TLS has been disabled!
case mTlsPath of
Just tlsPath -> do
-- | If we need to, we first check if there are certificates so we don't have
Expand All @@ -114,26 +112,17 @@ main = silence $ do
Right _ -> return ()
Nothing -> pure () -- TLS generation has been disabled

-- In the case the user wants to avoid installing the update now, we
-- run the update (if there is one) when we have it downloaded.
void $ runUpdater
RemoveArchiveAfterInstall
runDefaultUpdateProcess
loggingDependencies
updaterData

-- You still want to run the wallet even if the update fails
exitCode <- runWalletProcess
-- Finally, run the launcher once everything is set up!
exitCode <- runLauncher
loggingDependencies
WalletModeNormal
-- WalletPath -> WalletArguments -> IO ExitCode
walletRunnerProcess
walletPath
walletArgs
walletRunnerProcess
-- FilePath -> [String] -> IO ExitCode
runDefaultUpdateProcess
updaterData

Trace.logNotice baseTrace $
"Shutting down cardano-launcher with exitcode: " <> show exitCode

-- Exit the program with exit code.
exitWith exitCode

Expand Down
24 changes: 12 additions & 12 deletions cardano-launcher/src/Cardano/Shell/Configuration.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
{-# LANGUAGE GeneralizedNewtypeDeriving #-}

module Cardano.Shell.Configuration
( WalletArguments(..)
, WalletPath(..)
, LauncherOptions(..)
, ConfigurationOptions(..)
( WalletArguments (..)
, WalletPath (..)
, LauncherOptions (..)
, ConfigurationOptions (..)
-- * Getters
, getUpdaterData
, getWargs
Expand Down Expand Up @@ -42,14 +42,14 @@ newtype WalletPath = WalletPath
-- Todo: Add haddock comment for each field
-- | Launcher options
data LauncherOptions = LauncherOptions
{ loConfiguration :: !ConfigurationOptions
, loTlsPath :: !(Maybe FilePath)
, loUpdaterPath :: !FilePath
, loUpdaterArgs :: ![Text]
, loUpdateArchive :: !FilePath
, loWalletPath :: !FilePath
, loWalletArgs :: ![Text]
, loWorkingDirectory :: !FilePath
{ loConfiguration :: !ConfigurationOptions
, loTlsPath :: !(Maybe FilePath)
, loUpdaterPath :: !FilePath
, loUpdaterArgs :: ![Text]
, loUpdateArchive :: !FilePath
, loWalletPath :: !FilePath
, loWalletArgs :: ![Text]
, loWorkingDirectory :: !FilePath
-- On WIN it should set this directory as current.
} deriving (Show, Generic)

Expand Down
47 changes: 43 additions & 4 deletions cardano-launcher/src/Cardano/Shell/Launcher.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Cardano.Shell.Launcher
, walletRunnerProcess
, LoggingDependencies (..)
-- * Functions
, runLauncher
, runWalletProcess
-- * Critical exports (testing)
, DaedalusExitCode (..)
Expand All @@ -34,8 +35,8 @@ import Cardano.Shell.Configuration (ConfigurationOptions (..),
WalletPath (..))
import Cardano.Shell.Types (LoggingDependencies (..))
import Cardano.Shell.Update.Lib (RemoveArchiveAfterInstall (..),
UpdaterData (..),
runDefaultUpdateProcess, runUpdater)
RunUpdateFunc, UpdaterData (..),
runUpdater)
import Cardano.X509.Configuration (ConfigurationKey (..),
DirConfiguration (..), certChecks,
certFilename, certOutDir,
Expand Down Expand Up @@ -167,9 +168,17 @@ runWalletProcess
-> WalletPath
-> WalletArguments
-> WalletRunner
-> RunUpdateFunc
-> UpdaterData
-> IO ExitCode
runWalletProcess logDep walletMode walletPath walletArguments walletRunner updaterData = do
runWalletProcess
logDep
walletMode
walletPath
walletArguments
walletRunner
runUpdateFunc
updaterData = do

-- Parametrized by @WalletMode@ so we can change it on restart depending
-- on the Daedalus exit code.
Expand All @@ -180,6 +189,7 @@ runWalletProcess logDep walletMode walletPath walletArguments walletRunner updat
walletPath
walletArguments
walletRunner
runUpdateFunc
updaterData

-- Additional arguments we need to pass if it's a SAFE mode.
Expand Down Expand Up @@ -225,7 +235,7 @@ runWalletProcess logDep walletMode walletPath walletArguments walletRunner updat
-- We separate the description of the computation, from the computation itself.
-- There are other ways of doing this, of course.
isoFrom <$> handleDaedalusExitCode
(UpdateRunner $ runUpdater RemoveArchiveAfterInstall runDefaultUpdateProcess logDep updaterData)
(UpdateRunner $ runUpdater logDep RemoveArchiveAfterInstall runUpdateFunc updaterData)
(RestartRunner restart)
exitCode

Expand Down Expand Up @@ -259,6 +269,35 @@ newtype TLSPath = TLSPath { getTLSFilePath :: FilePath }
-- Functions
--------------------------------------------------------------------------------

-- | The function that runs the launcher once everything is set up!
runLauncher
:: LoggingDependencies
-> WalletRunner
-> WalletPath
-> WalletArguments
-> RunUpdateFunc
-> UpdaterData
-> IO ExitCode
runLauncher loggingDependencies walletRunner walletPath walletArgs runUpdateFunc updaterData = do

-- In the case the user wants to avoid installing the update now, we
-- run the update (if there is one) when we have it downloaded.
void $ runUpdater
loggingDependencies
RemoveArchiveAfterInstall
runUpdateFunc
updaterData

-- You still want to run the wallet even if the update fails
runWalletProcess
loggingDependencies
WalletModeNormal
walletPath
walletArgs
walletRunner
runUpdateFunc
updaterData

-- | Generation of the TLS certificates.
-- This just covers the generation of the TLS certificates and nothing else.
generateTlsCertificates
Expand Down
14 changes: 7 additions & 7 deletions cardano-launcher/src/Cardano/Shell/Update/Lib.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

module Cardano.Shell.Update.Lib
( UpdaterData (..)
, RunCmdFunc
, RunUpdateFunc
, runUpdater
, runDefaultUpdateProcess
-- * Intepretable language
Expand Down Expand Up @@ -84,7 +84,7 @@ runDefaultUpdateProcess path args =
$ \_in _out _err ph -> waitForProcess ph

-- The function for executing the update.
type RunCmdFunc = FilePath -> [String] -> IO ExitCode
type RunUpdateFunc = FilePath -> [String] -> IO ExitCode

-- | A flag for the existence of the updater file.
data UpdaterExists
Expand Down Expand Up @@ -128,7 +128,7 @@ isUpdaterRunOnUnix (UnixRunUpdate _ _) = True
isUpdaterRunOnUnix _ = False

-- | Interpret the small language into the "real" semantics.
evaluateUpdaterCmdExitCode :: RunCmdFunc -> UpdaterCommand -> IO ExitCode
evaluateUpdaterCmdExitCode :: RunUpdateFunc -> UpdaterCommand -> IO ExitCode
evaluateUpdaterCmdExitCode runCommand = \case
-- The update needs to be run on Windows.
WindowsRunUpdate updaterPath args -> do
Expand Down Expand Up @@ -162,12 +162,12 @@ evaluateUpdaterCmdLogging loggingDep = \case
-- first have to generate a @.bat@ file which will act as a script.
-- After it being generated, you run that script.
runUpdater
:: RemoveArchiveAfterInstall
-> RunCmdFunc
-> LoggingDependencies
:: LoggingDependencies
-> RemoveArchiveAfterInstall
-> RunUpdateFunc
-> UpdaterData
-> IO ExitCode
runUpdater removeArchive runCommand loggingDep updaterData = do
runUpdater loggingDep removeArchive runCommand updaterData = do

-- The update installation.
let archivePath = udArchivePath updaterData
Expand Down
4 changes: 2 additions & 2 deletions cardano-launcher/test/UpdaterSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ updaterSpec :: Spec
updaterSpec = describe "Update system" $ do
it "should be successful" $ monadicIO $ do
exitCode <- run $ runUpdater
nullLogging
DoNotRemoveArchiveAfterInstall
runDefaultUpdateProcess
nullLogging
testUpdaterData

run . putTextLn . show $ exitCode
assert $ exitCode == ExitSuccess

prop "should return expected error" $ \(exitNum :: ExitNum) -> monadicIO $ do
exitCode <- run $ runUpdater
nullLogging
DoNotRemoveArchiveAfterInstall
(testRunCmd exitNum)
nullLogging
testUpdaterData

assert $ exitCode == (ExitFailure . getExitNum $ exitNum)
Expand Down