root / src / Ganeti / Locking / Locks.hs @ 79786c6b
History | View | Annotate | Download (2.1 kB)
1 |
{-| Ganeti lock structure |
---|---|
2 |
|
3 |
-} |
4 |
|
5 |
{- |
6 |
|
7 |
Copyright (C) 2014 Google Inc. |
8 |
|
9 |
This program is free software; you can redistribute it and/or modify |
10 |
it under the terms of the GNU General Public License as published by |
11 |
the Free Software Foundation; either version 2 of the License, or |
12 |
(at your option) any later version. |
13 |
|
14 |
This program is distributed in the hope that it will be useful, but |
15 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 |
General Public License for more details. |
18 |
|
19 |
You should have received a copy of the GNU General Public License |
20 |
along with this program; if not, write to the Free Software |
21 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
22 |
02110-1301, USA. |
23 |
|
24 |
-} |
25 |
|
26 |
module Ganeti.Locking.Locks |
27 |
( GanetiLocks(..) |
28 |
, GanetiLockAllocation |
29 |
, loadLockAllocation |
30 |
) where |
31 |
|
32 |
import Control.Monad ((>=>)) |
33 |
import qualified Text.JSON as J |
34 |
|
35 |
|
36 |
import Ganeti.BasicTypes |
37 |
import Ganeti.Errors (ResultG) |
38 |
import Ganeti.JSON (readEitherString, fromJResultE) |
39 |
import Ganeti.Locking.Allocation |
40 |
import Ganeti.Locking.Types |
41 |
import Ganeti.Types |
42 |
|
43 |
-- | The type of Locks available in Ganeti. The order of this type |
44 |
-- is the lock oder. |
45 |
data GanetiLocks = BGL deriving (Ord, Eq, Show) |
46 |
-- TODO: add the remaining locks |
47 |
|
48 |
-- | Provide teh String representation of a lock |
49 |
lockName :: GanetiLocks -> String |
50 |
lockName BGL = "cluster/BGL" |
51 |
|
52 |
-- | Obtain a lock from its name. |
53 |
lockFromName :: String -> J.Result GanetiLocks |
54 |
lockFromName "cluster/BGL" = return BGL |
55 |
lockFromName n = fail $ "Unknown lock name '" ++ n ++ "'" |
56 |
|
57 |
instance J.JSON GanetiLocks where |
58 |
showJSON = J.JSString . J.toJSString . lockName |
59 |
readJSON = readEitherString >=> lockFromName |
60 |
|
61 |
|
62 |
instance Lock GanetiLocks where |
63 |
lockImplications BGL = [] |
64 |
|
65 |
-- | The type of lock Allocations in Ganeti. In Ganeti, the owner of |
66 |
-- locks are jobs. |
67 |
type GanetiLockAllocation = LockAllocation GanetiLocks (JobId, FilePath) |
68 |
|
69 |
-- | Load a lock allocation from disk. |
70 |
loadLockAllocation :: FilePath -> ResultG GanetiLockAllocation |
71 |
loadLockAllocation = |
72 |
liftIO . readFile |
73 |
>=> fromJResultE "parsing lock allocation" . J.decodeStrict |