2012-08-31 19:17:12 +00:00
|
|
|
{- git-annex assistant webapp configurator for ssh-based remotes
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
2012-09-24 18:48:47 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2012-08-31 19:17:12 +00:00
|
|
|
-}
|
|
|
|
|
2013-06-05 01:02:09 +00:00
|
|
|
{-# LANGUAGE QuasiQuotes, TemplateHaskell, OverloadedStrings #-}
|
2013-05-04 18:25:30 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
2012-08-31 19:17:12 +00:00
|
|
|
|
|
|
|
module Assistant.WebApp.Configurators.Ssh where
|
|
|
|
|
2012-11-25 04:26:46 +00:00
|
|
|
import Assistant.WebApp.Common
|
2012-09-10 19:20:18 +00:00
|
|
|
import Assistant.Ssh
|
2012-09-11 01:55:59 +00:00
|
|
|
import Assistant.MakeRemote
|
2012-09-19 18:28:32 +00:00
|
|
|
import Utility.Rsync (rsyncUrlIsShell)
|
2012-09-13 20:47:44 +00:00
|
|
|
import Logs.Remote
|
|
|
|
import Remote
|
2012-10-10 19:35:10 +00:00
|
|
|
import Logs.PreferredContent
|
2012-10-10 20:04:28 +00:00
|
|
|
import Types.StandardGroups
|
2012-10-25 22:17:32 +00:00
|
|
|
import Utility.UserInfo
|
2012-08-31 19:17:12 +00:00
|
|
|
|
|
|
|
import qualified Data.Text as T
|
2012-09-13 20:47:44 +00:00
|
|
|
import qualified Data.Map as M
|
2012-09-29 16:49:23 +00:00
|
|
|
import Network.Socket
|
2012-08-31 19:17:12 +00:00
|
|
|
|
2012-09-02 19:21:40 +00:00
|
|
|
sshConfigurator :: Widget -> Handler RepHtml
|
2012-12-30 03:10:18 +00:00
|
|
|
sshConfigurator = page "Add a remote server" (Just Configuration)
|
2012-09-02 19:21:40 +00:00
|
|
|
|
2012-09-13 20:47:44 +00:00
|
|
|
data SshInput = SshInput
|
2012-12-06 21:09:08 +00:00
|
|
|
{ inputHostname :: Maybe Text
|
|
|
|
, inputUsername :: Maybe Text
|
|
|
|
, inputDirectory :: Maybe Text
|
|
|
|
, inputPort :: Int
|
2012-08-31 22:59:57 +00:00
|
|
|
}
|
2012-09-02 04:27:48 +00:00
|
|
|
deriving (Show)
|
2012-09-02 01:10:40 +00:00
|
|
|
|
2012-09-13 20:47:44 +00:00
|
|
|
{- SshInput is only used for applicative form prompting, this converts
|
2012-09-04 19:27:06 +00:00
|
|
|
- the result of such a form into a SshData. -}
|
2012-09-13 20:47:44 +00:00
|
|
|
mkSshData :: SshInput -> SshData
|
|
|
|
mkSshData s = SshData
|
2012-12-06 21:09:08 +00:00
|
|
|
{ sshHostName = fromMaybe "" $ inputHostname s
|
|
|
|
, sshUserName = inputUsername s
|
|
|
|
, sshDirectory = fromMaybe "" $ inputDirectory s
|
2012-09-10 21:53:51 +00:00
|
|
|
, sshRepoName = genSshRepoName
|
2012-12-06 21:09:08 +00:00
|
|
|
(T.unpack $ fromJust $ inputHostname s)
|
|
|
|
(maybe "" T.unpack $ inputDirectory s)
|
|
|
|
, sshPort = inputPort s
|
2012-09-04 19:27:06 +00:00
|
|
|
, needsPubKey = False
|
|
|
|
, rsyncOnly = False
|
|
|
|
}
|
|
|
|
|
2013-03-16 16:58:59 +00:00
|
|
|
mkSshInput :: SshData -> SshInput
|
|
|
|
mkSshInput s = SshInput
|
|
|
|
{ inputHostname = Just $ sshHostName s
|
|
|
|
, inputUsername = sshUserName s
|
|
|
|
, inputDirectory = Just $ sshDirectory s
|
|
|
|
, inputPort = sshPort s
|
|
|
|
}
|
|
|
|
|
2013-06-03 20:33:05 +00:00
|
|
|
#if MIN_VERSION_yesod(1,2,0)
|
2013-06-02 19:57:22 +00:00
|
|
|
sshInputAForm :: Field Handler Text -> SshInput -> AForm Handler SshInput
|
2013-06-03 20:33:05 +00:00
|
|
|
#else
|
|
|
|
sshInputAForm :: Field WebApp WebApp Text -> SshInput -> AForm WebApp WebApp SshInput
|
|
|
|
#endif
|
2012-12-03 02:33:30 +00:00
|
|
|
sshInputAForm hostnamefield def = SshInput
|
2012-12-06 21:09:08 +00:00
|
|
|
<$> aopt check_hostname "Host name" (Just $ inputHostname def)
|
|
|
|
<*> aopt check_username "User name" (Just $ inputUsername def)
|
|
|
|
<*> aopt textField "Directory" (Just $ Just $ fromMaybe (T.pack gitAnnexAssistantDefaultDir) $ inputDirectory def)
|
|
|
|
<*> areq intField "Port" (Just $ inputPort def)
|
2012-10-31 06:34:03 +00:00
|
|
|
where
|
2013-05-04 20:36:51 +00:00
|
|
|
check_username = checkBool (all (`notElem` "/:@ \t") . T.unpack)
|
|
|
|
bad_username textField
|
|
|
|
|
2013-05-06 20:45:49 +00:00
|
|
|
bad_username = "bad user name" :: Text
|
2013-05-04 20:04:17 +00:00
|
|
|
#ifndef __ANDROID__
|
2013-05-04 20:36:51 +00:00
|
|
|
bad_hostname = "cannot resolve host name" :: Text
|
|
|
|
|
2012-12-03 02:33:30 +00:00
|
|
|
check_hostname = checkM (liftIO . checkdns) hostnamefield
|
2012-10-31 06:34:03 +00:00
|
|
|
checkdns t = do
|
|
|
|
let h = T.unpack t
|
2013-05-04 18:25:30 +00:00
|
|
|
let canonname = Just $ defaultHints { addrFlags = [AI_CANONNAME] }
|
2012-10-31 06:34:03 +00:00
|
|
|
r <- catchMaybeIO $ getAddrInfo canonname (Just h) Nothing
|
|
|
|
return $ case catMaybes . map addrCanonName <$> r of
|
|
|
|
-- canonicalize input hostname if it had no dot
|
|
|
|
Just (fullname:_)
|
|
|
|
| '.' `elem` h -> Right t
|
|
|
|
| otherwise -> Right $ T.pack fullname
|
|
|
|
Just [] -> Right t
|
|
|
|
Nothing -> Left bad_hostname
|
2013-05-04 18:25:30 +00:00
|
|
|
#else
|
2013-05-04 20:04:17 +00:00
|
|
|
-- getAddrInfo currently broken on Android
|
|
|
|
check_hostname = hostnamefield -- unchecked
|
2013-05-04 18:25:30 +00:00
|
|
|
#endif
|
2012-08-31 22:59:57 +00:00
|
|
|
|
|
|
|
data ServerStatus
|
|
|
|
= UntestedServer
|
|
|
|
| UnusableServer Text -- reason why it's not usable
|
|
|
|
| UsableRsyncServer
|
2012-09-13 20:47:44 +00:00
|
|
|
| UsableSshInput
|
2012-09-02 04:27:48 +00:00
|
|
|
deriving (Eq)
|
2012-08-31 22:59:57 +00:00
|
|
|
|
2012-09-02 00:37:35 +00:00
|
|
|
usable :: ServerStatus -> Bool
|
|
|
|
usable UntestedServer = False
|
|
|
|
usable (UnusableServer _) = False
|
|
|
|
usable UsableRsyncServer = True
|
2012-09-13 20:47:44 +00:00
|
|
|
usable UsableSshInput = True
|
2012-08-31 22:59:57 +00:00
|
|
|
|
|
|
|
getAddSshR :: Handler RepHtml
|
2013-03-16 22:48:23 +00:00
|
|
|
getAddSshR = postAddSshR
|
|
|
|
postAddSshR :: Handler RepHtml
|
|
|
|
postAddSshR = sshConfigurator $ do
|
2012-10-25 22:17:32 +00:00
|
|
|
u <- liftIO $ T.pack <$> myUserName
|
2013-06-03 17:51:54 +00:00
|
|
|
((result, form), enctype) <- liftH $
|
2013-03-16 22:48:23 +00:00
|
|
|
runFormPost $ renderBootstrap $ sshInputAForm textField $
|
2012-12-06 21:09:08 +00:00
|
|
|
SshInput Nothing (Just u) Nothing 22
|
2012-08-31 22:59:57 +00:00
|
|
|
case result of
|
2012-09-13 20:47:44 +00:00
|
|
|
FormSuccess sshinput -> do
|
|
|
|
s <- liftIO $ testServer sshinput
|
|
|
|
case s of
|
|
|
|
Left status -> showform form enctype status
|
2013-06-03 17:51:54 +00:00
|
|
|
Right sshdata -> liftH $ redirect $ ConfirmSshR sshdata
|
2012-08-31 22:59:57 +00:00
|
|
|
_ -> showform form enctype UntestedServer
|
2012-10-31 06:34:03 +00:00
|
|
|
where
|
2012-11-25 04:38:11 +00:00
|
|
|
showform form enctype status = $(widgetFile "configurators/ssh/add")
|
2012-08-31 22:59:57 +00:00
|
|
|
|
2013-03-16 16:58:59 +00:00
|
|
|
sshTestModal :: Widget
|
|
|
|
sshTestModal = $(widgetFile "configurators/ssh/testmodal")
|
|
|
|
|
2012-09-13 20:47:44 +00:00
|
|
|
{- To enable an existing rsync special remote, parse the SshInput from
|
|
|
|
- its rsyncurl, and display a form whose only real purpose is to check
|
|
|
|
- if ssh public keys need to be set up. From there, we can proceed with
|
|
|
|
- the usual repo setup; all that code is idempotent.
|
|
|
|
-
|
|
|
|
- Note that there's no EnableSshR because ssh remotes are not special
|
|
|
|
- remotes, and so their configuration is not shared between repositories.
|
|
|
|
-}
|
|
|
|
getEnableRsyncR :: UUID -> Handler RepHtml
|
2013-03-16 22:48:23 +00:00
|
|
|
getEnableRsyncR = postEnableRsyncR
|
|
|
|
postEnableRsyncR :: UUID -> Handler RepHtml
|
|
|
|
postEnableRsyncR u = do
|
2013-03-04 20:36:38 +00:00
|
|
|
m <- fromMaybe M.empty . M.lookup u <$> liftAnnex readRemoteLog
|
2012-10-31 20:14:52 +00:00
|
|
|
case (parseSshRsyncUrl =<< M.lookup "rsyncurl" m, M.lookup "name" m) of
|
|
|
|
(Just sshinput, Just reponame) -> sshConfigurator $ do
|
2013-06-03 17:51:54 +00:00
|
|
|
((result, form), enctype) <- liftH $
|
2013-03-16 22:48:23 +00:00
|
|
|
runFormPost $ renderBootstrap $ sshInputAForm textField sshinput
|
2012-09-13 20:47:44 +00:00
|
|
|
case result of
|
|
|
|
FormSuccess sshinput'
|
2012-12-06 21:09:08 +00:00
|
|
|
| isRsyncNet (inputHostname sshinput') ->
|
2013-06-03 17:51:54 +00:00
|
|
|
void $ liftH $ makeRsyncNet sshinput' reponame (const noop)
|
2012-09-13 20:47:44 +00:00
|
|
|
| otherwise -> do
|
|
|
|
s <- liftIO $ testServer sshinput'
|
|
|
|
case s of
|
|
|
|
Left status -> showform form enctype status
|
|
|
|
Right sshdata -> enable sshdata
|
2012-10-31 20:14:52 +00:00
|
|
|
{ sshRepoName = reponame }
|
2012-09-13 20:47:44 +00:00
|
|
|
_ -> showform form enctype UntestedServer
|
2012-10-31 20:14:52 +00:00
|
|
|
_ -> redirect AddSshR
|
2012-10-31 06:34:03 +00:00
|
|
|
where
|
|
|
|
showform form enctype status = do
|
2013-04-03 21:01:40 +00:00
|
|
|
description <- liftAnnex $ T.pack <$> prettyUUID u
|
2012-10-31 06:34:03 +00:00
|
|
|
$(widgetFile "configurators/ssh/enable")
|
2013-06-03 17:51:54 +00:00
|
|
|
enable sshdata = liftH $ redirect $ ConfirmSshR $
|
2012-10-31 06:34:03 +00:00
|
|
|
sshdata { rsyncOnly = True }
|
2012-09-13 20:47:44 +00:00
|
|
|
|
|
|
|
{- Converts a rsyncurl value to a SshInput. But only if it's a ssh rsync
|
|
|
|
- url; rsync:// urls or bare path names are not supported.
|
|
|
|
-
|
|
|
|
- The hostname is stored mangled in the remote log for rsync special
|
|
|
|
- remotes configured by this webapp. So that mangling has to reversed
|
|
|
|
- here to get back the original hostname.
|
|
|
|
-}
|
|
|
|
parseSshRsyncUrl :: String -> Maybe SshInput
|
|
|
|
parseSshRsyncUrl u
|
|
|
|
| not (rsyncUrlIsShell u) = Nothing
|
|
|
|
| otherwise = Just $ SshInput
|
2012-12-06 21:09:08 +00:00
|
|
|
{ inputHostname = val $ unMangleSshHostName host
|
|
|
|
, inputUsername = if null user then Nothing else val user
|
|
|
|
, inputDirectory = val dir
|
|
|
|
, inputPort = 22
|
2012-09-13 20:47:44 +00:00
|
|
|
}
|
2012-10-31 06:34:03 +00:00
|
|
|
where
|
|
|
|
val = Just . T.pack
|
|
|
|
(userhost, dir) = separate (== ':') u
|
|
|
|
(user, host) = if '@' `elem` userhost
|
|
|
|
then separate (== '@') userhost
|
|
|
|
else (userhost, "")
|
2012-09-13 20:47:44 +00:00
|
|
|
|
2012-09-02 00:37:35 +00:00
|
|
|
{- Test if we can ssh into the server.
|
|
|
|
-
|
|
|
|
- Two probe attempts are made. First, try sshing in using the existing
|
2012-09-02 01:10:40 +00:00
|
|
|
- configuration, but don't let ssh prompt for any password. If
|
2012-09-02 00:37:35 +00:00
|
|
|
- passwordless login is already enabled, use it. Otherwise,
|
2012-09-03 00:43:32 +00:00
|
|
|
- a special ssh key will need to be generated just for this server.
|
2012-09-02 00:37:35 +00:00
|
|
|
-
|
2012-09-02 01:10:40 +00:00
|
|
|
- Once logged into the server, probe to see if git-annex-shell is
|
2012-10-31 06:34:03 +00:00
|
|
|
- available, or rsync. Note that, ~/.ssh/git-annex-shell may be
|
2012-09-26 22:59:18 +00:00
|
|
|
- present, while git-annex-shell is not in PATH.
|
2012-09-02 00:37:35 +00:00
|
|
|
-}
|
2012-09-13 20:47:44 +00:00
|
|
|
testServer :: SshInput -> IO (Either ServerStatus SshData)
|
2012-12-06 21:09:08 +00:00
|
|
|
testServer (SshInput { inputHostname = Nothing }) = return $
|
2012-09-13 20:47:44 +00:00
|
|
|
Left $ UnusableServer "Please enter a host name."
|
2012-12-06 21:09:08 +00:00
|
|
|
testServer sshinput@(SshInput { inputHostname = Just hn }) = do
|
2012-09-04 19:27:06 +00:00
|
|
|
status <- probe [sshOpt "NumberOfPasswordPrompts" "0"]
|
2012-09-02 00:37:35 +00:00
|
|
|
if usable status
|
2012-09-13 20:47:44 +00:00
|
|
|
then ret status False
|
2012-09-02 00:37:35 +00:00
|
|
|
else do
|
2012-09-04 19:27:06 +00:00
|
|
|
status' <- probe []
|
2012-09-13 20:47:44 +00:00
|
|
|
if usable status'
|
|
|
|
then ret status' True
|
|
|
|
else return $ Left status'
|
2012-10-31 06:34:03 +00:00
|
|
|
where
|
|
|
|
ret status needspubkey = return $ Right $ (mkSshData sshinput)
|
|
|
|
{ needsPubKey = needspubkey
|
|
|
|
, rsyncOnly = status == UsableRsyncServer
|
|
|
|
}
|
|
|
|
probe extraopts = do
|
2013-04-23 00:24:53 +00:00
|
|
|
let remotecommand = shellWrap $ intercalate ";"
|
2012-10-31 06:34:03 +00:00
|
|
|
[ report "loggedin"
|
|
|
|
, checkcommand "git-annex-shell"
|
|
|
|
, checkcommand "rsync"
|
|
|
|
, checkcommand shim
|
|
|
|
]
|
|
|
|
knownhost <- knownHost hn
|
|
|
|
let sshopts = filter (not . null) $ extraopts ++
|
|
|
|
{- If this is an already known host, let
|
|
|
|
- ssh check it as usual.
|
|
|
|
- Otherwise, trust the host key. -}
|
|
|
|
[ if knownhost then "" else sshOpt "StrictHostKeyChecking" "no"
|
|
|
|
, "-n" -- don't read from stdin
|
2012-12-06 21:09:08 +00:00
|
|
|
, "-p", show (inputPort sshinput)
|
|
|
|
, genSshHost
|
|
|
|
(fromJust $ inputHostname sshinput)
|
|
|
|
(inputUsername sshinput)
|
2012-10-31 06:34:03 +00:00
|
|
|
, remotecommand
|
|
|
|
]
|
2013-02-26 17:04:37 +00:00
|
|
|
parsetranscript . fst <$> sshTranscript sshopts Nothing
|
2012-10-31 06:34:03 +00:00
|
|
|
parsetranscript s
|
|
|
|
| reported "git-annex-shell" = UsableSshInput
|
|
|
|
| reported shim = UsableSshInput
|
|
|
|
| reported "rsync" = UsableRsyncServer
|
|
|
|
| reported "loggedin" = UnusableServer
|
|
|
|
"Neither rsync nor git-annex are installed on the server. Perhaps you should go install them?"
|
|
|
|
| otherwise = UnusableServer $ T.pack $
|
|
|
|
"Failed to ssh to the server. Transcript: " ++ s
|
|
|
|
where
|
|
|
|
reported r = token r `isInfixOf` s
|
2013-03-29 17:09:30 +00:00
|
|
|
|
2012-10-31 06:34:03 +00:00
|
|
|
checkcommand c = "if which " ++ c ++ "; then " ++ report c ++ "; fi"
|
|
|
|
token r = "git-annex-probe " ++ r
|
|
|
|
report r = "echo " ++ token r
|
|
|
|
shim = "~/.ssh/git-annex-shell"
|
2012-09-04 19:27:06 +00:00
|
|
|
|
|
|
|
{- Runs a ssh command; if it fails shows the user the transcript,
|
|
|
|
- and if it succeeds, runs an action. -}
|
|
|
|
sshSetup :: [String] -> String -> Handler RepHtml -> Handler RepHtml
|
|
|
|
sshSetup opts input a = do
|
2013-02-26 17:04:37 +00:00
|
|
|
(transcript, ok) <- liftIO $ sshTranscript opts (Just input)
|
2012-09-04 19:27:06 +00:00
|
|
|
if ok
|
|
|
|
then a
|
|
|
|
else showSshErr transcript
|
|
|
|
|
|
|
|
showSshErr :: String -> Handler RepHtml
|
|
|
|
showSshErr msg = sshConfigurator $
|
2012-09-09 03:32:08 +00:00
|
|
|
$(widgetFile "configurators/ssh/error")
|
2012-09-04 19:27:06 +00:00
|
|
|
|
2012-09-02 04:27:48 +00:00
|
|
|
getConfirmSshR :: SshData -> Handler RepHtml
|
2012-11-25 04:38:11 +00:00
|
|
|
getConfirmSshR sshdata = sshConfigurator $
|
2012-09-09 03:32:08 +00:00
|
|
|
$(widgetFile "configurators/ssh/confirm")
|
2012-09-02 04:27:48 +00:00
|
|
|
|
2013-03-16 16:58:59 +00:00
|
|
|
getRetrySshR :: SshData -> Handler ()
|
|
|
|
getRetrySshR sshdata = do
|
|
|
|
s <- liftIO $ testServer $ mkSshInput sshdata
|
|
|
|
redirect $ either (const $ ConfirmSshR sshdata) ConfirmSshR s
|
|
|
|
|
2012-09-02 21:32:24 +00:00
|
|
|
getMakeSshGitR :: SshData -> Handler RepHtml
|
2012-10-09 18:24:17 +00:00
|
|
|
getMakeSshGitR = makeSsh False setupGroup
|
2012-09-02 21:32:24 +00:00
|
|
|
|
|
|
|
getMakeSshRsyncR :: SshData -> Handler RepHtml
|
2012-10-09 18:24:17 +00:00
|
|
|
getMakeSshRsyncR = makeSsh True setupGroup
|
2012-09-02 21:32:24 +00:00
|
|
|
|
2012-10-09 18:24:17 +00:00
|
|
|
makeSsh :: Bool -> (Remote -> Handler ()) -> SshData -> Handler RepHtml
|
|
|
|
makeSsh rsync setup sshdata
|
2012-09-03 00:43:32 +00:00
|
|
|
| needsPubKey sshdata = do
|
2012-09-13 04:57:52 +00:00
|
|
|
keypair <- liftIO genSshKeyPair
|
2012-09-10 18:42:46 +00:00
|
|
|
sshdata' <- liftIO $ setupSshKeyPair keypair sshdata
|
2013-06-25 18:46:20 +00:00
|
|
|
makeSsh' rsync setup sshdata sshdata' (Just keypair)
|
2012-12-06 21:09:08 +00:00
|
|
|
| sshPort sshdata /= 22 = do
|
|
|
|
sshdata' <- liftIO $ setSshConfig sshdata []
|
2013-06-25 18:46:20 +00:00
|
|
|
makeSsh' rsync setup sshdata sshdata' Nothing
|
|
|
|
| otherwise = makeSsh' rsync setup sshdata sshdata Nothing
|
2012-09-03 00:43:32 +00:00
|
|
|
|
2013-06-25 18:46:20 +00:00
|
|
|
makeSsh' :: Bool -> (Remote -> Handler ()) -> SshData -> SshData -> Maybe SshKeyPair -> Handler RepHtml
|
|
|
|
makeSsh' rsync setup origsshdata sshdata keypair = do
|
2012-09-04 19:27:06 +00:00
|
|
|
sshSetup [sshhost, remoteCommand] "" $
|
2012-10-09 18:24:17 +00:00
|
|
|
makeSshRepo rsync setup sshdata
|
2012-10-31 06:34:03 +00:00
|
|
|
where
|
2013-06-25 18:46:20 +00:00
|
|
|
sshhost = genSshHost (sshHostName origsshdata) (sshUserName origsshdata)
|
2012-10-31 06:34:03 +00:00
|
|
|
remotedir = T.unpack $ sshDirectory sshdata
|
2013-04-23 00:24:53 +00:00
|
|
|
remoteCommand = shellWrap $ intercalate "&&" $ catMaybes
|
2012-10-31 06:34:03 +00:00
|
|
|
[ Just $ "mkdir -p " ++ shellEscape remotedir
|
|
|
|
, Just $ "cd " ++ shellEscape remotedir
|
2013-04-22 20:21:37 +00:00
|
|
|
, if rsync then Nothing else Just "if [ ! -d .git ]; then git init --bare --shared; fi"
|
2012-10-31 06:34:03 +00:00
|
|
|
, if rsync then Nothing else Just "git annex init"
|
|
|
|
, if needsPubKey sshdata
|
2012-11-05 16:21:13 +00:00
|
|
|
then addAuthorizedKeysCommand (rsyncOnly sshdata) remotedir . sshPubKey <$> keypair
|
2012-10-31 06:34:03 +00:00
|
|
|
else Nothing
|
|
|
|
]
|
2012-09-04 19:27:06 +00:00
|
|
|
|
2012-10-09 18:24:17 +00:00
|
|
|
makeSshRepo :: Bool -> (Remote -> Handler ()) -> SshData -> Handler RepHtml
|
|
|
|
makeSshRepo forcersync setup sshdata = do
|
2013-03-13 18:10:29 +00:00
|
|
|
r <- liftAssistant $ makeSshRemote forcersync sshdata Nothing
|
2012-10-09 18:24:17 +00:00
|
|
|
setup r
|
2012-10-27 16:25:29 +00:00
|
|
|
redirect $ EditNewCloudRepositoryR $ Remote.uuid r
|
2012-09-02 21:32:24 +00:00
|
|
|
|
2012-09-03 04:39:55 +00:00
|
|
|
getAddRsyncNetR :: Handler RepHtml
|
2013-03-16 22:48:23 +00:00
|
|
|
getAddRsyncNetR = postAddRsyncNetR
|
|
|
|
postAddRsyncNetR :: Handler RepHtml
|
|
|
|
postAddRsyncNetR = do
|
|
|
|
((result, form), enctype) <- runFormPost $
|
2012-12-03 02:33:30 +00:00
|
|
|
renderBootstrap $ sshInputAForm hostnamefield $
|
2012-12-06 21:09:08 +00:00
|
|
|
SshInput Nothing Nothing Nothing 22
|
2012-12-30 03:10:18 +00:00
|
|
|
let showform status = page "Add a Rsync.net repository" (Just Configuration) $
|
2012-09-03 04:39:55 +00:00
|
|
|
$(widgetFile "configurators/addrsync.net")
|
|
|
|
case result of
|
2012-09-13 20:47:44 +00:00
|
|
|
FormSuccess sshinput
|
2012-12-06 21:09:08 +00:00
|
|
|
| isRsyncNet (inputHostname sshinput) -> do
|
2012-10-31 20:14:52 +00:00
|
|
|
let reponame = genSshRepoName "rsync.net"
|
2012-12-06 21:09:08 +00:00
|
|
|
(maybe "" T.unpack $ inputDirectory sshinput)
|
2012-10-31 20:14:52 +00:00
|
|
|
makeRsyncNet sshinput reponame setupGroup
|
2012-09-13 20:47:44 +00:00
|
|
|
| otherwise ->
|
|
|
|
showform $ UnusableServer
|
|
|
|
"That is not a rsync.net host name."
|
2012-09-03 04:39:55 +00:00
|
|
|
_ -> showform UntestedServer
|
2012-12-03 02:33:30 +00:00
|
|
|
where
|
2013-04-26 03:44:55 +00:00
|
|
|
hostnamefield = textField `withExpandableNote` ("Help", help)
|
2012-12-03 02:33:30 +00:00
|
|
|
help = [whamlet|
|
2013-04-26 03:44:55 +00:00
|
|
|
<div>
|
|
|
|
When you sign up for a Rsync.net account, you should receive an #
|
|
|
|
email from them with the host name and user name to put here.
|
|
|
|
<div>
|
|
|
|
The host name will be something like "usw-s001.rsync.net", and the #
|
|
|
|
user name something like "7491"
|
2012-12-03 02:33:30 +00:00
|
|
|
|]
|
2012-09-13 20:47:44 +00:00
|
|
|
|
2012-10-31 20:14:52 +00:00
|
|
|
makeRsyncNet :: SshInput -> String -> (Remote -> Handler ()) -> Handler RepHtml
|
|
|
|
makeRsyncNet sshinput reponame setup = do
|
2012-12-06 21:09:08 +00:00
|
|
|
knownhost <- liftIO $ maybe (return False) knownHost (inputHostname sshinput)
|
2012-09-13 20:47:44 +00:00
|
|
|
keypair <- liftIO $ genSshKeyPair
|
|
|
|
sshdata <- liftIO $ setupSshKeyPair keypair $
|
|
|
|
(mkSshData sshinput)
|
2012-10-31 20:14:52 +00:00
|
|
|
{ sshRepoName = reponame
|
2012-09-13 20:47:44 +00:00
|
|
|
, needsPubKey = True
|
|
|
|
, rsyncOnly = True
|
|
|
|
}
|
|
|
|
{- I'd prefer to separate commands with && , but
|
|
|
|
- rsync.net's shell does not support that.
|
|
|
|
-
|
|
|
|
- The dd method of appending to the authorized_keys file is the
|
|
|
|
- one recommended by rsync.net documentation. I touch the file first
|
|
|
|
- to not need to use a different method to create it.
|
|
|
|
-}
|
2013-04-23 00:24:53 +00:00
|
|
|
let remotecommand = intercalate ";"
|
2012-09-13 20:47:44 +00:00
|
|
|
[ "mkdir -p .ssh"
|
|
|
|
, "touch .ssh/authorized_keys"
|
|
|
|
, "dd of=.ssh/authorized_keys oflag=append conv=notrunc"
|
|
|
|
, "mkdir -p " ++ T.unpack (sshDirectory sshdata)
|
|
|
|
]
|
|
|
|
let sshopts = filter (not . null)
|
|
|
|
[ if knownhost then "" else sshOpt "StrictHostKeyChecking" "no"
|
|
|
|
, genSshHost (sshHostName sshdata) (sshUserName sshdata)
|
|
|
|
, remotecommand
|
|
|
|
]
|
|
|
|
sshSetup sshopts (sshPubKey keypair) $
|
2012-10-09 18:24:17 +00:00
|
|
|
makeSshRepo True setup sshdata
|
2012-09-13 20:47:44 +00:00
|
|
|
|
|
|
|
isRsyncNet :: Maybe Text -> Bool
|
|
|
|
isRsyncNet Nothing = False
|
|
|
|
isRsyncNet (Just host) = ".rsync.net" `T.isSuffixOf` T.toLower host
|
2012-10-09 18:24:17 +00:00
|
|
|
|
|
|
|
setupGroup :: Remote -> Handler ()
|
2013-03-04 20:36:38 +00:00
|
|
|
setupGroup r = liftAnnex $ setStandardGroup (Remote.uuid r) TransferGroup
|