mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-30 20:14:49 +00:00
agent: rip versionLatest
This commit is contained in:
committed by
Aiden McClelland
parent
ebd4cb8480
commit
b717853759
@@ -31,15 +31,13 @@ data ServerRes = ServerRes
|
|||||||
, serverStatus :: Maybe AppStatus
|
, serverStatus :: Maybe AppStatus
|
||||||
, serverStatusAt :: UTCTime
|
, serverStatusAt :: UTCTime
|
||||||
, serverVersionInstalled :: Version
|
, serverVersionInstalled :: Version
|
||||||
, serverVersionLatest :: Maybe Version
|
, serverNotifications :: [ Entity Notification ]
|
||||||
, serverNotifications :: [Entity Notification]
|
|
||||||
, serverWifi :: WifiList
|
, serverWifi :: WifiList
|
||||||
, serverSsh :: [SshKeyFingerprint]
|
, serverSsh :: [ SshKeyFingerprint ]
|
||||||
, serverAlternativeRegistryUrl :: Maybe Text
|
, serverAlternativeRegistryUrl :: Maybe Text
|
||||||
, serverSpecs :: SpecsRes
|
, serverSpecs :: SpecsRes
|
||||||
, serverWelcomeAck :: Bool
|
, serverWelcomeAck :: Bool
|
||||||
}
|
} deriving (Eq, Show)
|
||||||
deriving (Eq, Show)
|
|
||||||
|
|
||||||
type JsonEncoding a = Encoding
|
type JsonEncoding a = Encoding
|
||||||
jsonEncode :: (Monad m, ToJSON a) => a -> m (JsonEncoding a)
|
jsonEncode :: (Monad m, ToJSON a) => a -> m (JsonEncoding a)
|
||||||
@@ -53,7 +51,6 @@ instance ToJSON ServerRes where
|
|||||||
Nothing -> String "UPDATING"
|
Nothing -> String "UPDATING"
|
||||||
Just stat -> toJSON stat
|
Just stat -> toJSON stat
|
||||||
, "versionInstalled" .= serverVersionInstalled
|
, "versionInstalled" .= serverVersionInstalled
|
||||||
, "versionLatest" .= serverVersionLatest
|
|
||||||
, "notifications" .= serverNotifications
|
, "notifications" .= serverNotifications
|
||||||
, "wifi" .= serverWifi
|
, "wifi" .= serverWifi
|
||||||
, "ssh" .= serverSsh
|
, "ssh" .= serverSsh
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ module Handler.V0 where
|
|||||||
|
|
||||||
import Startlude hiding ( runReader )
|
import Startlude hiding ( runReader )
|
||||||
|
|
||||||
import Control.Carrier.Error.Either
|
|
||||||
import Control.Carrier.Lift ( runM )
|
import Control.Carrier.Lift ( runM )
|
||||||
import Data.Aeson
|
import Data.Aeson
|
||||||
import Data.IORef
|
import Data.IORef
|
||||||
@@ -21,10 +20,8 @@ import Handler.Types.V0.Specs
|
|||||||
import Handler.Types.V0.Ssh
|
import Handler.Types.V0.Ssh
|
||||||
import Handler.Types.V0.Base
|
import Handler.Types.V0.Base
|
||||||
import Handler.Types.V0.Wifi
|
import Handler.Types.V0.Wifi
|
||||||
import Lib.Algebra.State.RegistryUrl
|
|
||||||
import Lib.Error
|
import Lib.Error
|
||||||
import Lib.External.Metrics.Df
|
import Lib.External.Metrics.Df
|
||||||
import qualified Lib.External.Registry as Reg
|
|
||||||
import Lib.External.Specs.CPU
|
import Lib.External.Specs.CPU
|
||||||
import Lib.External.Specs.Memory
|
import Lib.External.Specs.Memory
|
||||||
import qualified Lib.External.WpaSupplicant as WpaSupplicant
|
import qualified Lib.External.WpaSupplicant as WpaSupplicant
|
||||||
@@ -61,9 +58,6 @@ getServerR = handleS9ErrT $ do
|
|||||||
wifi <- WpaSupplicant.runWlan0 $ liftA2 WifiList WpaSupplicant.getCurrentNetwork WpaSupplicant.listNetworks
|
wifi <- WpaSupplicant.runWlan0 $ liftA2 WifiList WpaSupplicant.getCurrentNetwork WpaSupplicant.listNetworks
|
||||||
specs <- getSpecs settings
|
specs <- getSpecs settings
|
||||||
welcomeAck <- fmap isJust . lift . runDB . Persist.get $ WelcomeAckKey agentVersion
|
welcomeAck <- fmap isJust . lift . runDB . Persist.get $ WelcomeAckKey agentVersion
|
||||||
versionLatest <- liftIO $ (try @SomeException $ getOsVersionLatest agentCtx) >>= \case
|
|
||||||
Left e -> (putStrLn @Text $ "Error fetching latest OS Version: " <> show e) $> Nothing
|
|
||||||
Right a -> pure a
|
|
||||||
|
|
||||||
let sid = T.drop 7 $ specsNetworkId specs
|
let sid = T.drop 7 $ specsNetworkId specs
|
||||||
|
|
||||||
@@ -78,7 +72,6 @@ getServerR = handleS9ErrT $ do
|
|||||||
, serverAlternativeRegistryUrl = alternativeRegistryUrl
|
, serverAlternativeRegistryUrl = alternativeRegistryUrl
|
||||||
, serverSpecs = specs
|
, serverSpecs = specs
|
||||||
, serverWelcomeAck = welcomeAck
|
, serverWelcomeAck = welcomeAck
|
||||||
, serverVersionLatest = versionLatest
|
|
||||||
}
|
}
|
||||||
where
|
where
|
||||||
parseSshKeys :: Text -> S9ErrT Handler [SshKeyFingerprint]
|
parseSshKeys :: Text -> S9ErrT Handler [SshKeyFingerprint]
|
||||||
@@ -146,35 +139,3 @@ patchNullableFile path = do
|
|||||||
runM $ injectFilesystemBaseFromContext settings $ case mVal of
|
runM $ injectFilesystemBaseFromContext settings $ case mVal of
|
||||||
Just val -> writeSystemPath path $ T.strip val
|
Just val -> writeSystemPath path $ T.strip val
|
||||||
Nothing -> deleteSystemPath path
|
Nothing -> deleteSystemPath path
|
||||||
|
|
||||||
expirationOsVersionLatest :: Num a => a
|
|
||||||
expirationOsVersionLatest = 60
|
|
||||||
|
|
||||||
getOsVersionLatest :: MonadIO m => AgentCtx -> m (Maybe Version)
|
|
||||||
getOsVersionLatest ctx = do
|
|
||||||
now <- liftIO getCurrentTime
|
|
||||||
let osVersionCache = appOsVersionLatest ctx
|
|
||||||
mCache <- liftIO . readIORef $ osVersionCache
|
|
||||||
|
|
||||||
case mCache of
|
|
||||||
Nothing -> repopulateCache ctx
|
|
||||||
Just cache -> if diffUTCTime now (lastChecked cache) >= expirationOsVersionLatest
|
|
||||||
then repopulateCache ctx
|
|
||||||
else pure . Just $ osVersion cache
|
|
||||||
|
|
||||||
repopulateCache :: MonadIO m => AgentCtx -> m (Maybe Version)
|
|
||||||
repopulateCache ctx = do
|
|
||||||
let osVersionCache = appOsVersionLatest ctx
|
|
||||||
let s = appSettings ctx
|
|
||||||
eitherV <- interp s $ Reg.getLatestAgentVersion
|
|
||||||
|
|
||||||
case eitherV of
|
|
||||||
Left error -> do
|
|
||||||
putStrLn $ "Repopulate OS Version Cache exception: " <> (show error :: Text)
|
|
||||||
fmap (fmap osVersion) . liftIO . readIORef $ osVersionCache
|
|
||||||
Right v -> do
|
|
||||||
res <- OsVersionCache v <$> liftIO getCurrentTime
|
|
||||||
liftIO $ writeIORef osVersionCache (Just res)
|
|
||||||
pure . Just $ osVersion res
|
|
||||||
where interp s = liftIO . runError @S9Error . injectFilesystemBaseFromContext s . runRegistryUrlIOC
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user