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-07-31 16:17:31 +00:00
|
|
|
import Assistant.DaemonStatus
|
2012-08-05 01:18:57 +00:00
|
|
|
import Assistant.ScanRemotes
|
2012-07-31 16:17:31 +00:00
|
|
|
import Assistant.TransferQueue
|
2012-08-12 16:11:20 +00:00
|
|
|
import Assistant.TransferSlots
|
2012-10-27 04:50:14 +00:00
|
|
|
import Assistant.Pushes
|
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
|
2012-08-01 20:29:38 +00:00
|
|
|
import Utility.Daemon (checkDaemon, lockPidFile)
|
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
|
|
|
|
|
2012-07-26 03:13:01 +00:00
|
|
|
def :: [Command]
|
2012-09-16 00:46:38 +00:00
|
|
|
def = [noCommit $ noRepo startNoRepo $ dontCheck repoExists $
|
2012-07-26 16:17:28 +00:00
|
|
|
command "webapp" paramNothing seek "launch webapp"]
|
|
|
|
|
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
|
|
|
|
start' allowauto = notBareRepo $ 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
|
|
|
|
where
|
|
|
|
go = do
|
2012-08-08 17:15:35 +00:00
|
|
|
browser <- fromRepo webBrowser
|
2012-07-27 19:40:52 +00:00
|
|
|
f <- liftIO . absPath =<< fromRepo gitAnnexHtmlShim
|
2012-09-13 04:57:52 +00:00
|
|
|
ifM (checkpid <&&> checkshim f)
|
2012-08-08 17:15:35 +00:00
|
|
|
( liftIO $ openBrowser browser f
|
2012-08-01 20:34:17 +00:00
|
|
|
, startDaemon True True $ Just $
|
2012-08-08 17:15:35 +00:00
|
|
|
const $ openBrowser browser
|
2012-07-27 19:40:52 +00:00
|
|
|
)
|
2012-10-11 16:08:11 +00:00
|
|
|
auto
|
|
|
|
| allowauto = liftIO startNoRepo
|
|
|
|
| otherwise = do
|
|
|
|
d <- liftIO getCurrentDirectory
|
2012-10-11 16:14:23 +00:00
|
|
|
error $ "no git repository in " ++ d
|
2012-07-27 19:33:24 +00:00
|
|
|
checkpid = do
|
2012-07-26 03:13:01 +00:00
|
|
|
pidfile <- fromRepo gitAnnexPidFile
|
2012-07-27 19:33:24 +00:00
|
|
|
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
|
|
|
|
dstatus <- atomically . newTMVar =<< newDaemonStatus
|
2012-08-05 01:18:57 +00:00
|
|
|
scanremotes <- newScanRemoteMap
|
2012-07-31 16:17:31 +00:00
|
|
|
transferqueue <- newTransferQueue
|
2012-08-12 16:11:20 +00:00
|
|
|
transferslots <- newTransferSlots
|
2012-09-08 23:57:15 +00:00
|
|
|
urlrenderer <- newUrlRenderer
|
2012-10-27 04:50:14 +00:00
|
|
|
pushnotifier <- newPushNotifier
|
2012-08-01 20:10:26 +00:00
|
|
|
v <- newEmptyMVar
|
|
|
|
let callback a = Just $ a v
|
2012-09-06 18:56:04 +00:00
|
|
|
void $ runNamedThread dstatus $
|
2012-09-08 23:57:15 +00:00
|
|
|
webAppThread Nothing dstatus scanremotes
|
2012-10-27 04:50:14 +00:00
|
|
|
transferqueue transferslots pushnotifier urlrenderer
|
2012-09-06 18:56:04 +00:00
|
|
|
(callback signaler) (callback mainthread)
|
2012-08-01 20:10:26 +00:00
|
|
|
where
|
|
|
|
signaler v = do
|
|
|
|
putMVar v ""
|
2012-08-01 20:34:17 +00:00
|
|
|
takeMVar v
|
2012-08-01 20:10:26 +00:00
|
|
|
mainthread v _url htmlshim = do
|
2012-09-23 16:43:14 +00:00
|
|
|
browser <- maybe Nothing webBrowser <$> Git.Config.global
|
2012-08-08 17:15:35 +00:00
|
|
|
openBrowser browser htmlshim
|
2012-08-01 20:10:26 +00:00
|
|
|
|
|
|
|
_wait <- takeMVar v
|
|
|
|
|
|
|
|
state <- Annex.new =<< Git.CurrentRepo.get
|
2012-08-01 20:29:38 +00:00
|
|
|
Annex.eval state $ do
|
|
|
|
dummydaemonize
|
2012-08-01 20:10:26 +00:00
|
|
|
startAssistant True id $ Just $ sendurlback v
|
|
|
|
sendurlback v url _htmlshim = putMVar v url
|
2012-08-01 20:29:38 +00:00
|
|
|
{- Set up the pid file in the new repo. -}
|
2012-09-13 04:57:52 +00:00
|
|
|
dummydaemonize =
|
2012-08-01 20:29:38 +00:00
|
|
|
liftIO . lockPidFile =<< fromRepo gitAnnexPidFile
|
2012-08-02 04:42:33 +00:00
|
|
|
|
2012-08-08 17:15:35 +00:00
|
|
|
openBrowser :: Maybe FilePath -> FilePath -> IO ()
|
|
|
|
openBrowser cmd htmlshim = go $ maybe runBrowser runCustomBrowser cmd
|
2012-08-02 04:42:33 +00:00
|
|
|
where
|
|
|
|
url = fileUrl htmlshim
|
2012-10-11 19:19:48 +00:00
|
|
|
go a = do
|
|
|
|
putStrLn ""
|
|
|
|
putStrLn $ "Launching web browser on " ++ url
|
|
|
|
unlessM (a url) $
|
|
|
|
error $ "failed to start web browser"
|
2012-08-08 17:15:35 +00:00
|
|
|
runCustomBrowser c u = boolSystem c [Param u]
|
|
|
|
|
|
|
|
{- 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
|