2018-07-27 12:34:50 +03:00
|
|
|
module Hasura.Server.CheckUpdates
|
|
|
|
( checkForUpdates
|
|
|
|
) where
|
|
|
|
|
|
|
|
import Control.Exception (try)
|
|
|
|
import Control.Lens
|
2020-01-23 00:55:55 +03:00
|
|
|
import Data.Text.Conversions (toText)
|
2018-07-27 12:34:50 +03:00
|
|
|
|
2019-06-26 09:23:40 +03:00
|
|
|
import qualified CI
|
2020-01-16 04:56:57 +03:00
|
|
|
import qualified Control.Concurrent.Extended as C
|
|
|
|
import qualified Data.Aeson as A
|
|
|
|
import qualified Data.Aeson.Casing as A
|
|
|
|
import qualified Data.Aeson.TH as A
|
|
|
|
import qualified Data.Text as T
|
|
|
|
import qualified Network.HTTP.Client as H
|
|
|
|
import qualified Network.URI.Encode as URI
|
|
|
|
import qualified Network.Wreq as Wreq
|
|
|
|
import qualified System.Log.FastLogger as FL
|
2018-07-27 12:34:50 +03:00
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
import Hasura.HTTP
|
2018-07-27 12:34:50 +03:00
|
|
|
import Hasura.Logging (LoggerCtx (..))
|
|
|
|
import Hasura.Prelude
|
2020-01-23 00:55:55 +03:00
|
|
|
import Hasura.Server.Version (HasVersion, Version, currentVersion)
|
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
|
|
|
|
|
|
|
$(A.deriveJSON (A.aesonDrop 2 A.snakeCase) ''UpdateInfo)
|
|
|
|
|
2020-10-28 19:40:33 +03:00
|
|
|
checkForUpdates :: HasVersion => LoggerCtx a -> H.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"
|
|
|
|
Just ci -> "server-" <> (T.toLower . T.pack $ show 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
|
|
|
|
decodeResp bs = case A.eitherDecode bs of
|
|
|
|
Left _ -> return $ UpdateInfo currentVersion
|
|
|
|
Right a -> return a
|
|
|
|
|
|
|
|
ignoreHttpErr :: H.HttpException -> IO ()
|
|
|
|
ignoreHttpErr _ = return ()
|