diff --git a/Propellor.hs b/Propellor.hs index 1bc1373..44673d1 100644 --- a/Propellor.hs +++ b/Propellor.hs @@ -1,5 +1,6 @@ import Property import HostName +import qualified Property.File as File import qualified Property.Apt as Apt import qualified Property.Ssh as Ssh import qualified Property.User as User @@ -47,7 +48,7 @@ standardSystem suite = propertyList "standard system" , User.nonsystem "joey" , Apt.installed ["sudo"] -- nopasswd because no password is set up for joey. - , lineInFile "/etc/sudoers" "joey ALL=(ALL:ALL) NOPASSWD:ALL" + , "/etc/sudoers" `File.containsLine` "joey ALL=(ALL:ALL) NOPASSWD:ALL" , GitHome.installedFor "joey" ] diff --git a/Property.hs b/Property.hs index 4aba094..f00ddfa 100644 --- a/Property.hs +++ b/Property.hs @@ -127,25 +127,6 @@ cmdProperty' cmd params env = CmdProperty desc cmd params env showp (Param s) = s showp (File s) = s -{- Replaces all the content of a file. -} -fileHasContent :: FilePath -> [Line] -> Property -fileHasContent f newcontent = FileProperty ("replace " ++ f) - f (\_oldcontent -> newcontent) - -{- Ensures that a line is present in a file, adding it to the end if not. -} -lineInFile :: FilePath -> Line -> Property -lineInFile f l = FileProperty (f ++ " contains:" ++ l) f go - where - go ls - | l `elem` ls = ls - | otherwise = ls++[l] - -{- Ensures that a line is not present in a file. - - Note that the file is ensured to exist, so if it doesn't, an empty - - file will be written. -} -lineNotInFile :: FilePath -> Line -> Property -lineNotInFile f l = FileProperty (f ++ " remove: " ++ l) f (filter (/= l)) - {- Makes a perhaps non-idempotent Property be idempotent by using a flag - file to indicate whether it has run before. - Use with caution. -} diff --git a/Property/Apt.hs b/Property/Apt.hs index 4d58574..653c0fc 100644 --- a/Property/Apt.hs +++ b/Property/Apt.hs @@ -7,6 +7,7 @@ import System.IO import Control.Monad import Property +import qualified Property.File as File import Utility.SafeCommand import Utility.Process @@ -47,7 +48,7 @@ stdSourcesList :: Suite -> Property stdSourcesList = setSourcesList . debCdn setSourcesList :: [Line] -> Property -setSourcesList ls = fileHasContent sourcesList ls `onChange` update +setSourcesList ls = sourcesList `File.hasContent` ls `onChange` update runApt :: [CommandParam] -> Property runApt ps = cmdProperty' "apt-get" ps env diff --git a/Property/File.hs b/Property/File.hs new file mode 100644 index 0000000..a4b2196 --- /dev/null +++ b/Property/File.hs @@ -0,0 +1,22 @@ +module Property.File where + +import Property + +{- Replaces all the content of a file. -} +hasContent :: FilePath -> [Line] -> Property +f `hasContent` newcontent = FileProperty ("replace " ++ f) + f (\_oldcontent -> newcontent) + +{- Ensures that a line is present in a file, adding it to the end if not. -} +containsLine :: FilePath -> Line -> Property +f `containsLine` l = FileProperty (f ++ " contains:" ++ l) f go + where + go ls + | l `elem` ls = ls + | otherwise = ls++[l] + +{- Ensures that a line is not present in a file. + - Note that the file is ensured to exist, so if it doesn't, an empty + - file will be written. -} +lacksLine :: FilePath -> Line -> Property +f `lacksLine` l = FileProperty (f ++ " remove: " ++ l) f (filter (/= l)) diff --git a/Property/Hostname.hs b/Property/Hostname.hs index 7baf717..38e9dbe 100644 --- a/Property/Hostname.hs +++ b/Property/Hostname.hs @@ -1,11 +1,11 @@ module Property.Hostname where import Property +import qualified Property.File as File import Utility.SafeCommand type HostName = String set :: HostName -> Property -set hostname = - fileHasContent "/etc/hostname" [hostname] - `onChange` cmdProperty "hostname" [Param hostname] +set hostname = "/etc/hostname" `File.hasContent` [hostname] + `onChange` cmdProperty "hostname" [Param hostname] diff --git a/Property/Ssh.hs b/Property/Ssh.hs index 141e349..98149bc 100644 --- a/Property/Ssh.hs +++ b/Property/Ssh.hs @@ -6,6 +6,7 @@ import System.FilePath import Property import Property.User +import qualified Property.File as File import Utility.SafeCommand import Utility.Exception @@ -18,8 +19,8 @@ sshdConfig = "/etc/ssh/sshd_config" setSshdConfig :: String -> Bool -> Property setSshdConfig setting allowed = combineProperties desc - [ lineNotInFile sshdConfig $ sshline (not allowed) - , lineInFile sshdConfig $ sshline allowed + [ sshdConfig `File.lacksLine` (sshline $ not allowed) + , sshdConfig `File.containsLine` (sshline allowed) ] `onChange` restartSshd where desc = unwords [ "ssh config:", setting, sshBool allowed ] diff --git a/Property/Tor.hs b/Property/Tor.hs index b26ba68..7f7e724 100644 --- a/Property/Tor.hs +++ b/Property/Tor.hs @@ -2,12 +2,13 @@ module Property.Tor where import Property import Utility.SafeCommand +import qualified Property.File as File import qualified Property.Apt as Apt isBridge :: Property isBridge = setup `requires` Apt.installed ["tor"] where - setup = fileHasContent "/etc/tor/torrc" + setup = "/etc/tor/torrc" `File.hasContent` [ "SocksPort 0" , "ORPort 443" , "BridgeRelay 1"