2012-07-26 03:13:01 +00:00
|
|
|
{- git-annex webapp launcher
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- 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
|
2012-07-31 20:19:24 +00:00
|
|
|
import 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-04-08 19:04:35 +00:00
|
|
|
import qualified Option
|
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-01-15 17:34:59 +00:00
|
|
|
import System.Process (env, std_out, std_err)
|
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
|
|
|
|
2012-07-26 03:13:01 +00:00
|
|
|
def :: [Command]
|
2013-04-08 19:04:35 +00:00
|
|
|
def = [ withOptions [listenOption] $
|
|
|
|
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
|
|
|
|
listenOption = Option.field [] "listen" paramAddress
|
|
|
|
"accept connections to this address"
|
|
|
|
|
2012-07-26 03:13:01 +00:00
|
|
|
seek :: [CommandSeek]
|
2013-04-08 19:04:35 +00:00
|
|
|
seek = [withField listenOption return $ \listenhost ->
|
|
|
|
withNothing $ start listenhost]
|
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
|
2012-09-26 20:50:04 +00:00
|
|
|
liftIO $ ensureInstalled
|
2012-10-11 16:08:11 +00:00
|
|
|
ifM isInitialized ( go , auto )
|
2012-07-31 20:19:24 +00:00
|
|
|
stop
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
|
|
|
go = do
|
|
|
|
browser <- fromRepo webBrowser
|
|
|
|
f <- liftIO . absPath =<< fromRepo gitAnnexHtmlShim
|
|
|
|
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
|
|
|
|
liftIO $ openBrowser browser f url Nothing Nothing
|
2013-04-08 19:04:35 +00:00
|
|
|
, startDaemon True True listenhost $ Just $
|
|
|
|
\origout origerr url htmlshim ->
|
|
|
|
if isJust listenhost
|
|
|
|
then maybe noop (`hPutStrLn` url) origout
|
2013-05-02 20:46:47 +00:00
|
|
|
else openBrowser browser htmlshim url origout origerr
|
2012-11-12 05:05:04 +00:00
|
|
|
)
|
|
|
|
auto
|
|
|
|
| allowauto = liftIO startNoRepo
|
|
|
|
| otherwise = do
|
|
|
|
d <- liftIO getCurrentDirectory
|
|
|
|
error $ "no git repository in " ++ d
|
|
|
|
checkpid = do
|
|
|
|
pidfile <- fromRepo gitAnnexPidFile
|
|
|
|
liftIO $ isJust <$> checkDaemon pidfile
|
|
|
|
checkshim f = liftIO $ doesFileExist f
|
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
|
|
|
|
- the autostart file. If not, it's our first time being run! -}
|
2012-08-02 04:42:33 +00:00
|
|
|
startNoRepo :: 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
|
|
|
|
|
2013-03-03 21:07:27 +00:00
|
|
|
dirs <- liftIO $ filterM doesDirectoryExist =<< readAutoStartFile
|
|
|
|
case dirs of
|
2013-04-08 19:04:35 +00:00
|
|
|
[] -> firstRun listenhost
|
2012-08-02 04:42:33 +00:00
|
|
|
(d:_) -> do
|
2013-05-11 23:14:30 +00:00
|
|
|
setCurrentDirectory d
|
2012-08-02 04:42:33 +00:00
|
|
|
state <- Annex.new =<< Git.CurrentRepo.get
|
2013-04-08 19:04:35 +00:00
|
|
|
void $ Annex.eval state $ doCommand $
|
|
|
|
start' False listenhost
|
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 $
|
2013-04-08 19:04:35 +00:00
|
|
|
webAppThread d urlrenderer True listenhost
|
2013-01-26 03:14:32 +00:00
|
|
|
(callback signaler)
|
|
|
|
(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
|
|
|
|
browser <- maybe Nothing webBrowser <$> 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 $
|
|
|
|
startDaemon True True listenhost $ Just $
|
|
|
|
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 ()
|
2013-05-28 22:25:27 +00:00
|
|
|
#ifndef __ANDROID__
|
2013-06-01 01:28:37 +00:00
|
|
|
openBrowser mcmd htmlshim _realurl outh errh = runbrowser
|
2013-05-28 22:25:27 +00:00
|
|
|
#else
|
|
|
|
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
|
|
|
|
Just cmd -> proc cmd [htmlshim]
|
2013-05-02 23:10:30 +00:00
|
|
|
Nothing -> browserProc url
|
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
|
|
|
|
unless (exitcode == ExitSuccess) $ do
|
|
|
|
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
|