-
Notifications
You must be signed in to change notification settings - Fork 0
/
spellbinder.hs
164 lines (139 loc) · 5.37 KB
/
spellbinder.hs
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#! /usr/bin/env nix-shell
#! nix-shell -i runghc
#! nix-shell -p "haskellPackages.ghcWithPackages (ps: [ ps.dhall ps.extra ps.optparse-applicative ps.text ])"
{-# LANGUAGE OverloadedStrings #-}
import Options.Applicative hiding (optional)
import Data.Semigroup ((<>))
import qualified Data.Text as T
import qualified Data.Text.IO as T
import Data.Char
import Data.Functor
import Data.List
import System.FilePath
import Control.Monad.Extra (concatMapM)
import Text.ParserCombinators.ReadP ( char, satisfy, endBy, skipMany
, skipSpaces, (+++), readP_to_S, optional
, manyTill, eof
)
import Dhall (Type, record, field, string, list) --inputFile)
inputFile :: Type a -> FilePath -> IO a
inputFile = error "Only available in recent dhall releases."
data Args = Args { root :: FilePath
, bindFiles :: [FilePath]
, workingDir :: FilePath
}
args :: Parser Args
args = Args
<$> strOption
( long "root"
<> short 'r'
<> metavar "PATH"
<> help "Root location of the resultant tree" )
<*> some (strOption
( long "binds"
<> short 'b'
<> metavar "PATH"
<> help "Declarative configuration to realize" ))
<*> strOption
( long "working"
<> short 'w'
<> metavar "PATH"
<> value "./"
<> help "Working directory where generated fstab files will be stored" )
data Bind = Bind { source :: FilePath
, target :: FilePath
}
data Binds = Binds { existing :: [Bind]
, necessary :: [Bind]
, redundant :: [Bind]
}
data DhallBind = DhallBind { dbTarget :: FilePath
, dbBinds :: [FilePath]
}
bindRecord :: Type DhallBind
bindRecord = record ( DhallBind <$> field "target" string
<*> field "binds" (list string)
)
-- We need to canonicalize paths to avoid things like including a trailing
-- slash for the root directory making a difference as to the fstab file
-- generated.
canonicalize :: FilePath -> FilePath
canonicalize fp = undefined
tip :: FilePath -> FilePath
tip = last . splitDirectories
readBinds :: FilePath -> [FilePath] -> IO [Bind]
readBinds rt = concatMapM ((toBinds <$>) . inputFile bindRecord)
where
toBinds :: DhallBind -> [Bind]
toBinds db = map (toBind (dbTarget db)) $ dbBinds db
toBind :: FilePath -> FilePath -> Bind
toBind tgt src = Bind { source = src
, target = rt </> tgt </> tip src
}
fstabSep :: String
fstabSep = " "
-- Read from fstab because you can't tell what the top of a mounted subtree is.
-- Even if you could a complete traversal for nested bind mounts would be
-- impractical.
-- XXX: Can we do this Stateless?
readMountpoints :: FilePath -> IO [FilePath]
readMountpoints rtFSTab = parseMountpoints <$> readFile rtFSTab
where
newline = char '\n'
notNewline = satisfy (/= '\n')
notWhiteSpace = satisfy (`notElem` ['\n', '\t', ' '])
fstabComment = char '#' *> endBy notNewline newline
irrelevant = skipMany (skipSpaces +++ optional fstabComment)
fstabEntryMountpoint = irrelevant
*> skipMany notWhiteSpace
*> skipSpaces
*> many notWhiteSpace
>>= \mp -> skipMany notNewline
*> newline
$> mp
parseMountpoints = concatMap fst
<$> readP_to_S (fstabEntryMountpoint `manyTill` eof)
removeMount :: FilePath -> IO ()
removeMount mountpoint = undefined
removeMountpoint :: FilePath -> IO ()
removeMountpoint mountpoint = undefined
removeBind :: Bind -> IO ()
removeBind bind = removeMount tgt >> removeMountpoint tgt
where tgt = target bind
createMountpoint :: FilePath -> IO ()
createMountpoint mountpoint = undefined
createBind :: Bind -> IO ()
createBind bind = createMountpoint (target bind)
reconcile :: [FilePath] -> [Bind] -> Binds
reconcile mountpoints binds = undefined
type Rule = String
createRule :: Bind -> Rule
createRule bind =
intercalate fstabSep [source bind, target bind, "none", "ro,bind"]
replaceFSTab :: FilePath -> [Bind] -> IO ()
-- TODO: include # generated by comment.
replaceFSTab bs = undefined
mountFSTab :: FilePath -> IO ()
mountFSTab = undefined
run :: Args -> IO ()
run as = do
let canonicalRoot = canonicalize $ root as
let canonicalWorkingDir = canonicalize $ workingDir as
let canonicalBinds = map canonicalize $ bindFiles as
let rootFSTab = canonicalWorkingDir </> canonicalRoot <.> "fstab"
desiredBinds <- readBinds canonicalRoot canonicalBinds
mountpoints <- readMountpoints rootFSTab
let binds = reconcile mountpoints desiredBinds
mapM_ removeBind (redundant binds)
mapM_ createBind (necessary binds)
replaceFSTab rootFSTab (necessary binds <> existing binds)
mountFSTab rootFSTab
main :: IO ()
main = run =<< execParser opts
where
versionOption = infoOption "Spellbinder version 1.0" (long "version" <>
short 'V' <> help "Show version")
opts = info (args <**> helper <**> versionOption)
( fullDesc
<> progDesc "Declaratively manage bind mounts"
<> header "spellbinder - A declarative bind mount manager" )