Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / OpCodes.hs @ 2e5eb96a

History | View | Annotate | Download (5.1 kB)

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