root / lib / uidpool.py @ 0fbae49a
History | View | Annotate | Download (5.4 kB)
1 | 6d127406 | Balazs Lecz | #
|
---|---|---|---|
2 | 6d127406 | Balazs Lecz | #
|
3 | 6d127406 | Balazs Lecz | |
4 | 6d127406 | Balazs Lecz | # Copyright (C) 2010 Google Inc.
|
5 | 6d127406 | Balazs Lecz | #
|
6 | 6d127406 | Balazs Lecz | # This program is free software; you can redistribute it and/or modify
|
7 | 6d127406 | Balazs Lecz | # it under the terms of the GNU General Public License as published by
|
8 | 6d127406 | Balazs Lecz | # the Free Software Foundation; either version 2 of the License, or
|
9 | 6d127406 | Balazs Lecz | # (at your option) any later version.
|
10 | 6d127406 | Balazs Lecz | #
|
11 | 6d127406 | Balazs Lecz | # This program is distributed in the hope that it will be useful, but
|
12 | 6d127406 | Balazs Lecz | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 6d127406 | Balazs Lecz | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 6d127406 | Balazs Lecz | # General Public License for more details.
|
15 | 6d127406 | Balazs Lecz | #
|
16 | 6d127406 | Balazs Lecz | # You should have received a copy of the GNU General Public License
|
17 | 6d127406 | Balazs Lecz | # along with this program; if not, write to the Free Software
|
18 | 6d127406 | Balazs Lecz | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | 6d127406 | Balazs Lecz | # 02110-1301, USA.
|
20 | 6d127406 | Balazs Lecz | |
21 | 6d127406 | Balazs Lecz | |
22 | 6d127406 | Balazs Lecz | """User-id pool related functions.
|
23 | 6d127406 | Balazs Lecz |
|
24 | 6d127406 | Balazs Lecz | The user-id pool is cluster-wide configuration option.
|
25 | 6d127406 | Balazs Lecz | It is stored as a list of user-id ranges.
|
26 | 6d127406 | Balazs Lecz | This module contains functions used for manipulating the
|
27 | 6d127406 | Balazs Lecz | user-id pool parameter and for requesting/returning user-ids
|
28 | 6d127406 | Balazs Lecz | from the pool.
|
29 | 6d127406 | Balazs Lecz |
|
30 | 6d127406 | Balazs Lecz | """
|
31 | 6d127406 | Balazs Lecz | |
32 | 6d127406 | Balazs Lecz | from ganeti import errors |
33 | 6d127406 | Balazs Lecz | from ganeti import constants |
34 | 852bbc95 | Balazs Lecz | from ganeti import utils |
35 | 6d127406 | Balazs Lecz | |
36 | 6d127406 | Balazs Lecz | |
37 | 6d127406 | Balazs Lecz | def ParseUidPool(value, separator=None): |
38 | 6d127406 | Balazs Lecz | """Parse a user-id pool definition.
|
39 | 6d127406 | Balazs Lecz |
|
40 | 6d127406 | Balazs Lecz | @param value: string representation of the user-id pool.
|
41 | 6d127406 | Balazs Lecz | The accepted input format is a list of integer ranges.
|
42 | 6d127406 | Balazs Lecz | The boundaries are inclusive.
|
43 | 6d127406 | Balazs Lecz | Example: '1000-5000,8000,9000-9010'.
|
44 | 6d127406 | Balazs Lecz | @param separator: the separator character between the uids/uid-ranges.
|
45 | 6d127406 | Balazs Lecz | Defaults to a comma.
|
46 | 6d127406 | Balazs Lecz | @return: a list of integer pairs (lower, higher range boundaries)
|
47 | 6d127406 | Balazs Lecz |
|
48 | 6d127406 | Balazs Lecz | """
|
49 | 6d127406 | Balazs Lecz | if separator is None: |
50 | 6d127406 | Balazs Lecz | separator = ","
|
51 | 6d127406 | Balazs Lecz | |
52 | 6d127406 | Balazs Lecz | ranges = [] |
53 | 6d127406 | Balazs Lecz | for range_def in value.split(separator): |
54 | 6d127406 | Balazs Lecz | if not range_def: |
55 | 6d127406 | Balazs Lecz | # Skip empty strings
|
56 | 6d127406 | Balazs Lecz | continue
|
57 | 6d127406 | Balazs Lecz | boundaries = range_def.split("-")
|
58 | 6d127406 | Balazs Lecz | n_elements = len(boundaries)
|
59 | 6d127406 | Balazs Lecz | if n_elements > 2: |
60 | 6d127406 | Balazs Lecz | raise errors.OpPrereqError(
|
61 | 6d127406 | Balazs Lecz | "Invalid user-id range definition. Only one hyphen allowed: %s"
|
62 | 6d127406 | Balazs Lecz | % boundaries) |
63 | 6d127406 | Balazs Lecz | try:
|
64 | 6d127406 | Balazs Lecz | lower = int(boundaries[0]) |
65 | 6d127406 | Balazs Lecz | except (ValueError, TypeError), err: |
66 | 6d127406 | Balazs Lecz | raise errors.OpPrereqError("Invalid user-id value for lower boundary of" |
67 | 6d127406 | Balazs Lecz | " user-id range: %s"
|
68 | 6d127406 | Balazs Lecz | % str(err), errors.ECODE_INVAL)
|
69 | 6d127406 | Balazs Lecz | try:
|
70 | 6d127406 | Balazs Lecz | higher = int(boundaries[n_elements - 1]) |
71 | 6d127406 | Balazs Lecz | except (ValueError, TypeError), err: |
72 | 6d127406 | Balazs Lecz | raise errors.OpPrereqError("Invalid user-id value for higher boundary of" |
73 | 6d127406 | Balazs Lecz | " user-id range: %s"
|
74 | 6d127406 | Balazs Lecz | % str(err), errors.ECODE_INVAL)
|
75 | 6d127406 | Balazs Lecz | |
76 | 6d127406 | Balazs Lecz | ranges.append((lower, higher)) |
77 | 6d127406 | Balazs Lecz | |
78 | 6d127406 | Balazs Lecz | ranges.sort() |
79 | 6d127406 | Balazs Lecz | return ranges
|
80 | 6d127406 | Balazs Lecz | |
81 | 6d127406 | Balazs Lecz | |
82 | fdad8c4d | Balazs Lecz | def AddToUidPool(uid_pool, add_uids): |
83 | fdad8c4d | Balazs Lecz | """Add a list of user-ids/user-id ranges to a user-id pool.
|
84 | fdad8c4d | Balazs Lecz |
|
85 | fdad8c4d | Balazs Lecz | @param uid_pool: a user-id pool (list of integer tuples)
|
86 | fdad8c4d | Balazs Lecz | @param add_uids: user-id ranges to be added to the pool
|
87 | fdad8c4d | Balazs Lecz | (list of integer tuples)
|
88 | fdad8c4d | Balazs Lecz |
|
89 | fdad8c4d | Balazs Lecz | """
|
90 | fdad8c4d | Balazs Lecz | for uid_range in add_uids: |
91 | fdad8c4d | Balazs Lecz | if uid_range not in uid_pool: |
92 | fdad8c4d | Balazs Lecz | uid_pool.append(uid_range) |
93 | fdad8c4d | Balazs Lecz | uid_pool.sort() |
94 | fdad8c4d | Balazs Lecz | |
95 | fdad8c4d | Balazs Lecz | |
96 | fdad8c4d | Balazs Lecz | def RemoveFromUidPool(uid_pool, remove_uids): |
97 | fdad8c4d | Balazs Lecz | """Remove a list of user-ids/user-id ranges from a user-id pool.
|
98 | fdad8c4d | Balazs Lecz |
|
99 | fdad8c4d | Balazs Lecz | @param uid_pool: a user-id pool (list of integer tuples)
|
100 | fdad8c4d | Balazs Lecz | @param remove_uids: user-id ranges to be removed from the pool
|
101 | fdad8c4d | Balazs Lecz | (list of integer tuples)
|
102 | fdad8c4d | Balazs Lecz |
|
103 | fdad8c4d | Balazs Lecz | """
|
104 | fdad8c4d | Balazs Lecz | for uid_range in remove_uids: |
105 | fdad8c4d | Balazs Lecz | if uid_range not in uid_pool: |
106 | fdad8c4d | Balazs Lecz | raise errors.OpPrereqError(
|
107 | fdad8c4d | Balazs Lecz | "User-id range to be removed is not found in the current"
|
108 | fdad8c4d | Balazs Lecz | " user-id pool: %s" % uid_range, errors.ECODE_INVAL)
|
109 | fdad8c4d | Balazs Lecz | uid_pool.remove(uid_range) |
110 | fdad8c4d | Balazs Lecz | |
111 | fdad8c4d | Balazs Lecz | |
112 | 852bbc95 | Balazs Lecz | def _FormatUidRange(lower, higher): |
113 | 852bbc95 | Balazs Lecz | """Convert a user-id range definition into a string.
|
114 | 852bbc95 | Balazs Lecz |
|
115 | 852bbc95 | Balazs Lecz | """
|
116 | 852bbc95 | Balazs Lecz | if lower == higher:
|
117 | 852bbc95 | Balazs Lecz | return str(lower) |
118 | 852bbc95 | Balazs Lecz | return "%s-%s" % (lower, higher) |
119 | 852bbc95 | Balazs Lecz | |
120 | 852bbc95 | Balazs Lecz | |
121 | 0fbae49a | Balazs Lecz | def FormatUidPool(uid_pool, separator=None): |
122 | 852bbc95 | Balazs Lecz | """Convert the internal representation of the user-id pool into a string.
|
123 | 852bbc95 | Balazs Lecz |
|
124 | 852bbc95 | Balazs Lecz | The output format is also accepted by ParseUidPool()
|
125 | 852bbc95 | Balazs Lecz |
|
126 | 852bbc95 | Balazs Lecz | @param uid_pool: a list of integer pairs representing UID ranges
|
127 | 0fbae49a | Balazs Lecz | @param separator: the separator character between the uids/uid-ranges.
|
128 | 0fbae49a | Balazs Lecz | Defaults to ", ".
|
129 | 852bbc95 | Balazs Lecz | @return: a string with the formatted results
|
130 | 852bbc95 | Balazs Lecz |
|
131 | 852bbc95 | Balazs Lecz | """
|
132 | 0fbae49a | Balazs Lecz | if separator is None: |
133 | 0fbae49a | Balazs Lecz | separator = ", "
|
134 | 0fbae49a | Balazs Lecz | return separator.join([_FormatUidRange(lower, higher)
|
135 | 0fbae49a | Balazs Lecz | for lower, higher in uid_pool]) |
136 | 852bbc95 | Balazs Lecz | |
137 | 852bbc95 | Balazs Lecz | |
138 | 6d127406 | Balazs Lecz | def CheckUidPool(uid_pool): |
139 | 6d127406 | Balazs Lecz | """Sanity check user-id pool range definition values.
|
140 | 6d127406 | Balazs Lecz |
|
141 | 6d127406 | Balazs Lecz | @param uid_pool: a list of integer pairs (lower, higher range boundaries)
|
142 | 6d127406 | Balazs Lecz |
|
143 | 6d127406 | Balazs Lecz | """
|
144 | 6d127406 | Balazs Lecz | for lower, higher in uid_pool: |
145 | 6d127406 | Balazs Lecz | if lower > higher:
|
146 | 6d127406 | Balazs Lecz | raise errors.OpPrereqError(
|
147 | 6d127406 | Balazs Lecz | "Lower user-id range boundary value (%s)"
|
148 | 6d127406 | Balazs Lecz | " is larger than higher boundary value (%s)" %
|
149 | 6d127406 | Balazs Lecz | (lower, higher), errors.ECODE_INVAL) |
150 | 6d127406 | Balazs Lecz | if lower < constants.UIDPOOL_UID_MIN:
|
151 | 6d127406 | Balazs Lecz | raise errors.OpPrereqError(
|
152 | 6d127406 | Balazs Lecz | "Lower user-id range boundary value (%s)"
|
153 | 6d127406 | Balazs Lecz | " is smaller than UIDPOOL_UID_MIN (%s)." %
|
154 | 6d127406 | Balazs Lecz | (lower, constants.UIDPOOL_UID_MIN), |
155 | 6d127406 | Balazs Lecz | errors.ECODE_INVAL) |
156 | 6d127406 | Balazs Lecz | if higher > constants.UIDPOOL_UID_MAX:
|
157 | 6d127406 | Balazs Lecz | raise errors.OpPrereqError(
|
158 | 6d127406 | Balazs Lecz | "Higher user-id boundary value (%s)"
|
159 | 6d127406 | Balazs Lecz | " is larger than UIDPOOL_UID_MAX (%s)." %
|
160 | 6d127406 | Balazs Lecz | (higher, constants.UIDPOOL_UID_MAX), |
161 | 6d127406 | Balazs Lecz | errors.ECODE_INVAL) |
162 | 6d127406 | Balazs Lecz | |
163 | 6d127406 | Balazs Lecz | |
164 | 6d127406 | Balazs Lecz | def ExpandUidPool(uid_pool): |
165 | 6d127406 | Balazs Lecz | """Expands a uid-pool definition to a list of uids.
|
166 | 6d127406 | Balazs Lecz |
|
167 | 6d127406 | Balazs Lecz | @param uid_pool: a list of integer pairs (lower, higher range boundaries)
|
168 | 6d127406 | Balazs Lecz | @return: a list of integers
|
169 | 6d127406 | Balazs Lecz |
|
170 | 6d127406 | Balazs Lecz | """
|
171 | 6d127406 | Balazs Lecz | uids = set()
|
172 | 6d127406 | Balazs Lecz | for lower, higher in uid_pool: |
173 | 6d127406 | Balazs Lecz | uids.update(range(lower, higher + 1)) |
174 | 6d127406 | Balazs Lecz | return list(uids) |