root / lib / utils / storage.py @ 178ad717
History | View | Annotate | Download (5.9 kB)
1 | 59ef0f15 | Helga Velroyen | #
|
---|---|---|---|
2 | 59ef0f15 | Helga Velroyen | #
|
3 | 59ef0f15 | Helga Velroyen | |
4 | 59ef0f15 | Helga Velroyen | # Copyright (C) 2013 Google Inc.
|
5 | 59ef0f15 | Helga Velroyen | #
|
6 | 59ef0f15 | Helga Velroyen | # This program is free software; you can redistribute it and/or modify
|
7 | 59ef0f15 | Helga Velroyen | # it under the terms of the GNU General Public License as published by
|
8 | 59ef0f15 | Helga Velroyen | # the Free Software Foundation; either version 2 of the License, or
|
9 | 59ef0f15 | Helga Velroyen | # (at your option) any later version.
|
10 | 59ef0f15 | Helga Velroyen | #
|
11 | 59ef0f15 | Helga Velroyen | # This program is distributed in the hope that it will be useful, but
|
12 | 59ef0f15 | Helga Velroyen | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 59ef0f15 | Helga Velroyen | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 59ef0f15 | Helga Velroyen | # General Public License for more details.
|
15 | 59ef0f15 | Helga Velroyen | #
|
16 | 59ef0f15 | Helga Velroyen | # You should have received a copy of the GNU General Public License
|
17 | 59ef0f15 | Helga Velroyen | # along with this program; if not, write to the Free Software
|
18 | 59ef0f15 | Helga Velroyen | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | 59ef0f15 | Helga Velroyen | # 02110-1301, USA.
|
20 | 59ef0f15 | Helga Velroyen | |
21 | 59ef0f15 | Helga Velroyen | """Utility functions for storage.
|
22 | 59ef0f15 | Helga Velroyen |
|
23 | 59ef0f15 | Helga Velroyen | """
|
24 | 59ef0f15 | Helga Velroyen | |
25 | 683335b3 | Helga Velroyen | import logging |
26 | 683335b3 | Helga Velroyen | |
27 | 59ef0f15 | Helga Velroyen | from ganeti import constants |
28 | 59ef0f15 | Helga Velroyen | |
29 | 59ef0f15 | Helga Velroyen | |
30 | 5a904197 | Santi Raffa | def GetDiskTemplatesOfStorageTypes(*storage_types): |
31 | 59ef0f15 | Helga Velroyen | """Given the storage type, returns a list of disk templates based on that
|
32 | 59ef0f15 | Helga Velroyen | storage type."""
|
33 | 59ef0f15 | Helga Velroyen | return [dt for dt in constants.DISK_TEMPLATES |
34 | 5a904197 | Santi Raffa | if constants.MAP_DISK_TEMPLATE_STORAGE_TYPE[dt] in storage_types] |
35 | 59ef0f15 | Helga Velroyen | |
36 | 59ef0f15 | Helga Velroyen | |
37 | ebe93784 | Helga Velroyen | def IsDiskTemplateEnabled(disk_template, enabled_disk_templates): |
38 | ebe93784 | Helga Velroyen | """Checks if a particular disk template is enabled.
|
39 | ebe93784 | Helga Velroyen |
|
40 | ebe93784 | Helga Velroyen | """
|
41 | ebe93784 | Helga Velroyen | return disk_template in enabled_disk_templates |
42 | ebe93784 | Helga Velroyen | |
43 | ebe93784 | Helga Velroyen | |
44 | ebe93784 | Helga Velroyen | def IsFileStorageEnabled(enabled_disk_templates): |
45 | ebe93784 | Helga Velroyen | """Checks if file storage is enabled.
|
46 | ebe93784 | Helga Velroyen |
|
47 | ebe93784 | Helga Velroyen | """
|
48 | ebe93784 | Helga Velroyen | return IsDiskTemplateEnabled(constants.DT_FILE, enabled_disk_templates)
|
49 | ebe93784 | Helga Velroyen | |
50 | ebe93784 | Helga Velroyen | |
51 | ebe93784 | Helga Velroyen | def IsSharedFileStorageEnabled(enabled_disk_templates): |
52 | ebe93784 | Helga Velroyen | """Checks if shared file storage is enabled.
|
53 | ebe93784 | Helga Velroyen |
|
54 | ebe93784 | Helga Velroyen | """
|
55 | ebe93784 | Helga Velroyen | return IsDiskTemplateEnabled(constants.DT_SHARED_FILE, enabled_disk_templates)
|
56 | ebe93784 | Helga Velroyen | |
57 | ebe93784 | Helga Velroyen | |
58 | 59ef0f15 | Helga Velroyen | def IsLvmEnabled(enabled_disk_templates): |
59 | 59ef0f15 | Helga Velroyen | """Check whether or not any lvm-based disk templates are enabled."""
|
60 | d48c944b | Helga Velroyen | return len(constants.DTS_LVM & set(enabled_disk_templates)) != 0 |
61 | 59ef0f15 | Helga Velroyen | |
62 | 59ef0f15 | Helga Velroyen | |
63 | 59ef0f15 | Helga Velroyen | def LvmGetsEnabled(enabled_disk_templates, new_enabled_disk_templates): |
64 | 59ef0f15 | Helga Velroyen | """Checks whether lvm was not enabled before, but will be enabled after
|
65 | 59ef0f15 | Helga Velroyen | the operation.
|
66 | 59ef0f15 | Helga Velroyen |
|
67 | 59ef0f15 | Helga Velroyen | """
|
68 | 59ef0f15 | Helga Velroyen | if IsLvmEnabled(enabled_disk_templates):
|
69 | 59ef0f15 | Helga Velroyen | return False |
70 | d48c944b | Helga Velroyen | return len(constants.DTS_LVM & set(new_enabled_disk_templates)) != 0 |
71 | 683335b3 | Helga Velroyen | |
72 | 683335b3 | Helga Velroyen | |
73 | 683335b3 | Helga Velroyen | def _GetDefaultStorageUnitForDiskTemplate(cfg, disk_template): |
74 | 683335b3 | Helga Velroyen | """Retrieves the identifier of the default storage entity for the given
|
75 | 683335b3 | Helga Velroyen | storage type.
|
76 | 683335b3 | Helga Velroyen |
|
77 | d721894a | Helga Velroyen | @type cfg: C{objects.ConfigData}
|
78 | d721894a | Helga Velroyen | @param cfg: the configuration data
|
79 | 683335b3 | Helga Velroyen | @type disk_template: string
|
80 | 683335b3 | Helga Velroyen | @param disk_template: a disk template, for example 'drbd'
|
81 | 683335b3 | Helga Velroyen | @rtype: string
|
82 | 683335b3 | Helga Velroyen | @return: identifier for a storage unit, for example the vg_name for lvm
|
83 | 683335b3 | Helga Velroyen | storage
|
84 | 683335b3 | Helga Velroyen |
|
85 | 683335b3 | Helga Velroyen | """
|
86 | 615551b2 | Helga Velroyen | storage_type = constants.MAP_DISK_TEMPLATE_STORAGE_TYPE[disk_template] |
87 | d721894a | Helga Velroyen | cluster = cfg.GetClusterInfo() |
88 | d48c944b | Helga Velroyen | if disk_template in constants.DTS_LVM: |
89 | 683335b3 | Helga Velroyen | return (storage_type, cfg.GetVGName())
|
90 | 683335b3 | Helga Velroyen | elif disk_template == constants.DT_FILE:
|
91 | d721894a | Helga Velroyen | return (storage_type, cluster.file_storage_dir)
|
92 | 683335b3 | Helga Velroyen | elif disk_template == constants.DT_SHARED_FILE:
|
93 | 2fe1e043 | Helga Velroyen | return (storage_type, cluster.shared_file_storage_dir)
|
94 | 8106dd64 | Santi Raffa | elif disk_template == constants.DT_GLUSTER:
|
95 | ac156ecd | Santi Raffa | return (storage_type, cluster.gluster_storage_dir)
|
96 | 683335b3 | Helga Velroyen | else:
|
97 | 683335b3 | Helga Velroyen | return (storage_type, None) |
98 | 683335b3 | Helga Velroyen | |
99 | 683335b3 | Helga Velroyen | |
100 | 6c00b2c7 | Helga Velroyen | def DiskTemplateSupportsSpaceReporting(disk_template): |
101 | 6c00b2c7 | Helga Velroyen | """Check whether the disk template supports storage space reporting."""
|
102 | 6c00b2c7 | Helga Velroyen | return (constants.MAP_DISK_TEMPLATE_STORAGE_TYPE[disk_template]
|
103 | 6c00b2c7 | Helga Velroyen | in constants.STS_REPORT)
|
104 | 683335b3 | Helga Velroyen | |
105 | 683335b3 | Helga Velroyen | |
106 | b669aa41 | Helga Velroyen | def GetStorageUnits(cfg, disk_templates): |
107 | b669aa41 | Helga Velroyen | """Get the cluster's storage units for the given disk templates.
|
108 | b669aa41 | Helga Velroyen |
|
109 | b669aa41 | Helga Velroyen | If any lvm-based disk template is requested, spindle information
|
110 | b669aa41 | Helga Velroyen | is added to the request.
|
111 | b669aa41 | Helga Velroyen |
|
112 | b669aa41 | Helga Velroyen | @type cfg: L{config.ConfigWriter}
|
113 | b669aa41 | Helga Velroyen | @param cfg: Cluster configuration
|
114 | b669aa41 | Helga Velroyen | @type disk_templates: list of string
|
115 | b669aa41 | Helga Velroyen | @param disk_templates: list of disk templates for which the storage
|
116 | b669aa41 | Helga Velroyen | units will be computed
|
117 | b669aa41 | Helga Velroyen | @rtype: list of tuples (string, string)
|
118 | b669aa41 | Helga Velroyen | @return: list of storage units, each storage unit being a tuple of
|
119 | b669aa41 | Helga Velroyen | (storage_type, storage_key); storage_type is in
|
120 | b669aa41 | Helga Velroyen | C{constants.STORAGE_TYPES} and the storage_key a string to
|
121 | b669aa41 | Helga Velroyen | identify an entity of that storage type, for example a volume group
|
122 | b669aa41 | Helga Velroyen | name for LVM storage or a file for file storage.
|
123 | b669aa41 | Helga Velroyen |
|
124 | b669aa41 | Helga Velroyen | """
|
125 | b669aa41 | Helga Velroyen | storage_units = [] |
126 | b669aa41 | Helga Velroyen | for disk_template in disk_templates: |
127 | 6c00b2c7 | Helga Velroyen | if DiskTemplateSupportsSpaceReporting(disk_template):
|
128 | b669aa41 | Helga Velroyen | storage_units.append( |
129 | b669aa41 | Helga Velroyen | _GetDefaultStorageUnitForDiskTemplate(cfg, disk_template)) |
130 | b669aa41 | Helga Velroyen | return storage_units
|
131 | b669aa41 | Helga Velroyen | |
132 | b669aa41 | Helga Velroyen | |
133 | b669aa41 | Helga Velroyen | def LookupSpaceInfoByDiskTemplate(storage_space_info, disk_template): |
134 | b669aa41 | Helga Velroyen | """Looks up the storage space info for a given disk template.
|
135 | b669aa41 | Helga Velroyen |
|
136 | b669aa41 | Helga Velroyen | @type storage_space_info: list of dicts
|
137 | b669aa41 | Helga Velroyen | @param storage_space_info: result of C{GetNodeInfo}
|
138 | b669aa41 | Helga Velroyen | @type disk_template: string
|
139 | b669aa41 | Helga Velroyen | @param disk_template: disk template to get storage space info
|
140 | b669aa41 | Helga Velroyen | @rtype: tuple
|
141 | b669aa41 | Helga Velroyen | @return: returns the element of storage_space_info that matches the given
|
142 | b669aa41 | Helga Velroyen | disk template
|
143 | b669aa41 | Helga Velroyen |
|
144 | b669aa41 | Helga Velroyen | """
|
145 | b669aa41 | Helga Velroyen | storage_type = constants.MAP_DISK_TEMPLATE_STORAGE_TYPE[disk_template] |
146 | b669aa41 | Helga Velroyen | return LookupSpaceInfoByStorageType(storage_space_info, storage_type)
|
147 | b669aa41 | Helga Velroyen | |
148 | b669aa41 | Helga Velroyen | |
149 | 683335b3 | Helga Velroyen | def LookupSpaceInfoByStorageType(storage_space_info, storage_type): |
150 | 683335b3 | Helga Velroyen | """Looks up the storage space info for a given storage type.
|
151 | 683335b3 | Helga Velroyen |
|
152 | 683335b3 | Helga Velroyen | Note that this lookup can be ambiguous if storage space reporting for several
|
153 | 683335b3 | Helga Velroyen | units of the same storage type was requested. This function is only supposed
|
154 | 683335b3 | Helga Velroyen | to be used for legacy code in situations where it actually is unambiguous.
|
155 | 683335b3 | Helga Velroyen |
|
156 | 683335b3 | Helga Velroyen | @type storage_space_info: list of dicts
|
157 | 683335b3 | Helga Velroyen | @param storage_space_info: result of C{GetNodeInfo}
|
158 | 683335b3 | Helga Velroyen | @type storage_type: string
|
159 | 683335b3 | Helga Velroyen | @param storage_type: a storage type, which is included in the storage_units
|
160 | 683335b3 | Helga Velroyen | list
|
161 | 683335b3 | Helga Velroyen | @rtype: tuple
|
162 | 683335b3 | Helga Velroyen | @return: returns the element of storage_space_info that matches the given
|
163 | 683335b3 | Helga Velroyen | storage type
|
164 | 683335b3 | Helga Velroyen |
|
165 | 683335b3 | Helga Velroyen | """
|
166 | 683335b3 | Helga Velroyen | result = None
|
167 | 683335b3 | Helga Velroyen | for unit_info in storage_space_info: |
168 | 683335b3 | Helga Velroyen | if unit_info["type"] == storage_type: |
169 | 683335b3 | Helga Velroyen | if result is None: |
170 | 683335b3 | Helga Velroyen | result = unit_info |
171 | 683335b3 | Helga Velroyen | else:
|
172 | 683335b3 | Helga Velroyen | # There is more than one storage type in the query, log a warning
|
173 | 683335b3 | Helga Velroyen | logging.warning("Storage space information requested for"
|
174 | 683335b3 | Helga Velroyen | " ambiguous storage type '%s'.", storage_type)
|
175 | 683335b3 | Helga Velroyen | return result |