2012-07-31 05:11:32 +00:00
|
|
|
{- git-annex assistant webapp configurators
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE TypeFamilies, QuasiQuotes, MultiParamTypeClasses, TemplateHaskell, OverloadedStrings, RankNTypes #-}
|
|
|
|
|
|
|
|
module Assistant.WebApp.Configurators where
|
|
|
|
|
|
|
|
import Assistant.Common
|
|
|
|
import Assistant.WebApp
|
|
|
|
import Assistant.WebApp.SideBar
|
2012-08-05 01:18:57 +00:00
|
|
|
import Assistant.Threads.MountWatcher (handleMount)
|
2012-07-31 05:11:32 +00:00
|
|
|
import Utility.Yesod
|
|
|
|
import qualified Remote
|
2012-08-05 18:49:47 +00:00
|
|
|
import Remote.List
|
2012-07-31 05:11:32 +00:00
|
|
|
import Logs.Web (webUUID)
|
|
|
|
import Logs.Trust
|
|
|
|
import Annex.UUID (getUUID)
|
2012-08-01 20:10:26 +00:00
|
|
|
import Init
|
2012-08-05 18:49:47 +00:00
|
|
|
import qualified Git
|
2012-08-01 20:10:26 +00:00
|
|
|
import qualified Git.Construct
|
|
|
|
import qualified Git.Config
|
2012-08-05 18:49:47 +00:00
|
|
|
import qualified Git.Command
|
2012-08-01 20:10:26 +00:00
|
|
|
import qualified Annex
|
2012-08-02 04:42:33 +00:00
|
|
|
import Locations.UserConfig
|
2012-08-02 11:47:50 +00:00
|
|
|
import Utility.FreeDesktop
|
2012-08-04 22:17:16 +00:00
|
|
|
import Utility.Mounts
|
|
|
|
import Utility.DiskFree
|
|
|
|
import Utility.DataUnits
|
2012-08-05 18:49:47 +00:00
|
|
|
import Utility.Network
|
2012-07-31 05:11:32 +00:00
|
|
|
|
|
|
|
import Yesod
|
2012-08-01 01:06:30 +00:00
|
|
|
import Data.Text (Text)
|
2012-08-01 00:56:10 +00:00
|
|
|
import qualified Data.Text as T
|
|
|
|
import Data.Char
|
2012-08-01 20:10:26 +00:00
|
|
|
import System.Posix.Directory
|
2012-08-04 22:17:16 +00:00
|
|
|
import qualified Control.Exception as E
|
2012-07-31 05:11:32 +00:00
|
|
|
|
2012-08-03 18:36:16 +00:00
|
|
|
{- The main configuration screen. -}
|
|
|
|
getConfigR :: Handler RepHtml
|
|
|
|
getConfigR = ifM (inFirstRun)
|
|
|
|
( getFirstRepositoryR
|
|
|
|
, bootstrap (Just Config) $ do
|
2012-08-04 00:40:34 +00:00
|
|
|
sideBarDisplay
|
2012-08-03 18:36:16 +00:00
|
|
|
setTitle "Configuration"
|
|
|
|
$(widgetFile "configurators/main")
|
|
|
|
)
|
|
|
|
|
2012-08-05 23:55:06 +00:00
|
|
|
{- Lists known repositories, followed by options to add more. -}
|
|
|
|
getRepositoriesR :: Handler RepHtml
|
|
|
|
getRepositoriesR = bootstrap (Just Config) $ do
|
2012-08-04 00:40:34 +00:00
|
|
|
sideBarDisplay
|
2012-08-05 23:55:06 +00:00
|
|
|
setTitle "Repositories"
|
2012-08-04 00:40:34 +00:00
|
|
|
repolist <- lift repoList
|
2012-08-05 23:55:06 +00:00
|
|
|
$(widgetFile "configurators/repositories")
|
2012-08-04 00:40:34 +00:00
|
|
|
|
|
|
|
{- A numbered list of known repositories, including the current one. -}
|
|
|
|
repoList :: Handler [(String, String)]
|
2012-08-03 18:36:16 +00:00
|
|
|
repoList = do
|
|
|
|
l <- runAnnex [] $ do
|
2012-07-31 05:11:32 +00:00
|
|
|
u <- getUUID
|
|
|
|
rs <- map Remote.uuid <$> Remote.remoteList
|
|
|
|
rs' <- snd <$> trustPartition DeadTrusted rs
|
|
|
|
Remote.prettyListUUIDs $ filter (/= webUUID) $ nub $ u:rs'
|
2012-08-04 00:40:34 +00:00
|
|
|
return $ zip counter l
|
2012-07-31 05:11:32 +00:00
|
|
|
where
|
|
|
|
counter = map show ([1..] :: [Int])
|
2012-08-03 18:36:16 +00:00
|
|
|
|
|
|
|
{- An intro message, list of repositories, and nudge to make more. -}
|
|
|
|
introDisplay :: Text -> Widget
|
|
|
|
introDisplay ident = do
|
|
|
|
webapp <- lift getYesod
|
2012-08-04 00:40:34 +00:00
|
|
|
repolist <- lift repoList
|
|
|
|
let n = length repolist
|
|
|
|
let numrepos = show n
|
|
|
|
let notenough = n < enough
|
|
|
|
let barelyenough = n == enough
|
|
|
|
let morethanenough = n > enough
|
2012-08-03 18:36:16 +00:00
|
|
|
$(widgetFile "configurators/intro")
|
|
|
|
lift $ modifyWebAppState $ \s -> s { showIntro = False }
|
2012-08-04 00:40:34 +00:00
|
|
|
where
|
|
|
|
enough = 2
|
2012-07-31 05:11:32 +00:00
|
|
|
|
2012-07-31 21:57:08 +00:00
|
|
|
data RepositoryPath = RepositoryPath Text
|
|
|
|
deriving Show
|
|
|
|
|
2012-08-01 00:56:10 +00:00
|
|
|
{- Custom field display for a RepositoryPath, with an icon etc.
|
|
|
|
-
|
|
|
|
- Validates that the path entered is not empty, and is a safe value
|
|
|
|
- to use as a repository. -}
|
|
|
|
repositoryPathField :: forall sub. Bool -> Field sub WebApp Text
|
|
|
|
repositoryPathField autofocus = Field { fieldParse = parse, fieldView = view }
|
|
|
|
where
|
|
|
|
view idAttr nameAttr attrs val isReq =
|
|
|
|
[whamlet|<input type="text" *{attrs} id="#{idAttr}" name="#{nameAttr}" :isReq:required :autofocus:autofocus value="#{either id id val}">|]
|
|
|
|
|
|
|
|
parse [path]
|
|
|
|
| T.null path = nopath
|
|
|
|
| otherwise = liftIO $ checkRepositoryPath path
|
|
|
|
parse [] = return $ Right Nothing
|
|
|
|
parse _ = nopath
|
|
|
|
|
|
|
|
nopath = return $ Left "Enter a location for the repository"
|
|
|
|
|
|
|
|
{- As well as checking the path for a lot of silly things, tilde is
|
|
|
|
- expanded in the returned path. -}
|
|
|
|
checkRepositoryPath :: Text -> IO (Either (SomeMessage WebApp) (Maybe Text))
|
|
|
|
checkRepositoryPath p = do
|
|
|
|
home <- myHomeDir
|
|
|
|
let basepath = expandTilde home $ T.unpack p
|
|
|
|
path <- absPath basepath
|
|
|
|
let parent = parentDir path
|
|
|
|
problems <- catMaybes <$> mapM runcheck
|
|
|
|
[ (return $ path == "/", "Enter the full path to use for the repository.")
|
|
|
|
, (return $ all isSpace basepath, "A blank path? Seems unlikely.")
|
|
|
|
, (doesFileExist path, "A file already exists with that name.")
|
|
|
|
, (return $ path == home, "Sorry, using git-annex for your whole home directory is not currently supported.")
|
|
|
|
, (not <$> doesDirectoryExist parent, "Parent directory does not exist.")
|
2012-08-04 22:17:16 +00:00
|
|
|
, (not <$> canWrite path, "Cannot write a repository there.")
|
|
|
|
, (not <$> canMakeSymlink path, "That directory is on a filesystem that does not support symlinks. Try a different location.")
|
2012-08-01 00:56:10 +00:00
|
|
|
]
|
|
|
|
return $
|
|
|
|
case headMaybe problems of
|
|
|
|
Nothing -> Right $ Just $ T.pack basepath
|
|
|
|
Just prob -> Left prob
|
|
|
|
where
|
|
|
|
runcheck (chk, msg) = ifM (chk)
|
|
|
|
( return $ Just msg
|
|
|
|
, return Nothing
|
|
|
|
)
|
2012-08-02 11:55:10 +00:00
|
|
|
expandTilde home ('~':'/':path) = home </> path
|
2012-08-01 00:56:10 +00:00
|
|
|
expandTilde _ path = path
|
|
|
|
|
2012-08-01 01:34:29 +00:00
|
|
|
{- On first run, if run in the home directory, default to putting it in
|
|
|
|
- ~/Desktop/annex, when a Desktop directory exists, and ~/annex otherwise.
|
2012-08-01 01:06:30 +00:00
|
|
|
-
|
|
|
|
- If run in another directory, the user probably wants to put it there. -}
|
2012-08-01 01:34:29 +00:00
|
|
|
defaultRepositoryPath :: Bool -> IO FilePath
|
|
|
|
defaultRepositoryPath firstrun = do
|
2012-08-01 01:06:30 +00:00
|
|
|
cwd <- liftIO $ getCurrentDirectory
|
|
|
|
home <- myHomeDir
|
2012-08-01 20:10:26 +00:00
|
|
|
if home == cwd && firstrun
|
2012-08-02 11:47:39 +00:00
|
|
|
then do
|
|
|
|
desktop <- userDesktopDir
|
|
|
|
ifM (doesDirectoryExist desktop)
|
|
|
|
(relHome (desktop </> "annex"), return "~/annex")
|
2012-08-01 01:06:30 +00:00
|
|
|
else return cwd
|
|
|
|
|
2012-08-04 22:17:16 +00:00
|
|
|
localRepositoryForm :: Form RepositoryPath
|
|
|
|
localRepositoryForm msg = do
|
2012-08-01 01:06:30 +00:00
|
|
|
path <- T.pack . addTrailingPathSeparator
|
2012-08-01 20:10:26 +00:00
|
|
|
<$> (liftIO . defaultRepositoryPath =<< lift inFirstRun)
|
|
|
|
(pathRes, pathView) <- mreq (repositoryPathField True) "" (Just path)
|
2012-08-01 00:56:10 +00:00
|
|
|
let (err, errmsg) = case pathRes of
|
|
|
|
FormMissing -> (False, "")
|
|
|
|
FormFailure l -> (True, concat $ map T.unpack l)
|
|
|
|
FormSuccess _ -> (False, "")
|
2012-07-31 22:33:19 +00:00
|
|
|
let form = do
|
2012-07-31 21:57:08 +00:00
|
|
|
webAppFormAuthToken
|
2012-08-03 18:36:16 +00:00
|
|
|
$(widgetFile "configurators/localrepositoryform")
|
2012-07-31 22:33:19 +00:00
|
|
|
return (RepositoryPath <$> pathRes, form)
|
2012-07-31 21:57:08 +00:00
|
|
|
|
2012-08-04 22:17:16 +00:00
|
|
|
{- Making the first repository, when starting the webapp for the first time. -}
|
2012-08-03 18:36:16 +00:00
|
|
|
getFirstRepositoryR :: Handler RepHtml
|
|
|
|
getFirstRepositoryR = bootstrap (Just Config) $ do
|
2012-08-04 00:40:34 +00:00
|
|
|
sideBarDisplay
|
2012-08-03 18:36:16 +00:00
|
|
|
setTitle "Getting started"
|
2012-08-04 22:17:16 +00:00
|
|
|
((res, form), enctype) <- lift $ runFormGet localRepositoryForm
|
2012-07-31 21:57:08 +00:00
|
|
|
case res of
|
2012-08-03 18:36:16 +00:00
|
|
|
FormSuccess (RepositoryPath p) -> lift $
|
|
|
|
startFullAssistant $ T.unpack p
|
|
|
|
_ -> $(widgetFile "configurators/firstrepository")
|
2012-08-01 20:10:26 +00:00
|
|
|
|
2012-08-04 22:17:16 +00:00
|
|
|
data RemovableDrive = RemovableDrive
|
|
|
|
{ diskFree :: Maybe Integer
|
|
|
|
, mountPoint :: Text
|
|
|
|
}
|
|
|
|
deriving (Show, Eq, Ord)
|
|
|
|
|
|
|
|
selectDriveForm :: [RemovableDrive] -> Maybe RemovableDrive -> Form RemovableDrive
|
|
|
|
selectDriveForm drives def = renderBootstrap $ RemovableDrive
|
|
|
|
<$> pure Nothing
|
|
|
|
<*> areq (selectFieldList pairs) "Select drive:" (mountPoint <$> def)
|
|
|
|
where
|
|
|
|
pairs = zip (map describe drives) (map mountPoint drives)
|
|
|
|
describe drive = case diskFree drive of
|
|
|
|
Nothing -> mountPoint drive
|
|
|
|
Just free ->
|
|
|
|
let sz = roughSize storageUnits True free
|
|
|
|
in T.unwords
|
|
|
|
[ mountPoint drive
|
|
|
|
, T.concat ["(", T.pack sz]
|
|
|
|
, "free)"
|
|
|
|
]
|
|
|
|
|
2012-08-05 01:18:57 +00:00
|
|
|
{- Adding a removable drive. -}
|
2012-08-04 22:17:16 +00:00
|
|
|
getAddDriveR :: Handler RepHtml
|
|
|
|
getAddDriveR = bootstrap (Just Config) $ do
|
|
|
|
sideBarDisplay
|
2012-08-05 19:57:30 +00:00
|
|
|
setTitle "Add a removable drive"
|
2012-08-04 22:17:16 +00:00
|
|
|
removabledrives <- liftIO $ driveList
|
|
|
|
writabledrives <- liftIO $
|
|
|
|
filterM (canWrite . T.unpack . mountPoint) removabledrives
|
|
|
|
((res, form), enctype) <- lift $ runFormGet $
|
|
|
|
selectDriveForm (sort writabledrives) Nothing
|
|
|
|
case res of
|
|
|
|
FormSuccess (RemovableDrive { mountPoint = d }) -> lift $ do
|
2012-08-05 18:49:47 +00:00
|
|
|
go $ T.unpack d
|
2012-08-04 22:17:16 +00:00
|
|
|
setMessage $ toHtml $ T.unwords ["Added", d]
|
2012-08-05 23:55:06 +00:00
|
|
|
redirect RepositoriesR
|
2012-08-04 22:17:16 +00:00
|
|
|
_ -> do
|
|
|
|
let authtoken = webAppFormAuthToken
|
|
|
|
$(widgetFile "configurators/adddrive")
|
|
|
|
where
|
2012-08-05 18:49:47 +00:00
|
|
|
go mountpoint = do
|
|
|
|
liftIO $ makerepo dir
|
|
|
|
liftIO $ initRepo dir $ Just remotename
|
|
|
|
addremotes dir remotename
|
|
|
|
webapp <- getYesod
|
|
|
|
liftIO $ syncrepo dir webapp
|
|
|
|
where
|
|
|
|
dir = mountpoint </> "annex"
|
|
|
|
remotename = takeFileName mountpoint
|
|
|
|
{- The repo may already exist, when adding removable media
|
|
|
|
- that has already been used elsewhere. -}
|
|
|
|
makerepo dir = liftIO $ do
|
|
|
|
r <- E.try (inDir dir $ return True) :: IO (Either E.SomeException Bool)
|
2012-08-05 01:18:57 +00:00
|
|
|
case r of
|
|
|
|
Right _ -> noop
|
2012-08-04 22:17:16 +00:00
|
|
|
Left _e -> do
|
|
|
|
createDirectoryIfMissing True dir
|
|
|
|
bare <- not <$> canMakeSymlink dir
|
|
|
|
makeRepo dir bare
|
2012-08-05 18:49:47 +00:00
|
|
|
{- Synthesize a mount event of the new git repository.
|
|
|
|
- This will sync it, and queue file transfers. -}
|
|
|
|
syncrepo dir webapp =
|
2012-08-05 01:18:57 +00:00
|
|
|
handleMount
|
|
|
|
(fromJust $ threadState webapp)
|
|
|
|
(daemonStatus webapp)
|
|
|
|
(scanRemotes webapp)
|
|
|
|
dir
|
2012-08-05 18:49:47 +00:00
|
|
|
{- Each repository is made a remote of the other. -}
|
|
|
|
addremotes dir name = runAnnex () $ do
|
|
|
|
hostname <- maybe "host" id <$> liftIO getHostname
|
|
|
|
hostlocation <- fromRepo Git.repoLocation
|
|
|
|
void $ liftIO $ inDir dir $
|
|
|
|
addremote hostname hostlocation
|
|
|
|
whenM (addremote name dir) $
|
|
|
|
void $ remoteListRefresh
|
|
|
|
{- Adds a remote only if there is not already one with
|
|
|
|
- the location. -}
|
|
|
|
addremote name location = inRepo $ \r ->
|
|
|
|
if (null $ filter samelocation $ Git.remotes r)
|
|
|
|
then do
|
|
|
|
let name' = uniqueremotename r name (0 :: Int)
|
|
|
|
Git.Command.runBool "remote"
|
|
|
|
[Param "add", Param name', Param location] r
|
|
|
|
else return False
|
|
|
|
where
|
|
|
|
samelocation x = Git.repoLocation x == location
|
|
|
|
{- Generate an unused name for a remote, adding a number if
|
|
|
|
- necessary. -}
|
|
|
|
uniqueremotename r basename n
|
|
|
|
| null namecollision = name
|
|
|
|
| otherwise = uniqueremotename r basename (succ n)
|
2012-08-04 22:17:16 +00:00
|
|
|
where
|
2012-08-05 18:49:47 +00:00
|
|
|
namecollision = filter samename (Git.remotes r)
|
|
|
|
samename x = Git.remoteName x == Just name
|
|
|
|
name
|
|
|
|
| n == 0 = basename
|
|
|
|
| otherwise = basename ++ show n
|
2012-08-04 22:17:16 +00:00
|
|
|
|
|
|
|
{- List of removable drives. -}
|
|
|
|
driveList :: IO [RemovableDrive]
|
|
|
|
driveList = mapM (gen . mnt_dir) =<< filter sane <$> getMounts
|
|
|
|
where
|
|
|
|
gen dir = RemovableDrive
|
|
|
|
<$> getDiskFree dir
|
|
|
|
<*> pure (T.pack dir)
|
|
|
|
-- filter out some things that are surely not removable drives
|
|
|
|
sane Mntent { mnt_dir = dir, mnt_fsname = dev }
|
2012-08-05 01:18:57 +00:00
|
|
|
{- We want real disks like /dev/foo, not
|
|
|
|
- dummy mount points like proc or tmpfs or
|
|
|
|
- gvfs-fuse-daemon. -}
|
2012-08-04 22:17:16 +00:00
|
|
|
| not ('/' `elem` dev) = False
|
2012-08-05 01:18:57 +00:00
|
|
|
{- Just in case: These mount points are surely not
|
|
|
|
- removable disks. -}
|
2012-08-04 22:17:16 +00:00
|
|
|
| dir == "/" = False
|
|
|
|
| dir == "/tmp" = False
|
|
|
|
| dir == "/run/shm" = False
|
|
|
|
| dir == "/run/lock" = False
|
|
|
|
| otherwise = True
|
|
|
|
|
2012-08-01 20:10:26 +00:00
|
|
|
{- Bootstraps from first run mode to a fully running assistant in a
|
|
|
|
- repository, by running the postFirstRun callback, which returns the
|
|
|
|
- url to the new webapp. -}
|
|
|
|
startFullAssistant :: FilePath -> Handler ()
|
|
|
|
startFullAssistant path = do
|
|
|
|
webapp <- getYesod
|
|
|
|
url <- liftIO $ do
|
2012-08-04 22:17:16 +00:00
|
|
|
makeRepo path False
|
|
|
|
initRepo path Nothing
|
|
|
|
addAutoStart path
|
2012-08-01 20:10:26 +00:00
|
|
|
changeWorkingDirectory path
|
2012-08-01 20:31:16 +00:00
|
|
|
fromJust $ postFirstRun webapp
|
2012-08-01 20:10:26 +00:00
|
|
|
redirect $ T.pack url
|
|
|
|
|
|
|
|
{- Makes a new git-annex repository. -}
|
2012-08-04 22:17:16 +00:00
|
|
|
makeRepo :: FilePath -> Bool -> IO ()
|
|
|
|
makeRepo path bare = do
|
|
|
|
unlessM (boolSystem "git" params) $
|
2012-08-01 20:10:26 +00:00
|
|
|
error "git init failed!"
|
2012-08-04 22:17:16 +00:00
|
|
|
where
|
|
|
|
baseparams = [Param "init", Param "--quiet"]
|
|
|
|
params
|
|
|
|
| bare = baseparams ++ [Param "--bare", File path]
|
|
|
|
| otherwise = baseparams ++ [File path]
|
|
|
|
|
2012-08-05 18:49:47 +00:00
|
|
|
{- Runs an action in the git-annex repository in the specified directory. -}
|
|
|
|
inDir :: FilePath -> Annex a -> IO a
|
|
|
|
inDir dir a = do
|
|
|
|
state <- Annex.new =<< Git.Config.read =<< Git.Construct.fromPath dir
|
|
|
|
Annex.eval state a
|
|
|
|
|
2012-08-04 22:17:16 +00:00
|
|
|
{- Initializes a git-annex repository in a directory with a description. -}
|
|
|
|
initRepo :: FilePath -> Maybe String -> IO ()
|
2012-08-05 18:49:47 +00:00
|
|
|
initRepo dir desc = inDir dir $
|
|
|
|
unlessM isInitialized $
|
|
|
|
initialize desc
|
2012-08-04 22:17:16 +00:00
|
|
|
|
|
|
|
{- Adds a directory to the autostart file. -}
|
|
|
|
addAutoStart :: FilePath -> IO ()
|
|
|
|
addAutoStart path = do
|
2012-08-02 04:42:33 +00:00
|
|
|
autostart <- autoStartFile
|
|
|
|
createDirectoryIfMissing True (parentDir autostart)
|
|
|
|
appendFile autostart $ path ++ "\n"
|
2012-08-04 22:17:16 +00:00
|
|
|
|
|
|
|
{- Checks if the user can write to a directory.
|
|
|
|
-
|
|
|
|
- The directory may be in the process of being created; if so
|
|
|
|
- the parent directory is checked instead. -}
|
|
|
|
canWrite :: FilePath -> IO Bool
|
|
|
|
canWrite dir = do
|
|
|
|
tocheck <- ifM (doesDirectoryExist dir)
|
|
|
|
(return dir, return $ parentDir dir)
|
|
|
|
catchBoolIO $ fileAccess tocheck False True False
|
|
|
|
|
|
|
|
{- Checks if a directory is on a filesystem that supports symlinks. -}
|
|
|
|
canMakeSymlink :: FilePath -> IO Bool
|
|
|
|
canMakeSymlink dir = catchBoolIO $ do
|
|
|
|
createSymbolicLink link link
|
|
|
|
removeLink link
|
|
|
|
return True
|
|
|
|
where
|
|
|
|
link = dir </> "delete.me"
|