This commit is contained in:
Joey Hess 2015-04-19 12:28:08 -04:00
parent 12170c3d5c
commit e49af37fe8
1 changed files with 59 additions and 0 deletions

View File

@ -0,0 +1,59 @@
[[!comment format=mdwn
username="joey"
subject="""comment 1"""
date="2015-04-19T16:07:24Z"
content="""
Propellor makes it very easy to put together a property like this.
Let's start with a property that combines the two properties you mentioned:
hasLoginShell :: UserName -> FilePath -> Property
hasLoginShell user shell = shellSetTo user shell `requires` shellEnabled shell
The shellEnabled property can be easily written using propellor's file
manipulation properties.
-- Need to add an import to the top of the source file.
import qualified Propellor.Property.File as File
shellEnabled :: FilePath -> Property
shellEnabled shell = "/etc/shells" `File.containsLine` shell
And then, we want to actually change the user's shell. The `chsh(1)`
program can do that, so we can simply tell propellor the command line to
run:
shellSetTo :: UserName -> FilePath -> Property
shellSetTo user shell = cmdProperty "chsh" ["--shell", shell, user]
The only remaining problem with this is that shellSetTo runs chsh every
time, and propellor will always display that it's made a change each time
it runs, even when it didn't really do much. Now, there's an easy way to
avoid that problem, we could just tell propellor that it's a trivial
property, and then it will run chsh every time and not think it made any
change:
shellSetTo :: UserName -> FilePath -> Property
shellSetTo user shell = trivial $
cmdProperty "chsh" ["--shell", shell, user]
But, it's not much harder to do this right. Let's make the property
check if the user's shell is already set to the desired value and avoid
doing anything in that case.
shellSetTo :: UserName -> FilePath -> Property
shellSetTo user shell = check needchangeshell $
cmdProperty "chsh" ["--shell", shell, user]
where
needchangeshell = do
currshell <- userShell <$> getUserEntryForName user
return (currshell /= shell)
And that will probably all work, although I've not tested it. You might
want to throw in some uses of `describe` to give the new properties
more useful descriptions.
I hope this has been helpful as an explanation of how to add properties to
Propellor, and if you get these properties to work, a patch adding them
to Propellor.User would be happily merged.
"""]]