Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Locking / Locks.hs @ e1e36b88

History | View | Annotate | Download (2.2 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 (liftM, (>=>))
33
import qualified Text.JSON as J
34

    
35

    
36
import Ganeti.BasicTypes
37
import Ganeti.Errors (ResultG)
38
import Ganeti.JSON (asJSObject, fromObj, 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
-- | Describe the parts the pieces of information that are needed to
49
-- describe the lock.
50
lockData :: GanetiLocks -> [(String, J.JSValue)]
51
lockData BGL = [("type", J.showJSON "BGL")]
52

    
53
-- | Read a lock form its JSON representation.
54
readLock :: J.JSValue -> J.Result GanetiLocks
55
readLock v =  do
56
  fields <- liftM J.fromJSObject $ asJSObject v
57
  tp <- fromObj fields "type"
58
  case tp of
59
    "BGL" -> return BGL
60
    _ -> fail $ "Unknown lock type " ++ tp
61

    
62
instance J.JSON GanetiLocks where
63
  showJSON = J.JSObject . J.toJSObject . lockData
64
  readJSON = readLock
65

    
66

    
67
instance Lock GanetiLocks where
68
  lockImplications BGL = []
69

    
70
-- | The type of lock Allocations in Ganeti. In Ganeti, the owner of
71
-- locks are jobs.
72
type GanetiLockAllocation = LockAllocation GanetiLocks (JobId, FilePath)
73

    
74
-- | Load a lock allocation from disk.
75
loadLockAllocation :: FilePath -> ResultG GanetiLockAllocation
76
loadLockAllocation =
77
  liftIO . readFile
78
  >=> fromJResultE "parsing lock allocation" . J.decodeStrict