root / htools / Ganeti / OpCodes.hs @ 6111e296
History | View | Annotate | Download (5.1 kB)
1 |
{-# LANGUAGE TemplateHaskell #-} |
---|---|
2 |
|
3 |
{-| Implementation of the opcodes. |
4 |
|
5 |
-} |
6 |
|
7 |
{- |
8 |
|
9 |
Copyright (C) 2009, 2010, 2011 Google Inc. |
10 |
|
11 |
This program is free software; you can redistribute it and/or modify |
12 |
it under the terms of the GNU General Public License as published by |
13 |
the Free Software Foundation; either version 2 of the License, or |
14 |
(at your option) any later version. |
15 |
|
16 |
This program is distributed in the hope that it will be useful, but |
17 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
18 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
19 |
General Public License for more details. |
20 |
|
21 |
You should have received a copy of the GNU General Public License |
22 |
along with this program; if not, write to the Free Software |
23 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
24 |
02110-1301, USA. |
25 |
|
26 |
-} |
27 |
|
28 |
module Ganeti.OpCodes |
29 |
( OpCode(..) |
30 |
, ReplaceDisksMode(..) |
31 |
, opID |
32 |
) where |
33 |
|
34 |
import Control.Monad |
35 |
import Text.JSON (readJSON, showJSON, makeObj, JSON) |
36 |
import qualified Text.JSON as J |
37 |
import Text.JSON.Types |
38 |
|
39 |
import qualified Ganeti.Constants as C |
40 |
import qualified Ganeti.THH as THH |
41 |
|
42 |
import Ganeti.HTools.Utils |
43 |
|
44 |
-- | Replace disks type. |
45 |
$(THH.declareSADT "ReplaceDisksMode" |
46 |
[ ("ReplaceOnPrimary", 'C.replaceDiskPri) |
47 |
, ("ReplaceOnSecondary", 'C.replaceDiskSec) |
48 |
, ("ReplaceNewSecondary", 'C.replaceDiskChg) |
49 |
, ("ReplaceAuto", 'C.replaceDiskAuto) |
50 |
]) |
51 |
$(THH.makeJSONInstance ''ReplaceDisksMode) |
52 |
|
53 |
-- | OpCode representation. |
54 |
-- |
55 |
-- We only implement a subset of Ganeti opcodes, but only what we |
56 |
-- actually use in the htools codebase. |
57 |
data OpCode = OpTestDelay Double Bool [String] |
58 |
| OpInstanceReplaceDisks String (Maybe String) ReplaceDisksMode |
59 |
[Int] (Maybe String) |
60 |
| OpInstanceFailover String Bool (Maybe String) |
61 |
| OpInstanceMigrate String Bool Bool Bool (Maybe String) |
62 |
deriving (Show, Read, Eq) |
63 |
|
64 |
|
65 |
$(THH.genOpID ''OpCode "opID") |
66 |
|
67 |
-- | Loads an OpCode from the JSON serialised form. |
68 |
loadOpCode :: JSValue -> J.Result OpCode |
69 |
loadOpCode v = do |
70 |
o <- liftM J.fromJSObject (readJSON v) |
71 |
let extract x = fromObj o x |
72 |
op_id <- extract "OP_ID" |
73 |
case op_id of |
74 |
"OP_TEST_DELAY" -> do |
75 |
on_nodes <- extract "on_nodes" |
76 |
on_master <- extract "on_master" |
77 |
duration <- extract "duration" |
78 |
return $ OpTestDelay duration on_master on_nodes |
79 |
"OP_INSTANCE_REPLACE_DISKS" -> do |
80 |
inst <- extract "instance_name" |
81 |
node <- maybeFromObj o "remote_node" |
82 |
mode <- extract "mode" |
83 |
disks <- extract "disks" |
84 |
ialloc <- maybeFromObj o "iallocator" |
85 |
return $ OpInstanceReplaceDisks inst node mode disks ialloc |
86 |
"OP_INSTANCE_FAILOVER" -> do |
87 |
inst <- extract "instance_name" |
88 |
consist <- extract "ignore_consistency" |
89 |
tnode <- maybeFromObj o "target_node" |
90 |
return $ OpInstanceFailover inst consist tnode |
91 |
"OP_INSTANCE_MIGRATE" -> do |
92 |
inst <- extract "instance_name" |
93 |
live <- extract "live" |
94 |
cleanup <- extract "cleanup" |
95 |
allow_failover <- fromObjWithDefault o "allow_failover" False |
96 |
tnode <- maybeFromObj o "target_node" |
97 |
return $ OpInstanceMigrate inst live cleanup |
98 |
allow_failover tnode |
99 |
_ -> J.Error $ "Unknown opcode " ++ op_id |
100 |
|
101 |
-- | Serialises an opcode to JSON. |
102 |
saveOpCode :: OpCode -> JSValue |
103 |
saveOpCode op@(OpTestDelay duration on_master on_nodes) = |
104 |
let ol = [ ("OP_ID", showJSON $ opID op) |
105 |
, ("duration", showJSON duration) |
106 |
, ("on_master", showJSON on_master) |
107 |
, ("on_nodes", showJSON on_nodes) ] |
108 |
in makeObj ol |
109 |
|
110 |
saveOpCode op@(OpInstanceReplaceDisks inst node mode disks iallocator) = |
111 |
let ol = [ ("OP_ID", showJSON $ opID op) |
112 |
, ("instance_name", showJSON inst) |
113 |
, ("mode", showJSON mode) |
114 |
, ("disks", showJSON disks)] |
115 |
ol2 = case node of |
116 |
Just n -> ("remote_node", showJSON n):ol |
117 |
Nothing -> ol |
118 |
ol3 = case iallocator of |
119 |
Just i -> ("iallocator", showJSON i):ol2 |
120 |
Nothing -> ol2 |
121 |
in makeObj ol3 |
122 |
|
123 |
saveOpCode op@(OpInstanceFailover inst consist tnode) = |
124 |
let ol = [ ("OP_ID", showJSON $ opID op) |
125 |
, ("instance_name", showJSON inst) |
126 |
, ("ignore_consistency", showJSON consist) ] |
127 |
ol' = case tnode of |
128 |
Nothing -> ol |
129 |
Just node -> ("target_node", showJSON node):ol |
130 |
in makeObj ol' |
131 |
|
132 |
saveOpCode op@(OpInstanceMigrate inst live cleanup allow_failover tnode) = |
133 |
let ol = [ ("OP_ID", showJSON $ opID op) |
134 |
, ("instance_name", showJSON inst) |
135 |
, ("live", showJSON live) |
136 |
, ("cleanup", showJSON cleanup) |
137 |
, ("allow_failover", showJSON allow_failover) ] |
138 |
ol' = case tnode of |
139 |
Nothing -> ol |
140 |
Just node -> ("target_node", showJSON node):ol |
141 |
in makeObj ol' |
142 |
|
143 |
instance JSON OpCode where |
144 |
readJSON = loadOpCode |
145 |
showJSON = saveOpCode |