summaryrefslogtreecommitdiff
path: root/src/Propellor/Property/ZFS/Process.hs
blob: 61d4473c132711e508baedc3ab41e0cc873357e3 (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
-- | 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
    do
      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 =
	let
		(p, a) = zfsCommand cmd args z
	in
		lines <$> readProcess p a

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