From 47bcd4e1306b808311f39f398b0e539700e5efc6 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Tue, 18 Nov 2014 18:42:36 -0400 Subject: factor out ssh module --- propellor.cabal | 1 + src/Propellor/CmdLine.hs | 38 +------------------------------------- src/Propellor/Ssh.hs | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 37 deletions(-) create mode 100644 src/Propellor/Ssh.hs diff --git a/propellor.cabal b/propellor.cabal index a9aa4d41..2a8e3a02 100644 --- a/propellor.cabal +++ b/propellor.cabal @@ -116,6 +116,7 @@ Library Propellor.Git Propellor.Gpg Propellor.SimpleSh + Propellor.Ssh Propellor.PrivData.Paths Propellor.Protocol Propellor.Property.Docker.Shim diff --git a/src/Propellor/CmdLine.hs b/src/Propellor/CmdLine.hs index c5dcd348..3e24dd34 100644 --- a/src/Propellor/CmdLine.hs +++ b/src/Propellor/CmdLine.hs @@ -10,7 +10,6 @@ import System.Log.Handler.Simple import System.PosixCompat import Control.Exception (bracket) import System.Posix.IO -import Data.Time.Clock.POSIX import Control.Concurrent.Async import qualified Data.ByteString as B import System.Process (std_in, std_out) @@ -20,11 +19,11 @@ import Propellor.Protocol import Propellor.PrivData.Paths import Propellor.Gpg import Propellor.Git +import Propellor.Ssh import qualified Propellor.Property.Docker as Docker import qualified Propellor.Property.Docker.Shim as DockerShim import Utility.FileMode import Utility.SafeCommand -import Utility.UserInfo usage :: IO a usage = do @@ -355,38 +354,3 @@ checkDebugMode = go =<< getEnv "PROPELLOR_DEBUG" updateGlobalLogger rootLoggerName $ setLevel DEBUG . setHandlers [f] go _ = noop - --- Parameters can be passed to both ssh and scp, to enable a ssh connection --- caching socket. --- --- If the socket already exists, check if its mtime is older than 10 --- minutes, and if so stop that ssh process, in order to not try to --- use an old stale connection. (atime would be nicer, but there's --- a good chance a laptop uses noatime) -sshCachingParams :: HostName -> IO [CommandParam] -sshCachingParams hn = do - home <- myHomeDir - let cachedir = home ".ssh" "propellor" - createDirectoryIfMissing False cachedir - let socketfile = cachedir hn ++ ".sock" - let ps = - [ Param "-o", Param ("ControlPath=" ++ socketfile) - , Params "-o ControlMaster=auto -o ControlPersist=yes" - ] - - maybe noop (expireold ps socketfile) - =<< catchMaybeIO (getFileStatus socketfile) - - return ps - - where - expireold ps f s = do - now <- truncate <$> getPOSIXTime :: IO Integer - if modificationTime s > fromIntegral now - tenminutes - then touchFile f - else do - void $ boolSystem "ssh" $ - [ Params "-O stop" ] ++ ps ++ - [ Param "localhost" ] - nukeFile f - tenminutes = 600 diff --git a/src/Propellor/Ssh.hs b/src/Propellor/Ssh.hs new file mode 100644 index 00000000..969517a8 --- /dev/null +++ b/src/Propellor/Ssh.hs @@ -0,0 +1,43 @@ +module Propellor.Ssh where + +import Propellor +import Utility.SafeCommand +import Utility.UserInfo + +import System.PosixCompat +import Data.Time.Clock.POSIX + +-- Parameters can be passed to both ssh and scp, to enable a ssh connection +-- caching socket. +-- +-- If the socket already exists, check if its mtime is older than 10 +-- minutes, and if so stop that ssh process, in order to not try to +-- use an old stale connection. (atime would be nicer, but there's +-- a good chance a laptop uses noatime) +sshCachingParams :: HostName -> IO [CommandParam] +sshCachingParams hn = do + home <- myHomeDir + let cachedir = home ".ssh" "propellor" + createDirectoryIfMissing False cachedir + let socketfile = cachedir hn ++ ".sock" + let ps = + [ Param "-o", Param ("ControlPath=" ++ socketfile) + , Params "-o ControlMaster=auto -o ControlPersist=yes" + ] + + maybe noop (expireold ps socketfile) + =<< catchMaybeIO (getFileStatus socketfile) + + return ps + + where + expireold ps f s = do + now <- truncate <$> getPOSIXTime :: IO Integer + if modificationTime s > fromIntegral now - tenminutes + then touchFile f + else do + void $ boolSystem "ssh" $ + [ Params "-O stop" ] ++ ps ++ + [ Param "localhost" ] + nukeFile f + tenminutes = 600 -- cgit v1.2.3