Commit 59072e7e authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

Use the kernel's ability to generate UUIDs.

This removes the dependency on either the uuid module or e2fsprogs' uuidgen.

Reviewed-by: iustinp
parent 2f8b60b3
......@@ -43,23 +43,6 @@ from ganeti import rpc
from ganeti import objects
def _my_uuidgen():
"""Poor-man's uuidgen using the uuidgen binary.
"""
result = utils.RunCmd(["uuidgen", "-r"])
if result.failed:
return None
return result.stdout.rstrip('\n')
try:
import uuid
_uuidgen = uuid.uuid4
except ImportError:
_uuidgen = _my_uuidgen
class ConfigWriter:
"""The interface to the cluster configuration.
......@@ -149,7 +132,7 @@ class ConfigWriter:
existing.update(exceptions)
retries = 64
while retries > 0:
unique_id = _uuidgen()
unique_id = utils.GetUUID()
if unique_id not in existing and unique_id is not None:
break
else:
......
......@@ -834,3 +834,14 @@ def GetHomeDir(uid, default=None):
except KeyError:
return default
return result.pw_dir
def GetUUID():
"""Returns a random UUID.
"""
f = open("/proc/sys/kernel/random/uuid", "r")
try:
return f.read(128).rstrip("\n")
finally:
f.close()
......@@ -29,9 +29,11 @@ import os.path
import md5
import socket
import shutil
import re
import ganeti
from ganeti import constants
from ganeti import utils
from ganeti.utils import IsProcessAlive, Lock, Unlock, RunCmd, \
RemoveFile, CheckDict, MatchNameComponent, FormatUnit, \
ParseUnit, AddAuthorizedKey, RemoveAuthorizedKey, \
......@@ -539,5 +541,15 @@ class TestListVisibleFiles(unittest.TestCase):
self._test(files, expected)
class TestGetUUID(unittest.TestCase):
"""Test case for GetUUID"""
_re_uuid = re.compile('^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-'
'[a-f0-9]{4}-[a-f0-9]{12}$')
def runTest(self):
self.failUnless(self._re_uuid.match(utils.GetUUID()))
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment