summaryrefslogtreecommitdiff
path: root/src/Propellor/Ssh.hs
diff options
context:
space:
mode:
authorJoey Hess <joey@kitenet.net>2014-11-18 21:18:26 -0400
committerJoey Hess <joey@kitenet.net>2014-11-18 21:18:26 -0400
commitaa3f31940b544e528a5eb3d2e9825a703a8b5013 (patch)
tree0a14bfdf3be969f6029fb54f0c95e2e06ae7d40d /src/Propellor/Ssh.hs
parenta19f01a508747fb1f04849616422d1530e8ec2da (diff)
parentb964b4836321832ad8d3be7268fd3af9ed8f5ea8 (diff)
Merge branch 'joeyconfig'
Diffstat (limited to 'src/Propellor/Ssh.hs')
-rw-r--r--src/Propellor/Ssh.hs43
1 files changed, 43 insertions, 0 deletions
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