Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Storage / Utils.hs @ 212b66c3

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.Maybe
38
import qualified Data.Map as M
39

    
40
-- | Get the cluster's default storage unit for a given disk template
41
getDefaultStorageKey :: ConfigData -> DiskTemplate -> Maybe StorageKey
42
getDefaultStorageKey cfg T.DTDrbd8 = clusterVolumeGroupName $ configCluster cfg
43
getDefaultStorageKey cfg T.DTPlain = clusterVolumeGroupName $ configCluster cfg
44
getDefaultStorageKey cfg T.DTFile =
45
    Just (clusterFileStorageDir $ configCluster cfg)
46
getDefaultStorageKey cfg T.DTSharedFile =
47
    Just (clusterSharedFileStorageDir $ configCluster cfg)
48
getDefaultStorageKey _ _ = Nothing
49

    
50
-- | Get the cluster's default spindle storage unit
51
getDefaultSpindleSU :: ConfigData -> (StorageType, Maybe StorageKey)
52
getDefaultSpindleSU cfg =
53
    (T.StorageLvmPv, clusterVolumeGroupName $ configCluster cfg)
54

    
55
-- | Get the cluster's storage units from the configuration
56
getClusterStorageUnitRaws :: ConfigData -> [StorageUnitRaw]
57
getClusterStorageUnitRaws cfg = foldSUs (maybe_units ++ [spindle_unit])
58
  where disk_templates = clusterEnabledDiskTemplates $ configCluster cfg
59
        storage_types = map diskTemplateToStorageType disk_templates
60
        maybe_units = zip storage_types (map (getDefaultStorageKey cfg)
61
            disk_templates)
62
        spindle_unit = getDefaultSpindleSU cfg
63

    
64
-- | fold the storage unit list by sorting out the ones without keys
65
foldSUs :: [(StorageType, Maybe StorageKey)] -> [StorageUnitRaw]
66
foldSUs = foldr ff []
67
  where ff (st, Just sk) acc = SURaw st sk : acc
68
        ff (_, Nothing) acc = acc
69

    
70
-- | Gets the value of the 'exclusive storage' flag of the node
71
getExclusiveStorage :: ConfigData -> Node -> Maybe Bool
72
getExclusiveStorage cfg n = liftM ndpExclusiveStorage (getNodeNdParams cfg n)
73

    
74
-- | Determines whether a node's config contains an 'exclusive storage' flag
75
hasExclusiveStorageFlag :: ConfigData -> Node -> Bool
76
hasExclusiveStorageFlag cfg = isJust . getExclusiveStorage cfg
77

    
78
-- | Filter for nodes with a valid config
79
nodesWithValidConfig :: ConfigData -> [Node] -> [Node]
80
nodesWithValidConfig cfg = filter (hasExclusiveStorageFlag cfg)
81

    
82
-- | Get the storage units of the node
83
getStorageUnitsOfNode :: ConfigData -> Node -> [StorageUnit]
84
getStorageUnitsOfNode cfg n =
85
  let clusterSUs = getClusterStorageUnitRaws cfg
86
      es = fromJust (getExclusiveStorage cfg n)
87
  in  map (addParamsToStorageUnit es) clusterSUs
88

    
89
-- | Get the storage unit map for all nodes
90
getStorageUnitsOfNodes :: ConfigData -> [Node] -> M.Map String [StorageUnit]
91
getStorageUnitsOfNodes cfg ns =
92
  M.fromList (map (\n -> (nodeUuid n, getStorageUnitsOfNode cfg n)) ns)