2022-03-16 03:39:21 +03:00
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
|
|
|
|
2018-07-27 12:34:50 +03:00
|
|
|
module Hasura.Server.CheckUpdates
|
|
|
|
( checkForUpdates,
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
2019-06-26 09:23:40 +03:00
|
|
|
import CI qualified
|
2020-01-16 04:56:57 +03:00
|
|
|
import Control.Concurrent.Extended qualified as C
|
2021-01-19 22:14:42 +03:00
|
|
|
import Control.Exception (try)
|
|
|
|
import Control.Lens
|
2020-01-16 04:56:57 +03:00
|
|
|
import Data.Aeson qualified as A
|
|
|
|
import Data.Aeson.Casing qualified as A
|
|
|
|
import Data.Aeson.TH qualified as A
|
2021-01-19 22:14:42 +03:00
|
|
|
import Data.Either (fromRight)
|
2020-01-16 04:56:57 +03:00
|
|
|
import Data.Text qualified as T
|
2021-01-19 22:14:42 +03:00
|
|
|
import Data.Text.Conversions (toText)
|
2018-12-13 10:26:15 +03:00
|
|
|
import Hasura.HTTP
|
2021-01-19 22:14:42 +03:00
|
|
|
import Hasura.Logging (LoggerCtx (..))
|
|
|
|
import Hasura.Prelude
|
2021-10-13 19:38:56 +03:00
|
|
|
import Hasura.Server.Version (Version, currentVersion)
|
2022-02-16 10:08:51 +03:00
|
|
|
import Network.HTTP.Client qualified as HTTP
|
2020-01-16 04:56:57 +03:00
|
|
|
import Network.URI.Encode qualified as URI
|
|
|
|
import Network.Wreq qualified as Wreq
|
|
|
|
import System.Log.FastLogger qualified as FL
|
2018-07-27 12:34:50 +03:00
|
|
|
|
|
|
|
newtype UpdateInfo = UpdateInfo
|
2020-01-23 00:55:55 +03:00
|
|
|
{ _uiLatest :: Version
|
|
|
|
}
|
|
|
|
deriving (Show)
|
2018-07-27 12:34:50 +03:00
|
|
|
|
2021-01-19 22:14:42 +03:00
|
|
|
-- note that this is erroneous and should drop three characters or use
|
|
|
|
-- aesonPrefix, but needs to remain like this for backwards compatibility
|
2018-07-27 12:34:50 +03:00
|
|
|
$(A.deriveJSON (A.aesonDrop 2 A.snakeCase) ''UpdateInfo)
|
|
|
|
|
2022-02-16 10:08:51 +03:00
|
|
|
checkForUpdates :: LoggerCtx a -> HTTP.Manager -> IO void
|
2019-07-11 08:37:06 +03:00
|
|
|
checkForUpdates (LoggerCtx loggerSet _ _ _) manager = do
|
2018-11-23 16:02:46 +03:00
|
|
|
let options = wreqOptions manager []
|
2018-09-12 14:03:36 +03:00
|
|
|
url <- getUrl
|
2018-07-27 12:34:50 +03:00
|
|
|
forever $ do
|
|
|
|
resp <- try $ Wreq.getWith options $ T.unpack url
|
|
|
|
case resp of
|
|
|
|
Left ex -> ignoreHttpErr ex
|
|
|
|
Right bs -> do
|
|
|
|
UpdateInfo latestVersion <- decodeResp $ bs ^. Wreq.responseBody
|
|
|
|
when (latestVersion /= currentVersion) $
|
|
|
|
FL.pushLogStrLn loggerSet $
|
|
|
|
FL.toLogStr $
|
|
|
|
updateMsg latestVersion
|
|
|
|
|
2020-01-16 04:56:57 +03:00
|
|
|
C.sleep $ days 1
|
2018-07-27 12:34:50 +03:00
|
|
|
where
|
2020-01-23 00:55:55 +03:00
|
|
|
updateMsg v = "Update: A new version is available: " <> toText v
|
2018-09-12 14:03:36 +03:00
|
|
|
getUrl = do
|
2019-06-26 09:23:40 +03:00
|
|
|
let buildUrl agent =
|
|
|
|
"https://releases.hasura.io/graphql-engine?agent="
|
2020-01-23 00:55:55 +03:00
|
|
|
<> agent
|
|
|
|
<> "&version="
|
|
|
|
<> URI.encodeText (toText currentVersion)
|
2019-06-26 09:23:40 +03:00
|
|
|
ciM <- CI.getCI
|
|
|
|
return . buildUrl $ case ciM of
|
|
|
|
Nothing -> "server"
|
2021-01-19 22:14:42 +03:00
|
|
|
Just ci -> "server-" <> T.toLower (tshow ci)
|
2018-09-12 14:03:36 +03:00
|
|
|
|
2018-07-27 12:34:50 +03:00
|
|
|
-- ignoring if there is any error in response and returning the current version
|
2021-01-19 22:14:42 +03:00
|
|
|
decodeResp = pure . fromRight (UpdateInfo currentVersion) . A.eitherDecode
|
2018-07-27 12:34:50 +03:00
|
|
|
|
2022-02-16 10:08:51 +03:00
|
|
|
ignoreHttpErr :: HTTP.HttpException -> IO ()
|
2018-07-27 12:34:50 +03:00
|
|
|
ignoreHttpErr _ = return ()
|