restart UI

Browser behavior is not ideal; a new tab is opened on restart.
Browsers won't let me redirect to a file:// so I cannot use the old tab.
This commit is contained in:
Joey Hess 2013-01-03 18:50:30 -04:00
parent e09b7198d4
commit bad9b6761d
9 changed files with 56 additions and 34 deletions

View file

@ -29,8 +29,8 @@ import Assistant.WebApp.Control
import Assistant.WebApp.OtherRepos
import Assistant.Types.ThreadedMonad
import Utility.WebApp
import Utility.FileMode
import Utility.TempFile
import Utility.FileMode
import Git
import Yesod
@ -84,36 +84,10 @@ webAppThread assistantdata urlrenderer noannex postfirstrun onstartup = thread $
=<< runThreadState (threadState assistantdata) (fromRepo repoPath))
go port webapp htmlshim urlfile = do
let url = myUrl webapp port
maybe noop (`writeFile` url) urlfile
writeHtmlShim url htmlshim
maybe noop (`writeFileProtected` url) urlfile
writeHtmlShim "Starting webapp..." url htmlshim
maybe noop (\a -> a url htmlshim) onstartup
{- Creates a html shim file that's used to redirect into the webapp,
- to avoid exposing the secretToken when launching the web browser. -}
writeHtmlShim :: String -> FilePath -> IO ()
writeHtmlShim url file = viaTmp go file $ genHtmlShim url
where
go tmpfile content = do
h <- openFile tmpfile WriteMode
modifyFileMode tmpfile $ removeModes [groupReadMode, otherReadMode]
hPutStr h content
hClose h
{- TODO: generate this static file using Yesod. -}
genHtmlShim :: String -> String
genHtmlShim url = unlines
[ "<html>"
, "<head>"
, "<title>Starting webapp...</title>"
, "<meta http-equiv=\"refresh\" content=\"0; URL="++url++"\">"
, "<body>"
, "<p>"
, "<a href=\"" ++ url ++ "\">Starting webapp...</a>"
, "</p>"
, "</body>"
, "</html>"
]
myUrl :: WebApp -> PortNumber -> Url
myUrl webapp port = unpack $ yesodRender webapp urlbase HomeR []
where

View file

@ -10,6 +10,7 @@
module Assistant.WebApp.Control where
import Assistant.WebApp.Common
import Locations.UserConfig
import Control.Concurrent
import System.Posix (getProcessID, signalProcess, sigTERM)
@ -26,3 +27,16 @@ getShutdownConfirmedR = page "Shutdown" Nothing $ do
threadDelay 2000000
signalProcess sigTERM =<< getProcessID
$(widgetFile "control/shutdownconfirmed")
{- Quite a hack, and doesn't redirect the browser window. -}
getRestartR :: Handler RepHtml
getRestartR = page "Restarting" Nothing $ do
void $ liftIO $ forkIO $ do
threadDelay 2000000
program <- readProgramFile
unlessM (boolSystem "sh" [Param "-c", Param $ restartcommand program]) $
error "restart failed"
$(widgetFile "control/restarting")
where
restartcommand program = program ++ " assistant --stop; " ++
program ++ " webapp"

View file

@ -125,9 +125,7 @@ openFileBrowser = do
boolSystem cmd [Param path]
return True
, do
clearUltDest
setUltDest $ "file://" ++ path
void $ redirectUltDest HomeR
void $ redirect $ "file://" ++ path
return False
)
where

View file

@ -7,6 +7,7 @@
/shutdown ShutdownR GET
/shutdown/confirm ShutdownConfirmedR GET
/restart RestartR GET
/config ConfigurationR GET
/config/repository RepositoriesR GET

View file

@ -101,3 +101,12 @@ isSticky = checkMode stickyMode
setSticky :: FilePath -> IO ()
setSticky f = modifyFileMode f $ addModes [stickyMode]
{- Writes a file, ensuring that its modes do not allow it to be read
- by anyone other than the current user, before any content is written. -}
writeFileProtected :: FilePath -> String -> IO ()
writeFileProtected file content = do
h <- openFile file WriteMode
modifyFileMode file $ removeModes [groupReadMode, otherReadMode]
hPutStr h content
hClose h

View file

@ -10,6 +10,8 @@
module Utility.WebApp where
import Common
import Utility.TempFile
import Utility.FileMode
import qualified Yesod
import qualified Network.Wai as Wai
@ -188,3 +190,23 @@ insertAuthToken extractToken predicate webapp root pathbits params =
params'
| predicate pathbits = authparam:params
| otherwise = params
{- Creates a html shim file that's used to redirect into the webapp,
- to avoid exposing the secret token when launching the web browser. -}
writeHtmlShim :: String -> String -> FilePath -> IO ()
writeHtmlShim title url file = viaTmp writeFileProtected file $ genHtmlShim title url
{- TODO: generate this static file using Yesod. -}
genHtmlShim :: String -> String -> String
genHtmlShim title url = unlines
[ "<html>"
, "<head>"
, "<title>"++ title ++ "</title>"
, "<meta http-equiv=\"refresh\" content=\"1; URL="++url++"\">"
, "<body>"
, "<p>"
, "<a href=\"" ++ url ++ "\">" ++ title ++ "</a>"
, "</p>"
, "</body>"
, "</html>"
]

2
debian/changelog vendored
View file

@ -1,6 +1,6 @@
git-annex (3.20130103) UNRELEASED; urgency=low
* webapp: Add UI to stop assistant.
* webapp: Add UI to stop and restart assistant.
-- Joey Hess <joeyh@debian.org> Thu, 03 Jan 2013 14:58:45 -0400

View file

@ -0,0 +1,2 @@
<div .span9 .hero-unit>
Restarting...

View file

@ -4,5 +4,7 @@
<i .icon-plus-sign></i> Add another local repository
<a href="@{RepositorySwitcherR}">
<i .icon-folder-close></i> Switch repository
<a href="@{RestartR}">
<i .icon-repeat></i> Restart daemon
<a href="@{ShutdownR}">
<i .icon-off></i> Shut down
<i .icon-off></i> Shutdown daemon