1 {-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
2 {-| Data types for Xen-specific hypervisor functionalities.
7 Copyright (C) 2013 Google Inc.
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.
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.
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
25 module Ganeti.Hypervisor.Xen.Types
33 import qualified Text.JSON as J
35 import Ganeti.BasicTypes
37 -- | Data type representing configuration data as produced by the
38 -- @xm list --long@ command.
39 data LispConfig = LCList [LispConfig]
44 -- | Data type representing a Xen Domain.
48 , domCpuTime :: Double
49 , domState :: ActualState
50 , domIsHung :: Maybe Bool
53 -- | Class representing all the types that can be extracted from LispConfig.
54 class FromLispConfig a where
55 fromLispConfig :: LispConfig -> Result a
57 -- | Instance of FromLispConfig for Int.
58 instance FromLispConfig Int where
59 fromLispConfig (LCDouble d) = Ok $ floor d
60 fromLispConfig (LCList (LCString _:LCDouble d:[])) = Ok $ floor d
62 Bad $ "Unable to extract a Int from this configuration: "
65 -- | Instance of FromLispConfig for Double.
66 instance FromLispConfig Double where
67 fromLispConfig (LCDouble d) = Ok d
68 fromLispConfig (LCList (LCString _:LCDouble d:[])) = Ok d
70 Bad $ "Unable to extract a Double from this configuration: "
73 -- | Instance of FromLispConfig for String
74 instance FromLispConfig String where
75 fromLispConfig (LCString s) = Ok s
76 fromLispConfig (LCList (LCString _:LCString s:[])) = Ok s
78 Bad $ "Unable to extract a String from this configuration: "
81 -- | Instance of FromLispConfig for [LispConfig]
82 instance FromLispConfig [LispConfig] where
83 fromLispConfig (LCList l) = Ok l
85 Bad $ "Unable to extract a List from this configuration: "
88 -- Data type representing the information that can be obtained from @xm uptime@
89 data UptimeInfo = UptimeInfo
92 , uInfoUptime :: String
95 data ActualState = ActualRunning -- ^ The instance is running
96 | ActualBlocked -- ^ The instance is not running or runnable
97 | ActualPaused -- ^ The instance has been paused
98 | ActualShutdown -- ^ The instance is shut down
99 | ActualCrashed -- ^ The instance has crashed
100 | ActualDying -- ^ The instance is in process of dying
101 | ActualHung -- ^ The instance is hung
102 | ActualUnknown -- ^ Unknown state. Parsing error.
105 instance J.JSON ActualState where
106 showJSON ActualRunning = J.showJSON "running"
107 showJSON ActualBlocked = J.showJSON "blocked"
108 showJSON ActualPaused = J.showJSON "paused"
109 showJSON ActualShutdown = J.showJSON "shutdown"
110 showJSON ActualCrashed = J.showJSON "crashed"
111 showJSON ActualDying = J.showJSON "dying"
112 showJSON ActualHung = J.showJSON "hung"
113 showJSON ActualUnknown = J.showJSON "unknown"
115 readJSON = error "JSON read instance not implemented for type ActualState"