From 24fe88f64069bd7463cb49d923a36abadae8a127 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Wed, 26 Jul 2017 23:49:04 -0400 Subject: Added Network.dhcp' and Network.static', which allow specifying additional options for interfaces files. --- src/Propellor/Property/Network.hs | 59 +++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/Propellor/Property/Network.hs b/src/Propellor/Property/Network.hs index a50d3f95..b581fa3f 100644 --- a/src/Propellor/Property/Network.hs +++ b/src/Propellor/Property/Network.hs @@ -7,6 +7,9 @@ import Data.Char type Interface = String +-- | Options to put in a stanza of an ifupdown interfaces file. +type InterfaceOptions = [(String, String)] + ifUp :: Interface -> Property DebianLike ifUp iface = tightenTargets $ cmdProperty "ifup" [iface] `assume` MadeChange @@ -19,44 +22,51 @@ ifUp iface = tightenTargets $ cmdProperty "ifup" [iface] -- -- No interfaces are brought up or down by this property. cleanInterfacesFile :: Property DebianLike -cleanInterfacesFile = tightenTargets $ hasContent interfacesFile - [ "# Deployed by propellor, do not edit." - , "" - , "source-directory interfaces.d" +cleanInterfacesFile = interfaceFileContains interfacesFile + [ "source-directory interfaces.d" , "" , "# The loopback network interface" , "auto lo" , "iface lo inet loopback" ] + [] `describe` ("clean " ++ interfacesFile) -- | Configures an interface to get its address via dhcp. dhcp :: Interface -> Property DebianLike -dhcp iface = tightenTargets $ hasContent (interfaceDFile iface) +dhcp iface = dhcp' iface mempty + +dhcp' :: Interface -> InterfaceOptions -> Property DebianLike +dhcp' iface options = interfaceFileContains (interfaceDFile iface) [ "auto " ++ iface , "iface " ++ iface ++ " inet dhcp" - ] + ] options `describe` ("dhcp " ++ iface) `requires` interfacesDEnabled newtype Gateway = Gateway IPAddr -- | Configures an interface with a static address and gateway. -static :: Interface -> IPAddr -> Maybe Gateway-> Property DebianLike -static iface addr gateway = - tightenTargets $ hasContent (interfaceDFile iface) ls +static :: Interface -> IPAddr -> Maybe Gateway -> Property DebianLike +static iface addr gateway = static' iface addr gateway mempty + +static' :: Interface -> IPAddr -> Maybe Gateway -> InterfaceOptions -> Property DebianLike +static' iface addr gateway options = + interfaceFileContains (interfaceDFile iface) headerlines options' `describe` ("static IP address for " ++ iface) `requires` interfacesDEnabled where - ls = catMaybes - [ Just $ "auto " ++ iface - , Just $ "iface " ++ iface ++ " " ++ inet ++ " static" - , Just $ "\taddress " ++ val addr + headerlines = + [ "auto " ++ iface + , "iface " ++ iface ++ " " ++ inet ++ " static" + ] + options' = catMaybes + [ Just $ ("address", val addr) , case gateway of Just (Gateway gaddr) -> - Just $ "\tgateway " ++ val gaddr + Just ("gateway", val gaddr) Nothing -> Nothing - ] + ] ++ options inet = case addr of IPv4 _ -> "inet" IPv6 _ -> "inet6" @@ -107,13 +117,13 @@ preserveStatic iface = tightenTargets $ -- | 6to4 ipv6 connection, should work anywhere ipv6to4 :: Property DebianLike -ipv6to4 = tightenTargets $ hasContent (interfaceDFile "sit0") - [ "# Deployed by propellor, do not edit." +ipv6to4 = tightenTargets $ interfaceFileContains (interfaceDFile "sit0") + [ "auto sit0" , "iface sit0 inet6 static" - , "\taddress 2002:5044:5531::1" - , "\tnetmask 64" - , "\tgateway ::192.88.99.1" - , "auto sit0" + ] + [ ("address", "2002:5044:5531::1") + , ("netmask", "64") + , ("gateway", "::192.88.99.1") ] `describe` "ipv6to4" `requires` interfacesDEnabled @@ -137,3 +147,10 @@ interfacesDEnabled :: Property DebianLike interfacesDEnabled = tightenTargets $ containsLine interfacesFile "source-directory interfaces.d" `describe` "interfaces.d directory enabled" + +interfaceFileContains :: FilePath -> [String] -> InterfaceOptions -> Property DebianLike +interfaceFileContains f headerlines options = tightenTargets $ hasContent f $ + warning : headerlines ++ map fmt options + where + fmt (k, v) = "\t" ++ k ++ " " ++ v + warning = "# Deployed by propellor, do not edit." -- cgit v1.2.3