2012-07-26 03:13:01 +00:00
|
|
|
{- git-annex webapp launcher
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2012 Joey Hess <id@joeyh.name>
|
2012-07-26 03:13:01 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2012-07-26 03:13:01 +00:00
|
|
|
-}
|
|
|
|
|
2013-05-02 23:00:50 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
2019-12-05 19:10:23 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2013-05-02 23:00:50 +00:00
|
|
|
|
2012-07-26 03:13:01 +00:00
|
|
|
module Command.WebApp where
|
|
|
|
|
|
|
|
import Command
|
|
|
|
import Assistant
|
2012-09-06 18:56:04 +00:00
|
|
|
import Assistant.Common
|
2012-10-30 18:34:48 +00:00
|
|
|
import Assistant.NamedThread
|
2012-07-31 16:17:31 +00:00
|
|
|
import Assistant.Threads.WebApp
|
2012-09-08 23:57:15 +00:00
|
|
|
import Assistant.WebApp
|
2012-09-26 20:50:04 +00:00
|
|
|
import Assistant.Install
|
2013-04-22 19:36:34 +00:00
|
|
|
import Annex.Environment
|
2012-07-26 03:13:01 +00:00
|
|
|
import Utility.WebApp
|
2013-01-15 17:34:59 +00:00
|
|
|
import Utility.Daemon (checkDaemon)
|
2016-04-13 18:07:50 +00:00
|
|
|
import Utility.UserInfo
|
2014-01-26 20:36:31 +00:00
|
|
|
import Annex.Init
|
2012-08-08 17:15:35 +00:00
|
|
|
import qualified Git
|
2019-12-05 19:10:23 +00:00
|
|
|
import Git.Types (fromConfigValue)
|
2012-08-08 17:15:35 +00:00
|
|
|
import qualified Git.Config
|
2012-08-01 20:10:26 +00:00
|
|
|
import qualified Git.CurrentRepo
|
|
|
|
import qualified Annex
|
2013-04-23 15:38:52 +00:00
|
|
|
import Config.Files
|
2013-11-17 18:58:35 +00:00
|
|
|
import Upgrade
|
|
|
|
import Annex.Version
|
2018-04-25 18:38:42 +00:00
|
|
|
import Utility.Android
|
2012-07-26 03:13:01 +00:00
|
|
|
|
2012-08-01 20:10:26 +00:00
|
|
|
import Control.Concurrent
|
2012-07-31 16:17:31 +00:00
|
|
|
import Control.Concurrent.STM
|
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2015-07-13 15:53:42 +00:00
|
|
|
cmd = noCommit $ dontCheck repoExists $ notBareRepo $
|
|
|
|
noRepo (startNoRepo <$$> optParser) $
|
2015-07-08 19:39:05 +00:00
|
|
|
command "webapp" SectionCommon "launch webapp"
|
2015-07-13 15:53:42 +00:00
|
|
|
paramNothing (seek <$$> optParser)
|
2012-07-26 16:17:28 +00:00
|
|
|
|
2015-07-13 15:53:42 +00:00
|
|
|
data WebAppOptions = WebAppOptions
|
|
|
|
{ listenAddress :: Maybe String
|
|
|
|
}
|
2013-04-08 19:04:35 +00:00
|
|
|
|
2015-07-13 15:53:42 +00:00
|
|
|
optParser :: CmdParamsDesc -> Parser WebAppOptions
|
|
|
|
optParser _ = WebAppOptions
|
|
|
|
<$> optional (strOption
|
|
|
|
( long "listen" <> metavar paramAddress
|
|
|
|
<> help "accept connections to this address"
|
|
|
|
))
|
2012-07-26 03:13:01 +00:00
|
|
|
|
2015-07-13 15:53:42 +00:00
|
|
|
seek :: WebAppOptions -> CommandSeek
|
|
|
|
seek = commandAction . start
|
|
|
|
|
|
|
|
start :: WebAppOptions -> CommandStart
|
2012-10-11 16:08:11 +00:00
|
|
|
start = start' True
|
|
|
|
|
2015-07-13 15:53:42 +00:00
|
|
|
start' :: Bool -> WebAppOptions -> CommandStart
|
|
|
|
start' allowauto o = do
|
2013-09-25 07:09:06 +00:00
|
|
|
liftIO ensureInstalled
|
2016-04-13 18:07:50 +00:00
|
|
|
ifM (isInitialized <&&> notHome)
|
2014-05-21 19:41:36 +00:00
|
|
|
( maybe notinitialized (go <=< needsUpgrade) =<< getVersion
|
|
|
|
, if allowauto
|
2015-07-13 15:53:42 +00:00
|
|
|
then liftIO $ startNoRepo o
|
2014-05-21 19:41:36 +00:00
|
|
|
else notinitialized
|
2013-11-17 18:58:35 +00:00
|
|
|
)
|
2012-07-31 20:19:24 +00:00
|
|
|
stop
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
2014-05-21 19:41:36 +00:00
|
|
|
go cannotrun = do
|
2012-11-12 05:05:04 +00:00
|
|
|
browser <- fromRepo webBrowser
|
|
|
|
f <- liftIO . absPath =<< fromRepo gitAnnexHtmlShim
|
2015-07-13 15:53:42 +00:00
|
|
|
listenAddress' <- if isJust (listenAddress o)
|
|
|
|
then pure (listenAddress o)
|
2014-03-01 04:31:17 +00:00
|
|
|
else annexListen <$> Annex.getGitConfig
|
2012-11-12 05:05:04 +00:00
|
|
|
ifM (checkpid <&&> checkshim f)
|
2015-07-13 15:53:42 +00:00
|
|
|
( if isJust (listenAddress o)
|
2016-11-16 01:29:54 +00:00
|
|
|
then giveup "The assistant is already running, so --listen cannot be used."
|
2013-05-03 00:38:57 +00:00
|
|
|
else do
|
|
|
|
url <- liftIO . readFile
|
|
|
|
=<< fromRepo gitAnnexUrlFile
|
2015-07-13 15:53:42 +00:00
|
|
|
liftIO $ if isJust listenAddress'
|
2014-03-01 04:31:17 +00:00
|
|
|
then putStrLn url
|
|
|
|
else liftIO $ openBrowser browser f url Nothing Nothing
|
|
|
|
, do
|
2015-07-13 15:53:42 +00:00
|
|
|
startDaemon True True Nothing cannotrun listenAddress' $ Just $
|
2014-03-01 04:31:17 +00:00
|
|
|
\origout origerr url htmlshim ->
|
2015-07-13 15:53:42 +00:00
|
|
|
if isJust listenAddress'
|
2014-03-01 04:31:17 +00:00
|
|
|
then maybe noop (`hPutStrLn` url) origout
|
|
|
|
else openBrowser browser htmlshim url origout origerr
|
2012-11-12 05:05:04 +00:00
|
|
|
)
|
|
|
|
checkpid = do
|
|
|
|
pidfile <- fromRepo gitAnnexPidFile
|
|
|
|
liftIO $ isJust <$> checkDaemon pidfile
|
|
|
|
checkshim f = liftIO $ doesFileExist f
|
2014-05-21 19:41:36 +00:00
|
|
|
notinitialized = do
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
liftIO $ cannotStartIn (Git.repoLocation g) "repository has not been initialized by git-annex"
|
2015-07-13 15:53:42 +00:00
|
|
|
liftIO $ firstRun o
|
2012-07-31 16:17:31 +00:00
|
|
|
|
2016-04-13 18:07:50 +00:00
|
|
|
{- If HOME is a git repo, even if it's initialized for git-annex,
|
|
|
|
- the user almost certianly does not want to run the assistant there. -}
|
|
|
|
notHome :: Annex Bool
|
|
|
|
notHome = do
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
d <- liftIO $ absPath (Git.repoLocation g)
|
|
|
|
h <- liftIO $ absPath =<< myHomeDir
|
|
|
|
return (d /= h)
|
|
|
|
|
2013-03-03 21:07:27 +00:00
|
|
|
{- When run without a repo, start the first available listed repository in
|
2014-04-17 20:47:49 +00:00
|
|
|
- the autostart file. If none, it's our first time being run! -}
|
2015-07-13 15:53:42 +00:00
|
|
|
startNoRepo :: WebAppOptions -> IO ()
|
|
|
|
startNoRepo o = go =<< liftIO (filterM doesDirectoryExist =<< readAutoStartFile)
|
2014-04-17 20:47:49 +00:00
|
|
|
where
|
2015-07-13 15:53:42 +00:00
|
|
|
go [] = firstRun o
|
|
|
|
go (d:ds) = do
|
2014-04-17 20:47:49 +00:00
|
|
|
v <- tryNonAsync $ do
|
2013-05-11 23:14:30 +00:00
|
|
|
setCurrentDirectory d
|
2014-04-17 20:47:49 +00:00
|
|
|
Annex.new =<< Git.CurrentRepo.get
|
|
|
|
case v of
|
|
|
|
Left e -> do
|
2014-05-21 19:41:36 +00:00
|
|
|
cannotStartIn d (show e)
|
2015-07-13 15:53:42 +00:00
|
|
|
go ds
|
2014-04-17 20:47:49 +00:00
|
|
|
Right state -> void $ Annex.eval state $ do
|
2014-02-28 23:44:15 +00:00
|
|
|
whenM (fromRepo Git.repoIsLocalBare) $
|
2016-11-16 01:29:54 +00:00
|
|
|
giveup $ d ++ " is a bare git repository, cannot run the webapp in it"
|
2014-02-28 23:44:15 +00:00
|
|
|
callCommandAction $
|
2015-07-13 15:53:42 +00:00
|
|
|
start' False o
|
2012-08-01 20:10:26 +00:00
|
|
|
|
2014-05-21 19:41:36 +00:00
|
|
|
cannotStartIn :: FilePath -> String -> IO ()
|
|
|
|
cannotStartIn d reason = warningIO $ "unable to start webapp in repository " ++ d ++ ": " ++ reason
|
|
|
|
|
2012-08-01 20:10:26 +00:00
|
|
|
{- Run the webapp without a repository, which prompts the user, makes one,
|
|
|
|
- changes to it, starts the regular assistant, and redirects the
|
|
|
|
- browser to its url.
|
|
|
|
-
|
|
|
|
- This is a very tricky dance -- The first webapp calls the signaler,
|
|
|
|
- which signals the main thread when it's ok to continue by writing to a
|
|
|
|
- MVar. The main thread starts the second webapp, and uses its callback
|
|
|
|
- to write its url back to the MVar, from where the signaler retrieves it,
|
|
|
|
- returning it to the first webapp, which does the redirect.
|
|
|
|
-
|
|
|
|
- Note that it's important that mainthread never terminates! Much
|
|
|
|
- of this complication is due to needing to keep the mainthread running.
|
|
|
|
-}
|
2015-07-13 15:53:42 +00:00
|
|
|
firstRun :: WebAppOptions -> IO ()
|
|
|
|
firstRun o = do
|
2013-04-22 19:36:34 +00:00
|
|
|
checkEnvironmentIO
|
2012-10-29 04:15:43 +00:00
|
|
|
{- Without a repository, we cannot have an Annex monad, so cannot
|
2015-04-19 04:38:29 +00:00
|
|
|
- get a ThreadState. This is only safe because the
|
2012-10-29 04:15:43 +00:00
|
|
|
- webapp checks its noAnnex field before accessing the
|
|
|
|
- threadstate. -}
|
2015-04-19 04:38:29 +00:00
|
|
|
let st = error "annex state not available"
|
2012-10-29 04:15:43 +00:00
|
|
|
{- Get a DaemonStatus without running in the Annex monad. -}
|
2016-09-30 23:51:16 +00:00
|
|
|
dstatus <- atomically . newTVar =<< newDaemonStatus
|
2012-10-29 04:15:43 +00:00
|
|
|
d <- newAssistantData st dstatus
|
2012-09-08 23:57:15 +00:00
|
|
|
urlrenderer <- newUrlRenderer
|
2012-08-01 20:10:26 +00:00
|
|
|
v <- newEmptyMVar
|
|
|
|
let callback a = Just $ a v
|
2013-01-26 03:14:32 +00:00
|
|
|
runAssistant d $ do
|
2013-04-03 21:44:34 +00:00
|
|
|
startNamedThread urlrenderer $
|
2014-03-01 04:31:17 +00:00
|
|
|
webAppThread d urlrenderer True Nothing
|
2013-01-26 03:14:32 +00:00
|
|
|
(callback signaler)
|
2015-07-13 15:53:42 +00:00
|
|
|
(listenAddress o)
|
2013-01-26 03:14:32 +00:00
|
|
|
(callback mainthread)
|
|
|
|
waitNamedThreads
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
|
|
|
signaler v = do
|
|
|
|
putMVar v ""
|
|
|
|
takeMVar v
|
2013-04-08 19:04:35 +00:00
|
|
|
mainthread v url htmlshim
|
2015-07-13 15:53:42 +00:00
|
|
|
| isJust (listenAddress o)= do
|
2013-04-08 19:04:35 +00:00
|
|
|
putStrLn url
|
2013-05-01 15:48:16 +00:00
|
|
|
hFlush stdout
|
2013-04-08 19:04:35 +00:00
|
|
|
go
|
|
|
|
| otherwise = do
|
2014-03-05 17:43:56 +00:00
|
|
|
browser <- maybe Nothing webBrowser
|
|
|
|
<$> catchDefaultIO Nothing Git.Config.global
|
2013-05-02 20:46:47 +00:00
|
|
|
openBrowser browser htmlshim url Nothing Nothing
|
2013-04-08 19:04:35 +00:00
|
|
|
go
|
|
|
|
where
|
|
|
|
go = do
|
|
|
|
_wait <- takeMVar v
|
|
|
|
state <- Annex.new =<< Git.CurrentRepo.get
|
|
|
|
Annex.eval state $
|
2015-07-13 15:53:42 +00:00
|
|
|
startDaemon True True Nothing Nothing (listenAddress o) $ Just $
|
2013-04-08 19:04:35 +00:00
|
|
|
sendurlback v
|
2018-10-13 05:36:06 +00:00
|
|
|
sendurlback v _origout _origerr url _htmlshim = putMVar v url
|
2013-01-15 17:34:59 +00:00
|
|
|
|
2013-05-02 20:46:47 +00:00
|
|
|
openBrowser :: Maybe FilePath -> FilePath -> String -> Maybe Handle -> Maybe Handle -> IO ()
|
2015-02-09 20:34:42 +00:00
|
|
|
openBrowser mcmd htmlshim realurl outh errh = do
|
|
|
|
htmlshim' <- absPath htmlshim
|
|
|
|
openBrowser' mcmd htmlshim' realurl outh errh
|
|
|
|
|
|
|
|
openBrowser' :: Maybe FilePath -> FilePath -> String -> Maybe Handle -> Maybe Handle -> IO ()
|
2018-04-25 18:38:42 +00:00
|
|
|
openBrowser' mcmd htmlshim realurl outh errh =
|
|
|
|
ifM osAndroid
|
|
|
|
{- Android does not support file:// urls well, but neither
|
|
|
|
- is the security of the url in the process table important
|
|
|
|
- there, so just use the real url. -}
|
|
|
|
( runbrowser realurl
|
|
|
|
, runbrowser (fileUrl htmlshim)
|
|
|
|
)
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
2018-04-25 18:38:42 +00:00
|
|
|
runbrowser url = do
|
|
|
|
let p = case mcmd of
|
|
|
|
Just c -> proc c [url]
|
|
|
|
Nothing ->
|
2014-06-10 22:29:15 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2018-04-25 18:38:42 +00:00
|
|
|
browserProc url
|
2013-05-02 20:46:47 +00:00
|
|
|
#else
|
2018-04-25 18:38:42 +00:00
|
|
|
{- Windows hack to avoid using the full path,
|
|
|
|
- which might contain spaces that cause problems
|
|
|
|
- for browserProc. -}
|
|
|
|
(browserProc (takeFileName htmlshim))
|
|
|
|
{ cwd = Just (takeDirectory htmlshim) }
|
2013-05-02 20:46:47 +00:00
|
|
|
#endif
|
2013-06-01 01:28:37 +00:00
|
|
|
hPutStrLn (fromMaybe stdout outh) $ "Launching web browser on " ++ url
|
|
|
|
hFlush stdout
|
|
|
|
environ <- cleanEnvironment
|
|
|
|
(_, _, _, pid) <- createProcess p
|
|
|
|
{ env = environ
|
|
|
|
, std_out = maybe Inherit UseHandle outh
|
|
|
|
, std_err = maybe Inherit UseHandle errh
|
|
|
|
}
|
|
|
|
exitcode <- waitForProcess pid
|
2013-09-25 07:09:06 +00:00
|
|
|
unless (exitcode == ExitSuccess) $
|
2013-06-01 01:28:37 +00:00
|
|
|
hPutStrLn (fromMaybe stderr errh) "failed to start web browser"
|
2012-08-08 17:15:35 +00:00
|
|
|
|
|
|
|
{- web.browser is a generic git config setting for a web browser program -}
|
|
|
|
webBrowser :: Git.Repo -> Maybe FilePath
|
2019-12-05 19:10:23 +00:00
|
|
|
webBrowser = fmap fromConfigValue <$> Git.Config.getMaybe "web.browser"
|
2012-08-02 04:42:33 +00:00
|
|
|
|
|
|
|
fileUrl :: FilePath -> String
|
|
|
|
fileUrl file = "file://" ++ file
|