root / htools / Ganeti / OpCodes.hs @ cd67e337
History | View | Annotate | Download (3.3 kB)
1 | e9aaa3c6 | Iustin Pop | {-# LANGUAGE TemplateHaskell #-} |
---|---|---|---|
2 | e9aaa3c6 | Iustin Pop | |
3 | 702a4ee0 | Iustin Pop | {-| Implementation of the opcodes. |
4 | 702a4ee0 | Iustin Pop | |
5 | 702a4ee0 | Iustin Pop | -} |
6 | 702a4ee0 | Iustin Pop | |
7 | 702a4ee0 | Iustin Pop | {- |
8 | 702a4ee0 | Iustin Pop | |
9 | a583ec5d | Iustin Pop | Copyright (C) 2009, 2010, 2011, 2012 Google Inc. |
10 | 702a4ee0 | Iustin Pop | |
11 | 702a4ee0 | Iustin Pop | This program is free software; you can redistribute it and/or modify |
12 | 702a4ee0 | Iustin Pop | it under the terms of the GNU General Public License as published by |
13 | 702a4ee0 | Iustin Pop | the Free Software Foundation; either version 2 of the License, or |
14 | 702a4ee0 | Iustin Pop | (at your option) any later version. |
15 | 702a4ee0 | Iustin Pop | |
16 | 702a4ee0 | Iustin Pop | This program is distributed in the hope that it will be useful, but |
17 | 702a4ee0 | Iustin Pop | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | 702a4ee0 | Iustin Pop | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
19 | 702a4ee0 | Iustin Pop | General Public License for more details. |
20 | 702a4ee0 | Iustin Pop | |
21 | 702a4ee0 | Iustin Pop | You should have received a copy of the GNU General Public License |
22 | 702a4ee0 | Iustin Pop | along with this program; if not, write to the Free Software |
23 | 702a4ee0 | Iustin Pop | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
24 | 702a4ee0 | Iustin Pop | 02110-1301, USA. |
25 | 702a4ee0 | Iustin Pop | |
26 | 702a4ee0 | Iustin Pop | -} |
27 | 702a4ee0 | Iustin Pop | |
28 | 702a4ee0 | Iustin Pop | module Ganeti.OpCodes |
29 | ebf38064 | Iustin Pop | ( OpCode(..) |
30 | ebf38064 | Iustin Pop | , ReplaceDisksMode(..) |
31 | 4a1dc2bf | Iustin Pop | , DiskIndex |
32 | 4a1dc2bf | Iustin Pop | , mkDiskIndex |
33 | 4a1dc2bf | Iustin Pop | , unDiskIndex |
34 | ebf38064 | Iustin Pop | , opID |
35 | a583ec5d | Iustin Pop | , allOpIDs |
36 | ebf38064 | Iustin Pop | ) where |
37 | 702a4ee0 | Iustin Pop | |
38 | 0903280b | Iustin Pop | import Text.JSON (readJSON, showJSON, makeObj, JSON) |
39 | 702a4ee0 | Iustin Pop | |
40 | e9aaa3c6 | Iustin Pop | import qualified Ganeti.Constants as C |
41 | 12c19659 | Iustin Pop | import Ganeti.THH |
42 | e9aaa3c6 | Iustin Pop | |
43 | f3baf5ef | Iustin Pop | import Ganeti.JSON |
44 | 702a4ee0 | Iustin Pop | |
45 | 525bfb36 | Iustin Pop | -- | Replace disks type. |
46 | 12c19659 | Iustin Pop | $(declareSADT "ReplaceDisksMode" |
47 | ebf38064 | Iustin Pop | [ ("ReplaceOnPrimary", 'C.replaceDiskPri) |
48 | ebf38064 | Iustin Pop | , ("ReplaceOnSecondary", 'C.replaceDiskSec) |
49 | ebf38064 | Iustin Pop | , ("ReplaceNewSecondary", 'C.replaceDiskChg) |
50 | ebf38064 | Iustin Pop | , ("ReplaceAuto", 'C.replaceDiskAuto) |
51 | ebf38064 | Iustin Pop | ]) |
52 | 12c19659 | Iustin Pop | $(makeJSONInstance ''ReplaceDisksMode) |
53 | 702a4ee0 | Iustin Pop | |
54 | 4a1dc2bf | Iustin Pop | -- | Disk index type (embedding constraints on the index value via a |
55 | 4a1dc2bf | Iustin Pop | -- smart constructor). |
56 | 4a1dc2bf | Iustin Pop | newtype DiskIndex = DiskIndex { unDiskIndex :: Int } |
57 | 4a1dc2bf | Iustin Pop | deriving (Show, Read, Eq, Ord) |
58 | 4a1dc2bf | Iustin Pop | |
59 | 4a1dc2bf | Iustin Pop | -- | Smart constructor for 'DiskIndex'. |
60 | 4a1dc2bf | Iustin Pop | mkDiskIndex :: (Monad m) => Int -> m DiskIndex |
61 | 4a1dc2bf | Iustin Pop | mkDiskIndex i | i >= 0 && i < C.maxDisks = return (DiskIndex i) |
62 | 4a1dc2bf | Iustin Pop | | otherwise = fail $ "Invalid value for disk index '" ++ |
63 | 4a1dc2bf | Iustin Pop | show i ++ "', required between 0 and " ++ |
64 | 4a1dc2bf | Iustin Pop | show C.maxDisks |
65 | 4a1dc2bf | Iustin Pop | |
66 | 4a1dc2bf | Iustin Pop | instance JSON DiskIndex where |
67 | 4a1dc2bf | Iustin Pop | readJSON v = readJSON v >>= mkDiskIndex |
68 | 4a1dc2bf | Iustin Pop | showJSON = showJSON . unDiskIndex |
69 | 4a1dc2bf | Iustin Pop | |
70 | 525bfb36 | Iustin Pop | -- | OpCode representation. |
71 | 525bfb36 | Iustin Pop | -- |
72 | 525bfb36 | Iustin Pop | -- We only implement a subset of Ganeti opcodes, but only what we |
73 | 525bfb36 | Iustin Pop | -- actually use in the htools codebase. |
74 | 12c19659 | Iustin Pop | $(genOpCode "OpCode" |
75 | ebf38064 | Iustin Pop | [ ("OpTestDelay", |
76 | a1505857 | Iustin Pop | [ simpleField "duration" [t| Double |] |
77 | a1505857 | Iustin Pop | , simpleField "on_master" [t| Bool |] |
78 | a1505857 | Iustin Pop | , simpleField "on_nodes" [t| [String] |] |
79 | ebf38064 | Iustin Pop | ]) |
80 | ebf38064 | Iustin Pop | , ("OpInstanceReplaceDisks", |
81 | a1505857 | Iustin Pop | [ simpleField "instance_name" [t| String |] |
82 | a1505857 | Iustin Pop | , optionalField $ simpleField "remote_node" [t| String |] |
83 | a1505857 | Iustin Pop | , simpleField "mode" [t| ReplaceDisksMode |] |
84 | 4a1dc2bf | Iustin Pop | , simpleField "disks" [t| [DiskIndex] |] |
85 | a1505857 | Iustin Pop | , optionalField $ simpleField "iallocator" [t| String |] |
86 | ebf38064 | Iustin Pop | ]) |
87 | ebf38064 | Iustin Pop | , ("OpInstanceFailover", |
88 | a1505857 | Iustin Pop | [ simpleField "instance_name" [t| String |] |
89 | a1505857 | Iustin Pop | , simpleField "ignore_consistency" [t| Bool |] |
90 | a1505857 | Iustin Pop | , optionalField $ simpleField "target_node" [t| String |] |
91 | ebf38064 | Iustin Pop | ]) |
92 | ebf38064 | Iustin Pop | , ("OpInstanceMigrate", |
93 | a1505857 | Iustin Pop | [ simpleField "instance_name" [t| String |] |
94 | a1505857 | Iustin Pop | , simpleField "live" [t| Bool |] |
95 | a1505857 | Iustin Pop | , simpleField "cleanup" [t| Bool |] |
96 | a1505857 | Iustin Pop | , defaultField [| False |] $ simpleField "allow_failover" [t| Bool |] |
97 | a1505857 | Iustin Pop | , optionalField $ simpleField "target_node" [t| String |] |
98 | ebf38064 | Iustin Pop | ]) |
99 | ebf38064 | Iustin Pop | ]) |
100 | 12c19659 | Iustin Pop | |
101 | a583ec5d | Iustin Pop | -- | Returns the OP_ID for a given opcode value. |
102 | 12c19659 | Iustin Pop | $(genOpID ''OpCode "opID") |
103 | 702a4ee0 | Iustin Pop | |
104 | a583ec5d | Iustin Pop | -- | A list of all defined/supported opcode IDs. |
105 | a583ec5d | Iustin Pop | $(genAllOpIDs ''OpCode "allOpIDs") |
106 | a583ec5d | Iustin Pop | |
107 | 702a4ee0 | Iustin Pop | instance JSON OpCode where |
108 | ebf38064 | Iustin Pop | readJSON = loadOpCode |
109 | ebf38064 | Iustin Pop | showJSON = saveOpCode |