Skip to content

Commit

Permalink
refactor: rename top-level Lhp module to HostPatrol
Browse files Browse the repository at this point in the history
  • Loading branch information
vst committed Apr 14, 2024
1 parent 58e96d7 commit 4013460
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 47 deletions.
2 changes: 1 addition & 1 deletion app/Main.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Main where

import qualified Lhp.Cli as Cli
import qualified HostPatrol.Cli as Cli
import System.Exit (exitWith)


Expand Down
10 changes: 5 additions & 5 deletions src/Lhp/Cli.hs → src/HostPatrol/Cli.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{-# LANGUAGE TypeApplications #-}

-- | This module provides top-level definitions for the CLI program.
module Lhp.Cli where
module HostPatrol.Cli where

import qualified Autodocodec.Schema as ADC.Schema
import Control.Applicative ((<**>))
Expand All @@ -12,10 +12,10 @@ import qualified Data.Aeson as Aeson
import qualified Data.ByteString.Lazy.Char8 as BLC
import qualified Data.Text as T
import qualified Data.Text.IO as TIO
import qualified Lhp.Config as Config
import qualified Lhp.Meta as Meta
import Lhp.Remote (compileReport)
import Lhp.Types (Report)
import qualified HostPatrol.Config as Config
import qualified HostPatrol.Meta as Meta
import HostPatrol.Remote (compileReport)
import HostPatrol.Types (Report)
import Options.Applicative ((<|>))
import qualified Options.Applicative as OA
import System.Exit (ExitCode (..))
Expand Down
2 changes: 1 addition & 1 deletion src/Lhp/Config.hs → src/HostPatrol/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

-- | This module defines public data and type definitions to represent
-- application configuration.
module Lhp.Config where
module HostPatrol.Config where

import qualified Autodocodec as ADC
import qualified Data.Aeson as Aeson
Expand Down
2 changes: 1 addition & 1 deletion src/Lhp/Meta.hs → src/HostPatrol/Meta.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{-# LANGUAGE TemplateHaskell #-}

-- | This module provides project metadata information definitions.
module Lhp.Meta where
module HostPatrol.Meta where

import Data.Aeson (ToJSON (toEncoding))
import qualified Data.Aeson as Aeson
Expand Down
75 changes: 37 additions & 38 deletions src/Lhp/Remote.hs → src/HostPatrol/Remote.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

-- | This module provides definitions to retrieve and parse remote
-- host information and produce host report.
module Lhp.Remote where
module HostPatrol.Remote where

import Control.Monad.Except (ExceptT, MonadError (..), runExceptT)
import Control.Monad.IO.Class (MonadIO (liftIO))
Expand All @@ -20,9 +20,8 @@ import qualified Data.List as List
import Data.Maybe (catMaybes, fromMaybe)
import qualified Data.Scientific as S
import qualified Data.Text as T
import qualified Lhp.Config as Config
import Lhp.Types (HostReport (_hostReportTimezone))
import qualified Lhp.Types as Types
import qualified HostPatrol.Config as Config
import qualified HostPatrol.Types as Types
import System.Exit (ExitCode (..))
import System.IO (hPutStrLn, stderr)
import qualified System.Process.Typed as TP
Expand All @@ -36,7 +35,7 @@ import qualified Zamazingo.Text as Z.Text

-- | Attempts to compile host patrol report for a given configuration.
compileReport
:: MonadError LhpError m
:: MonadError HostPatrolError m
=> MP.MonadParallel m
=> MonadIO m
=> Bool
Expand All @@ -60,7 +59,7 @@ compileReport par Config.Config {..} = do
-- report.
compileHostReport
:: MonadIO m
=> MonadError LhpError m
=> MonadError HostPatrolError m
=> Config.HostSpec
-> m Types.HostReport
compileHostReport ch = do
Expand All @@ -83,7 +82,7 @@ compileHostReport ch = do

-- | Consumes a 'Config.HostSpec' and produces a 'Types.Host'.
_makeHostFromConfigHostSpec
:: MonadError LhpError m
:: MonadError HostPatrolError m
=> MonadIO m
=> Config.HostSpec
-> m Types.Host
Expand All @@ -104,17 +103,17 @@ _makeHostFromConfigHostSpec Config.HostSpec {..} =

-- | Data definition for error(s) which can be thrown while retrieving
-- remote host information and producing host report.
data LhpError
= LhpErrorSsh Z.Ssh.Destination Z.Ssh.SshError
| LhpErrorParse Z.Ssh.Destination T.Text
| LhpErrorUnknown T.Text
data HostPatrolError
= HostPatrolErrorSsh Z.Ssh.Destination Z.Ssh.SshError
| HostPatrolErrorParse Z.Ssh.Destination T.Text
| HostPatrolErrorUnknown T.Text
deriving (Eq, Show)


instance Aeson.ToJSON LhpError where
toJSON (LhpErrorSsh h err) = Aeson.object [("type", "ssh"), "host" Aeson..= h, "error" Aeson..= err]
toJSON (LhpErrorParse h err) = Aeson.object [("type", "parse"), "host" Aeson..= h, "error" Aeson..= err]
toJSON (LhpErrorUnknown err) = Aeson.object [("type", "unknown"), "error" Aeson..= err]
instance Aeson.ToJSON HostPatrolError where
toJSON (HostPatrolErrorSsh h err) = Aeson.object [("type", "ssh"), "host" Aeson..= h, "error" Aeson..= err]
toJSON (HostPatrolErrorParse h err) = Aeson.object [("type", "parse"), "host" Aeson..= h, "error" Aeson..= err]
toJSON (HostPatrolErrorUnknown err) = Aeson.object [("type", "unknown"), "error" Aeson..= err]


-- * Internal
Expand All @@ -133,7 +132,7 @@ getHostSshConfig Types.Host {..} =
-- list of key/value tuples.
_fetchHostInfo
:: MonadIO m
=> MonadError LhpError m
=> MonadError HostPatrolError m
=> Types.Host
-> m [(T.Text, T.Text)]
_fetchHostInfo h@Types.Host {..} =
Expand All @@ -144,7 +143,7 @@ _fetchHostInfo [email protected] {..} =
-- as a list of key/value tuples.
_fetchHostCloudInfo
:: MonadIO m
=> MonadError LhpError m
=> MonadError HostPatrolError m
=> Types.Host
-> m [(T.Text, T.Text)]
_fetchHostCloudInfo h@Types.Host {..} =
Expand All @@ -157,7 +156,7 @@ _fetchHostCloudInfo [email protected] {..} =
-- host, a list of rudimentary Docker container information otherwise.
_fetchHostDockerContainers
:: MonadIO m
=> MonadError LhpError m
=> MonadError HostPatrolError m
=> Types.Host
-> m (Maybe [Types.DockerContainer])
_fetchHostDockerContainers h@Types.Host {..} =
Expand All @@ -166,15 +165,15 @@ _fetchHostDockerContainers [email protected] {..} =
prog = _toSshError _hostName (Z.Ssh.runScript (getHostSshConfig h) $(embedStringFile "src/scripts/docker-containers.sh") ["bash"])
_parseDockerContainers b =
case ACD.eitherDecode (ACD.list _jsonDecoderDockerContainer) b of
Left err -> throwError (LhpErrorParse _hostName ("Error while parsing containers information: " <> T.pack err))
Left err -> throwError (HostPatrolErrorParse _hostName ("Error while parsing containers information: " <> T.pack err))
Right sv -> pure sv


-- | Attempts to find and return all public SSH host keys on the remote
-- host.
_fetchHostPublicSshHostKeys
:: MonadIO m
=> MonadError LhpError m
=> MonadError HostPatrolError m
=> Types.Host
-> m [T.Text]
_fetchHostPublicSshHostKeys h@Types.Host {..} =
Expand All @@ -187,7 +186,7 @@ _fetchHostPublicSshHostKeys [email protected] {..} =
-- host.
_fetchHostAuthorizedSshKeys
:: MonadIO m
=> MonadError LhpError m
=> MonadError HostPatrolError m
=> Types.Host
-> m [T.Text]
_fetchHostAuthorizedSshKeys h@Types.Host {..} =
Expand All @@ -200,7 +199,7 @@ _fetchHostAuthorizedSshKeys [email protected] {..} =
-- host.
_fetchHostSystemdServices
:: MonadIO m
=> MonadError LhpError m
=> MonadError HostPatrolError m
=> Types.Host
-> m [T.Text]
_fetchHostSystemdServices h@Types.Host {..} =
Expand All @@ -213,7 +212,7 @@ _fetchHostSystemdServices [email protected] {..} =
-- host.
_fetchHostSystemdTimers
:: MonadIO m
=> MonadError LhpError m
=> MonadError HostPatrolError m
=> Types.Host
-> m [T.Text]
_fetchHostSystemdTimers h@Types.Host {..} =
Expand All @@ -224,7 +223,7 @@ _fetchHostSystemdTimers [email protected] {..} =

-- | Smart constructor for remote host cloud information.
_mkCloud
:: MonadError LhpError m
:: MonadError HostPatrolError m
=> Z.Ssh.Destination
-> [(T.Text, T.Text)]
-> m Types.Cloud
Expand All @@ -246,7 +245,7 @@ _mkCloud h kvs =
-- | Smart constructor for remote host rudimentary hardware
-- information.
_mkHardware
:: MonadError LhpError m
:: MonadError HostPatrolError m
=> Z.Ssh.Destination
-> [(T.Text, T.Text)]
-> m Types.Hardware
Expand All @@ -260,7 +259,7 @@ _mkHardware h kvs =

-- | Smart constructor for remote host kernel information.
_mkKernel
:: MonadError LhpError m
:: MonadError HostPatrolError m
=> Z.Ssh.Destination
-> [(T.Text, T.Text)]
-> m Types.Kernel
Expand All @@ -277,7 +276,7 @@ _mkKernel h kvs =

-- | Smart constructor for remote host distribution information.
_mkDistribution
:: MonadError LhpError m
:: MonadError HostPatrolError m
=> Z.Ssh.Destination
-> [(T.Text, T.Text)]
-> m Types.Distribution
Expand Down Expand Up @@ -409,26 +408,26 @@ _parseRead =
either (throwError . T.pack) pure . readEither . T.unpack


-- | Lifts @'ExceptT' 'T.Text'@ to @'MonadError' 'LhpError'@ with
-- | Lifts @'ExceptT' 'T.Text'@ to @'MonadError' 'HostPatrolError'@ with
-- parse error.
_toParseError
:: MonadError LhpError m
:: MonadError HostPatrolError m
=> Z.Ssh.Destination
-> ExceptT T.Text m a
-> m a
_toParseError h =
_modifyError (LhpErrorParse h)
_modifyError (HostPatrolErrorParse h)


-- | Lifts @'ExceptT' 'Z.Ssh.SshError'@ to @'MonadError' 'LhpError'@
-- | Lifts @'ExceptT' 'Z.Ssh.SshError'@ to @'MonadError' 'HostPatrolError'@
-- with SSH error.
_toSshError
:: MonadError LhpError m
:: MonadError HostPatrolError m
=> Z.Ssh.Destination
-> ExceptT Z.Ssh.SshError m a
-> m a
_toSshError h =
_modifyError (LhpErrorSsh h)
_modifyError (HostPatrolErrorSsh h)


-- | Creates list of 'Types.SshPublicKey' from given 'T.Text' using @ssh-keygen@.
Expand All @@ -447,7 +446,7 @@ _toSshError h =
-- >>> runExceptT $ parseSshPublicKeys "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILdd2ubdTn5LPsN0zaxylrpkQTW+1Vr/uWQaEQXoGkd3 some more comment"
-- Right [SshPublicKey {_sshPublicKeyData = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILdd2ubdTn5LPsN0zaxylrpkQTW+1Vr/uWQaEQXoGkd3 some more comment", _sshPublicKeyType = "ED25519", _sshPublicKeyLength = 256, _sshPublicKeyComment = "some more comment", _sshPublicKeyFingerprint = "MD5:ec:4b:ff:8d:c7:43:a9:ab:16:9f:0d:fa:8f:e2:6f:6c"}]
parseSshPublicKeys
:: MonadError LhpError m
:: MonadError HostPatrolError m
=> MonadIO m
=> T.Text
-> m [Types.SshPublicKey]
Expand All @@ -464,7 +463,7 @@ parseSshPublicKeys s = do
-- | Attempts to create 'Types.SshPublicKey' from given SSH public key
-- represented as 'T.Text' using @ssh-keygen@.
parseSshPublicKey
:: MonadError LhpError m
:: MonadError HostPatrolError m
=> MonadIO m
=> T.Text
-> m Types.SshPublicKey
Expand All @@ -484,15 +483,15 @@ parseSshPublicKey s = do
}
_ -> throwUnknown "Could not parse ssh-keygen output."
where
throwUnknown = throwError . LhpErrorUnknown
throwUnknown = throwError . HostPatrolErrorUnknown
stdin = TP.byteStringInput (Z.Text.blFromText s)
process = TP.setStdin stdin (TP.proc "ssh-keygen" ["-E", "md5", "-l", "-f", "-"])


-- | Attempts to get the list of SSH public keys from GitHub for a
-- given GitHub username.
listGitHubSshKeys
:: MonadError LhpError m
:: MonadError HostPatrolError m
=> MonadIO m
=> T.Text
-> m [T.Text]
Expand All @@ -502,6 +501,6 @@ listGitHubSshKeys u = do
ExitFailure _ -> throwUnknown (Z.Text.unsafeTextFromBL err)
ExitSuccess -> pure (toKeys out)
where
throwUnknown = throwError . LhpErrorUnknown
throwUnknown = throwError . HostPatrolErrorUnknown
process = TP.proc "curl" ["-s", "https://github.com/" <> T.unpack u <> ".keys"]
toKeys = filter (not . T.null) . T.lines . Z.Text.unsafeTextFromBL
2 changes: 1 addition & 1 deletion src/Lhp/Types.hs → src/HostPatrol/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

-- | This module defines public data and type definitions to represent
-- a complete Host Patrol report.
module Lhp.Types where
module HostPatrol.Types where

import qualified Autodocodec as ADC
import qualified Data.Aeson as Aeson
Expand Down

0 comments on commit 4013460

Please sign in to comment.