Prefer the UuidObject type class over specific functions
[ganeti-github.git] / src / Ganeti / Storage / Utils.hs
1 {-| Implementation of Utility functions for storage
2
3 -}
4
5 {-
6
7 Copyright (C) 2013 Google Inc.
8 All rights reserved.
9
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are
12 met:
13
14 1. Redistributions of source code must retain the above copyright notice,
15 this list of conditions and the following disclaimer.
16
17 2. Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in the
19 documentation and/or other materials provided with the distribution.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
25 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33 -}
34
35 module Ganeti.Storage.Utils
36 ( getStorageUnitsOfNodes
37 , nodesWithValidConfig
38 ) where
39
40 import Ganeti.Config
41 import Ganeti.Objects
42 import Ganeti.Types
43 import qualified Ganeti.Types as T
44
45 import Control.Monad
46 import Data.List (nub)
47 import Data.Maybe
48 import qualified Data.Map as M
49
50 -- | Get the cluster's default storage unit for a given disk template
51 getDefaultStorageKey :: ConfigData -> DiskTemplate -> Maybe StorageKey
52 getDefaultStorageKey cfg T.DTDrbd8 = clusterVolumeGroupName $ configCluster cfg
53 getDefaultStorageKey cfg T.DTPlain = clusterVolumeGroupName $ configCluster cfg
54 getDefaultStorageKey cfg T.DTFile =
55 Just (clusterFileStorageDir $ configCluster cfg)
56 getDefaultStorageKey _ _ = Nothing
57
58 -- | Get the cluster's default spindle storage unit
59 getDefaultSpindleSU :: ConfigData -> (StorageType, Maybe StorageKey)
60 getDefaultSpindleSU cfg =
61 (T.StorageLvmPv, clusterVolumeGroupName $ configCluster cfg)
62
63 -- | Get the cluster's storage units from the configuration
64 getClusterStorageUnitRaws :: ConfigData -> [StorageUnitRaw]
65 getClusterStorageUnitRaws cfg =
66 foldSUs (nub (maybe_units ++ [spindle_unit]))
67 where disk_templates = clusterEnabledDiskTemplates $ configCluster cfg
68 storage_types = map diskTemplateToStorageType disk_templates
69 maybe_units = zip storage_types (map (getDefaultStorageKey cfg)
70 disk_templates)
71 spindle_unit = getDefaultSpindleSU cfg
72
73 -- | fold the storage unit list by sorting out the ones without keys
74 foldSUs :: [(StorageType, Maybe StorageKey)] -> [StorageUnitRaw]
75 foldSUs = foldr ff []
76 where ff (st, Just sk) acc = SURaw st sk : acc
77 ff (_, Nothing) acc = acc
78
79 -- | Gets the value of the 'exclusive storage' flag of the node
80 getExclusiveStorage :: ConfigData -> Node -> Maybe Bool
81 getExclusiveStorage cfg n = liftM ndpExclusiveStorage (getNodeNdParams cfg n)
82
83 -- | Determines whether a node's config contains an 'exclusive storage' flag
84 hasExclusiveStorageFlag :: ConfigData -> Node -> Bool
85 hasExclusiveStorageFlag cfg = isJust . getExclusiveStorage cfg
86
87 -- | Filter for nodes with a valid config
88 nodesWithValidConfig :: ConfigData -> [Node] -> [Node]
89 nodesWithValidConfig cfg = filter (hasExclusiveStorageFlag cfg)
90
91 -- | Get the storage units of the node
92 getStorageUnitsOfNode :: ConfigData -> Node -> [StorageUnit]
93 getStorageUnitsOfNode cfg n =
94 let clusterSUs = getClusterStorageUnitRaws cfg
95 es = fromJust (getExclusiveStorage cfg n)
96 in map (addParamsToStorageUnit es) clusterSUs
97
98 -- | Get the storage unit map for all nodes
99 getStorageUnitsOfNodes :: ConfigData -> [Node] -> M.Map String [StorageUnit]
100 getStorageUnitsOfNodes cfg ns =
101 M.fromList (map (\n -> (uuidOf n, getStorageUnitsOfNode cfg n)) ns)