split out Property.FIle

This commit is contained in:
Joey Hess 2014-03-30 13:12:33 -04:00
parent e741b7b82e
commit 8e7b296e82
7 changed files with 34 additions and 27 deletions

View File

@ -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"
]

View File

@ -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. -}

View File

@ -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

22
Property/File.hs Normal file
View File

@ -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))

View File

@ -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]

View File

@ -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 ]

View File

@ -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"