Merge branch 'joeyconfig'

This commit is contained in:
Joey Hess 2015-05-30 13:58:59 -04:00
commit f23b10ffd6
3 changed files with 27 additions and 16 deletions

2
debian/changelog vendored
View File

@ -13,6 +13,8 @@ propellor (2.5.0) UNRELEASED; urgency=medium
* Fix Postfix.satellite bug; the default relayhost was set to the * Fix Postfix.satellite bug; the default relayhost was set to the
domain, not to smtp.domain as documented. domain, not to smtp.domain as documented.
* Mount /proc inside a chroot before provisioning it, to work around #787227 * Mount /proc inside a chroot before provisioning it, to work around #787227
* --spin now works when given a short hostname that only resolves to an
ipv6 address.
-- Joey Hess <id@joeyh.name> Thu, 07 May 2015 12:08:34 -0400 -- Joey Hess <id@joeyh.name> Thu, 07 May 2015 12:08:34 -0400

View File

@ -7,7 +7,7 @@ import System.Environment (getArgs)
import Data.List import Data.List
import System.Exit import System.Exit
import System.PosixCompat import System.PosixCompat
import qualified Network.BSD import Network.Socket
import Propellor import Propellor
import Propellor.Gpg import Propellor.Gpg
@ -165,9 +165,15 @@ updateFirst' cmdline next = ifM fetchOrigin
, next , next
) )
-- Gets the fully qualified domain name, given a string that might be
-- a short name to look up in the DNS.
hostname :: String -> IO HostName hostname :: String -> IO HostName
hostname s hostname s = go =<< catchDefaultIO [] dnslookup
| "." `isInfixOf` s = pure s where
| otherwise = do dnslookup = getAddrInfo (Just canonname) (Just s) Nothing
h <- Network.BSD.getHostByName s canonname = defaultHints { addrFlags = [AI_CANONNAME] }
return (Network.BSD.hostName h) go (AddrInfo { addrCanonName = Just v } : _) = pure v
go _
| "." `isInfixOf` s = pure s -- assume it's a fqdn
| otherwise =
error $ "cannot find host " ++ s ++ " in the DNS"

View File

@ -14,8 +14,7 @@ import System.Posix.Directory
import Control.Concurrent.Async import Control.Concurrent.Async
import qualified Data.ByteString as B import qualified Data.ByteString as B
import qualified Data.Set as S import qualified Data.Set as S
import qualified Network.BSD as BSD import Network.Socket (getAddrInfo, defaultHints, AddrInfo(..), AddrInfoFlag(..), SockAddr)
import Network.Socket (inet_ntoa)
import Propellor import Propellor
import Propellor.Protocol import Propellor.Protocol
@ -98,17 +97,21 @@ spin target relay hst = do
getSshTarget :: HostName -> Host -> IO String getSshTarget :: HostName -> Host -> IO String
getSshTarget target hst getSshTarget target hst
| null configips = return target | null configips = return target
| otherwise = go =<< tryIO (BSD.getHostByName target) | otherwise = go =<< tryIO (dnslookup target)
where where
go (Left e) = useip (show e) go (Left e) = useip (show e)
go (Right hostentry) = ifM (anyM matchingconfig (BSD.hostAddresses hostentry)) go (Right addrinfos) = do
( return target configaddrinfos <- catMaybes <$> mapM iptoaddr configips
, do if any (`elem` configaddrinfos) (map addrAddress addrinfos)
ips <- mapM inet_ntoa (BSD.hostAddresses hostentry) then return target
useip ("DNS " ++ show ips ++ " vs configured " ++ show configips) else useip ("DNS lookup did not return any of the expected addresses " ++ show configips)
)
matchingconfig a = flip elem configips <$> inet_ntoa a dnslookup h = getAddrInfo (Just $ defaultHints { addrFlags = [AI_CANONNAME] }) (Just h) Nothing
-- Convert a string containing an IP address into a SockAddr.
iptoaddr :: String -> IO (Maybe SockAddr)
iptoaddr ip = catchDefaultIO Nothing $ headMaybe . map addrAddress
<$> getAddrInfo (Just $ defaultHints { addrFlags = [AI_NUMERICHOST] }) (Just ip) Nothing
useip why = case headMaybe configips of useip why = case headMaybe configips of
Nothing -> return target Nothing -> return target