From 48ee64f4ff8b8b290d467cb93a5cba5da62e8e4d Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Tue, 27 Mar 2012 13:33:51 -0700 Subject: [PATCH] More cleanup and fixing nova + quantum using options instead of instances --- conf/templates/nova/nova-network-init.sh | 2 +- devstack/components/keystone.py | 13 +------- devstack/components/nova.py | 14 ++------- devstack/importer.py | 13 ++++++++ devstack/libvirt.py | 11 ++----- devstack/utils.py | 39 +++++++++++------------- 6 files changed, 38 insertions(+), 54 deletions(-) diff --git a/conf/templates/nova/nova-network-init.sh b/conf/templates/nova/nova-network-init.sh index a9e67a11..c7999317 100644 --- a/conf/templates/nova/nova-network-init.sh +++ b/conf/templates/nova/nova-network-init.sh @@ -6,7 +6,7 @@ set -o errexit # Create a small network nova-manage --flagfile %CFG_FILE% network create private %FIXED_RANGE% 1 %FIXED_NETWORK_SIZE% -if [[ "$ENABLED_SERVICES" =~ "quantum-server" ]]; then +if [[ "$ENABLED_SERVICES" =~ "quantum" ]]; then echo "Not creating floating IPs (not supported by quantum server)" else # Create some floating ips diff --git a/devstack/components/keystone.py b/devstack/components/keystone.py index b97a56ce..9ab9cb2a 100644 --- a/devstack/components/keystone.py +++ b/devstack/components/keystone.py @@ -20,7 +20,6 @@ from urlparse import urlunparse from devstack import cfg from devstack import component as comp -from devstack import date from devstack import log as logging from devstack import shell as sh from devstack import utils @@ -222,17 +221,7 @@ class KeystoneRuntime(comp.PythonRuntime): setup_cmd = MANAGE_CMD_ROOT + [tgt_fn] LOG.info("Running %r command to initialize keystone." % (" ".join(setup_cmd))) sh.execute(*setup_cmd, env_overrides=env, run_as_root=False) - self._toggle_key_init(tgt_fn, env) - - def _toggle_key_init(self, src_fn, env): - add_lines = list() - add_lines.append('') - add_lines.append('# Ran on %s by %s' % (date.rcf8222date(), sh.getuser())) - add_lines.append('# With environment:') - for (k, v) in env.items(): - add_lines.append('# %s => %s' % (k, v)) - sh.append_file(src_fn, utils.joinlinesep(*add_lines)) - sh.chmod(src_fn, 0644) + utils.mark_unexecute_file(tgt_fn, env) def _get_apps_to_start(self): apps = list() diff --git a/devstack/components/nova.py b/devstack/components/nova.py index 6095e5dd..0a4e475f 100644 --- a/devstack/components/nova.py +++ b/devstack/components/nova.py @@ -363,16 +363,6 @@ class NovaRuntime(NovaMixin, comp.PythonRuntime): self.wait_time = max(self.cfg.getint('default', 'service_wait_seconds'), 1) self.virsh = lv.Virsh(self.cfg, self.distro) - def _toggle_network_init(self, src_fn, env): - add_lines = list() - add_lines.append('') - add_lines.append('# Ran on %s by %s' % (date.rcf8222date(), sh.getuser())) - add_lines.append('# With environment:') - for k, v in env.items(): - add_lines.append('# %s => %s' % (k, v)) - sh.append_file(src_fn, utils.joinlinesep(*add_lines)) - sh.chmod(src_fn, 0644) - def _setup_network_init(self): tgt_fn = sh.joinpths(self.bin_dir, NET_INIT_CONF) if sh.is_executable(tgt_fn): @@ -384,11 +374,11 @@ class NovaRuntime(NovaMixin, comp.PythonRuntime): LOG.info("Waiting %s seconds so that quantum can start up before running first time init." % (self.wait_time)) sh.sleep(self.wait_time) env = dict() - env['ENABLED_SERVICES'] = ",".join(self.instances.keys()) + env['ENABLED_SERVICES'] = ",".join(self.options) setup_cmd = NET_INIT_CMD_ROOT + [tgt_fn] LOG.info("Running %r command to initialize nova's network." % (" ".join(setup_cmd))) sh.execute(*setup_cmd, env_overrides=env, run_as_root=False) - self._toggle_network_init(tgt_fn, env) + utils.mark_unexecute_file(tgt_fn, env) def post_start(self): self._setup_network_init() diff --git a/devstack/importer.py b/devstack/importer.py index fb6545ac..e59e63ab 100644 --- a/devstack/importer.py +++ b/devstack/importer.py @@ -15,6 +15,8 @@ # License for the specific language governing permissions and limitations # under the License. +import sys + def partition(fullname): """ @@ -40,3 +42,14 @@ def import_entry_point(fullname): raise RuntimeError('Could not load entry point %s: %s' % (fullname, err)) return cls + + +def import_module(module_name, quiet=True): + try: + __import__(module_name) + return sys.modules.get(module_name, None) + except ImportError: + if quiet: + return None + else: + raise diff --git a/devstack/libvirt.py b/devstack/libvirt.py index 2f7361e1..7ee3ccbd 100644 --- a/devstack/libvirt.py +++ b/devstack/libvirt.py @@ -17,6 +17,7 @@ import contextlib from devstack import exceptions as excp +from devstack import importer from devstack import log as logging from devstack import shell as sh from devstack import utils @@ -50,12 +51,6 @@ def canon_libvirt_type(virt_type): return virt_type -def _get_virt_lib(): - # Late import so that we don't always need this library to be active - # ie if u aren't using libvirt in the first place... - return utils.import_module('libvirt') - - class Virsh(object): def __init__(self, config, distro): @@ -73,7 +68,7 @@ class Virsh(object): return _DEAD def _destroy_domain(self, conn, dom_name): - libvirt = _get_virt_lib() + libvirt = importer.import_module('libvirt') try: dom = conn.lookupByName(dom_name) LOG.debug("Destroying domain (%r) (id=%s) running %r" % (dom_name, dom.ID(), dom.OSType())) @@ -103,7 +98,7 @@ class Virsh(object): utils.execute_template(*cmds, params=mp) def clear_domains(self, virt_type, inst_prefix): - libvirt = _get_virt_lib() + libvirt = importer.import_module('libvirt') if not libvirt: LOG.warn("Could not clear out libvirt domains, libvirt not available for python.") return diff --git a/devstack/utils.py b/devstack/utils.py index 0a286f19..4fea7978 100644 --- a/devstack/utils.py +++ b/devstack/utils.py @@ -100,9 +100,8 @@ def configure_logging(verbosity_level=1, dry_run=False): def load_template(component, template_name): - full_pth = sh.joinpths(settings.STACK_TEMPLATE_DIR, component, template_name) - contents = sh.load_file(full_pth) - return (full_pth, contents) + templ_pth = sh.joinpths(settings.STACK_TEMPLATE_DIR, component, template_name) + return (templ_pth, sh.load_file(templ_pth)) def execute_template(*cmds, **kargs): @@ -143,6 +142,17 @@ def to_bytes(text): return byte_val +def mark_unexecute_file(fn, kvs, comment_start='#'): + add_lines = list() + add_lines.append('') + add_lines.append(comment_start + ' Ran on %s by %s' % (date.rcf8222date(), sh.getuser())) + add_lines.append(comment_start + ' With data:') + for (k, v) in kvs.items(): + add_lines.append(comment_start + ' %s => %s' % (k, v)) + sh.append_file(fn, joinlinesep(*add_lines)) + sh.chmod(fn, 0644) + + @contextlib.contextmanager def progress_bar(name, max_am, reverse=False): widgets = list() @@ -167,6 +177,7 @@ def progress_bar(name, max_am, reverse=False): def tempdir(): # This seems like it was only added in python 3.2 # Make it since its useful... + # See: http://bugs.python.org/file12970/tempdir.patch tdir = tempfile.mkdtemp() try: yield tdir @@ -174,17 +185,6 @@ def tempdir(): sh.deldir(tdir) -def import_module(module_name, quiet=True): - try: - __import__(module_name) - return sys.modules.get(module_name, None) - except ImportError: - if quiet: - return None - else: - raise - - def versionize(input_version): segments = input_version.split(".") cleaned_segments = list() @@ -262,9 +262,7 @@ def get_host_ip(): def is_interface(intfc): - if intfc in get_interfaces(): - return True - return False + return intfc in get_interfaces() def get_interfaces(): @@ -273,11 +271,11 @@ def get_interfaces(): interface_info = dict() interface_addresses = netifaces.ifaddresses(intfc) ip6 = interface_addresses.get(netifaces.AF_INET6) - if ip6 and len(ip6): + if ip6: # Just take the first interface_info[settings.IPV6] = ip6[0] ip4 = interface_addresses.get(netifaces.AF_INET) - if ip4 and len(ip4): + if ip4: # Just take the first interface_info[settings.IPV4] = ip4[0] # Note: there are others but this is good for now.. @@ -418,8 +416,7 @@ ____ ___ ____ _ _ ____ ___ ____ ____ _ _ def center_text(text, fill, max_len): - centered_str = '{0:{fill}{align}{size}}'.format(text, fill=fill, align="^", size=max_len) - return centered_str + return '{0:{fill}{align}{size}}'.format(text, fill=fill, align="^", size=max_len) def _welcome_slang():