summaryrefslogtreecommitdiff
path: root/doc/todo/Wishlist:_User.hasLoginShell/comment_1_c02e8783b91c3c0326bf1b317be4694f._comment
blob: 520434067d8135a52ead9da2ba56fd29361d8e5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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.
"""]]