summaryrefslogtreecommitdiff
path: root/src/Propellor/Property/ZFS/Process.hs
blob: 372bac6dfaf84373f2dbb3e9a6b23d0bbf170684 (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
-- | Maintainer: 2016 Evan Cofsky <evan@theunixman.com>
-- 
-- Functions running zfs processes.

module Propellor.Property.ZFS.Process where

import Propellor.Base
import Data.String.Utils (split)
import Data.List

-- | Gets the properties of a ZFS volume.
zfsGetProperties ::  ZFS -> IO ZFSProperties
zfsGetProperties z =
	let plist = fromPropertyList . map (\(_:k:v:_) -> (k, v)) . (map (split "\t"))
	in plist <$> runZfs "get" [Just "-H", Just "-p", Just "all"] z

zfsExists :: ZFS -> IO Bool
zfsExists z = any id . map (isInfixOf (zfsName z))
	<$> runZfs "list" [Just "-H"] z

-- | Runs the zfs command with the arguments.
--
-- Runs the command with -H which will skip the header line and
-- separate all fields with tabs.
--
-- Replaces Nothing in the argument list with the ZFS pool/dataset.
runZfs :: String -> [Maybe String] -> ZFS -> IO [String]
runZfs cmd args z = lines <$> uncurry readProcess (zfsCommand cmd args z)

-- | Return the ZFS command line suitable for readProcess or cmdProperty.
zfsCommand :: String -> [Maybe String] -> ZFS -> (String, [String])
zfsCommand cmd args z = ("zfs", cmd:(map (maybe (zfsName z) id) args))