summaryrefslogtreecommitdiffstats
path: root/nova/utils.py
diff options
context:
space:
mode:
authorThierry Carrez <thierry@openstack.org>2011-02-22 17:16:43 +0100
committerThierry Carrez <thierry@openstack.org>2011-02-22 17:16:43 +0100
commitf80d4c859b95773b8637f044f975354964ee0989 (patch)
tree7059f6f55f3acf4d9a8a08a885723808832eadd4 /nova/utils.py
parentbf570ca5f199091d505d96b91a3dc3acfbfc9fc7 (diff)
parentf9f9bf52f50604afa05fdd7300601f28d7b441c0 (diff)
downloadnova-f80d4c859b95773b8637f044f975354964ee0989.tar.gz
nova-f80d4c859b95773b8637f044f975354964ee0989.tar.xz
nova-f80d4c859b95773b8637f044f975354964ee0989.zip
Merged trunk
Diffstat (limited to 'nova/utils.py')
-rw-r--r--nova/utils.py27
1 files changed, 25 insertions, 2 deletions
diff --git a/nova/utils.py b/nova/utils.py
index 8d7ff1f64..2a3acf042 100644
--- a/nova/utils.py
+++ b/nova/utils.py
@@ -20,13 +20,14 @@
System-level utilities and helper functions.
"""
+import base64
import datetime
import inspect
import json
import os
import random
-import subprocess
import socket
+import string
import struct
import sys
import time
@@ -36,6 +37,7 @@ import netaddr
from eventlet import event
from eventlet import greenthread
+from eventlet.green import subprocess
from nova import exception
from nova.exception import ProcessExecutionError
@@ -53,7 +55,7 @@ def import_class(import_str):
__import__(mod_str)
return getattr(sys.modules[mod_str], class_str)
except (ImportError, ValueError, AttributeError), exc:
- logging.debug(_('Inner Exception: %s'), exc)
+ LOG.debug(_('Inner Exception: %s'), exc)
raise exception.NotFound(_('Class %s cannot be found') % class_str)
@@ -235,6 +237,15 @@ def generate_mac():
return ':'.join(map(lambda x: "%02x" % x, mac))
+def generate_password(length=20):
+ """Generate a random sequence of letters and digits
+ to be used as a password. Note that this is not intended
+ to represent the ultimate in security.
+ """
+ chrs = string.letters + string.digits
+ return "".join([random.choice(chrs) for i in xrange(length)])
+
+
def last_octet(address):
return int(address.split(".")[-1])
@@ -476,3 +487,15 @@ def dumps(value):
def loads(s):
return json.loads(s)
+
+
+def ensure_b64_encoding(val):
+ """Safety method to ensure that values expected to be base64-encoded
+ actually are. If they are, the value is returned unchanged. Otherwise,
+ the encoded value is returned.
+ """
+ try:
+ dummy = base64.decode(val)
+ return val
+ except TypeError:
+ return base64.b64encode(val)