|
1 |
#
|
|
2 |
#
|
|
3 |
|
|
4 |
# Copyright (C) 2010 Google Inc.
|
|
5 |
#
|
|
6 |
# This program is free software; you can redistribute it and/or modify
|
|
7 |
# it under the terms of the GNU General Public License as published by
|
|
8 |
# the Free Software Foundation; either version 2 of the License, or
|
|
9 |
# (at your option) any later version.
|
|
10 |
#
|
|
11 |
# This program is distributed in the hope that it will be useful, but
|
|
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
14 |
# General Public License for more details.
|
|
15 |
#
|
|
16 |
# You should have received a copy of the GNU General Public License
|
|
17 |
# along with this program; if not, write to the Free Software
|
|
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
19 |
# 02110-1301, USA.
|
|
20 |
|
|
21 |
|
|
22 |
"""User-id pool related functions.
|
|
23 |
|
|
24 |
The user-id pool is cluster-wide configuration option.
|
|
25 |
It is stored as a list of user-id ranges.
|
|
26 |
This module contains functions used for manipulating the
|
|
27 |
user-id pool parameter and for requesting/returning user-ids
|
|
28 |
from the pool.
|
|
29 |
|
|
30 |
"""
|
|
31 |
|
|
32 |
from ganeti import errors
|
|
33 |
from ganeti import constants
|
|
34 |
|
|
35 |
|
|
36 |
def ParseUidPool(value, separator=None):
|
|
37 |
"""Parse a user-id pool definition.
|
|
38 |
|
|
39 |
@param value: string representation of the user-id pool.
|
|
40 |
The accepted input format is a list of integer ranges.
|
|
41 |
The boundaries are inclusive.
|
|
42 |
Example: '1000-5000,8000,9000-9010'.
|
|
43 |
@param separator: the separator character between the uids/uid-ranges.
|
|
44 |
Defaults to a comma.
|
|
45 |
@return: a list of integer pairs (lower, higher range boundaries)
|
|
46 |
|
|
47 |
"""
|
|
48 |
if separator is None:
|
|
49 |
separator = ","
|
|
50 |
|
|
51 |
ranges = []
|
|
52 |
for range_def in value.split(separator):
|
|
53 |
if not range_def:
|
|
54 |
# Skip empty strings
|
|
55 |
continue
|
|
56 |
boundaries = range_def.split("-")
|
|
57 |
n_elements = len(boundaries)
|
|
58 |
if n_elements > 2:
|
|
59 |
raise errors.OpPrereqError(
|
|
60 |
"Invalid user-id range definition. Only one hyphen allowed: %s"
|
|
61 |
% boundaries)
|
|
62 |
try:
|
|
63 |
lower = int(boundaries[0])
|
|
64 |
except (ValueError, TypeError), err:
|
|
65 |
raise errors.OpPrereqError("Invalid user-id value for lower boundary of"
|
|
66 |
" user-id range: %s"
|
|
67 |
% str(err), errors.ECODE_INVAL)
|
|
68 |
try:
|
|
69 |
higher = int(boundaries[n_elements - 1])
|
|
70 |
except (ValueError, TypeError), err:
|
|
71 |
raise errors.OpPrereqError("Invalid user-id value for higher boundary of"
|
|
72 |
" user-id range: %s"
|
|
73 |
% str(err), errors.ECODE_INVAL)
|
|
74 |
|
|
75 |
ranges.append((lower, higher))
|
|
76 |
|
|
77 |
ranges.sort()
|
|
78 |
return ranges
|
|
79 |
|
|
80 |
|
|
81 |
def CheckUidPool(uid_pool):
|
|
82 |
"""Sanity check user-id pool range definition values.
|
|
83 |
|
|
84 |
@param uid_pool: a list of integer pairs (lower, higher range boundaries)
|
|
85 |
|
|
86 |
"""
|
|
87 |
for lower, higher in uid_pool:
|
|
88 |
if lower > higher:
|
|
89 |
raise errors.OpPrereqError(
|
|
90 |
"Lower user-id range boundary value (%s)"
|
|
91 |
" is larger than higher boundary value (%s)" %
|
|
92 |
(lower, higher), errors.ECODE_INVAL)
|
|
93 |
if lower < constants.UIDPOOL_UID_MIN:
|
|
94 |
raise errors.OpPrereqError(
|
|
95 |
"Lower user-id range boundary value (%s)"
|
|
96 |
" is smaller than UIDPOOL_UID_MIN (%s)." %
|
|
97 |
(lower, constants.UIDPOOL_UID_MIN),
|
|
98 |
errors.ECODE_INVAL)
|
|
99 |
if higher > constants.UIDPOOL_UID_MAX:
|
|
100 |
raise errors.OpPrereqError(
|
|
101 |
"Higher user-id boundary value (%s)"
|
|
102 |
" is larger than UIDPOOL_UID_MAX (%s)." %
|
|
103 |
(higher, constants.UIDPOOL_UID_MAX),
|
|
104 |
errors.ECODE_INVAL)
|
|
105 |
|
|
106 |
|
|
107 |
def ExpandUidPool(uid_pool):
|
|
108 |
"""Expands a uid-pool definition to a list of uids.
|
|
109 |
|
|
110 |
@param uid_pool: a list of integer pairs (lower, higher range boundaries)
|
|
111 |
@return: a list of integers
|
|
112 |
|
|
113 |
"""
|
|
114 |
uids = set()
|
|
115 |
for lower, higher in uid_pool:
|
|
116 |
uids.update(range(lower, higher + 1))
|
|
117 |
return list(uids)
|