root / lib / uidpool.py @ 6d127406
History | View | Annotate | Download (3.8 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 | 6d127406 | Balazs Lecz | |
35 | 6d127406 | Balazs Lecz | |
36 | 6d127406 | Balazs Lecz | def ParseUidPool(value, separator=None): |
37 | 6d127406 | Balazs Lecz | """Parse a user-id pool definition.
|
38 | 6d127406 | Balazs Lecz |
|
39 | 6d127406 | Balazs Lecz | @param value: string representation of the user-id pool.
|
40 | 6d127406 | Balazs Lecz | The accepted input format is a list of integer ranges.
|
41 | 6d127406 | Balazs Lecz | The boundaries are inclusive.
|
42 | 6d127406 | Balazs Lecz | Example: '1000-5000,8000,9000-9010'.
|
43 | 6d127406 | Balazs Lecz | @param separator: the separator character between the uids/uid-ranges.
|
44 | 6d127406 | Balazs Lecz | Defaults to a comma.
|
45 | 6d127406 | Balazs Lecz | @return: a list of integer pairs (lower, higher range boundaries)
|
46 | 6d127406 | Balazs Lecz |
|
47 | 6d127406 | Balazs Lecz | """
|
48 | 6d127406 | Balazs Lecz | if separator is None: |
49 | 6d127406 | Balazs Lecz | separator = ","
|
50 | 6d127406 | Balazs Lecz | |
51 | 6d127406 | Balazs Lecz | ranges = [] |
52 | 6d127406 | Balazs Lecz | for range_def in value.split(separator): |
53 | 6d127406 | Balazs Lecz | if not range_def: |
54 | 6d127406 | Balazs Lecz | # Skip empty strings
|
55 | 6d127406 | Balazs Lecz | continue
|
56 | 6d127406 | Balazs Lecz | boundaries = range_def.split("-")
|
57 | 6d127406 | Balazs Lecz | n_elements = len(boundaries)
|
58 | 6d127406 | Balazs Lecz | if n_elements > 2: |
59 | 6d127406 | Balazs Lecz | raise errors.OpPrereqError(
|
60 | 6d127406 | Balazs Lecz | "Invalid user-id range definition. Only one hyphen allowed: %s"
|
61 | 6d127406 | Balazs Lecz | % boundaries) |
62 | 6d127406 | Balazs Lecz | try:
|
63 | 6d127406 | Balazs Lecz | lower = int(boundaries[0]) |
64 | 6d127406 | Balazs Lecz | except (ValueError, TypeError), err: |
65 | 6d127406 | Balazs Lecz | raise errors.OpPrereqError("Invalid user-id value for lower boundary of" |
66 | 6d127406 | Balazs Lecz | " user-id range: %s"
|
67 | 6d127406 | Balazs Lecz | % str(err), errors.ECODE_INVAL)
|
68 | 6d127406 | Balazs Lecz | try:
|
69 | 6d127406 | Balazs Lecz | higher = int(boundaries[n_elements - 1]) |
70 | 6d127406 | Balazs Lecz | except (ValueError, TypeError), err: |
71 | 6d127406 | Balazs Lecz | raise errors.OpPrereqError("Invalid user-id value for higher boundary of" |
72 | 6d127406 | Balazs Lecz | " user-id range: %s"
|
73 | 6d127406 | Balazs Lecz | % str(err), errors.ECODE_INVAL)
|
74 | 6d127406 | Balazs Lecz | |
75 | 6d127406 | Balazs Lecz | ranges.append((lower, higher)) |
76 | 6d127406 | Balazs Lecz | |
77 | 6d127406 | Balazs Lecz | ranges.sort() |
78 | 6d127406 | Balazs Lecz | return ranges
|
79 | 6d127406 | Balazs Lecz | |
80 | 6d127406 | Balazs Lecz | |
81 | 6d127406 | Balazs Lecz | def CheckUidPool(uid_pool): |
82 | 6d127406 | Balazs Lecz | """Sanity check user-id pool range definition values.
|
83 | 6d127406 | Balazs Lecz |
|
84 | 6d127406 | Balazs Lecz | @param uid_pool: a list of integer pairs (lower, higher range boundaries)
|
85 | 6d127406 | Balazs Lecz |
|
86 | 6d127406 | Balazs Lecz | """
|
87 | 6d127406 | Balazs Lecz | for lower, higher in uid_pool: |
88 | 6d127406 | Balazs Lecz | if lower > higher:
|
89 | 6d127406 | Balazs Lecz | raise errors.OpPrereqError(
|
90 | 6d127406 | Balazs Lecz | "Lower user-id range boundary value (%s)"
|
91 | 6d127406 | Balazs Lecz | " is larger than higher boundary value (%s)" %
|
92 | 6d127406 | Balazs Lecz | (lower, higher), errors.ECODE_INVAL) |
93 | 6d127406 | Balazs Lecz | if lower < constants.UIDPOOL_UID_MIN:
|
94 | 6d127406 | Balazs Lecz | raise errors.OpPrereqError(
|
95 | 6d127406 | Balazs Lecz | "Lower user-id range boundary value (%s)"
|
96 | 6d127406 | Balazs Lecz | " is smaller than UIDPOOL_UID_MIN (%s)." %
|
97 | 6d127406 | Balazs Lecz | (lower, constants.UIDPOOL_UID_MIN), |
98 | 6d127406 | Balazs Lecz | errors.ECODE_INVAL) |
99 | 6d127406 | Balazs Lecz | if higher > constants.UIDPOOL_UID_MAX:
|
100 | 6d127406 | Balazs Lecz | raise errors.OpPrereqError(
|
101 | 6d127406 | Balazs Lecz | "Higher user-id boundary value (%s)"
|
102 | 6d127406 | Balazs Lecz | " is larger than UIDPOOL_UID_MAX (%s)." %
|
103 | 6d127406 | Balazs Lecz | (higher, constants.UIDPOOL_UID_MAX), |
104 | 6d127406 | Balazs Lecz | errors.ECODE_INVAL) |
105 | 6d127406 | Balazs Lecz | |
106 | 6d127406 | Balazs Lecz | |
107 | 6d127406 | Balazs Lecz | def ExpandUidPool(uid_pool): |
108 | 6d127406 | Balazs Lecz | """Expands a uid-pool definition to a list of uids.
|
109 | 6d127406 | Balazs Lecz |
|
110 | 6d127406 | Balazs Lecz | @param uid_pool: a list of integer pairs (lower, higher range boundaries)
|
111 | 6d127406 | Balazs Lecz | @return: a list of integers
|
112 | 6d127406 | Balazs Lecz |
|
113 | 6d127406 | Balazs Lecz | """
|
114 | 6d127406 | Balazs Lecz | uids = set()
|
115 | 6d127406 | Balazs Lecz | for lower, higher in uid_pool: |
116 | 6d127406 | Balazs Lecz | uids.update(range(lower, higher + 1)) |
117 | 6d127406 | Balazs Lecz | return list(uids) |