From 18df87b8f5d927adbc911ce0726963e66d1abed1 Mon Sep 17 00:00:00 2001 From: Keagan McClelland Date: Wed, 3 Feb 2021 19:11:46 -0700 Subject: [PATCH] adds log fetch feature to agent --- agent/config/routes | 1 + agent/src/Handler/Status.hs | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/agent/config/routes b/agent/config/routes index f598519c2..899d90131 100644 --- a/agent/config/routes +++ b/agent/config/routes @@ -13,6 +13,7 @@ /v0/specs SpecsR GET /v0/metrics MetricsR GET +/v0/logs LogsR GET /v0/sshKeys SshKeysR GET POST /v0/sshKeys/#Text SshKeyByFingerprintR DELETE /v0/password PasswordR PATCH diff --git a/agent/src/Handler/Status.hs b/agent/src/Handler/Status.hs index 4066ccce5..5627e1dd4 100644 --- a/agent/src/Handler/Status.hs +++ b/agent/src/Handler/Status.hs @@ -28,6 +28,11 @@ import Lib.SystemPaths hiding ( () ) import Lib.Tor import Settings import Control.Carrier.Lift ( runM ) +import System.Process +import qualified UnliftIO +import System.FileLock +import Yesod.Core.Content ( typePlain ) +import Conduit getVersionR :: Handler AppVersionRes getVersionR = pure . AppVersionRes $ agentVersion @@ -69,3 +74,9 @@ patchServerR = do getGitR :: Handler Text getGitR = pure $embedGitRevision +getLogsR :: Handler TypedContent +getLogsR = do + let debugLock = "/root/agent/tmp/debug.lock" + UnliftIO.bracket (liftIO $ lockFile debugLock Exclusive) (liftIO . unlockFile) $ const $ do + liftIO $ callCommand "journalctl -u agent --since \"1 hour ago\" > /root/agent/tmp/debug.log" + respondSource typePlain $ sourceFile "/root/agent/tmp/debug.log" .| awaitForever sendChunkBS