From f27962c702ed6970f884e4203b68ec41906e8fbb Mon Sep 17 00:00:00 2001 From: Tim McGilchrist Date: Wed, 29 Sep 2021 13:24:11 +1000 Subject: [PATCH] Place TH gitRev into cardano-db-sync project. --- .../app/cardano-db-sync-extended.hs | 94 +----------- .../cardano-db-sync-extended.cabal | 2 - cardano-db-sync/app/cardano-db-sync.hs | 101 +------------ cardano-db-sync/cardano-db-sync.cabal | 9 +- cardano-db-sync/src/Cardano/DbSync/Cli.hs | 142 ++++++++++++++++++ cardano-db-sync/src/Cardano/DbSync/TH.hs | 24 +++ 6 files changed, 177 insertions(+), 195 deletions(-) create mode 100644 cardano-db-sync/src/Cardano/DbSync/Cli.hs create mode 100644 cardano-db-sync/src/Cardano/DbSync/TH.hs diff --git a/cardano-db-sync-extended/app/cardano-db-sync-extended.hs b/cardano-db-sync-extended/app/cardano-db-sync-extended.hs index 4ecb31c5b..fe3ecf18f 100644 --- a/cardano-db-sync-extended/app/cardano-db-sync-extended.hs +++ b/cardano-db-sync-extended/app/cardano-db-sync-extended.hs @@ -3,23 +3,18 @@ import Cardano.Prelude -import Cardano.Config.Git.Rev (gitRev) - import Cardano.DbSync (runDbSyncNode) +import Cardano.DbSync.Cli import Cardano.DbSync.Metrics (withMetricSetters) import Cardano.DbSync.Plugin.Extended (extendedDbSyncNodePlugin) -import Cardano.Slotting.Slot (SlotNo (..)) import Cardano.Sync.Config -import Cardano.Sync.Config.Types -import Data.String (String) import qualified Data.Text as Text import Data.Version (showVersion) import MigrationValidations (KnownMigration (..), knownMigrations) -import Options.Applicative (Parser, ParserInfo) import qualified Options.Applicative as Opt import Paths_cardano_db_sync_extended (version) @@ -43,93 +38,6 @@ main = do -- ------------------------------------------------------------------------------------------------- -opts :: ParserInfo SyncCommand -opts = - Opt.info (pCommandLine <**> Opt.helper) - ( Opt.fullDesc - <> Opt.progDesc "Extended Cardano POstgreSQL sync node." - ) - -pCommandLine :: Parser SyncCommand -pCommandLine = - asum - [ pVersionCommand - , CmdRun <$> pRunDbSyncNode - ] - -pRunDbSyncNode :: Parser SyncNodeParams -pRunDbSyncNode = - SyncNodeParams - <$> pConfigFile - <*> pSocketPath - <*> pLedgerStateDir - <*> pMigrationDir - <*> optional pSlotNo - -pConfigFile :: Parser ConfigFile -pConfigFile = - ConfigFile <$> Opt.strOption - ( Opt.long "config" - <> Opt.help "Path to the db-sync node config file" - <> Opt.completer (Opt.bashCompleter "file") - <> Opt.metavar "FILEPATH" - ) - -pLedgerStateDir :: Parser LedgerStateDir -pLedgerStateDir = - LedgerStateDir <$> Opt.strOption - ( Opt.long "state-dir" - <> Opt.help "The directory for persistung ledger state." - <> Opt.completer (Opt.bashCompleter "directory") - <> Opt.metavar "FILEPATH" - ) - -pMigrationDir :: Parser MigrationDir -pMigrationDir = - MigrationDir <$> Opt.strOption - ( Opt.long "schema-dir" - <> Opt.help "The directory containing the migrations." - <> Opt.completer (Opt.bashCompleter "directory") - <> Opt.metavar "FILEPATH" - ) - -pSocketPath :: Parser SocketPath -pSocketPath = - SocketPath <$> Opt.strOption - ( Opt.long "socket-path" - <> Opt.help "Path to a cardano-node socket" - <> Opt.completer (Opt.bashCompleter "file") - <> Opt.metavar "FILEPATH" - ) - -pSlotNo :: Parser SlotNo -pSlotNo = - SlotNo <$> Opt.option Opt.auto - ( Opt.long "rollback-to-slot" - <> Opt.help "Force a rollback to the specified slot (mainly for testing and debugging)." - <> Opt.metavar "WORD" - ) - -pVersionCommand :: Parser SyncCommand -pVersionCommand = - asum - [ Opt.subparser - ( mconcat - [ command' "version" "Show the program version" (pure CmdVersion) ] - ) - , Opt.flag' CmdVersion - ( Opt.long "version" - <> Opt.help "Show the program version" - <> Opt.hidden - ) - ] - -command' :: String -> String -> Parser a -> Opt.Mod Opt.CommandFields a -command' c descr p = - Opt.command c - $ Opt.info (p <**> Opt.helper) - $ mconcat [ Opt.progDesc descr ] - runVersionCommand :: IO () runVersionCommand = do liftIO . putTextLn $ mconcat diff --git a/cardano-db-sync-extended/cardano-db-sync-extended.cabal b/cardano-db-sync-extended/cardano-db-sync-extended.cabal index 6712f6469..6f24922e9 100644 --- a/cardano-db-sync-extended/cardano-db-sync-extended.cabal +++ b/cardano-db-sync-extended/cardano-db-sync-extended.cabal @@ -73,11 +73,9 @@ executable cardano-db-sync-extended MigrationValidations build-depends: base >= 4.14 && < 4.16 - , cardano-config , cardano-db-sync , cardano-db-sync-extended , cardano-sync , cardano-prelude - , cardano-slotting , optparse-applicative , text diff --git a/cardano-db-sync/app/cardano-db-sync.hs b/cardano-db-sync/app/cardano-db-sync.hs index 5c552fd49..4a4c19d03 100644 --- a/cardano-db-sync/app/cardano-db-sync.hs +++ b/cardano-db-sync/app/cardano-db-sync.hs @@ -3,29 +3,21 @@ import Cardano.Prelude -import Cardano.Config.Git.Rev (gitRev) - import Cardano.DbSync (defDbSyncNodePlugin, runDbSyncNode) +import Cardano.DbSync.Cli import Cardano.DbSync.Metrics (withMetricSetters) - -import Cardano.Sync.Config +import Cardano.Sync.Config (readSyncNodeConfig) import Cardano.Sync.Config.Types -import Cardano.Slotting.Slot (SlotNo (..)) - -import Data.String (String) import qualified Data.Text as Text import Data.Version (showVersion) -import Options.Applicative (Parser, ParserInfo) -import qualified Options.Applicative as Opt - import MigrationValidations (KnownMigration (..), knownMigrations) +import qualified Options.Applicative as Opt import Paths_cardano_db_sync (version) import System.Info (arch, compilerName, compilerVersion, os) - main :: IO () main = do cmd <- Opt.execParser opts @@ -42,93 +34,6 @@ main = do -- ------------------------------------------------------------------------------------------------- -opts :: ParserInfo SyncCommand -opts = - Opt.info (pCommandLine <**> Opt.helper) - ( Opt.fullDesc - <> Opt.progDesc "Cardano PostgreSQL sync node." - ) - -pCommandLine :: Parser SyncCommand -pCommandLine = - asum - [ pVersionCommand - , CmdRun <$> pRunDbSyncNode - ] - -pRunDbSyncNode :: Parser SyncNodeParams -pRunDbSyncNode = - SyncNodeParams - <$> pConfigFile - <*> pSocketPath - <*> pLedgerStateDir - <*> pMigrationDir - <*> optional pSlotNo - -pConfigFile :: Parser ConfigFile -pConfigFile = - ConfigFile <$> Opt.strOption - ( Opt.long "config" - <> Opt.help "Path to the db-sync node config file" - <> Opt.completer (Opt.bashCompleter "file") - <> Opt.metavar "FILEPATH" - ) - -pLedgerStateDir :: Parser LedgerStateDir -pLedgerStateDir = - LedgerStateDir <$> Opt.strOption - ( Opt.long "state-dir" - <> Opt.help "The directory for persistung ledger state." - <> Opt.completer (Opt.bashCompleter "directory") - <> Opt.metavar "FILEPATH" - ) - -pMigrationDir :: Parser MigrationDir -pMigrationDir = - MigrationDir <$> Opt.strOption - ( Opt.long "schema-dir" - <> Opt.help "The directory containing the migrations." - <> Opt.completer (Opt.bashCompleter "directory") - <> Opt.metavar "FILEPATH" - ) - -pSocketPath :: Parser SocketPath -pSocketPath = - SocketPath <$> Opt.strOption - ( Opt.long "socket-path" - <> Opt.help "Path to a cardano-node socket" - <> Opt.completer (Opt.bashCompleter "file") - <> Opt.metavar "FILEPATH" - ) - -pSlotNo :: Parser SlotNo -pSlotNo = - SlotNo <$> Opt.option Opt.auto - ( Opt.long "rollback-to-slot" - <> Opt.help "Force a rollback to the specified slot (mainly for testing and debugging)." - <> Opt.metavar "WORD" - ) - -pVersionCommand :: Parser SyncCommand -pVersionCommand = - asum - [ Opt.subparser - ( mconcat - [ command' "version" "Show the program version" (pure CmdVersion) ] - ) - , Opt.flag' CmdVersion - ( Opt.long "version" - <> Opt.help "Show the program version" - <> Opt.hidden - ) - ] - -command' :: String -> String -> Parser a -> Opt.Mod Opt.CommandFields a -command' c descr p = - Opt.command c - $ Opt.info (p <**> Opt.helper) - $ mconcat [ Opt.progDesc descr ] - runVersionCommand :: IO () runVersionCommand = do liftIO . putTextLn $ mconcat diff --git a/cardano-db-sync/cardano-db-sync.cabal b/cardano-db-sync/cardano-db-sync.cabal index 71ab72646..b49fb2b91 100644 --- a/cardano-db-sync/cardano-db-sync.cabal +++ b/cardano-db-sync/cardano-db-sync.cabal @@ -44,6 +44,9 @@ library exposed-modules: Cardano.DbSync + Cardano.DbSync.Cli + Cardano.DbSync.TH + Cardano.DbSync.Era Cardano.DbSync.Era.Byron.Genesis @@ -107,6 +110,7 @@ library , containers , esqueleto , extra + , file-embed , groups , http-client , http-client-tls @@ -120,8 +124,10 @@ library , ouroboros-consensus-cardano , ouroboros-consensus-shelley , ouroboros-network + , optparse-applicative , persistent , persistent-postgresql + , process , prometheus , random-shuffle , small-steps @@ -130,6 +136,7 @@ library , strict-containers , swagger2 , text + , template-haskell , time , transformers , transformers-except @@ -160,10 +167,8 @@ executable cardano-db-sync MigrationValidations build-depends: base >= 4.14 && < 4.16 - , cardano-config , cardano-sync , cardano-db-sync , cardano-prelude - , cardano-slotting , optparse-applicative , text diff --git a/cardano-db-sync/src/Cardano/DbSync/Cli.hs b/cardano-db-sync/src/Cardano/DbSync/Cli.hs new file mode 100644 index 000000000..fefcb76ec --- /dev/null +++ b/cardano-db-sync/src/Cardano/DbSync/Cli.hs @@ -0,0 +1,142 @@ +{-# LANGUAGE CPP #-} +{-# LANGUAGE NoImplicitPrelude #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE TemplateHaskell #-} + +module Cardano.DbSync.Cli ( + command' + , gitRev + , opts + , pCommandLine + , pConfigFile + , pLedgerStateDir + , pMigrationDir + , pRunDbSyncNode + , pSlotNo + , pSocketPath + , pVersionCommand + ) where + +import Cardano.DbSync.TH (gitRevFromGit) +import Cardano.Prelude +import Cardano.Slotting.Slot (SlotNo (..)) +import Cardano.Sync.Config.Types (ConfigFile (..), LedgerStateDir (..), MigrationDir (..), + SocketPath (..), SyncCommand (..), SyncNodeParams (..)) + +import Data.FileEmbed (dummySpaceWith) +import Data.String (String) +import qualified Data.Text as Text + +import Options.Applicative (Parser, ParserInfo) +import qualified Options.Applicative as Opt + +opts :: ParserInfo SyncCommand +opts = + Opt.info (pCommandLine <**> Opt.helper) + ( Opt.fullDesc + <> Opt.progDesc "Cardano PostgreSQL sync node." + ) + +pCommandLine :: Parser SyncCommand +pCommandLine = + asum + [ pVersionCommand + , CmdRun <$> pRunDbSyncNode + ] + +pRunDbSyncNode :: Parser SyncNodeParams +pRunDbSyncNode = + SyncNodeParams + <$> pConfigFile + <*> pSocketPath + <*> pLedgerStateDir + <*> pMigrationDir + <*> optional pSlotNo + +pConfigFile :: Parser ConfigFile +pConfigFile = + ConfigFile <$> Opt.strOption + ( Opt.long "config" + <> Opt.help "Path to the db-sync node config file" + <> Opt.completer (Opt.bashCompleter "file") + <> Opt.metavar "FILEPATH" + ) + +pLedgerStateDir :: Parser LedgerStateDir +pLedgerStateDir = + LedgerStateDir <$> Opt.strOption + ( Opt.long "state-dir" + <> Opt.help "The directory for persistung ledger state." + <> Opt.completer (Opt.bashCompleter "directory") + <> Opt.metavar "FILEPATH" + ) + +pMigrationDir :: Parser MigrationDir +pMigrationDir = + MigrationDir <$> Opt.strOption + ( Opt.long "schema-dir" + <> Opt.help "The directory containing the migrations." + <> Opt.completer (Opt.bashCompleter "directory") + <> Opt.metavar "FILEPATH" + ) + +pSocketPath :: Parser SocketPath +pSocketPath = + SocketPath <$> Opt.strOption + ( Opt.long "socket-path" + <> Opt.help "Path to a cardano-node socket" + <> Opt.completer (Opt.bashCompleter "file") + <> Opt.metavar "FILEPATH" + ) + +pSlotNo :: Parser SlotNo +pSlotNo = + SlotNo <$> Opt.option Opt.auto + ( Opt.long "rollback-to-slot" + <> Opt.help "Force a rollback to the specified slot (mainly for testing and debugging)." + <> Opt.metavar "WORD" + ) + +pVersionCommand :: Parser SyncCommand +pVersionCommand = + asum + [ Opt.subparser + ( mconcat + [ command' "version" "Show the program version" (pure CmdVersion) ] + ) + , Opt.flag' CmdVersion + ( Opt.long "version" + <> Opt.help "Show the program version" + <> Opt.hidden + ) + ] + +command' :: String -> String -> Parser a -> Opt.Mod Opt.CommandFields a +command' c descr p = + Opt.command c + $ Opt.info (p <**> Opt.helper) + $ mconcat [ Opt.progDesc descr ] + +{-# NOINLINE gitRev #-} +gitRev :: Text +gitRev | gitRevEmbed /= zeroRev = gitRevEmbed + | Text.null fromGit = zeroRev + | otherwise = fromGit + where + -- Git revision embedded after compilation using + -- Data.FileEmbed.injectWith. If nothing has been injected, + -- this will be filled with 0 characters. + gitRevEmbed :: Text + gitRevEmbed = decodeUtf8 $(dummySpaceWith "gitrev" 40) + + -- Git revision found during compilation by running git. If + -- git could not be run, then this will be empty. +#if defined(arm_HOST_ARCH) + -- cross compiling to arm fails; due to a linker bug + fromGit = "" +#else + fromGit = Text.strip (Text.pack $(gitRevFromGit)) +#endif + +zeroRev :: Text +zeroRev = "0000000000000000000000000000000000000000" diff --git a/cardano-db-sync/src/Cardano/DbSync/TH.hs b/cardano-db-sync/src/Cardano/DbSync/TH.hs new file mode 100644 index 000000000..23ad8c765 --- /dev/null +++ b/cardano-db-sync/src/Cardano/DbSync/TH.hs @@ -0,0 +1,24 @@ +{-# LANGUAGE NoImplicitPrelude #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE TemplateHaskellQuotes #-} + +module Cardano.DbSync.TH ( gitRevFromGit ) where + +import Cardano.Prelude +import Data.String (String) +import qualified Language.Haskell.TH as TH +import System.IO.Error (ioeGetErrorType, isDoesNotExistErrorType) +import System.Process (readProcessWithExitCode) + +gitRevFromGit :: TH.Q TH.Exp +gitRevFromGit = TH.LitE . TH.StringL <$> TH.runIO runGitRevParse + where + runGitRevParse :: IO String + runGitRevParse = handleJust missingGit (const $ pure "") $ do + (exitCode, output, _) <- + readProcessWithExitCode "git" ["rev-parse", "--verify", "HEAD"] "" + pure $ case exitCode of + ExitSuccess -> output + _ -> "" + + missingGit e = if isDoesNotExistErrorType (ioeGetErrorType e) then Just () else Nothing