2022-03-16 03:39:21 +03:00
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
|
|
|
|
2018-07-27 12:34:50 +03:00
|
|
|
module Hasura.Server.CheckUpdates
|
2021-09-24 01:56:37 +03:00
|
|
|
( checkForUpdates,
|
|
|
|
)
|
|
|
|
where
|
2018-07-27 12:34:50 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
import CI qualified
|
|
|
|
import Control.Concurrent.Extended qualified as C
|
|
|
|
import Control.Exception (try)
|
|
|
|
import Control.Lens
|
|
|
|
import Data.Aeson qualified as A
|
|
|
|
import Data.Aeson.Casing qualified as A
|
|
|
|
import Data.Aeson.TH qualified as A
|
|
|
|
import Data.Either (fromRight)
|
|
|
|
import Data.Text qualified as T
|
|
|
|
import Data.Text.Conversions (toText)
|
|
|
|
import Hasura.HTTP
|
|
|
|
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
|
2021-09-24 01:56:37 +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
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
newtype UpdateInfo = UpdateInfo
|
2020-01-23 00:55:55 +03:00
|
|
|
{ _uiLatest :: Version
|
2021-09-24 01:56:37 +03:00
|
|
|
}
|
|
|
|
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
|
2021-09-24 01:56:37 +03:00
|
|
|
let buildUrl agent =
|
|
|
|
"https://releases.hasura.io/graphql-engine?agent="
|
|
|
|
<> 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 ()
|