Revision 12c19659 htools/Ganeti/OpCodes.hs

b/htools/Ganeti/OpCodes.hs
31 31
    , opID
32 32
    ) where
33 33

  
34
import Control.Monad
35 34
import Text.JSON (readJSON, showJSON, makeObj, JSON)
36 35
import qualified Text.JSON as J
37
import Text.JSON.Types
38 36

  
39 37
import qualified Ganeti.Constants as C
40
import qualified Ganeti.THH as THH
38
import Ganeti.THH
41 39

  
42 40
import Ganeti.HTools.Utils
43 41

  
44 42
-- | Replace disks type.
45
$(THH.declareSADT "ReplaceDisksMode"
43
$(declareSADT "ReplaceDisksMode"
46 44
     [ ("ReplaceOnPrimary",    'C.replaceDiskPri)
47 45
     , ("ReplaceOnSecondary",  'C.replaceDiskSec)
48 46
     , ("ReplaceNewSecondary", 'C.replaceDiskChg)
49 47
     , ("ReplaceAuto",         'C.replaceDiskAuto)
50 48
     ])
51
$(THH.makeJSONInstance ''ReplaceDisksMode)
49
$(makeJSONInstance ''ReplaceDisksMode)
52 50

  
53 51
-- | OpCode representation.
54 52
--
55 53
-- We only implement a subset of Ganeti opcodes, but only what we
56 54
-- 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'
55
$(genOpCode "OpCode"
56
         [ ("OpTestDelay",
57
            [ ("duration",  [t| Double   |], noDefault)
58
            , ("on_master", [t| Bool     |], noDefault)
59
            , ("on_nodes",  [t| [String] |], noDefault)
60
            ])
61
         , ("OpInstanceReplaceDisks",
62
            [ ("instance_name", [t| String           |], noDefault)
63
            , ("remote_node",   [t| Maybe String     |], noDefault)
64
            , ("mode",          [t| ReplaceDisksMode |], noDefault)
65
            , ("disks",         [t| [Int]            |], noDefault)
66
            , ("iallocator",    [t| Maybe String     |], noDefault)
67
            ])
68
         , ("OpInstanceFailover",
69
            [ ("instance_name",      [t| String       |], noDefault)
70
            , ("ignore_consistency", [t| Bool         |], noDefault)
71
            , ("target_node",        [t| Maybe String |], noDefault)
72
            ])
73
         , ("OpInstanceMigrate",
74
            [ ("instance_name",  [t| String       |], noDefault)
75
            , ("live",           [t| Bool         |], noDefault)
76
            , ("cleanup",        [t| Bool         |], noDefault)
77
            , ("allow_failover", [t| Bool         |], [| Just False |])
78
            , ("target_node",    [t| Maybe String |], noDefault)
79
            ])
80
         ])
81

  
82
$(genOpID ''OpCode "opID")
142 83

  
143 84
instance JSON OpCode where
144 85
    readJSON = loadOpCode

Also available in: Unified diff