git.iko.soy/typeable/octopod
on master help sign in
Improved `check` and `notification` control script arguments (#34)

* Corrected control script arguments * Updated documentation * added project name and base domain to check script * split the checking and archive checking commands * checkArchiveArgs -> archiveCheckArgs

commit2ef6b48c3a85aeda8a810ea966db36f8cd895eed authoriko <ilyakooo0@gmail.com> dateTue 9 Feb 2021, 20:02 (6 years ago) parentf68291d
6 files changed+103−43
docs/en/Control_scripts.md+10 docs/ru/Control_scripts.md+10 octopod-backend/src/Database/PostgreSQL/Simple/Instances.hs−10 octopod-backend/src/Octopod/Server.hs+24−29 octopod-backend/src/Octopod/Server/ControlScriptUtils.hs+49−4 octopod-common/src/Common/Types.hs+10
docs/en/Control_scripts.md+10
@@ -312,6 +312,16 @@ This script receives the following additional command-line arguments as input:
312312- `--old-status` – The previous status the deployment was in.
313313- `--new-status` – The new status the deployment transitioned to.
314314
315+The last two arguments can have one of the following values:
316+- `Running`
317+- `GenericFailure`
318+- `TagMismatch`
319+- `PartialAvailability`
320+- `CreatePending`
321+- `UpdatePending`
322+- `ArchivePending`
323+- `Archived`
324+
315325#### Execution example
316326
317327The script might be called something like this:
docs/ru/Control_scripts.md+10
@@ -228,3 +228,13 @@ echo "api,https://api.${name}.example.com"
228228* `--tag` – тег развертывания
229229- `--old-status` – предыдущий статус
230230- `--new-status` – новый статус
231+
232+Последние два аргумент могут иметь одно из следующих значений:
233+- `Running`
234+- `GenericFailure`
235+- `TagMismatch`
236+- `PartialAvailability`
237+- `CreatePending`
238+- `UpdatePending`
239+- `ArchivePending`
240+- `Archived`
octopod-backend/src/Database/PostgreSQL/Simple/Instances.hs−10
@@ -13,16 +13,6 @@ import Database.PostgreSQL.Simple.FromField
1313import Database.PostgreSQL.Simple.ToField
1414import qualified Data.Text.Encoding as T
1515
16−deploymentStatusText :: DeploymentStatus -> Text
17−deploymentStatusText Running = "Running"
18−deploymentStatusText (Failure GenericFailure) = "GenericFailure"
19−deploymentStatusText (Failure TagMismatch) = "TagMismatch"
20−deploymentStatusText (Failure PartialAvailability) = "PartialAvailability"
21−deploymentStatusText CreatePending = "CreatePending"
22−deploymentStatusText UpdatePending = "UpdatePending"
23−deploymentStatusText ArchivePending = "ArchivePending"
24−deploymentStatusText Archived = "Archived"
25−
2616instance ToField DeploymentStatus where
2717 toField = toField @Text . deploymentStatusText
2818
octopod-backend/src/Octopod/Server.hs+24−29
@@ -557,6 +557,13 @@ transitionErrorToServerError = \case
557557 where
558558 show'' = BSLC.pack . show
559559
560+getDeploymentS
561+ :: (MonadReader AppState m, MonadBase IO m, MonadError ServerError m)
562+ => Connection -> DeploymentName -> m DeploymentFullInfo
563+getDeploymentS conn dName = (getFullInfo' conn (FullInfoOnlyForOne dName)) >>= \case
564+ [x] -> return x
565+ _ -> throwError err404 {errBody = "Deployment not found."}
566+
560567transitionToStatus
561568 :: (MonadError ServerError m, MonadReader AppState m, MonadBaseControl IO m)
562569 => DeploymentName -> DeploymentStatusTransition -> ExceptT StatusTransitionError m ()
@@ -564,10 +571,8 @@ transitionToStatus dName s = do
564571 p <- asks pool
565572 st <- ask
566573 let log = liftBase . logInfo (logger st)
567− (oldS, newS) <- withResource p $ \conn -> liftBaseOp_ (withTransaction conn) $ do
568− dep <- lift (getFullInfo' conn (FullInfoOnlyForOne dName)) >>= \case
569− [x] -> return x
570− _ -> throwError $ DeploymentNotFound dName
574+ (oldS, newS, dep :: DeploymentFullInfo) <- withResource p $ \conn -> liftBaseOp_ (withTransaction conn) $ do
575+ dep <- lift $ getDeploymentS conn dName
571576 let
572577 oldS = recordedStatus $ dep ^. #status
573578 newS = transitionStatus s
@@ -589,10 +594,11 @@ transitionToStatus dName s = do
589594 (output ^. #duration)
590595 (output ^. #stdout)
591596 (output ^. #stderr)
592− return (oldS, newS)
597+ return (oldS, newS, dep)
593598 notificationCmd <- asks notificationCommand
594599 forM_ notificationCmd $ \nCmd ->
595− runBgWorker . void $ runCommandArgs' nCmd =<< notificationCommandArgs dName oldS newS
600+ runBgWorker . void $ runCommandArgs' nCmd
601+ =<< notificationCommandArgs dName (dep ^. #deployment . #tag) oldS newS
596602 liftBase $ sendReloadEvent st
597603
598604assertStatusTransitionPossible
@@ -915,15 +921,9 @@ projectNameH = projectName <$> ask
915921-- | Handles the 'status' request.
916922statusH :: DeploymentName -> AppM CurrentDeploymentStatus
917923statusH dName = do
918− st <- ask
919− let
920− log = logInfo (logger st)
921− cmd = checkingCommand st
922− args =
923− [ "--namespace", coerce $ namespace st
924− , "--name", coerce $ dName ]
925− liftIO $ log $ "call " <> unwords (coerce cmd : args)
926− ec <- liftIO $ runCommandWithoutPipes (unpack $ coerce cmd) (unpack <$> args)
924+ pgPool <- asks pool
925+ dep <- withResource pgPool $ \conn -> getDeploymentS conn dName
926+ (ec, _, _) <- runCommandArgs checkingCommand =<< checkCommandArgs dName (dep ^. #deployment . #tag)
927927 pure . CurrentDeploymentStatus $
928928 case ec of
929929 ExitSuccess -> Ok
@@ -1210,6 +1210,7 @@ runStatusUpdater state = do
12101210 "SELECT name, status::text, \
12111211 \extract(epoch from now())::int - \
12121212 \extract(epoch from status_updated_at)::int \
1213+ \, tag \
12131214 \FROM deployments \
12141215 \WHERE checked_at < now() - interval '?' second AND status != 'Archived'"
12151216 updateCheckedAt =
@@ -1218,22 +1219,16 @@ runStatusUpdater state = do
12181219 logErr = logWarning (logger state)
12191220
12201221 forever $ do
1221− rows :: [(DeploymentName, DeploymentStatus, Int)] <- liftIO $
1222+ rows :: [(DeploymentName, DeploymentStatus, Int, DeploymentTag)] <- liftIO $
12221223 withResource pgPool $ \conn -> query conn selectDeps (Only interval)
12231224 let
1224− checkList :: [(DeploymentName, DeploymentStatus, Timestamp)] =
1225− (\(n, s, t) -> (n, s, coerce t)) <$> rows
1226− checkResult <- for checkList $ \(dName, dStatus, ts) -> do
1227− let
1228− args =
1229− [ "--project-name", unpack . coerce $ projectName state
1230− , "--base-domain", unpack . coerce $ baseDomain state
1231− , "--namespace", unpack . coerce $ namespace state
1232− , "--name", unpack . coerce $ dName ]
1233− cmd ArchivePending = unpack . coerce $ archiveCheckingCommand state
1234− cmd _ = unpack . coerce $ checkingCommand state
1235− timeout = statusUpdateTimeout state
1236− ec <- runCommandWithoutPipes (cmd dStatus) args
1225+ checkList :: [(DeploymentName, DeploymentStatus, Timestamp, DeploymentTag)] =
1226+ (\(n, s, t, dTag) -> (n, s, coerce t, dTag)) <$> rows
1227+ checkResult <- for checkList $ \(dName, dStatus, ts, dTag) -> do
1228+ let timeout = statusUpdateTimeout state
1229+ (ec, _, _) <- flip runReaderT state case dStatus of
1230+ ArchivePending -> runCommandArgs archiveCheckingCommand =<< archiveCheckArgs dName
1231+ _ -> runCommandArgs checkingCommand =<< checkCommandArgs dName dTag
12371232 pure (dName, statusTransition ec dStatus ts timeout, dStatus)
12381233 updated <-
12391234 for checkResult $ \(dName, transitionM, dStatus) ->
octopod-backend/src/Octopod/Server/ControlScriptUtils.hs+49−4
@@ -13,6 +13,8 @@ module Octopod.Server.ControlScriptUtils
1313 , runCommandWithoutPipes
1414 , runCommandArgs
1515 , runCommandArgs'
16+ , checkCommandArgs
17+ , archiveCheckArgs
1618 ) where
1719
1820
@@ -52,12 +54,13 @@ notificationCommandArgs
5254 , HasType Domain r
5355 )
5456 => DeploymentName
57+ -> DeploymentTag
5558 -> DeploymentStatus
5659 -- ^ Previous status
5760 -> DeploymentStatus
5861 -- ^ New status
5962 -> m ControlScriptArgs
60−notificationCommandArgs dName old new = do
63+notificationCommandArgs dName dTag old new = do
6164 (Namespace namespace) <- asks getTyped
6265 (ProjectName projectName) <- asks getTyped
6366 (Domain domain) <- asks getTyped
@@ -66,9 +69,51 @@ notificationCommandArgs dName old new = do
6669 , "--base-domain", T.unpack domain
6770 , "--namespace", T.unpack namespace
6871 , "--name", T.unpack . coerce $ dName
69− , "--tag", T.unpack . coerce $ dName
70− , "--old-status", show old
71− , "--new-status", show new
72+ , "--tag", T.unpack . coerce $ dTag
73+ , "--old-status", T.unpack $ deploymentStatusText old
74+ , "--new-status", T.unpack $ deploymentStatusText new
75+ ]
76+
77+checkCommandArgs
78+ ::
79+ ( MonadReader r m
80+ , HasType Namespace r
81+ , HasType ProjectName r
82+ , HasType Domain r
83+ )
84+ => DeploymentName
85+ -> DeploymentTag
86+ -> m ControlScriptArgs
87+checkCommandArgs dName dTag = do
88+ (ProjectName projectName) <- asks getTyped
89+ (Domain domain) <- asks getTyped
90+ (Namespace namespace) <- asks getTyped
91+ return $ ControlScriptArgs
92+ [ "--namespace", T.unpack namespace
93+ , "--name", T.unpack . coerce $ dName
94+ , "--tag", T.unpack . unDeploymentTag $ dTag
95+ , "--project-name", T.unpack projectName
96+ , "--base-domain", T.unpack domain
97+ ]
98+
99+archiveCheckArgs
100+ ::
101+ ( MonadReader r m
102+ , HasType Namespace r
103+ , HasType ProjectName r
104+ , HasType Domain r
105+ )
106+ => DeploymentName
107+ -> m ControlScriptArgs
108+archiveCheckArgs dName = do
109+ (ProjectName projectName) <- asks getTyped
110+ (Domain domain) <- asks getTyped
111+ (Namespace namespace) <- asks getTyped
112+ return $ ControlScriptArgs
113+ [ "--project-name", T.unpack projectName
114+ , "--base-domain", T.unpack domain
115+ , "--namespace", T.unpack namespace
116+ , "--name", T.unpack . coerce $ dName
72117 ]
73118
74119runCommandArgs
octopod-common/src/Common/Types.hs+10
@@ -85,6 +85,16 @@ newtype Timestamp = Timestamp { unTimestamp :: Int }
8585newtype ProjectName = ProjectName { uProjectName :: Text }
8686 deriving (Show, FromJSON, ToJSON)
8787
88+deploymentStatusText :: DeploymentStatus -> Text
89+deploymentStatusText Running = "Running"
90+deploymentStatusText (Failure GenericFailure) = "GenericFailure"
91+deploymentStatusText (Failure TagMismatch) = "TagMismatch"
92+deploymentStatusText (Failure PartialAvailability) = "PartialAvailability"
93+deploymentStatusText CreatePending = "CreatePending"
94+deploymentStatusText UpdatePending = "UpdatePending"
95+deploymentStatusText ArchivePending = "ArchivePending"
96+deploymentStatusText Archived = "Archived"
97+
8898data DeploymentStatus
8999 = Running
90100 | Failure FailureType