From 82df17d8547a61aa2a3b855a61909f756771bb86 Mon Sep 17 00:00:00 2001 From: Keagan McClelland Date: Wed, 28 Apr 2021 12:23:33 -0600 Subject: [PATCH] adds notification for successful OS update, as well as failure --- agent/src/Lib/Notifications.hs | 5 +++++ agent/src/Lib/SelfUpdate.hs | 8 +++++++- agent/src/Lib/Synchronizers.hs | 11 +++++++++-- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/agent/src/Lib/Notifications.hs b/agent/src/Lib/Notifications.hs index 8da2e63ee..0ee484f51 100644 --- a/agent/src/Lib/Notifications.hs +++ b/agent/src/Lib/Notifications.hs @@ -43,6 +43,8 @@ data AgentNotification = | RestartFailed S9Error | DockerFuckening | CertRenewFailed ExitCode String String + | OsUpdateSucceeded + | OsUpdateFailed Text -- CODES -- RULES: @@ -73,6 +75,7 @@ toCode :: AgentNotification -> Text toCode InstallSuccess = "100" toCode BackupSucceeded = "101" toCode RestoreSucceeded = "102" +toCode OsUpdateSucceeded = "103" toCode InstallFailedGetApp = "300" toCode (InstallFailedAppMgrExitCode _) = "301" toCode DockerFuckening = "302" @@ -86,6 +89,7 @@ toTitle :: AgentNotification -> Text toTitle InstallSuccess = "Install succeeded" toTitle BackupSucceeded = "Backup succeeded" toTitle RestoreSucceeded = "Restore succeeded" +toTitle OsUpdateSucceeded = "EmbassyOS Update Succeeded" toTitle InstallFailedGetApp = "Install failed" toTitle (InstallFailedAppMgrExitCode _) = "Install failed" toTitle (InstallFailedS9Error _) = "Install failed" @@ -118,3 +122,4 @@ Stdout: Stderr: #{e} |] +toMessage _ version OsUpdateSucceeded = [i|The update to EmbassyOS #{version} succeeded.|] diff --git a/agent/src/Lib/SelfUpdate.hs b/agent/src/Lib/SelfUpdate.hs index 449e24ce7..505f799af 100644 --- a/agent/src/Lib/SelfUpdate.hs +++ b/agent/src/Lib/SelfUpdate.hs @@ -20,14 +20,17 @@ import System.Posix.Files import System.Process import Constants +import Database.Persist.Sqlite ( runSqlPool ) import Foundation import Handler.Types.V0.Base import Lib.Algebra.State.RegistryUrl import Lib.Error import Lib.External.Registry +import qualified Lib.Notifications as Notifications import Lib.Sound as Sound import Lib.Synchronizers import Lib.SystemPaths +import Lib.Types.Core import Lib.Types.Emver import Lib.WebServer import Settings @@ -214,7 +217,10 @@ synchronizeSystemState ctx _version = handle @_ @SomeException cleanup $ flip ru Nothing -> pure () Just tid -> liftIO $ killThread tid setUpdate False - when (any snd restartsAndRuns) $ liftIO $ playSong 400 marioPowerUp + when (any snd restartsAndRuns) $ liftIO $ do + _ <- flip runSqlPool (appConnPool ctx) + $ Notifications.emit (AppId "embassy-os") agentVersion Notifications.OsUpdateSucceeded + playSong 400 marioPowerUp when (any (uncurry (&&)) restartsAndRuns) $ liftIO do callCommand "/bin/sync" callCommand "/sbin/reboot" diff --git a/agent/src/Lib/Synchronizers.hs b/agent/src/Lib/Synchronizers.hs index 02855619e..d5b96e18e 100644 --- a/agent/src/Lib/Synchronizers.hs +++ b/agent/src/Lib/Synchronizers.hs @@ -55,6 +55,7 @@ import Data.ByteString.Char8 ( split ) import qualified Data.ByteString.Char8 as C8 import Data.Conduit.List ( consume ) import qualified Data.Text as T +import Database.Persist.Sqlite ( runSqlPool ) import Foundation import Handler.Network import qualified Lib.Algebra.Domain.AppMgr as AppMgr2 @@ -62,6 +63,7 @@ import Lib.ClientManifest import Lib.Error import qualified Lib.External.AppMgr as AppMgr import Lib.External.Registry +import qualified Lib.Notifications as Notifications import Lib.Sound import Lib.Ssl import Lib.SystemCtl @@ -651,8 +653,13 @@ syncRemoveDefaultNginxCfg = SyncOp "Remove Default Nginx Configuration" check mi failUpdate :: S9Error -> ExceptT Void (ReaderT AgentCtx IO) () failUpdate e = do - ref <- asks appIsUpdateFailed - putStrLn $ "UPDATE FAILED: " <> errorMessage (toError e) + ref <- asks appIsUpdateFailed + pool <- asks appConnPool + let msg = errorMessage (toError e) + putStrLn $ "UPDATE FAILED: " <> msg + _ <- liftIO . flip runSqlPool pool $ Notifications.emit (AppId "embassy-os") + agentVersion + (Notifications.OsUpdateFailed msg) liftIO $ playSong 216 beethoven liftIO $ writeIORef ref (Just e)