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
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2013-05-02 23:00:50 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2012-07-26 03:13:01 +00:00
|
|
|
module Command.WebApp where
|
|
|
|
|
|
|
|
import Common.Annex
|
|
|
|
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)
|
2013-06-02 18:06:17 +00:00
|
|
|
#ifdef __ANDROID__
|
2013-06-01 01:28:37 +00:00
|
|
|
import Utility.Env
|
2013-06-02 18:06:17 +00:00
|
|
|
#endif
|
2014-01-26 20:36:31 +00:00
|
|
|
import Annex.Init
|
2012-08-08 17:15:35 +00:00
|
|
|
import qualified Git
|
|
|
|
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
|
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
|
2013-04-08 19:04:35 +00:00
|
|
|
import Network.Socket (HostName)
|
2013-06-01 01:28:37 +00:00
|
|
|
import System.Environment (getArgs)
|
2012-07-31 16:17:31 +00:00
|
|
|
|
2014-10-14 18:20:10 +00:00
|
|
|
cmd :: [Command]
|
|
|
|
cmd = [ withOptions [listenOption] $
|
2013-04-08 19:04:35 +00:00
|
|
|
noCommit $ noRepo startNoRepo $ dontCheck repoExists $ notBareRepo $
|
2013-03-24 22:28:21 +00:00
|
|
|
command "webapp" paramNothing seek SectionCommon "launch webapp"]
|
2012-07-26 16:17:28 +00:00
|
|
|
|
2013-04-08 19:04:35 +00:00
|
|
|
listenOption :: Option
|
2014-01-26 20:25:55 +00:00
|
|
|
listenOption = fieldOption [] "listen" paramAddress
|
2013-04-08 19:04:35 +00:00
|
|
|
"accept connections to this address"
|
|
|
|
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
seek :: CommandSeek
|
|
|
|
seek ps = do
|
|
|
|
listenhost <- getOptionField listenOption return
|
|
|
|
withNothing (start listenhost) ps
|
2012-07-26 03:13:01 +00:00
|
|
|
|
2013-04-08 19:04:35 +00:00
|
|
|
start :: Maybe HostName -> CommandStart
|
2012-10-11 16:08:11 +00:00
|
|
|
start = start' True
|
|
|
|
|
2013-04-08 19:04:35 +00:00
|
|
|
start' :: Bool -> Maybe HostName -> CommandStart
|
|
|
|
start' allowauto listenhost = do
|
2013-09-25 07:09:06 +00:00
|
|
|
liftIO ensureInstalled
|
2013-11-17 18:58:35 +00:00
|
|
|
ifM isInitialized
|
2014-05-21 19:41:36 +00:00
|
|
|
( maybe notinitialized (go <=< needsUpgrade) =<< getVersion
|
|
|
|
, if allowauto
|
|
|
|
then liftIO $ startNoRepo []
|
|
|
|
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
|
2014-03-01 04:31:17 +00:00
|
|
|
listenhost' <- if isJust listenhost
|
|
|
|
then pure listenhost
|
|
|
|
else annexListen <$> Annex.getGitConfig
|
2012-11-12 05:05:04 +00:00
|
|
|
ifM (checkpid <&&> checkshim f)
|
2013-04-08 19:04:35 +00:00
|
|
|
( if isJust listenhost
|
|
|
|
then error "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
|
2014-03-01 04:31:17 +00:00
|
|
|
liftIO $ if isJust listenhost'
|
|
|
|
then putStrLn url
|
|
|
|
else liftIO $ openBrowser browser f url Nothing Nothing
|
|
|
|
, do
|
|
|
|
startDaemon True True Nothing cannotrun listenhost' $ Just $
|
|
|
|
\origout origerr url htmlshim ->
|
|
|
|
if isJust listenhost'
|
|
|
|
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"
|
|
|
|
liftIO $ firstRun listenhost
|
2012-07-31 16:17:31 +00:00
|
|
|
|
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! -}
|
2013-11-30 19:18:40 +00:00
|
|
|
startNoRepo :: CmdParams -> IO ()
|
|
|
|
startNoRepo _ = do
|
2013-04-08 19:04:35 +00:00
|
|
|
-- FIXME should be able to reuse regular getopt, but
|
|
|
|
-- it currently runs in the Annex monad.
|
|
|
|
args <- getArgs
|
|
|
|
let listenhost = headMaybe $ map (snd . separate (== '=')) $
|
|
|
|
filter ("--listen=" `isPrefixOf`) args
|
|
|
|
|
2014-04-17 20:47:49 +00:00
|
|
|
go listenhost =<< liftIO (filterM doesDirectoryExist =<< readAutoStartFile)
|
|
|
|
where
|
|
|
|
go listenhost [] = firstRun listenhost
|
|
|
|
go listenhost (d:ds) = do
|
|
|
|
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)
|
2014-04-17 20:47:49 +00:00
|
|
|
go listenhost ds
|
|
|
|
Right state -> void $ Annex.eval state $ do
|
2014-02-28 23:44:15 +00:00
|
|
|
whenM (fromRepo Git.repoIsLocalBare) $
|
|
|
|
error $ d ++ " is a bare git repository, cannot run the webapp in it"
|
|
|
|
callCommandAction $
|
|
|
|
start' False listenhost
|
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.
|
|
|
|
-}
|
2013-04-08 19:04:35 +00:00
|
|
|
firstRun :: Maybe HostName -> IO ()
|
|
|
|
firstRun listenhost = 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
|
|
|
|
- get a ThreadState. Using undefined is only safe because the
|
|
|
|
- webapp checks its noAnnex field before accessing the
|
|
|
|
- threadstate. -}
|
|
|
|
let st = undefined
|
|
|
|
{- Get a DaemonStatus without running in the Annex monad. -}
|
2012-07-31 16:17:31 +00:00
|
|
|
dstatus <- atomically . newTMVar =<< 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)
|
2014-03-01 04:31:17 +00:00
|
|
|
listenhost
|
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
|
|
|
|
| isJust listenhost = do
|
|
|
|
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 $
|
2013-11-17 18:58:35 +00:00
|
|
|
startDaemon True True Nothing Nothing listenhost $ Just $
|
2013-04-08 19:04:35 +00:00
|
|
|
sendurlback v
|
2013-06-11 03:16:18 +00:00
|
|
|
sendurlback v _origout _origerr url _htmlshim = do
|
|
|
|
recordUrl url
|
|
|
|
putMVar v url
|
|
|
|
|
|
|
|
recordUrl :: String -> IO ()
|
|
|
|
#ifdef __ANDROID__
|
|
|
|
{- The Android app has a menu item that opens the url recorded
|
|
|
|
- in this file. -}
|
|
|
|
recordUrl url = writeFile "/sdcard/git-annex.home/.git-annex-url" url
|
|
|
|
#else
|
|
|
|
recordUrl _ = noop
|
|
|
|
#endif
|
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 ()
|
2013-05-28 22:25:27 +00:00
|
|
|
#ifndef __ANDROID__
|
2015-02-09 20:34:42 +00:00
|
|
|
openBrowser' mcmd htmlshim _realurl outh errh = runbrowser
|
2013-05-28 22:25:27 +00:00
|
|
|
#else
|
2015-02-09 20:34:42 +00:00
|
|
|
openBrowser' mcmd htmlshim realurl outh errh = do
|
2013-06-11 03:16:18 +00:00
|
|
|
recordUrl url
|
2013-06-01 01:28:37 +00:00
|
|
|
{- Android's `am` command does not work reliably across the
|
|
|
|
- wide range of Android devices. Intead, FIFO should be set to
|
|
|
|
- the filename of a fifo that we can write the URL to. -}
|
|
|
|
v <- getEnv "FIFO"
|
|
|
|
case v of
|
|
|
|
Nothing -> runbrowser
|
|
|
|
Just f -> void $ forkIO $ do
|
|
|
|
fd <- openFd f WriteOnly Nothing defaultFileFlags
|
|
|
|
void $ fdWrite fd url
|
|
|
|
closeFd fd
|
2013-05-28 22:25:27 +00:00
|
|
|
#endif
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
2013-04-18 16:52:55 +00:00
|
|
|
p = case mcmd of
|
2014-10-14 18:29:53 +00:00
|
|
|
Just c -> proc c [htmlshim]
|
2014-06-10 22:29:15 +00:00
|
|
|
Nothing ->
|
|
|
|
#ifndef mingw32_HOST_OS
|
|
|
|
browserProc url
|
|
|
|
#else
|
|
|
|
{- Windows hack to avoid using the full path,
|
|
|
|
- which might contain spaces that cause problems
|
|
|
|
- for browserProc. -}
|
|
|
|
(browserProc (takeFileName htmlshim))
|
|
|
|
{ cwd = Just (takeDirectory htmlshim) }
|
|
|
|
#endif
|
2013-05-02 20:46:47 +00:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
{- Android does not support file:// urls, but neither is
|
|
|
|
- the security of the url in the process table important
|
|
|
|
- there, so just use the real url. -}
|
|
|
|
url = realurl
|
|
|
|
#else
|
|
|
|
url = fileUrl htmlshim
|
|
|
|
#endif
|
2013-06-01 01:28:37 +00:00
|
|
|
runbrowser = do
|
|
|
|
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
|
|
|
|
webBrowser = Git.Config.getMaybe "web.browser"
|
2012-08-02 04:42:33 +00:00
|
|
|
|
|
|
|
fileUrl :: FilePath -> String
|
|
|
|
fileUrl file = "file://" ++ file
|