root / src / Ganeti / Storage / Utils.hs @ ede6df3d
History | View | Annotate | Download (3.5 kB)
1 |
{-| Implementation of Utility functions for storage |
---|---|
2 |
|
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 |
|
26 |
module Ganeti.Storage.Utils |
27 |
( getStorageUnitsOfNodes |
28 |
, nodesWithValidConfig |
29 |
) where |
30 |
|
31 |
import Ganeti.Config |
32 |
import Ganeti.Objects |
33 |
import Ganeti.Types |
34 |
import qualified Ganeti.Types as T |
35 |
|
36 |
import Control.Monad |
37 |
import Data.List (nub) |
38 |
import Data.Maybe |
39 |
import qualified Data.Map as M |
40 |
|
41 |
-- | Get the cluster's default storage unit for a given disk template |
42 |
getDefaultStorageKey :: ConfigData -> DiskTemplate -> Maybe StorageKey |
43 |
getDefaultStorageKey cfg T.DTDrbd8 = clusterVolumeGroupName $ configCluster cfg |
44 |
getDefaultStorageKey cfg T.DTPlain = clusterVolumeGroupName $ configCluster cfg |
45 |
getDefaultStorageKey cfg T.DTFile = |
46 |
Just (clusterFileStorageDir $ configCluster cfg) |
47 |
getDefaultStorageKey cfg T.DTSharedFile = |
48 |
Just (clusterSharedFileStorageDir $ configCluster cfg) |
49 |
getDefaultStorageKey _ _ = Nothing |
50 |
|
51 |
-- | Get the cluster's default spindle storage unit |
52 |
getDefaultSpindleSU :: ConfigData -> (StorageType, Maybe StorageKey) |
53 |
getDefaultSpindleSU cfg = |
54 |
(T.StorageLvmPv, clusterVolumeGroupName $ configCluster cfg) |
55 |
|
56 |
-- | Get the cluster's storage units from the configuration |
57 |
getClusterStorageUnitRaws :: ConfigData -> [StorageUnitRaw] |
58 |
getClusterStorageUnitRaws cfg = foldSUs (maybe_units ++ [spindle_unit]) |
59 |
where disk_templates = clusterEnabledDiskTemplates $ configCluster cfg |
60 |
storage_types = nub $ map diskTemplateToStorageType disk_templates |
61 |
maybe_units = zip storage_types (map (getDefaultStorageKey cfg) |
62 |
disk_templates) |
63 |
spindle_unit = getDefaultSpindleSU cfg |
64 |
|
65 |
-- | fold the storage unit list by sorting out the ones without keys |
66 |
foldSUs :: [(StorageType, Maybe StorageKey)] -> [StorageUnitRaw] |
67 |
foldSUs = foldr ff [] |
68 |
where ff (st, Just sk) acc = SURaw st sk : acc |
69 |
ff (_, Nothing) acc = acc |
70 |
|
71 |
-- | Gets the value of the 'exclusive storage' flag of the node |
72 |
getExclusiveStorage :: ConfigData -> Node -> Maybe Bool |
73 |
getExclusiveStorage cfg n = liftM ndpExclusiveStorage (getNodeNdParams cfg n) |
74 |
|
75 |
-- | Determines whether a node's config contains an 'exclusive storage' flag |
76 |
hasExclusiveStorageFlag :: ConfigData -> Node -> Bool |
77 |
hasExclusiveStorageFlag cfg = isJust . getExclusiveStorage cfg |
78 |
|
79 |
-- | Filter for nodes with a valid config |
80 |
nodesWithValidConfig :: ConfigData -> [Node] -> [Node] |
81 |
nodesWithValidConfig cfg = filter (hasExclusiveStorageFlag cfg) |
82 |
|
83 |
-- | Get the storage units of the node |
84 |
getStorageUnitsOfNode :: ConfigData -> Node -> [StorageUnit] |
85 |
getStorageUnitsOfNode cfg n = |
86 |
let clusterSUs = getClusterStorageUnitRaws cfg |
87 |
es = fromJust (getExclusiveStorage cfg n) |
88 |
in map (addParamsToStorageUnit es) clusterSUs |
89 |
|
90 |
-- | Get the storage unit map for all nodes |
91 |
getStorageUnitsOfNodes :: ConfigData -> [Node] -> M.Map String [StorageUnit] |
92 |
getStorageUnitsOfNodes cfg ns = |
93 |
M.fromList (map (\n -> (nodeUuid n, getStorageUnitsOfNode cfg n)) ns) |