364 lines
10 KiB
Python
Raw Normal View History

2010-05-27 23:05:26 -07:00
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2010-06-23 22:04:16 -07:00
# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
2010-06-23 22:04:16 -07:00
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
2010-05-27 23:05:26 -07:00
# Unless required by applicable law or agreed to in writing, software
2010-06-23 22:04:16 -07:00
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
2010-05-27 23:05:26 -07:00
"""
System-level utilities and helper functions.
"""
2010-08-16 14:16:21 +02:00
import datetime
import inspect
2010-05-27 23:05:26 -07:00
import logging
2010-07-07 12:24:24 -07:00
import os
2010-05-27 23:05:26 -07:00
import random
import subprocess
import socket
2010-11-23 21:48:32 +00:00
import struct
import sys
import time
from xml.sax import saxutils
2010-05-27 23:05:26 -07:00
from eventlet import event
from eventlet import greenthread
from nova import exception
from nova.exception import ProcessExecutionError
2010-06-10 18:39:07 +01:00
2010-08-16 14:16:21 +02:00
2010-07-26 17:00:50 -04:00
TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
2010-06-10 18:39:07 +01:00
def import_class(import_str):
"""Returns a class from a string including module and class"""
mod_str, _sep, class_str = import_str.rpartition('.')
try:
__import__(mod_str)
return getattr(sys.modules[mod_str], class_str)
except (ImportError, ValueError, AttributeError):
raise exception.NotFound(_('Class %s cannot be found') % class_str)
2010-06-10 18:39:07 +01:00
def import_object(import_str):
"""Returns an object including a module or module and class"""
try:
2010-08-30 00:55:19 -07:00
__import__(import_str)
return sys.modules[import_str]
except ImportError:
cls = import_class(import_str)
return cls()
2010-08-16 14:16:21 +02:00
2010-11-23 21:48:32 +00:00
def vpn_ping(address, port, timeout=0.05, session_id=None):
"""Sends a vpn negotiation packet and returns the server session.
Returns False on a failure. Basic packet structure is below.
Client packet (14 bytes)::
0 1 8 9 13
+-+--------+-----+
|x| cli_id |?????|
+-+--------+-----+
x = packet identifier 0x38
cli_id = 64 bit identifier
? = unknown, probably flags/padding
Server packet (26 bytes)::
0 1 8 9 13 14 21 2225
+-+--------+-----+--------+----+
|x| srv_id |?????| cli_id |????|
+-+--------+-----+--------+----+
x = packet identifier 0x40
cli_id = 64 bit identifier
? = unknown, probably flags/padding
bit 9 was 1 and the rest were 0 in testing
"""
if session_id is None:
session_id = random.randint(0, 0xffffffffffffffff)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
data = struct.pack("!BQxxxxxx", 0x38, session_id)
sock.sendto(data, (address, port))
sock.settimeout(timeout)
try:
received = sock.recv(2048)
except socket.timeout:
return False
finally:
sock.close()
fmt = "!BQxxxxxQxxxx"
if len(received) != struct.calcsize(fmt):
print struct.calcsize(fmt)
return False
(identifier, server_sess, client_sess) = struct.unpack(fmt, received)
if identifier == 0x40 and client_sess == session_id:
return server_sess
2010-05-27 23:05:26 -07:00
def fetchfile(url, target):
logging.debug(_("Fetching %s") % url)
2010-05-27 23:05:26 -07:00
# c = pycurl.Curl()
# fp = open(target, "wb")
# c.setopt(c.URL, url)
# c.setopt(c.WRITEDATA, fp)
# c.perform()
# c.close()
# fp.close()
execute("curl --fail %s -o %s" % (url, target))
2010-05-27 23:05:26 -07:00
def execute(cmd, process_input=None, addl_env=None, check_exit_code=True):
logging.debug(_("Running cmd (subprocess): %s"), cmd)
env = os.environ.copy()
if addl_env:
env.update(addl_env)
2010-05-27 23:05:26 -07:00
obj = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
2010-05-27 23:05:26 -07:00
result = None
if process_input != None:
result = obj.communicate(process_input)
2010-05-27 23:05:26 -07:00
else:
result = obj.communicate()
obj.stdin.close()
if obj.returncode:
logging.debug(_("Result was %s") % (obj.returncode))
if check_exit_code and obj.returncode != 0:
(stdout, stderr) = result
raise ProcessExecutionError(exit_code=obj.returncode,
stdout=stdout,
stderr=stderr,
cmd=cmd)
# NOTE(termie): this appears to be necessary to let the subprocess call
# clean something up in between calls, without it two
# execute calls in a row hangs the second one
greenthread.sleep(0)
2010-05-27 23:05:26 -07:00
return result
2010-06-10 18:39:07 +01:00
2010-05-27 23:05:26 -07:00
def abspath(s):
return os.path.join(os.path.dirname(__file__), s)
2010-06-10 18:39:07 +01:00
2010-05-27 23:05:26 -07:00
def default_flagfile(filename='nova.conf'):
for arg in sys.argv:
if arg.find('flagfile') != -1:
break
else:
if not os.path.isabs(filename):
# turn relative filename into an absolute path
script_dir = os.path.dirname(inspect.stack()[-1][1])
filename = os.path.abspath(os.path.join(script_dir, filename))
if os.path.exists(filename):
flagfile = ['--flagfile=%s' % filename]
sys.argv = sys.argv[:1] + flagfile + sys.argv[1:]
2010-05-27 23:05:26 -07:00
2010-06-10 18:39:07 +01:00
2010-05-27 23:05:26 -07:00
def debug(arg):
logging.debug('debug in callback: %s', arg)
return arg
2010-06-10 18:39:07 +01:00
def runthis(prompt, cmd, check_exit_code=True):
logging.debug(_("Running %s") % (cmd))
rv, err = execute(cmd, check_exit_code=check_exit_code)
2010-05-27 23:05:26 -07:00
def generate_uid(topic, size=8):
2010-10-25 17:20:10 -07:00
characters = '01234567890abcdefghijklmnopqrstuvwxyz'
choices = [random.choice(characters) for x in xrange(size)]
return '%s-%s' % (topic, ''.join(choices))
2010-05-27 23:05:26 -07:00
2010-06-10 18:39:07 +01:00
2010-05-27 23:05:26 -07:00
def generate_mac():
mac = [0x02, 0x16, 0x3e,
random.randint(0x00, 0x7f),
random.randint(0x00, 0xff),
random.randint(0x00, 0xff)]
2010-05-27 23:05:26 -07:00
return ':'.join(map(lambda x: "%02x" % x, mac))
2010-06-10 18:39:07 +01:00
2010-05-27 23:05:26 -07:00
def last_octet(address):
return int(address.split(".")[-1])
2010-06-10 18:39:07 +01:00
2010-05-27 23:05:26 -07:00
def get_my_ip():
2010-08-30 15:15:00 -07:00
"""Returns the actual ip of the local machine."""
try:
csock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
csock.connect(('8.8.8.8', 80))
(addr, port) = csock.getsockname()
csock.close()
return addr
except socket.gaierror as ex:
logging.warn(_("Couldn't get IP, using 127.0.0.1 %s"), ex)
return "127.0.0.1"
def utcnow():
"""Overridable version of datetime.datetime.utcnow."""
if utcnow.override_time:
return utcnow.override_time
return datetime.datetime.utcnow()
utcnow.override_time = None
def utcnow_ts():
"""Timestamp version of our utcnow function."""
return time.mktime(utcnow().timetuple())
def set_time_override(override_time=datetime.datetime.utcnow()):
"""Override utils.utcnow to return a constant time."""
utcnow.override_time = override_time
def advance_time_delta(timedelta):
"""Advance overriden time using a datetime.timedelta."""
assert(not utcnow.override_time is None)
utcnow.override_time += timedelta
def advance_time_seconds(seconds):
"""Advance overriden time by seconds."""
advance_time_delta(datetime.timedelta(0, seconds))
def clear_time_override():
"""Remove the overridden time."""
utcnow.override_time = None
def isotime(at=None):
"""Returns iso formatted utcnow."""
if not at:
at = utcnow()
2010-07-26 17:00:50 -04:00
return at.strftime(TIME_FORMAT)
2010-08-16 14:16:21 +02:00
2010-07-26 17:00:50 -04:00
def parse_isotime(timestr):
"""Turn an iso formatted time back into a datetime"""
2010-08-16 14:16:21 +02:00
return datetime.datetime.strptime(timestr, TIME_FORMAT)
2010-08-19 12:28:45 +02:00
def parse_mailmap(mailmap='.mailmap'):
mapping = {}
if os.path.exists(mailmap):
fp = open(mailmap, 'r')
for l in fp:
l = l.strip()
if not l.startswith('#') and ' ' in l:
canonical_email, alias = l.split(' ')
mapping[alias] = canonical_email
return mapping
2010-11-23 21:58:46 +01:00
def str_dict_replace(s, mapping):
for s1, s2 in mapping.iteritems():
s = s.replace(s1, s2)
return s
2010-08-19 12:28:45 +02:00
class LazyPluggable(object):
"""A pluggable backend loaded lazily based on some value."""
2010-08-19 12:28:45 +02:00
def __init__(self, pivot, **backends):
self.__backends = backends
self.__pivot = pivot
self.__backend = None
2010-08-19 12:28:45 +02:00
def __get_backend(self):
if not self.__backend:
backend_name = self.__pivot.value
if backend_name not in self.__backends:
raise exception.Error(_('Invalid backend: %s') % backend_name)
2010-08-19 12:28:45 +02:00
backend = self.__backends[backend_name]
if type(backend) == type(tuple()):
name = backend[0]
fromlist = backend[1]
else:
name = backend
fromlist = backend
2010-08-19 12:28:45 +02:00
self.__backend = __import__(name, None, None, fromlist)
logging.info('backend %s', self.__backend)
2010-08-19 12:28:45 +02:00
return self.__backend
2010-08-19 12:28:45 +02:00
def __getattr__(self, key):
backend = self.__get_backend()
return getattr(backend, key)
class LoopingCall(object):
def __init__(self, f=None, *args, **kw):
self.args = args
self.kw = kw
self.f = f
self._running = False
def start(self, interval, now=True):
self._running = True
done = event.Event()
2010-12-16 10:52:30 -08:00
def _inner():
if not now:
greenthread.sleep(interval)
try:
while self._running:
self.f(*self.args, **self.kw)
greenthread.sleep(interval)
except Exception:
2010-12-14 16:05:39 -08:00
logging.exception('in looping call')
done.send_exception(*sys.exc_info())
return
2010-12-16 10:52:30 -08:00
done.send(True)
2010-12-09 15:19:56 -08:00
self.done = done
2010-12-16 10:52:30 -08:00
greenthread.spawn(_inner)
2010-12-09 15:19:56 -08:00
return self.done
2010-12-16 10:52:30 -08:00
def stop(self):
self._running = False
2010-12-09 15:19:56 -08:00
def wait(self):
return self.done.wait()
def xhtml_escape(value):
"""Escapes a string so it is valid within XML or XHTML.
Code is directly from the utf8 function in
http://github.com/facebook/tornado/blob/master/tornado/escape.py
"""
return saxutils.escape(value, {'"': """})
def utf8(value):
"""Try to turn a string into utf-8 if possible.
Code is directly from the utf8 function in
http://github.com/facebook/tornado/blob/master/tornado/escape.py
"""
if isinstance(value, unicode):
return value.encode("utf-8")
assert isinstance(value, str)
return value