2012-07-31 16:17:31 +00:00
|
|
|
{- git-annex assistant webapp thread
|
2012-07-26 01:26:13 +00:00
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2012-2014 Joey Hess <id@joeyh.name>
|
2012-07-26 01:26:13 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2012-07-26 01:26:13 +00:00
|
|
|
-}
|
|
|
|
|
2013-06-05 01:02:09 +00:00
|
|
|
{-# LANGUAGE TemplateHaskell, MultiParamTypeClasses #-}
|
2014-10-09 21:07:35 +00:00
|
|
|
{-# LANGUAGE ViewPatterns, OverloadedStrings #-}
|
2012-07-29 01:21:22 +00:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
2012-07-26 01:26:13 +00:00
|
|
|
|
|
|
|
module Assistant.Threads.WebApp where
|
|
|
|
|
|
|
|
import Assistant.Common
|
2012-07-31 05:11:32 +00:00
|
|
|
import Assistant.WebApp
|
2012-09-02 04:27:48 +00:00
|
|
|
import Assistant.WebApp.Types
|
2012-07-31 05:11:32 +00:00
|
|
|
import Assistant.WebApp.DashBoard
|
|
|
|
import Assistant.WebApp.SideBar
|
|
|
|
import Assistant.WebApp.Notifications
|
2013-03-13 01:51:03 +00:00
|
|
|
import Assistant.WebApp.RepoList
|
2012-07-31 05:11:32 +00:00
|
|
|
import Assistant.WebApp.Configurators
|
2012-08-31 19:17:12 +00:00
|
|
|
import Assistant.WebApp.Configurators.Local
|
|
|
|
import Assistant.WebApp.Configurators.Ssh
|
2012-09-08 04:26:47 +00:00
|
|
|
import Assistant.WebApp.Configurators.Pairing
|
2012-11-24 20:30:15 +00:00
|
|
|
import Assistant.WebApp.Configurators.AWS
|
2013-04-25 16:23:36 +00:00
|
|
|
import Assistant.WebApp.Configurators.IA
|
2012-11-17 19:30:11 +00:00
|
|
|
import Assistant.WebApp.Configurators.WebDAV
|
2013-03-03 21:07:27 +00:00
|
|
|
import Assistant.WebApp.Configurators.Preferences
|
assistant unused file handling
Make sanity checker run git annex unused daily, and queue up transfers
of unused files to any remotes that will have them. The transfer retrying
code works for us here, so eg when a backup disk remote is plugged in,
any transfers to it are done. Once the unused files reach a remote,
they'll be removed locally as unwanted.
If the setup does not cause unused files to go to a remote, they'll pile
up, and the sanity checker detects this using some heuristics that are
pretty good -- 1000 unused files, or 10% of disk used by unused files,
or more disk wasted by unused files than is left free. Once it detects
this, it pops up an alert in the webapp, with a button to take action.
TODO: Webapp UI to configure this, and also the ability to launch an
immediate cleanup of all unused files.
This commit was sponsored by Simon Michael.
2014-01-23 02:48:56 +00:00
|
|
|
import Assistant.WebApp.Configurators.Unused
|
2013-03-31 20:38:05 +00:00
|
|
|
import Assistant.WebApp.Configurators.Edit
|
|
|
|
import Assistant.WebApp.Configurators.Delete
|
2013-10-10 22:02:33 +00:00
|
|
|
import Assistant.WebApp.Configurators.Fsck
|
2013-11-21 21:49:56 +00:00
|
|
|
import Assistant.WebApp.Configurators.Upgrade
|
2012-07-31 06:30:26 +00:00
|
|
|
import Assistant.WebApp.Documentation
|
2013-01-03 19:16:40 +00:00
|
|
|
import Assistant.WebApp.Control
|
2012-09-18 21:50:07 +00:00
|
|
|
import Assistant.WebApp.OtherRepos
|
2013-10-22 20:02:52 +00:00
|
|
|
import Assistant.WebApp.Repair
|
2016-12-27 20:36:05 +00:00
|
|
|
import Assistant.WebApp.Pairing
|
2012-10-29 23:07:10 +00:00
|
|
|
import Assistant.Types.ThreadedMonad
|
2012-07-26 01:26:13 +00:00
|
|
|
import Utility.WebApp
|
2016-11-22 18:18:34 +00:00
|
|
|
import Utility.AuthToken
|
2013-05-12 23:19:28 +00:00
|
|
|
import Utility.Tmp
|
2013-01-03 22:50:30 +00:00
|
|
|
import Utility.FileMode
|
2012-07-26 06:45:01 +00:00
|
|
|
import Git
|
2014-03-01 04:31:17 +00:00
|
|
|
import qualified Annex
|
2012-07-26 01:26:13 +00:00
|
|
|
|
|
|
|
import Yesod
|
2013-04-08 19:04:35 +00:00
|
|
|
import Network.Socket (SockAddr, HostName)
|
2012-07-31 05:11:32 +00:00
|
|
|
import Data.Text (pack, unpack)
|
2014-03-01 01:32:18 +00:00
|
|
|
import qualified Network.Wai.Handler.WarpTLS as TLS
|
2014-06-11 05:29:00 +00:00
|
|
|
import Network.Wai.Middleware.RequestLogger
|
2012-07-26 08:50:09 +00:00
|
|
|
|
2012-07-31 05:11:32 +00:00
|
|
|
mkYesodDispatch "WebApp" $(parseRoutesFile "Assistant/WebApp/routes")
|
2012-07-26 01:26:13 +00:00
|
|
|
|
2012-08-01 20:10:26 +00:00
|
|
|
type Url = String
|
|
|
|
|
2012-10-29 04:15:43 +00:00
|
|
|
webAppThread
|
|
|
|
:: AssistantData
|
2012-09-08 23:57:15 +00:00
|
|
|
-> UrlRenderer
|
2012-10-29 04:15:43 +00:00
|
|
|
-> Bool
|
2013-11-17 18:58:35 +00:00
|
|
|
-> Maybe String
|
2013-06-11 03:16:18 +00:00
|
|
|
-> Maybe (IO Url)
|
2014-03-01 04:31:17 +00:00
|
|
|
-> Maybe HostName
|
2012-08-01 20:10:26 +00:00
|
|
|
-> Maybe (Url -> FilePath -> IO ())
|
2012-09-06 18:56:04 +00:00
|
|
|
-> NamedThread
|
2014-03-01 04:31:17 +00:00
|
|
|
webAppThread assistantdata urlrenderer noannex cannotrun postfirstrun listenhost onstartup = thread $ liftIO $ do
|
|
|
|
listenhost' <- if isJust listenhost
|
|
|
|
then pure listenhost
|
|
|
|
else getAnnex $ annexListen <$> Annex.getGitConfig
|
|
|
|
tlssettings <- getAnnex getTlsSettings
|
2012-07-31 16:17:31 +00:00
|
|
|
webapp <- WebApp
|
2012-10-29 04:15:43 +00:00
|
|
|
<$> pure assistantdata
|
2016-11-30 18:19:26 +00:00
|
|
|
<*> genAuthToken 128
|
2012-10-29 04:15:43 +00:00
|
|
|
<*> getreldir
|
2013-04-17 05:37:08 +00:00
|
|
|
<*> pure staticRoutes
|
2012-08-01 20:10:26 +00:00
|
|
|
<*> pure postfirstrun
|
2013-11-17 18:58:35 +00:00
|
|
|
<*> pure cannotrun
|
2012-10-29 04:15:43 +00:00
|
|
|
<*> pure noannex
|
2014-03-01 04:31:17 +00:00
|
|
|
<*> pure listenhost'
|
2016-12-27 20:36:05 +00:00
|
|
|
<*> newWormholePairingState
|
2012-09-08 23:57:15 +00:00
|
|
|
setUrlRenderer urlrenderer $ yesodRender webapp (pack "")
|
2012-07-27 03:55:51 +00:00
|
|
|
app <- toWaiAppPlain webapp
|
2012-07-26 01:26:13 +00:00
|
|
|
app' <- ifM debugEnabled
|
2014-06-11 05:29:00 +00:00
|
|
|
( return $ logStdout app
|
2012-07-26 01:26:13 +00:00
|
|
|
, return app
|
|
|
|
)
|
2014-03-01 04:31:17 +00:00
|
|
|
runWebApp tlssettings listenhost' app' $ \addr -> if noannex
|
2013-12-07 15:45:01 +00:00
|
|
|
then withTmpFile "webapp.html" $ \tmpfile h -> do
|
|
|
|
hClose h
|
2014-03-01 01:32:18 +00:00
|
|
|
go tlssettings addr webapp tmpfile Nothing
|
2012-10-29 04:15:43 +00:00
|
|
|
else do
|
2014-03-01 04:31:17 +00:00
|
|
|
htmlshim <- getAnnex' $ fromRepo gitAnnexHtmlShim
|
|
|
|
urlfile <- getAnnex' $ fromRepo gitAnnexUrlFile
|
2014-03-01 01:32:18 +00:00
|
|
|
go tlssettings addr webapp htmlshim (Just urlfile)
|
2012-10-29 18:30:10 +00:00
|
|
|
where
|
2014-10-09 18:53:13 +00:00
|
|
|
-- The webapp thread does not wait for the startupSanityCheckThread
|
assistant: Detect stale git lock files at startup time, and remove them.
Extends the index.lock handling to other git lock files. I surveyed
all lock files used by git, and found more than I expected. All are
handled the same in git; it leaves them open while doing the operation,
possibly writing the new file content to the lock file, and then closes
them when done.
The gc.pid file is excluded because it won't affect the normal operation
of the assistant, and waiting for a gc to finish on startup wouldn't be
good.
All threads except the webapp thread wait on the new startup sanity checker
thread to complete, so they won't try to do things with git that fail
due to stale lock files. The webapp thread mostly avoids doing that kind of
thing itself. A few configurators might fail on lock files, but only if the
user is explicitly trying to run them. The webapp needs to start
immediately when the user has opened it, even if there are stale lock
files.
Arranging for the threads to wait on the startup sanity checker was a bit
of a bear. Have to get all the NotificationHandles set up before the
startup sanity checker runs, or they won't see its signal. Perhaps
the NotificationBroadcaster is not the best interface to have used for
this. Oh well, it works.
This commit was sponsored by Michael Jakl
2013-10-05 21:02:11 +00:00
|
|
|
-- to finish, so that the user interface remains responsive while
|
|
|
|
-- that's going on.
|
|
|
|
thread = namedThreadUnchecked "WebApp"
|
2012-10-29 18:30:10 +00:00
|
|
|
getreldir
|
|
|
|
| noannex = return Nothing
|
|
|
|
| otherwise = Just <$>
|
|
|
|
(relHome =<< absPath
|
2014-03-01 04:31:17 +00:00
|
|
|
=<< getAnnex' (fromRepo repoPath))
|
2014-03-01 01:32:18 +00:00
|
|
|
go tlssettings addr webapp htmlshim urlfile = do
|
|
|
|
let url = myUrl tlssettings webapp addr
|
2013-01-03 22:50:30 +00:00
|
|
|
maybe noop (`writeFileProtected` url) urlfile
|
|
|
|
writeHtmlShim "Starting webapp..." url htmlshim
|
2012-10-29 18:30:10 +00:00
|
|
|
maybe noop (\a -> a url htmlshim) onstartup
|
2012-07-26 03:13:01 +00:00
|
|
|
|
2014-03-01 04:31:17 +00:00
|
|
|
getAnnex a
|
|
|
|
| noannex = pure Nothing
|
|
|
|
| otherwise = getAnnex' a
|
|
|
|
getAnnex' = runThreadState (threadState assistantdata)
|
|
|
|
|
2014-03-01 01:32:18 +00:00
|
|
|
myUrl :: Maybe TLS.TLSSettings -> WebApp -> SockAddr -> Url
|
|
|
|
myUrl tlssettings webapp addr = unpack $ yesodRender webapp urlbase DashboardR []
|
2012-10-29 18:30:10 +00:00
|
|
|
where
|
2014-03-01 01:32:18 +00:00
|
|
|
urlbase = pack $ proto ++ "://" ++ show addr
|
|
|
|
proto
|
|
|
|
| isJust tlssettings = "https"
|
|
|
|
| otherwise = "http"
|
|
|
|
|
|
|
|
getTlsSettings :: Annex (Maybe TLS.TLSSettings)
|
|
|
|
getTlsSettings = do
|
|
|
|
cert <- fromRepo gitAnnexWebCertificate
|
|
|
|
privkey <- fromRepo gitAnnexWebPrivKey
|
|
|
|
ifM (liftIO $ allM doesFileExist [cert, privkey])
|
|
|
|
( return $ Just $ TLS.tlsSettings cert privkey
|
|
|
|
, return Nothing
|
|
|
|
)
|