Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (2.2 kB)

1 95eb97c8 Klaus Aehlig
{-| Ganeti lock structure
2 95eb97c8 Klaus Aehlig
3 95eb97c8 Klaus Aehlig
-}
4 95eb97c8 Klaus Aehlig
5 95eb97c8 Klaus Aehlig
{-
6 95eb97c8 Klaus Aehlig
7 95eb97c8 Klaus Aehlig
Copyright (C) 2014 Google Inc.
8 95eb97c8 Klaus Aehlig
9 95eb97c8 Klaus Aehlig
This program is free software; you can redistribute it and/or modify
10 95eb97c8 Klaus Aehlig
it under the terms of the GNU General Public License as published by
11 95eb97c8 Klaus Aehlig
the Free Software Foundation; either version 2 of the License, or
12 95eb97c8 Klaus Aehlig
(at your option) any later version.
13 95eb97c8 Klaus Aehlig
14 95eb97c8 Klaus Aehlig
This program is distributed in the hope that it will be useful, but
15 95eb97c8 Klaus Aehlig
WITHOUT ANY WARRANTY; without even the implied warranty of
16 95eb97c8 Klaus Aehlig
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 95eb97c8 Klaus Aehlig
General Public License for more details.
18 95eb97c8 Klaus Aehlig
19 95eb97c8 Klaus Aehlig
You should have received a copy of the GNU General Public License
20 95eb97c8 Klaus Aehlig
along with this program; if not, write to the Free Software
21 95eb97c8 Klaus Aehlig
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 95eb97c8 Klaus Aehlig
02110-1301, USA.
23 95eb97c8 Klaus Aehlig
24 95eb97c8 Klaus Aehlig
-}
25 95eb97c8 Klaus Aehlig
26 95eb97c8 Klaus Aehlig
module Ganeti.Locking.Locks
27 95eb97c8 Klaus Aehlig
  ( GanetiLocks(..)
28 95eb97c8 Klaus Aehlig
  , GanetiLockAllocation
29 c8751a72 Klaus Aehlig
  , loadLockAllocation
30 95eb97c8 Klaus Aehlig
  ) where
31 95eb97c8 Klaus Aehlig
32 c8751a72 Klaus Aehlig
import Control.Monad (liftM, (>=>))
33 15a53b1e Klaus Aehlig
import qualified Text.JSON as J
34 15a53b1e Klaus Aehlig
35 c8751a72 Klaus Aehlig
36 c8751a72 Klaus Aehlig
import Ganeti.BasicTypes
37 c8751a72 Klaus Aehlig
import Ganeti.Errors (ResultG)
38 c8751a72 Klaus Aehlig
import Ganeti.JSON (asJSObject, fromObj, fromJResultE)
39 95eb97c8 Klaus Aehlig
import Ganeti.Locking.Allocation
40 75033afd Klaus Aehlig
import Ganeti.Locking.Types
41 95eb97c8 Klaus Aehlig
import Ganeti.Types
42 95eb97c8 Klaus Aehlig
43 95eb97c8 Klaus Aehlig
-- | The type of Locks available in Ganeti. The order of this type
44 95eb97c8 Klaus Aehlig
-- is the lock oder.
45 95eb97c8 Klaus Aehlig
data GanetiLocks = BGL deriving (Ord, Eq, Show)
46 95eb97c8 Klaus Aehlig
-- TODO: add the remaining locks
47 95eb97c8 Klaus Aehlig
48 15a53b1e Klaus Aehlig
-- | Describe the parts the pieces of information that are needed to
49 15a53b1e Klaus Aehlig
-- describe the lock.
50 15a53b1e Klaus Aehlig
lockData :: GanetiLocks -> [(String, J.JSValue)]
51 15a53b1e Klaus Aehlig
lockData BGL = [("type", J.showJSON "BGL")]
52 15a53b1e Klaus Aehlig
53 15a53b1e Klaus Aehlig
-- | Read a lock form its JSON representation.
54 15a53b1e Klaus Aehlig
readLock :: J.JSValue -> J.Result GanetiLocks
55 15a53b1e Klaus Aehlig
readLock v =  do
56 15a53b1e Klaus Aehlig
  fields <- liftM J.fromJSObject $ asJSObject v
57 15a53b1e Klaus Aehlig
  tp <- fromObj fields "type"
58 15a53b1e Klaus Aehlig
  case tp of
59 15a53b1e Klaus Aehlig
    "BGL" -> return BGL
60 15a53b1e Klaus Aehlig
    _ -> fail $ "Unknown lock type " ++ tp
61 15a53b1e Klaus Aehlig
62 15a53b1e Klaus Aehlig
instance J.JSON GanetiLocks where
63 15a53b1e Klaus Aehlig
  showJSON = J.JSObject . J.toJSObject . lockData
64 15a53b1e Klaus Aehlig
  readJSON = readLock
65 15a53b1e Klaus Aehlig
66 15a53b1e Klaus Aehlig
67 75033afd Klaus Aehlig
instance Lock GanetiLocks where
68 75033afd Klaus Aehlig
  lockImplications BGL = []
69 75033afd Klaus Aehlig
70 95eb97c8 Klaus Aehlig
-- | The type of lock Allocations in Ganeti. In Ganeti, the owner of
71 95eb97c8 Klaus Aehlig
-- locks are jobs.
72 95eb97c8 Klaus Aehlig
type GanetiLockAllocation = LockAllocation GanetiLocks JobId
73 c8751a72 Klaus Aehlig
74 c8751a72 Klaus Aehlig
-- | Load a lock allocation from disk.
75 c8751a72 Klaus Aehlig
loadLockAllocation :: FilePath -> ResultG GanetiLockAllocation
76 c8751a72 Klaus Aehlig
loadLockAllocation =
77 c8751a72 Klaus Aehlig
  liftIO . readFile
78 c8751a72 Klaus Aehlig
  >=> fromJResultE "parsing lock allocation" . J.decodeStrict