Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Hypervisor / Xen / Types.hs @ b8585908

History | View | Annotate | Download (3.7 kB)

1
{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
2
{-| Data types for Xen-specific hypervisor functionalities.
3

    
4
-}
5
{-
6

    
7
Copyright (C) 2013 Google Inc.
8

    
9
This program is free software; you can redistribute it and/or modify
10
it under the terms of the GNU General Public License as published by
11
the Free Software Foundation; either version 2 of the License, or
12
(at your option) any later version.
13

    
14
This program is distributed in the hope that it will be useful, but
15
WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
General Public License for more details.
18

    
19
You should have received a copy of the GNU General Public License
20
along with this program; if not, write to the Free Software
21
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22
02110-1301, USA.
23

    
24
-}
25
module Ganeti.Hypervisor.Xen.Types
26
  ( LispConfig(..)
27
  , Domain(..)
28
  , FromLispConfig(..)
29
  , ActualState(..)
30
  ) where
31

    
32
import qualified Text.JSON as J
33

    
34
import Ganeti.BasicTypes
35

    
36
-- | Data type representing configuration data as produced by the
37
-- @xm list --long@ command.
38
data LispConfig = LCList [LispConfig]
39
                | LCString String
40
                | LCDouble Double
41
                deriving (Eq, Show)
42

    
43
-- | Data type representing a Xen Domain.
44
data Domain = Domain
45
  { domId      :: Int
46
  , domName    :: String
47
  , domCpuTime :: Double
48
  , domState   :: ActualState
49
  , domIsHung  :: Maybe Bool
50
  } deriving (Show, Eq)
51

    
52
-- | Class representing all the types that can be extracted from LispConfig.
53
class FromLispConfig a where
54
  fromLispConfig :: LispConfig -> Result a
55

    
56
-- | Instance of FromLispConfig for Int.
57
instance FromLispConfig Int where
58
  fromLispConfig (LCDouble d) = Ok $ floor d
59
  fromLispConfig (LCList (LCString _:LCDouble d:[])) = Ok $ floor d
60
  fromLispConfig c =
61
    Bad $ "Unable to extract a Int from this configuration: "
62
      ++ show c
63

    
64
-- | Instance of FromLispConfig for Double.
65
instance FromLispConfig Double where
66
  fromLispConfig (LCDouble d) = Ok d
67
  fromLispConfig (LCList (LCString _:LCDouble d:[])) = Ok d
68
  fromLispConfig c =
69
    Bad $ "Unable to extract a Double from this configuration: "
70
      ++ show c
71

    
72
-- | Instance of FromLispConfig for String
73
instance FromLispConfig String where
74
  fromLispConfig (LCString s) = Ok s
75
  fromLispConfig (LCList (LCString _:LCString s:[])) = Ok s
76
  fromLispConfig c =
77
    Bad $ "Unable to extract a String from this configuration: "
78
      ++ show c
79

    
80
-- | Instance of FromLispConfig for [LispConfig]
81
instance FromLispConfig [LispConfig] where
82
  fromLispConfig (LCList l) = Ok l
83
  fromLispConfig c =
84
    Bad $ "Unable to extract a List from this configuration: "
85
      ++ show c
86

    
87
data ActualState = ActualRunning  -- ^ The instance is running
88
                 | ActualBlocked  -- ^ The instance is not running or runnable
89
                 | ActualPaused   -- ^ The instance has been paused
90
                 | ActualShutdown -- ^ The instance is shut down
91
                 | ActualCrashed  -- ^ The instance has crashed
92
                 | ActualDying    -- ^ The instance is in process of dying
93
                 | ActualHung     -- ^ The instance is hung
94
                 | ActualUnknown  -- ^ Unknown state. Parsing error.
95
                 deriving (Show, Eq)
96

    
97
instance J.JSON ActualState where
98
  showJSON ActualRunning = J.showJSON "running"
99
  showJSON ActualBlocked = J.showJSON "blocked"
100
  showJSON ActualPaused = J.showJSON "paused"
101
  showJSON ActualShutdown = J.showJSON "shutdown"
102
  showJSON ActualCrashed = J.showJSON "crashed"
103
  showJSON ActualDying = J.showJSON "dying"
104
  showJSON ActualHung = J.showJSON "hung"
105
  showJSON ActualUnknown = J.showJSON "unknown"
106

    
107
  readJSON = error "JSON read instance not implemented for type ActualState"