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.
|
|
|
|
-}
|
|
|
|
|
|
|
|
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
|
2012-07-26 03:13:01 +00:00
|
|
|
import Utility.WebApp
|
2013-01-15 17:34:59 +00:00
|
|
|
import Utility.Daemon (checkDaemon)
|
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
|
2012-08-02 04:42:33 +00:00
|
|
|
import Locations.UserConfig
|
2012-07-26 03:13:01 +00:00
|
|
|
|
2012-08-02 04:42:33 +00:00
|
|
|
import System.Posix.Directory
|
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)
|
2012-07-31 16:17:31 +00:00
|
|
|
|
2012-07-26 03:13:01 +00:00
|
|
|
def :: [Command]
|
2012-12-29 18:45:19 +00:00
|
|
|
def = [noCommit $ noRepo startNoRepo $ dontCheck repoExists $ notBareRepo $
|
2012-12-13 04:45:27 +00:00
|
|
|
command "webapp" paramNothing seek "launch webapp"]
|
2012-07-26 16:17:28 +00:00
|
|
|
|
2012-07-26 03:13:01 +00:00
|
|
|
seek :: [CommandSeek]
|
2012-08-01 20:40:38 +00:00
|
|
|
seek = [withNothing start]
|
2012-07-26 03:13:01 +00:00
|
|
|
|
2012-08-01 20:40:38 +00:00
|
|
|
start :: CommandStart
|
2012-10-11 16:08:11 +00:00
|
|
|
start = start' True
|
|
|
|
|
|
|
|
start' :: Bool -> CommandStart
|
2012-12-29 18:45:19 +00:00
|
|
|
start' allowauto = 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-01-15 17:34:59 +00:00
|
|
|
( liftIO $ openBrowser browser f Nothing Nothing
|
|
|
|
, startDaemon True True $ Just $
|
|
|
|
\origout origerr _url htmlshim ->
|
|
|
|
openBrowser browser htmlshim 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
|
|
|
|
2012-08-02 04:42:33 +00:00
|
|
|
{- When run without a repo, see if there is an autoStartFile,
|
|
|
|
- and if so, start the first available listed repository.
|
|
|
|
- If not, it's our first time being run! -}
|
|
|
|
startNoRepo :: IO ()
|
|
|
|
startNoRepo = do
|
|
|
|
autostartfile <- autoStartFile
|
|
|
|
ifM (doesFileExist autostartfile) ( autoStart autostartfile , firstRun )
|
2012-07-31 16:17:31 +00:00
|
|
|
|
2012-08-02 04:42:33 +00:00
|
|
|
autoStart :: FilePath -> IO ()
|
|
|
|
autoStart autostartfile = do
|
2012-10-14 19:19:34 +00:00
|
|
|
dirs <- nub . lines <$> readFile autostartfile
|
2012-08-02 04:42:33 +00:00
|
|
|
edirs <- filterM doesDirectoryExist dirs
|
|
|
|
case edirs of
|
|
|
|
[] -> firstRun -- what else can I do? Nothing works..
|
|
|
|
(d:_) -> do
|
|
|
|
changeWorkingDirectory d
|
|
|
|
state <- Annex.new =<< Git.CurrentRepo.get
|
2012-10-11 16:08:11 +00:00
|
|
|
void $ Annex.eval state $ doCommand $ start' False
|
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.
|
|
|
|
-}
|
2012-07-31 16:17:31 +00:00
|
|
|
firstRun :: IO ()
|
|
|
|
firstRun = do
|
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-01-26 06:09:33 +00:00
|
|
|
startNamedThread (Just urlrenderer) $
|
2013-01-26 03:14:32 +00:00
|
|
|
webAppThread d urlrenderer True
|
|
|
|
(callback signaler)
|
|
|
|
(callback mainthread)
|
|
|
|
waitNamedThreads
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
|
|
|
signaler v = do
|
|
|
|
putMVar v ""
|
|
|
|
takeMVar v
|
|
|
|
mainthread v _url htmlshim = do
|
|
|
|
browser <- maybe Nothing webBrowser <$> Git.Config.global
|
2013-01-15 17:34:59 +00:00
|
|
|
openBrowser browser htmlshim Nothing Nothing
|
2012-08-01 20:10:26 +00:00
|
|
|
|
2012-11-12 05:05:04 +00:00
|
|
|
_wait <- takeMVar v
|
2012-08-01 20:10:26 +00:00
|
|
|
|
2012-11-12 05:05:04 +00:00
|
|
|
state <- Annex.new =<< Git.CurrentRepo.get
|
2013-01-15 17:34:59 +00:00
|
|
|
Annex.eval state $
|
|
|
|
startDaemon True True $ Just $ sendurlback v
|
|
|
|
sendurlback v _origout _origerr url _htmlshim = putMVar v url
|
|
|
|
|
|
|
|
openBrowser :: Maybe FilePath -> FilePath -> Maybe Handle -> Maybe Handle -> IO ()
|
|
|
|
openBrowser cmd htmlshim outh errh = do
|
|
|
|
hPutStrLn (fromMaybe stdout outh) $ "Launching web browser on " ++ url
|
|
|
|
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) $
|
|
|
|
hPutStrLn (fromMaybe stderr errh) "failed to start web browser"
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
|
|
|
url = fileUrl htmlshim
|
2013-01-15 17:34:59 +00:00
|
|
|
p = proc (fromMaybe browserCommand cmd) [htmlshim]
|
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
|