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
|
2010-06-23 23:15:06 -07:00
|
|
|
# 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
|
2010-07-08 10:53:27 -07:00
|
|
|
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
|
2010-07-08 10:53:27 -07:00
|
|
|
import subprocess
|
2010-07-14 16:27:18 -05:00
|
|
|
import socket
|
2010-07-08 10:53:27 -07:00
|
|
|
import sys
|
2010-05-27 23:05:26 -07:00
|
|
|
|
2010-07-28 12:53:27 -07:00
|
|
|
from nova import exception
|
2010-06-10 18:39:07 +01:00
|
|
|
from nova import flags
|
|
|
|
|
2010-08-16 14:16:21 +02:00
|
|
|
|
2010-06-10 18:39:07 +01:00
|
|
|
FLAGS = flags.FLAGS
|
2010-07-26 17:00:50 -04:00
|
|
|
TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
|
2010-06-10 18:39:07 +01:00
|
|
|
|
2010-08-20 13:26:24 +01:00
|
|
|
class ProcessExecutionError(IOError):
|
|
|
|
def __init__( self, stdout=None, stderr=None, exit_code=None, cmd=None,
|
|
|
|
description=None):
|
|
|
|
if description is None:
|
|
|
|
description = "Unexpected error while running command."
|
|
|
|
if exit_code is None:
|
|
|
|
exit_code = '-'
|
|
|
|
message = "%s\nCommand: %s\nExit code: %s\nStdout: %r\nStderr: %r" % (
|
|
|
|
description, cmd, exit_code, stdout, stderr)
|
|
|
|
IOError.__init__(self, message)
|
2010-08-16 14:16:21 +02:00
|
|
|
|
2010-07-28 12:53:27 -07: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)
|
2010-07-28 23:19:07 -07:00
|
|
|
except (ImportError, ValueError, AttributeError):
|
2010-07-28 12:53:27 -07:00
|
|
|
raise exception.NotFound('Class %s cannot be found' % class_str)
|
2010-06-10 18:39:07 +01:00
|
|
|
|
2010-08-16 14:16:21 +02:00
|
|
|
|
2010-05-27 23:05:26 -07:00
|
|
|
def fetchfile(url, target):
|
|
|
|
logging.debug("Fetching %s" % url)
|
|
|
|
# c = pycurl.Curl()
|
|
|
|
# fp = open(target, "wb")
|
|
|
|
# c.setopt(c.URL, url)
|
|
|
|
# c.setopt(c.WRITEDATA, fp)
|
|
|
|
# c.perform()
|
|
|
|
# c.close()
|
|
|
|
# fp.close()
|
2010-07-29 14:48:10 -07:00
|
|
|
execute("curl --fail %s -o %s" % (url, target))
|
2010-05-27 23:05:26 -07:00
|
|
|
|
2010-08-08 12:57:33 -07:00
|
|
|
def execute(cmd, process_input=None, addl_env=None, check_exit_code=True):
|
2010-07-07 12:06:34 -07:00
|
|
|
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,
|
2010-07-07 12:06:34 -07:00
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
|
2010-05-27 23:05:26 -07:00
|
|
|
result = None
|
2010-08-08 12:57:33 -07:00
|
|
|
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))
|
2010-07-22 12:28:47 -07:00
|
|
|
if check_exit_code and obj.returncode <> 0:
|
2010-08-20 13:26:24 +01:00
|
|
|
(stdout, stderr) = result
|
|
|
|
raise ProcessExecutionError(exit_code=obj.returncode,
|
|
|
|
stdout=stdout,
|
|
|
|
stderr=stderr,
|
|
|
|
cmd=cmd)
|
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):
|
|
|
|
sys.argv = sys.argv[:1] + ['--flagfile=%s' % filename] + sys.argv[1:]
|
|
|
|
|
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
|
|
|
|
2010-07-22 12:28:47 -07:00
|
|
|
def runthis(prompt, cmd, check_exit_code = True):
|
2010-05-27 23:05:26 -07:00
|
|
|
logging.debug("Running %s" % (cmd))
|
2010-07-22 12:28:47 -07:00
|
|
|
exit_code = subprocess.call(cmd.split(" "))
|
|
|
|
logging.debug(prompt % (exit_code))
|
|
|
|
if check_exit_code and exit_code <> 0:
|
2010-08-08 12:57:33 -07:00
|
|
|
raise Exception( "Unexpected exit code: %s from cmd: %s"
|
|
|
|
% (exit_code, cmd))
|
2010-05-27 23:05:26 -07:00
|
|
|
|
|
|
|
|
|
|
|
def generate_uid(topic, size=8):
|
|
|
|
return '%s-%s' % (topic, ''.join([random.choice('01234567890abcdefghijklmnopqrstuvwxyz') for x in xrange(size)]))
|
|
|
|
|
2010-06-10 18:39:07 +01:00
|
|
|
|
2010-05-27 23:05:26 -07:00
|
|
|
def generate_mac():
|
2010-07-20 20:28:34 -05:00
|
|
|
mac = [0x02, 0x16, 0x3e, random.randint(0x00, 0x7f),
|
2010-05-27 23:05:26 -07:00
|
|
|
random.randint(0x00, 0xff), random.randint(0x00, 0xff)
|
|
|
|
]
|
|
|
|
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():
|
|
|
|
''' returns the actual ip of the local machine.
|
|
|
|
'''
|
2010-06-11 10:08:13 +01:00
|
|
|
if getattr(FLAGS, 'fake_tests', None):
|
|
|
|
return '127.0.0.1'
|
2010-08-03 11:02:58 -07:00
|
|
|
try:
|
|
|
|
csock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
csock.connect(('www.google.com', 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"
|
2010-07-08 10:53:27 -07:00
|
|
|
|
|
|
|
|
|
|
|
def isotime(at=None):
|
|
|
|
if not at:
|
2010-08-16 14:16:21 +02:00
|
|
|
at = datetime.datetime.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):
|
2010-08-16 14:16:21 +02:00
|
|
|
return datetime.datetime.strptime(timestr, TIME_FORMAT)
|