2013-10-10 22:02:33 +00:00
|
|
|
{- git-annex assistant fsck configuration
|
|
|
|
-
|
|
|
|
- Copyright 2013 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2013-10-18 15:24:41 +00:00
|
|
|
{-# LANGUAGE QuasiQuotes, TemplateHaskell, OverloadedStrings #-}
|
2013-10-10 22:02:33 +00:00
|
|
|
|
|
|
|
module Assistant.WebApp.Configurators.Fsck where
|
|
|
|
|
2013-10-11 04:45:46 +00:00
|
|
|
import qualified Data.Map as M
|
2013-10-11 06:57:36 +00:00
|
|
|
import qualified Data.Set as S
|
2013-10-11 04:45:46 +00:00
|
|
|
import qualified Data.Text as T
|
|
|
|
|
2013-10-10 22:02:33 +00:00
|
|
|
import Assistant.WebApp.Common
|
2013-10-11 04:45:46 +00:00
|
|
|
import Types.ScheduledActivity
|
|
|
|
import Utility.HumanTime
|
|
|
|
import Utility.Scheduled
|
2013-10-11 06:57:36 +00:00
|
|
|
import Logs.Schedule
|
|
|
|
import Annex.UUID
|
2013-10-11 21:25:32 +00:00
|
|
|
import qualified Remote
|
|
|
|
import Assistant.DaemonStatus
|
2013-10-14 20:24:13 +00:00
|
|
|
import qualified Annex.Branch
|
2013-10-29 20:48:06 +00:00
|
|
|
import Assistant.Fsck
|
|
|
|
import Config
|
|
|
|
import Git.Config
|
|
|
|
import qualified Annex
|
2013-10-11 04:45:46 +00:00
|
|
|
|
2013-10-11 06:57:36 +00:00
|
|
|
{- This adds a form to the page. It does not handle posting of the form,
|
|
|
|
- because unlike a typical yesod form that posts using the same url
|
|
|
|
- that generated it, this form posts using one of two other routes. -}
|
2013-10-11 21:25:32 +00:00
|
|
|
showFsckForm :: Bool -> ScheduledActivity -> Widget
|
|
|
|
showFsckForm new activity = do
|
2013-10-11 06:57:36 +00:00
|
|
|
u <- liftAnnex getUUID
|
|
|
|
let action = if new
|
|
|
|
then AddActivityR u
|
|
|
|
else ChangeActivityR u activity
|
2013-10-11 21:25:32 +00:00
|
|
|
((res, form), enctype) <- liftH $ runFsckForm new activity
|
2013-10-11 06:57:36 +00:00
|
|
|
case res of
|
|
|
|
FormSuccess _ -> noop
|
|
|
|
_ -> $(widgetFile "configurators/fsck/form")
|
|
|
|
|
|
|
|
{- This does not display a form, but it does get it from a post, and run
|
|
|
|
- some Annex action on it. -}
|
|
|
|
withFsckForm :: (ScheduledActivity -> Annex ()) -> Handler ()
|
|
|
|
withFsckForm a = do
|
2013-10-29 20:48:06 +00:00
|
|
|
((res, _form), _enctype) <- runFsckForm False $ defaultFsck Nothing
|
2013-10-11 06:57:36 +00:00
|
|
|
case res of
|
|
|
|
FormSuccess activity -> liftAnnex $ a activity
|
|
|
|
_ -> noop
|
|
|
|
|
2013-10-11 21:25:32 +00:00
|
|
|
mkFsck :: UUID -> UUID -> Schedule -> Duration -> ScheduledActivity
|
|
|
|
mkFsck hereu u s d
|
|
|
|
| u == hereu = ScheduledSelfFsck s d
|
|
|
|
| otherwise = ScheduledRemoteFsck u s d
|
|
|
|
|
|
|
|
runFsckForm :: Bool -> ScheduledActivity -> Handler ((FormResult ScheduledActivity, Widget), Enctype)
|
|
|
|
runFsckForm new activity = case activity of
|
|
|
|
ScheduledSelfFsck s d -> go s d =<< liftAnnex getUUID
|
|
|
|
ScheduledRemoteFsck ru s d -> go s d ru
|
2013-10-11 04:45:46 +00:00
|
|
|
where
|
2013-10-11 21:25:32 +00:00
|
|
|
go (Schedule r t) d ru = do
|
|
|
|
u <- liftAnnex getUUID
|
|
|
|
repolist <- liftAssistant (getrepolist ru)
|
2013-10-14 16:18:04 +00:00
|
|
|
runFormPostNoToken $ \msg -> do
|
2013-10-11 21:25:32 +00:00
|
|
|
(reposRes, reposView) <- mreq (selectFieldList repolist) "" (Just ru)
|
|
|
|
(durationRes, durationView) <- mreq intField "" (Just $ durationSeconds d `quot` 60 )
|
|
|
|
(timeRes, timeView) <- mreq (selectFieldList times) "" (Just t)
|
|
|
|
(recurranceRes, recurranceView) <- mreq (selectFieldList recurrances) "" (Just r)
|
|
|
|
let form = do
|
|
|
|
webAppFormAuthToken
|
|
|
|
$(widgetFile "configurators/fsck/formcontent")
|
|
|
|
let formresult = mkFsck
|
|
|
|
<$> pure u
|
|
|
|
<*> reposRes
|
|
|
|
<*> (Schedule <$> recurranceRes <*> timeRes)
|
|
|
|
<*> (Duration <$> ((60 *) <$> durationRes))
|
|
|
|
return (formresult, form)
|
|
|
|
where
|
|
|
|
times :: [(Text, ScheduledTime)]
|
|
|
|
times = ensurevalue t (T.pack $ fromScheduledTime t) $
|
|
|
|
map (\x -> (T.pack $ fromScheduledTime x, x)) $
|
|
|
|
AnyTime : map (\h -> SpecificTime h 0) [0..23]
|
|
|
|
recurrances :: [(Text, Recurrance)]
|
|
|
|
recurrances = ensurevalue r (T.pack $ fromRecurrance r) $
|
|
|
|
[ ("every day", Daily)
|
2013-10-15 17:05:41 +00:00
|
|
|
, ("every Sunday", Weekly $ Just 1)
|
|
|
|
, ("every Monday", Weekly $ Just 2)
|
|
|
|
, ("every Tuesday", Weekly $ Just 3)
|
|
|
|
, ("every Wednesday", Weekly $ Just 4)
|
|
|
|
, ("every Thursday", Weekly $ Just 5)
|
|
|
|
, ("every Friday", Weekly $ Just 6)
|
|
|
|
, ("every Saturday", Weekly $ Just 7)
|
|
|
|
, ("monthly", Monthly Nothing)
|
|
|
|
, ("twice a month", Divisible 2 (Weekly Nothing))
|
|
|
|
, ("yearly", Yearly Nothing)
|
|
|
|
, ("twice a year", Divisible 6 (Monthly Nothing))
|
|
|
|
, ("quarterly", Divisible 4 (Monthly Nothing))
|
2013-10-11 21:25:32 +00:00
|
|
|
]
|
2013-10-11 04:45:46 +00:00
|
|
|
ensurevalue v desc l = case M.lookup v (M.fromList $ map (\(x,y) -> (y,x)) l) of
|
|
|
|
Just _ -> l
|
|
|
|
Nothing -> (desc, v) : l
|
2013-10-11 21:25:32 +00:00
|
|
|
getrepolist :: UUID -> Assistant [(Text, UUID)]
|
|
|
|
getrepolist ensureu = do
|
|
|
|
-- It is possible to have fsck jobs for remotes that
|
|
|
|
-- do not implement remoteFsck, but it's not too useful,
|
|
|
|
-- so omit them from the UI normally.
|
|
|
|
remotes <- filter (\r -> Remote.uuid r == ensureu || isJust (Remote.remoteFsck r)) . syncRemotes
|
|
|
|
<$> getDaemonStatus
|
|
|
|
u <- liftAnnex getUUID
|
|
|
|
let us = u : (map Remote.uuid remotes)
|
|
|
|
liftAnnex $
|
|
|
|
zip <$> (map T.pack <$> Remote.prettyListUUIDs us) <*> pure us
|
2013-10-11 04:45:46 +00:00
|
|
|
|
2013-10-29 20:48:06 +00:00
|
|
|
defaultFsck :: Maybe Remote -> ScheduledActivity
|
|
|
|
defaultFsck Nothing = ScheduledSelfFsck (Schedule Daily AnyTime) (Duration $ 60*60)
|
|
|
|
defaultFsck (Just r) = ScheduledRemoteFsck (Remote.uuid r) (Schedule Daily AnyTime) (Duration $ 60*60)
|
2013-10-10 22:02:33 +00:00
|
|
|
|
2013-10-14 20:05:10 +00:00
|
|
|
showFsckStatus :: ScheduledActivity -> Widget
|
|
|
|
showFsckStatus activity = do
|
|
|
|
m <- liftAnnex getLastRunTimes
|
|
|
|
let lastrun = M.lookup activity m
|
|
|
|
$(widgetFile "configurators/fsck/status")
|
|
|
|
|
2013-10-10 22:02:33 +00:00
|
|
|
getConfigFsckR :: Handler Html
|
2013-10-11 04:45:46 +00:00
|
|
|
getConfigFsckR = postConfigFsckR
|
|
|
|
postConfigFsckR :: Handler Html
|
|
|
|
postConfigFsckR = page "Consistency checks" (Just Configuration) $ do
|
2013-10-29 20:48:06 +00:00
|
|
|
scheduledchecks <- liftAnnex $
|
|
|
|
S.toList <$> (scheduleGet =<< getUUID)
|
|
|
|
rs <- liftAssistant $
|
|
|
|
filter fsckableRemote . syncRemotes <$> getDaemonStatus
|
|
|
|
recommendedchecks <- liftAnnex $ map defaultFsck
|
|
|
|
<$> filterM (not <$$> checkFscked) (Nothing : map Just rs)
|
2013-10-11 06:57:36 +00:00
|
|
|
$(widgetFile "configurators/fsck")
|
|
|
|
|
2013-10-14 20:24:13 +00:00
|
|
|
changeSchedule :: Handler () -> Handler Html
|
|
|
|
changeSchedule a = do
|
|
|
|
a
|
|
|
|
liftAnnex $ Annex.Branch.commit "update"
|
|
|
|
redirect ConfigFsckR
|
|
|
|
|
2013-10-11 06:57:36 +00:00
|
|
|
getRemoveActivityR :: UUID -> ScheduledActivity -> Handler Html
|
2013-10-14 20:24:13 +00:00
|
|
|
getRemoveActivityR u activity = changeSchedule $
|
2013-10-11 06:57:36 +00:00
|
|
|
liftAnnex $ scheduleRemove u activity
|
|
|
|
|
|
|
|
getAddActivityR :: UUID -> Handler Html
|
|
|
|
getAddActivityR = postAddActivityR
|
|
|
|
postAddActivityR :: UUID -> Handler Html
|
2013-10-14 20:24:13 +00:00
|
|
|
postAddActivityR u = changeSchedule $
|
2013-10-11 06:57:36 +00:00
|
|
|
withFsckForm $ scheduleAdd u
|
|
|
|
|
|
|
|
getChangeActivityR :: UUID -> ScheduledActivity -> Handler Html
|
|
|
|
getChangeActivityR = postChangeActivityR
|
|
|
|
postChangeActivityR :: UUID -> ScheduledActivity -> Handler Html
|
2013-10-14 20:24:13 +00:00
|
|
|
postChangeActivityR u oldactivity = changeSchedule $
|
2013-10-11 06:57:36 +00:00
|
|
|
withFsckForm $ \newactivity -> scheduleChange u $
|
|
|
|
S.insert newactivity . S.delete oldactivity
|
2013-10-29 20:48:06 +00:00
|
|
|
|
|
|
|
data FsckPreferences = FsckPreferences
|
|
|
|
{ enableFsckNudge :: Bool
|
|
|
|
}
|
|
|
|
|
|
|
|
getFsckPreferences :: Annex FsckPreferences
|
|
|
|
getFsckPreferences = FsckPreferences
|
|
|
|
<$> (annexFsckNudge <$> Annex.getGitConfig)
|
|
|
|
|
|
|
|
fsckPreferencesAForm :: FsckPreferences -> MkAForm FsckPreferences
|
|
|
|
fsckPreferencesAForm def = FsckPreferences
|
|
|
|
<$> areq (checkBoxField `withNote` nudgenote) "Reminders" (Just $ enableFsckNudge def)
|
|
|
|
where
|
|
|
|
nudgenote = [whamlet|Remind me when using repositories that lack consistency checks.|]
|
|
|
|
|
|
|
|
runFsckPreferencesForm :: Handler ((FormResult FsckPreferences, Widget), Enctype)
|
|
|
|
runFsckPreferencesForm = do
|
|
|
|
prefs <- liftAnnex getFsckPreferences
|
|
|
|
runFormPostNoToken $ renderBootstrap $ fsckPreferencesAForm prefs
|
|
|
|
|
|
|
|
showFsckPreferencesForm :: Widget
|
|
|
|
showFsckPreferencesForm = do
|
|
|
|
((res, form), enctype) <- liftH $ runFsckPreferencesForm
|
|
|
|
case res of
|
|
|
|
FormSuccess _ -> noop
|
|
|
|
_ -> $(widgetFile "configurators/fsck/preferencesform")
|
|
|
|
|
|
|
|
postConfigFsckPreferencesR :: Handler Html
|
|
|
|
postConfigFsckPreferencesR = do
|
|
|
|
((res, _form), _enctype) <- runFsckPreferencesForm
|
|
|
|
case res of
|
|
|
|
FormSuccess prefs ->
|
|
|
|
liftAnnex $ setConfig (annexConfig "fscknudge")
|
|
|
|
(boolConfig $ enableFsckNudge prefs)
|
|
|
|
_ -> noop
|
|
|
|
redirect ConfigFsckR
|