Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Storage / Utils.hs @ ac156ecd

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 =
59
    foldSUs (nub (maybe_units ++ [spindle_unit]))
60
  where disk_templates = clusterEnabledDiskTemplates $ configCluster cfg
61
        storage_types = map diskTemplateToStorageType disk_templates
62
        maybe_units = zip storage_types (map (getDefaultStorageKey cfg)
63
            disk_templates)
64
        spindle_unit = getDefaultSpindleSU cfg
65

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

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

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

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

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

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