Statistics
| Branch: | Tag: | Revision:

root / lib / uidpool.py @ fdad8c4d

History | View | Annotate | Download (4.7 kB)

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 AddToUidPool(uid_pool, add_uids):
82
  """Add a list of user-ids/user-id ranges to a user-id pool.
83

84
  @param uid_pool: a user-id pool (list of integer tuples)
85
  @param add_uids: user-id ranges to be added to the pool
86
                   (list of integer tuples)
87

88
  """
89
  for uid_range in add_uids:
90
    if uid_range not in uid_pool:
91
      uid_pool.append(uid_range)
92
  uid_pool.sort()
93

    
94

    
95
def RemoveFromUidPool(uid_pool, remove_uids):
96
  """Remove a list of user-ids/user-id ranges from a user-id pool.
97

98
  @param uid_pool: a user-id pool (list of integer tuples)
99
  @param remove_uids: user-id ranges to be removed from the pool
100
                      (list of integer tuples)
101

102
  """
103
  for uid_range in remove_uids:
104
    if uid_range not in uid_pool:
105
      raise errors.OpPrereqError(
106
          "User-id range to be removed is not found in the current"
107
          " user-id pool: %s" % uid_range, errors.ECODE_INVAL)
108
    uid_pool.remove(uid_range)
109

    
110

    
111
def CheckUidPool(uid_pool):
112
  """Sanity check user-id pool range definition values.
113

114
  @param uid_pool: a list of integer pairs (lower, higher range boundaries)
115

116
  """
117
  for lower, higher in uid_pool:
118
    if lower > higher:
119
      raise errors.OpPrereqError(
120
          "Lower user-id range boundary value (%s)"
121
          " is larger than higher boundary value (%s)" %
122
          (lower, higher), errors.ECODE_INVAL)
123
    if lower < constants.UIDPOOL_UID_MIN:
124
      raise errors.OpPrereqError(
125
          "Lower user-id range boundary value (%s)"
126
          " is smaller than UIDPOOL_UID_MIN (%s)." %
127
          (lower, constants.UIDPOOL_UID_MIN),
128
          errors.ECODE_INVAL)
129
    if higher > constants.UIDPOOL_UID_MAX:
130
      raise errors.OpPrereqError(
131
          "Higher user-id boundary value (%s)"
132
          " is larger than UIDPOOL_UID_MAX (%s)." %
133
          (higher, constants.UIDPOOL_UID_MAX),
134
          errors.ECODE_INVAL)
135

    
136

    
137
def ExpandUidPool(uid_pool):
138
  """Expands a uid-pool definition to a list of uids.
139

140
  @param uid_pool: a list of integer pairs (lower, higher range boundaries)
141
  @return: a list of integers
142

143
  """
144
  uids = set()
145
  for lower, higher in uid_pool:
146
    uids.update(range(lower, higher + 1))
147
  return list(uids)