2015-04-05 16:50:02 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
|
|
|
- Copyright 2015 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.Expire where
|
|
|
|
|
|
|
|
import Command
|
|
|
|
import Logs.Activity
|
|
|
|
import Logs.UUID
|
|
|
|
import Logs.MapLog
|
|
|
|
import Logs.Trust
|
|
|
|
import Annex.UUID
|
2017-08-14 17:55:38 +00:00
|
|
|
import Annex.VectorClock
|
2015-04-05 16:50:02 +00:00
|
|
|
import qualified Remote
|
|
|
|
import Utility.HumanTime
|
|
|
|
|
|
|
|
import Data.Time.Clock.POSIX
|
|
|
|
import qualified Data.Map as M
|
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2015-07-10 20:26:23 +00:00
|
|
|
cmd = command "expire" SectionMaintenance
|
|
|
|
"expire inactive repositories"
|
|
|
|
paramExpire (seek <$$> optParser)
|
2015-04-05 16:50:02 +00:00
|
|
|
|
|
|
|
paramExpire :: String
|
|
|
|
paramExpire = (paramRepeating $ paramOptional paramRemote ++ ":" ++ paramTime)
|
|
|
|
|
2015-07-10 20:26:23 +00:00
|
|
|
data ExpireOptions = ExpireOptions
|
|
|
|
{ expireParams :: CmdParams
|
|
|
|
, activityOption :: Maybe Activity
|
|
|
|
, noActOption :: Bool
|
|
|
|
}
|
2015-04-05 16:50:02 +00:00
|
|
|
|
2015-07-10 20:26:23 +00:00
|
|
|
optParser :: CmdParamsDesc -> Parser ExpireOptions
|
|
|
|
optParser desc = ExpireOptions
|
|
|
|
<$> cmdParams desc
|
|
|
|
<*> optional (option (str >>= parseActivity)
|
|
|
|
( long "activity" <> metavar paramName
|
|
|
|
<> help "specify activity that prevents expiry"
|
|
|
|
))
|
|
|
|
<*> switch
|
|
|
|
( long "no-act"
|
|
|
|
<> help "don't really do anything"
|
|
|
|
)
|
2015-04-05 19:02:55 +00:00
|
|
|
|
2015-07-10 20:26:23 +00:00
|
|
|
seek :: ExpireOptions -> CommandSeek
|
|
|
|
seek o = do
|
|
|
|
expire <- parseExpire (expireParams o)
|
|
|
|
actlog <- lastActivities (activityOption o)
|
2015-04-05 16:50:02 +00:00
|
|
|
u <- getUUID
|
|
|
|
us <- filter (/= u) . M.keys <$> uuidMap
|
|
|
|
descs <- uuidMap
|
2015-07-10 20:26:23 +00:00
|
|
|
seekActions $ pure $ map (start expire (noActOption o) actlog descs) us
|
2015-04-05 16:50:02 +00:00
|
|
|
|
2015-04-05 19:02:55 +00:00
|
|
|
start :: Expire -> Bool -> Log Activity -> M.Map UUID String -> UUID -> CommandStart
|
|
|
|
start (Expire expire) noact actlog descs u =
|
2015-04-05 16:50:02 +00:00
|
|
|
case lastact of
|
|
|
|
Just ent | notexpired ent -> checktrust (== DeadTrusted) $ do
|
2017-11-28 18:40:26 +00:00
|
|
|
showStart' "unexpire" (Just desc)
|
2015-04-05 16:50:02 +00:00
|
|
|
showNote =<< whenactive
|
2015-04-05 19:02:55 +00:00
|
|
|
unless noact $
|
|
|
|
trustSet u SemiTrusted
|
2015-04-05 16:50:02 +00:00
|
|
|
_ -> checktrust (/= DeadTrusted) $ do
|
2017-11-28 18:40:26 +00:00
|
|
|
showStart' "expire" (Just desc)
|
2015-04-05 16:50:02 +00:00
|
|
|
showNote =<< whenactive
|
2015-04-05 19:02:55 +00:00
|
|
|
unless noact $
|
|
|
|
trustSet u DeadTrusted
|
2015-04-05 16:50:02 +00:00
|
|
|
where
|
|
|
|
lastact = changed <$> M.lookup u actlog
|
|
|
|
whenactive = case lastact of
|
2017-08-14 17:55:38 +00:00
|
|
|
Just (VectorClock c) -> do
|
|
|
|
d <- liftIO $ durationSince $ posixSecondsToUTCTime c
|
2015-04-05 16:50:02 +00:00
|
|
|
return $ "last active: " ++ fromDuration d ++ " ago"
|
|
|
|
_ -> return "no activity"
|
|
|
|
desc = fromUUID u ++ " " ++ fromMaybe "" (M.lookup u descs)
|
|
|
|
notexpired ent = case ent of
|
|
|
|
Unknown -> False
|
2017-08-14 17:55:38 +00:00
|
|
|
VectorClock c -> case lookupexpire of
|
|
|
|
Just (Just expiretime) -> c >= expiretime
|
2015-04-05 16:50:02 +00:00
|
|
|
_ -> True
|
|
|
|
lookupexpire = headMaybe $ catMaybes $
|
|
|
|
map (`M.lookup` expire) [Just u, Nothing]
|
|
|
|
checktrust want a = ifM (want <$> lookupTrust u)
|
|
|
|
( do
|
|
|
|
void a
|
|
|
|
next $ next $ return True
|
|
|
|
, stop
|
|
|
|
)
|
|
|
|
|
|
|
|
data Expire = Expire (M.Map (Maybe UUID) (Maybe POSIXTime))
|
|
|
|
|
|
|
|
parseExpire :: [String] -> Annex Expire
|
2016-11-16 01:29:54 +00:00
|
|
|
parseExpire [] = giveup "Specify an expire time."
|
2015-04-05 16:50:02 +00:00
|
|
|
parseExpire ps = do
|
|
|
|
now <- liftIO getPOSIXTime
|
|
|
|
Expire . M.fromList <$> mapM (parse now) ps
|
|
|
|
where
|
|
|
|
parse now s = case separate (== ':') s of
|
|
|
|
(t, []) -> return (Nothing, parsetime now t)
|
|
|
|
(n, t) -> do
|
|
|
|
r <- Remote.nameToUUID n
|
|
|
|
return (Just r, parsetime now t)
|
|
|
|
parsetime _ "never" = Nothing
|
|
|
|
parsetime now s = case parseDuration s of
|
2016-11-16 01:29:54 +00:00
|
|
|
Nothing -> giveup $ "bad expire time: " ++ s
|
2015-04-05 16:50:02 +00:00
|
|
|
Just d -> Just (now - durationToPOSIXTime d)
|
|
|
|
|
2015-07-10 20:26:23 +00:00
|
|
|
parseActivity :: Monad m => String -> m Activity
|
|
|
|
parseActivity s = case readish s of
|
|
|
|
Nothing -> fail $ "Unknown activity. Choose from: " ++
|
2015-04-05 16:50:02 +00:00
|
|
|
unwords (map show [minBound..maxBound :: Activity])
|
2015-07-10 20:26:23 +00:00
|
|
|
Just v -> return v
|
2015-04-05 16:50:02 +00:00
|
|
|
|