Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / OpCodes.hs @ ad1c1e41

History | View | Annotate | Download (16.4 kB)

1
{-# LANGUAGE TemplateHaskell #-}
2

    
3
{-| Implementation of the opcodes.
4

    
5
-}
6

    
7
{-
8

    
9
Copyright (C) 2009, 2010, 2011, 2012 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
  , TagObject(..)
31
  , tagObjectFrom
32
  , encodeTagObject
33
  , decodeTagObject
34
  , ReplaceDisksMode(..)
35
  , DiskIndex
36
  , mkDiskIndex
37
  , unDiskIndex
38
  , opID
39
  , allOpIDs
40
  , allOpFields
41
  , opSummary
42
  , CommonOpParams(..)
43
  , defOpParams
44
  , MetaOpCode(..)
45
  , wrapOpCode
46
  , setOpComment
47
  ) where
48

    
49
import Data.Maybe (fromMaybe)
50
import Text.JSON (readJSON, showJSON, JSON, JSValue, makeObj)
51
import qualified Text.JSON
52

    
53
import Ganeti.THH
54

    
55
import Ganeti.OpParams
56
import Ganeti.Types (OpSubmitPriority(..), fromNonEmpty)
57
import Ganeti.Query.Language (queryTypeOpToRaw)
58

    
59
-- | OpCode representation.
60
--
61
-- We only implement a subset of Ganeti opcodes: those which are actually used
62
-- in the htools codebase.
63
$(genOpCode "OpCode"
64
  [ ("OpTestDelay",
65
     [ pDelayDuration
66
     , pDelayOnMaster
67
     , pDelayOnNodes
68
     , pDelayRepeat
69
     ])
70
  , ("OpInstanceReplaceDisks",
71
     [ pInstanceName
72
     , pEarlyRelease
73
     , pIgnoreIpolicy
74
     , pReplaceDisksMode
75
     , pReplaceDisksList
76
     , pRemoteNode
77
     , pIallocator
78
     ])
79
  , ("OpInstanceFailover",
80
     [ pInstanceName
81
     , pShutdownTimeout
82
     , pIgnoreConsistency
83
     , pMigrationTargetNode
84
     , pIgnoreIpolicy
85
     , pIallocator
86
     ])
87
  , ("OpInstanceMigrate",
88
     [ pInstanceName
89
     , pMigrationMode
90
     , pMigrationLive
91
     , pMigrationTargetNode
92
     , pAllowRuntimeChgs
93
     , pIgnoreIpolicy
94
     , pMigrationCleanup
95
     , pIallocator
96
     , pAllowFailover
97
     ])
98
  , ("OpTagsGet",
99
     [ pTagsObject
100
     , pUseLocking
101
     ])
102
  , ("OpTagsSearch",
103
     [ pTagSearchPattern ])
104
  , ("OpTagsSet",
105
     [ pTagsObject
106
     , pTagsList
107
     ])
108
  , ("OpTagsDel",
109
     [ pTagsObject
110
     , pTagsList
111
     ])
112
  , ("OpClusterPostInit", [])
113
  , ("OpClusterDestroy", [])
114
  , ("OpClusterQuery", [])
115
  , ("OpClusterVerify",
116
     [ pDebugSimulateErrors
117
     , pErrorCodes
118
     , pSkipChecks
119
     , pIgnoreErrors
120
     , pVerbose
121
     , pOptGroupName
122
     ])
123
  , ("OpClusterVerifyConfig",
124
     [ pDebugSimulateErrors
125
     , pErrorCodes
126
     , pIgnoreErrors
127
     , pVerbose
128
     ])
129
  , ("OpClusterVerifyGroup",
130
     [ pGroupName
131
     , pDebugSimulateErrors
132
     , pErrorCodes
133
     , pSkipChecks
134
     , pIgnoreErrors
135
     , pVerbose
136
     ])
137
  , ("OpClusterVerifyDisks", [])
138
  , ("OpGroupVerifyDisks",
139
     [ pGroupName
140
     ])
141
  , ("OpClusterRepairDiskSizes",
142
     [ pInstances
143
     ])
144
  , ("OpClusterConfigQuery",
145
     [ pOutputFields
146
     ])
147
  , ("OpClusterRename",
148
     [ pName
149
     ])
150
  , ("OpClusterSetParams",
151
     [ pHvState
152
     , pDiskState
153
     , pVgName
154
     , pEnabledHypervisors
155
     , pClusterHvParams
156
     , pClusterBeParams
157
     , pOsHvp
158
     , pClusterOsParams
159
     , pDiskParams
160
     , pCandidatePoolSize
161
     , pUidPool
162
     , pAddUids
163
     , pRemoveUids
164
     , pMaintainNodeHealth
165
     , pPreallocWipeDisks
166
     , pNicParams
167
     , pNdParams
168
     , pIpolicy
169
     , pDrbdHelper
170
     , pDefaultIAllocator
171
     , pMasterNetdev
172
     , pMasterNetmask
173
     , pReservedLvs
174
     , pHiddenOs
175
     , pBlacklistedOs
176
     , pUseExternalMipScript
177
     ])
178
  , ("OpClusterRedistConf", [])
179
  , ("OpClusterActivateMasterIp", [])
180
  , ("OpClusterDeactivateMasterIp", [])
181
  , ("OpQuery",
182
     [ pQueryWhat
183
     , pUseLocking
184
     , pQueryFields
185
     , pQueryFilter
186
     ])
187
  , ("OpQueryFields",
188
     [ pQueryWhat
189
     , pQueryFields
190
     ])
191
  , ("OpOobCommand",
192
     [ pNodeNames
193
     , pOobCommand
194
     , pOobTimeout
195
     , pIgnoreStatus
196
     , pPowerDelay
197
     ])
198
  , ("OpNodeRemove", [ pNodeName ])
199
  , ("OpNodeAdd",
200
     [ pNodeName
201
     , pHvState
202
     , pDiskState
203
     , pPrimaryIp
204
     , pSecondaryIp
205
     , pReadd
206
     , pNodeGroup
207
     , pMasterCapable
208
     , pVmCapable
209
     , pNdParams
210
    ])
211
  , ("OpNodeQuery", dOldQuery)
212
  , ("OpNodeQueryvols",
213
     [ pOutputFields
214
     , pNodes
215
     ])
216
  , ("OpNodeQueryStorage",
217
     [ pOutputFields
218
     , pStorageType
219
     , pNodes
220
     , pStorageName
221
     ])
222
  , ("OpNodeModifyStorage",
223
     [ pNodeName
224
     , pStorageType
225
     , pStorageName
226
     , pStorageChanges
227
     ])
228
  , ("OpRepairNodeStorage",
229
     [ pNodeName
230
     , pStorageType
231
     , pStorageName
232
     , pIgnoreConsistency
233
     ])
234
  , ("OpNodeSetParams",
235
     [ pNodeName
236
     , pForce
237
     , pHvState
238
     , pDiskState
239
     , pMasterCandidate
240
     , pOffline
241
     , pDrained
242
     , pAutoPromote
243
     , pMasterCapable
244
     , pVmCapable
245
     , pSecondaryIp
246
     , pNdParams
247
     , pPowered
248
     ])
249
  , ("OpNodePowercycle",
250
     [ pNodeName
251
     , pForce
252
     ])
253
  , ("OpNodeMigrate",
254
     [ pNodeName
255
     , pMigrationMode
256
     , pMigrationLive
257
     , pMigrationTargetNode
258
     , pAllowRuntimeChgs
259
     , pIgnoreIpolicy
260
     , pIallocator
261
     ])
262
  , ("OpNodeEvacuate",
263
     [ pEarlyRelease
264
     , pNodeName
265
     , pRemoteNode
266
     , pIallocator
267
     , pEvacMode
268
     ])
269
  , ("OpInstanceCreate",
270
     [ pInstanceName
271
     , pForceVariant
272
     , pWaitForSync
273
     , pNameCheck
274
     , pIgnoreIpolicy
275
     , pInstBeParams
276
     , pInstDisks
277
     , pDiskTemplate
278
     , pFileDriver
279
     , pFileStorageDir
280
     , pInstHvParams
281
     , pHypervisor
282
     , pIallocator
283
     , pResetDefaults
284
     , pIpCheck
285
     , pIpConflictsCheck
286
     , pInstCreateMode
287
     , pInstNics
288
     , pNoInstall
289
     , pInstOsParams
290
     , pInstOs
291
     , pPrimaryNode
292
     , pSecondaryNode
293
     , pSourceHandshake
294
     , pSourceInstance
295
     , pSourceShutdownTimeout
296
     , pSourceX509Ca
297
     , pSrcNode
298
     , pSrcPath
299
     , pStartInstance
300
     , pOpportunisticLocking
301
     , pInstTags
302
     ])
303
  , ("OpInstanceMultiAlloc",
304
     [ pIallocator
305
     , pMultiAllocInstances
306
     , pOpportunisticLocking
307
     ])
308
  , ("OpInstanceReinstall",
309
     [ pInstanceName
310
     , pForceVariant
311
     , pInstOs
312
     , pTempOsParams
313
     ])
314
  , ("OpInstanceRemove",
315
     [ pInstanceName
316
     , pShutdownTimeout
317
     , pIgnoreFailures
318
     ])
319
  , ("OpInstanceRename",
320
     [ pInstanceName
321
     , pNewName
322
     , pNameCheck
323
     , pIpCheck
324
     ])
325
  , ("OpInstanceStartup",
326
     [ pInstanceName
327
     , pForce
328
     , pIgnoreOfflineNodes
329
     , pTempHvParams
330
     , pTempBeParams
331
     , pNoRemember
332
     , pStartupPaused
333
     ])
334
  , ("OpInstanceShutdown",
335
     [ pInstanceName
336
     , pIgnoreOfflineNodes
337
     , pShutdownTimeout'
338
     , pNoRemember
339
     ])
340
  , ("OpInstanceReboot",
341
     [ pInstanceName
342
     , pShutdownTimeout
343
     , pIgnoreSecondaries
344
     , pRebootType
345
     ])
346
  , ("OpInstanceMove",
347
     [ pInstanceName
348
     , pShutdownTimeout
349
     , pIgnoreIpolicy
350
     , pMoveTargetNode
351
     , pIgnoreConsistency
352
     ])
353
  , ("OpInstanceConsole",
354
     [ pInstanceName ])
355
  , ("OpInstanceActivateDisks",
356
     [ pInstanceName
357
     , pIgnoreDiskSize
358
     , pWaitForSyncFalse
359
     ])
360
  , ("OpInstanceDeactivateDisks",
361
     [ pInstanceName
362
     , pForce
363
     ])
364
  , ("OpInstanceRecreateDisks",
365
     [ pInstanceName
366
     , pRecreateDisksInfo
367
     , pNodes
368
     , pIallocator
369
     ])
370
  , ("OpInstanceQuery", dOldQuery)
371
  , ("OpInstanceQueryData",
372
     [ pUseLocking
373
     , pInstances
374
     , pStatic
375
     ])
376
  , ("OpInstanceSetParams",
377
     [ pInstanceName
378
     , pForce
379
     , pForceVariant
380
     , pIgnoreIpolicy
381
     , pInstParamsNicChanges
382
     , pInstParamsDiskChanges
383
     , pInstBeParams
384
     , pRuntimeMem
385
     , pInstHvParams
386
     , pDiskTemplate
387
     , pRemoteNode
388
     , pOsNameChange
389
     , pInstOsParams
390
     , pWaitForSync
391
     , pOffline
392
     , pIpConflictsCheck
393
     ])
394
  , ("OpInstanceGrowDisk",
395
     [ pInstanceName
396
     , pWaitForSync
397
     , pDiskIndex
398
     , pDiskChgAmount
399
     , pDiskChgAbsolute
400
     ])
401
  , ("OpInstanceChangeGroup",
402
     [ pInstanceName
403
     , pEarlyRelease
404
     , pIallocator
405
     , pTargetGroups
406
     ])
407
  , ("OpGroupAdd",
408
     [ pGroupName
409
     , pNodeGroupAllocPolicy
410
     , pGroupNodeParams
411
     , pDiskParams
412
     , pHvState
413
     , pDiskState
414
     , pIpolicy
415
     ])
416
  , ("OpGroupAssignNodes",
417
     [ pGroupName
418
     , pForce
419
     , pRequiredNodes
420
     ])
421
  , ("OpGroupQuery", dOldQueryNoLocking)
422
  , ("OpGroupSetParams",
423
     [ pGroupName
424
     , pNodeGroupAllocPolicy
425
     , pGroupNodeParams
426
     , pDiskParams
427
     , pHvState
428
     , pDiskState
429
     , pIpolicy
430
     ])
431
  , ("OpGroupRemove",
432
     [ pGroupName ])
433
  , ("OpGroupRename",
434
     [ pGroupName
435
     , pNewName
436
     ])
437
  , ("OpGroupEvacuate",
438
     [ pGroupName
439
     , pEarlyRelease
440
     , pIallocator
441
     , pTargetGroups
442
     ])
443
  , ("OpOsDiagnose",
444
     [ pOutputFields
445
     , pNames ])
446
  , ("OpBackupQuery",
447
     [ pUseLocking
448
     , pNodes
449
     ])
450
  , ("OpBackupPrepare",
451
     [ pInstanceName
452
     , pExportMode
453
     ])
454
  , ("OpBackupExport",
455
     [ pInstanceName
456
     , pShutdownTimeout
457
     , pExportTargetNode
458
     , pShutdownInstance
459
     , pRemoveInstance
460
     , pIgnoreRemoveFailures
461
     , pExportMode
462
     , pX509KeyName
463
     , pX509DestCA
464
     ])
465
  , ("OpBackupRemove",
466
     [ pInstanceName ])
467
  , ("OpTestAllocator",
468
     [ pIAllocatorDirection
469
     , pIAllocatorMode
470
     , pIAllocatorReqName
471
     , pIAllocatorNics
472
     , pIAllocatorDisks
473
     , pHypervisor
474
     , pIallocator
475
     , pInstTags
476
     , pIAllocatorMemory
477
     , pIAllocatorVCpus
478
     , pIAllocatorOs
479
     , pDiskTemplate
480
     , pIAllocatorInstances
481
     , pIAllocatorEvacMode
482
     , pTargetGroups
483
     , pIAllocatorSpindleUse
484
     , pIAllocatorCount
485
     ])
486
  , ("OpTestJqueue",
487
     [ pJQueueNotifyWaitLock
488
     , pJQueueNotifyExec
489
     , pJQueueLogMessages
490
     , pJQueueFail
491
     ])
492
  , ("OpTestDummy",
493
     [ pTestDummyResult
494
     , pTestDummyMessages
495
     , pTestDummyFail
496
     , pTestDummySubmitJobs
497
     ])
498
  , ("OpNetworkAdd",
499
     [ pNetworkName
500
     , pNetworkType
501
     , pNetworkAddress4
502
     , pNetworkGateway4
503
     , pNetworkAddress6
504
     , pNetworkGateway6
505
     , pNetworkMacPrefix
506
     , pNetworkAddRsvdIps
507
     , pIpConflictsCheck
508
     , pInstTags
509
     ])
510
  , ("OpNetworkRemove",
511
     [ pNetworkName
512
     , pForce
513
     ])
514
  , ("OpNetworkSetParams",
515
     [ pNetworkName
516
     , pNetworkType
517
     , pNetworkGateway4
518
     , pNetworkAddress6
519
     , pNetworkGateway6
520
     , pNetworkMacPrefix
521
     , pNetworkAddRsvdIps
522
     , pNetworkRemoveRsvdIps
523
     ])
524
  , ("OpNetworkConnect",
525
     [ pGroupName
526
     , pNetworkName
527
     , pNetworkMode
528
     , pNetworkLink
529
     , pIpConflictsCheck
530
     ])
531
  , ("OpNetworkDisconnect",
532
     [ pGroupName
533
     , pNetworkName
534
     , pIpConflictsCheck
535
     ])
536
  , ("OpNetworkQuery", dOldQueryNoLocking)
537
  , ("OpRestrictedCommand",
538
     [ pUseLocking
539
     , pRequiredNodes
540
     , pRestrictedCommand
541
     ])
542
  ])
543

    
544
-- | Returns the OP_ID for a given opcode value.
545
$(genOpID ''OpCode "opID")
546

    
547
-- | A list of all defined/supported opcode IDs.
548
$(genAllOpIDs ''OpCode "allOpIDs")
549

    
550
instance JSON OpCode where
551
  readJSON = loadOpCode
552
  showJSON = saveOpCode
553

    
554
-- | Generates the summary value for an opcode.
555
opSummaryVal :: OpCode -> Maybe String
556
opSummaryVal OpClusterVerifyGroup { opGroupName = s } = Just (fromNonEmpty s)
557
opSummaryVal OpGroupVerifyDisks { opGroupName = s } = Just (fromNonEmpty s)
558
opSummaryVal OpClusterRename { opName = s } = Just (fromNonEmpty s)
559
opSummaryVal OpQuery { opWhat = s } = Just (queryTypeOpToRaw s)
560
opSummaryVal OpQueryFields { opWhat = s } = Just (queryTypeOpToRaw s)
561
opSummaryVal OpNodeRemove { opNodeName = s } = Just (fromNonEmpty s)
562
opSummaryVal OpNodeAdd { opNodeName = s } = Just (fromNonEmpty s)
563
opSummaryVal OpNodeModifyStorage { opNodeName = s } = Just (fromNonEmpty s)
564
opSummaryVal OpRepairNodeStorage  { opNodeName = s } = Just (fromNonEmpty s)
565
opSummaryVal OpNodeSetParams { opNodeName = s } = Just (fromNonEmpty s)
566
opSummaryVal OpNodePowercycle { opNodeName = s } = Just (fromNonEmpty s)
567
opSummaryVal OpNodeMigrate { opNodeName = s } = Just (fromNonEmpty s)
568
opSummaryVal OpNodeEvacuate { opNodeName = s } = Just (fromNonEmpty s)
569
opSummaryVal OpInstanceCreate { opInstanceName = s } = Just s
570
opSummaryVal OpInstanceReinstall { opInstanceName = s } = Just s
571
opSummaryVal OpInstanceRemove { opInstanceName = s } = Just s
572
-- FIXME: instance rename should show both names; currently it shows none
573
-- opSummaryVal OpInstanceRename { opInstanceName = s } = Just s
574
opSummaryVal OpInstanceStartup { opInstanceName = s } = Just s
575
opSummaryVal OpInstanceShutdown { opInstanceName = s } = Just s
576
opSummaryVal OpInstanceReboot { opInstanceName = s } = Just s
577
opSummaryVal OpInstanceReplaceDisks { opInstanceName = s } = Just s
578
opSummaryVal OpInstanceFailover { opInstanceName = s } = Just s
579
opSummaryVal OpInstanceMigrate { opInstanceName = s } = Just s
580
opSummaryVal OpInstanceMove { opInstanceName = s } = Just s
581
opSummaryVal OpInstanceConsole { opInstanceName = s } = Just s
582
opSummaryVal OpInstanceActivateDisks { opInstanceName = s } = Just s
583
opSummaryVal OpInstanceDeactivateDisks { opInstanceName = s } = Just s
584
opSummaryVal OpInstanceRecreateDisks { opInstanceName = s } = Just s
585
opSummaryVal OpInstanceSetParams { opInstanceName = s } = Just s
586
opSummaryVal OpInstanceGrowDisk { opInstanceName = s } = Just s
587
opSummaryVal OpInstanceChangeGroup { opInstanceName = s } = Just s
588
opSummaryVal OpGroupAdd { opGroupName = s } = Just (fromNonEmpty s)
589
opSummaryVal OpGroupAssignNodes { opGroupName = s } = Just (fromNonEmpty s)
590
opSummaryVal OpGroupSetParams { opGroupName = s } = Just (fromNonEmpty s)
591
opSummaryVal OpGroupRemove { opGroupName = s } = Just (fromNonEmpty s)
592
opSummaryVal OpGroupEvacuate { opGroupName = s } = Just (fromNonEmpty s)
593
opSummaryVal OpBackupPrepare { opInstanceName = s } = Just s
594
opSummaryVal OpBackupExport { opInstanceName = s } = Just s
595
opSummaryVal OpBackupRemove { opInstanceName = s } = Just s
596
opSummaryVal OpTagsGet { opKind = k } =
597
  Just . fromMaybe "None" $ tagNameOf k
598
opSummaryVal OpTagsSearch { opTagSearchPattern = s } = Just (fromNonEmpty s)
599
opSummaryVal OpTestDelay { opDelayDuration = d } = Just (show d)
600
opSummaryVal OpTestAllocator { opIallocator = s } =
601
  -- FIXME: Python doesn't handle None fields well, so we have behave the same
602
  Just $ maybe "None" fromNonEmpty s
603
opSummaryVal OpNetworkAdd { opNetworkName = s} = Just (fromNonEmpty s)
604
opSummaryVal OpNetworkRemove { opNetworkName = s} = Just (fromNonEmpty s)
605
opSummaryVal OpNetworkSetParams { opNetworkName = s} = Just (fromNonEmpty s)
606
opSummaryVal OpNetworkConnect { opNetworkName = s} = Just (fromNonEmpty s)
607
opSummaryVal OpNetworkDisconnect { opNetworkName = s} = Just (fromNonEmpty s)
608
opSummaryVal _ = Nothing
609

    
610
-- | Computes the summary of the opcode.
611
opSummary :: OpCode -> String
612
opSummary op =
613
  case opSummaryVal op of
614
    Nothing -> op_suffix
615
    Just s -> op_suffix ++ "(" ++ s ++ ")"
616
  where op_suffix = drop 3 $ opID op
617

    
618
-- | Generic\/common opcode parameters.
619
$(buildObject "CommonOpParams" "op"
620
  [ pDryRun
621
  , pDebugLevel
622
  , pOpPriority
623
  , pDependencies
624
  , pComment
625
  ])
626

    
627
-- | Default common parameter values.
628
defOpParams :: CommonOpParams
629
defOpParams =
630
  CommonOpParams { opDryRun     = Nothing
631
                 , opDebugLevel = Nothing
632
                 , opPriority   = OpPrioNormal
633
                 , opDepends    = Nothing
634
                 , opComment    = Nothing
635
                 }
636

    
637
-- | The top-level opcode type.
638
data MetaOpCode = MetaOpCode { metaParams :: CommonOpParams
639
                             , metaOpCode :: OpCode
640
                             } deriving (Show, Eq)
641

    
642
-- | JSON serialisation for 'MetaOpCode'.
643
showMeta :: MetaOpCode -> JSValue
644
showMeta (MetaOpCode params op) =
645
  let objparams = toDictCommonOpParams params
646
      objop = toDictOpCode op
647
  in makeObj (objparams ++ objop)
648

    
649
-- | JSON deserialisation for 'MetaOpCode'
650
readMeta :: JSValue -> Text.JSON.Result MetaOpCode
651
readMeta v = do
652
  meta <- readJSON v
653
  op <- readJSON v
654
  return $ MetaOpCode meta op
655

    
656
instance JSON MetaOpCode where
657
  showJSON = showMeta
658
  readJSON = readMeta
659

    
660
-- | Wraps an 'OpCode' with the default parameters to build a
661
-- 'MetaOpCode'.
662
wrapOpCode :: OpCode -> MetaOpCode
663
wrapOpCode = MetaOpCode defOpParams
664

    
665
-- | Sets the comment on a meta opcode.
666
setOpComment :: String -> MetaOpCode -> MetaOpCode
667
setOpComment comment (MetaOpCode common op) =
668
  MetaOpCode (common { opComment = Just comment}) op