Revision 59072e7e
b/lib/config.py | ||
---|---|---|
43 | 43 |
from ganeti import objects |
44 | 44 |
|
45 | 45 |
|
46 |
def _my_uuidgen(): |
|
47 |
"""Poor-man's uuidgen using the uuidgen binary. |
|
48 |
|
|
49 |
""" |
|
50 |
result = utils.RunCmd(["uuidgen", "-r"]) |
|
51 |
if result.failed: |
|
52 |
return None |
|
53 |
return result.stdout.rstrip('\n') |
|
54 |
|
|
55 |
|
|
56 |
try: |
|
57 |
import uuid |
|
58 |
_uuidgen = uuid.uuid4 |
|
59 |
except ImportError: |
|
60 |
_uuidgen = _my_uuidgen |
|
61 |
|
|
62 |
|
|
63 | 46 |
class ConfigWriter: |
64 | 47 |
"""The interface to the cluster configuration. |
65 | 48 |
|
... | ... | |
149 | 132 |
existing.update(exceptions) |
150 | 133 |
retries = 64 |
151 | 134 |
while retries > 0: |
152 |
unique_id = _uuidgen()
|
|
135 |
unique_id = utils.GetUUID()
|
|
153 | 136 |
if unique_id not in existing and unique_id is not None: |
154 | 137 |
break |
155 | 138 |
else: |
b/lib/utils.py | ||
---|---|---|
834 | 834 |
except KeyError: |
835 | 835 |
return default |
836 | 836 |
return result.pw_dir |
837 |
|
|
838 |
|
|
839 |
def GetUUID(): |
|
840 |
"""Returns a random UUID. |
|
841 |
|
|
842 |
""" |
|
843 |
f = open("/proc/sys/kernel/random/uuid", "r") |
|
844 |
try: |
|
845 |
return f.read(128).rstrip("\n") |
|
846 |
finally: |
|
847 |
f.close() |
b/test/ganeti.utils_unittest.py | ||
---|---|---|
29 | 29 |
import md5 |
30 | 30 |
import socket |
31 | 31 |
import shutil |
32 |
import re |
|
32 | 33 |
|
33 | 34 |
import ganeti |
34 | 35 |
from ganeti import constants |
36 |
from ganeti import utils |
|
35 | 37 |
from ganeti.utils import IsProcessAlive, Lock, Unlock, RunCmd, \ |
36 | 38 |
RemoveFile, CheckDict, MatchNameComponent, FormatUnit, \ |
37 | 39 |
ParseUnit, AddAuthorizedKey, RemoveAuthorizedKey, \ |
... | ... | |
539 | 541 |
self._test(files, expected) |
540 | 542 |
|
541 | 543 |
|
544 |
class TestGetUUID(unittest.TestCase): |
|
545 |
"""Test case for GetUUID""" |
|
546 |
|
|
547 |
_re_uuid = re.compile('^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-' |
|
548 |
'[a-f0-9]{4}-[a-f0-9]{12}$') |
|
549 |
|
|
550 |
def runTest(self): |
|
551 |
self.failUnless(self._re_uuid.match(utils.GetUUID())) |
|
552 |
|
|
553 |
|
|
542 | 554 |
if __name__ == '__main__': |
543 | 555 |
unittest.main() |
Also available in: Unified diff