summaryrefslogtreecommitdiffstats
path: root/nova/cmd
diff options
context:
space:
mode:
authorMonty Taylor <mordred@inaugust.com>2012-08-15 15:02:51 -0400
committerMichael Still <mikal@stillhq.com>2013-04-04 13:14:27 +1100
commit799a925c1f4a388c30a7f0d9e1ffe1b82e8ced9d (patch)
tree596812f9f17ca76741cfec8ca0899a99bd75a7b2 /nova/cmd
parenta01f907cecc24c18cbe3d32921247125294dd2b9 (diff)
downloadnova-799a925c1f4a388c30a7f0d9e1ffe1b82e8ced9d.tar.gz
nova-799a925c1f4a388c30a7f0d9e1ffe1b82e8ced9d.tar.xz
nova-799a925c1f4a388c30a7f0d9e1ffe1b82e8ced9d.zip
Move console scripts to entrypoints.
As part of the move of plugins to entrypoints, take advantage of the entrypoints based console scripts, which will make our command line scripts available for unittesting. Part of blueprint entrypoints-plugins Co-authored-by: Michael Still <mikal@stillhq.com> Change-Id: I5f17348b7b3cc896c92263dd518abb128757d81f
Diffstat (limited to 'nova/cmd')
-rw-r--r--nova/cmd/__init__.py20
-rw-r--r--nova/cmd/all.py91
-rw-r--r--nova/cmd/api.py54
-rw-r--r--nova/cmd/api_ec2.py36
-rw-r--r--nova/cmd/api_metadata.py36
-rw-r--r--nova/cmd/api_os_compute.py36
-rw-r--r--nova/cmd/baremetal_deploy_helper.py320
-rw-r--r--nova/cmd/baremetal_manage.py208
-rw-r--r--nova/cmd/cells.py43
-rw-r--r--nova/cmd/cert.py39
-rw-r--r--nova/cmd/clear_rabbit_queues.py71
-rw-r--r--nova/cmd/compute.py65
-rw-r--r--nova/cmd/conductor.py41
-rw-r--r--nova/cmd/console.py39
-rw-r--r--nova/cmd/consoleauth.py38
-rw-r--r--nova/cmd/dhcpbridge.py138
-rw-r--r--nova/cmd/manage.py1244
-rw-r--r--nova/cmd/network.py42
-rw-r--r--nova/cmd/novncproxy.py97
-rw-r--r--nova/cmd/objectstore.py37
-rw-r--r--nova/cmd/rootwrap.py128
-rw-r--r--nova/cmd/rpc_zmq_receiver.py38
-rw-r--r--nova/cmd/scheduler.py45
-rw-r--r--nova/cmd/spicehtml5proxy.py97
-rw-r--r--nova/cmd/xvpvncproxy.py35
25 files changed, 3038 insertions, 0 deletions
diff --git a/nova/cmd/__init__.py b/nova/cmd/__init__.py
new file mode 100644
index 000000000..cc641efc0
--- /dev/null
+++ b/nova/cmd/__init__.py
@@ -0,0 +1,20 @@
+# Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
+# 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
+#
+# Unless required by applicable law or agreed to in writing, software
+# 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.
+
+import eventlet
+eventlet.monkey_patch(os=False)
+
+import gettext
+gettext.install('nova', unicode=1)
diff --git a/nova/cmd/all.py b/nova/cmd/all.py
new file mode 100644
index 000000000..f7e76fe30
--- /dev/null
+++ b/nova/cmd/all.py
@@ -0,0 +1,91 @@
+#!/usr/bin/env python
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2011 OpenStack Foundation
+# Copyright 2010 United States Government as represented by the
+# Administrator of the National Aeronautics and Space Administration.
+# 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
+#
+# Unless required by applicable law or agreed to in writing, software
+# 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.
+
+"""Starter script for all nova services.
+
+This script attempts to start all the nova services in one process. Each
+service is started in its own greenthread. Please note that exceptions and
+sys.exit() on the starting of a service are logged and the script will
+continue attempting to launch the rest of the services.
+
+"""
+
+import sys
+
+from oslo.config import cfg
+
+from nova import config
+from nova.objectstore import s3server
+from nova.openstack.common import log as logging
+from nova import service
+from nova import utils
+from nova.vnc import xvp_proxy
+
+
+CONF = cfg.CONF
+CONF.import_opt('manager', 'nova.conductor.api', group='conductor')
+CONF.import_opt('topic', 'nova.conductor.api', group='conductor')
+CONF.import_opt('enabled_apis', 'nova.service')
+LOG = logging.getLogger('nova.all')
+
+
+def main():
+ config.parse_args(sys.argv)
+ logging.setup("nova")
+ utils.monkey_patch()
+ launcher = service.ProcessLauncher()
+
+ # nova-api
+ for api in CONF.enabled_apis:
+ try:
+ server = service.WSGIService(api)
+ launcher.launch_server(server, workers=server.workers or 1)
+ except (Exception, SystemExit):
+ LOG.exception(_('Failed to load %s') % '%s-api' % api)
+
+ for mod in [s3server, xvp_proxy]:
+ try:
+ launcher.launch_server(mod.get_wsgi_server())
+ except (Exception, SystemExit):
+ LOG.exception(_('Failed to load %s') % mod.__name__)
+
+ for binary in ['nova-compute', 'nova-network', 'nova-scheduler',
+ 'nova-cert', 'nova-conductor']:
+
+ # FIXME(sirp): Most service configs are defined in nova/service.py, but
+ # conductor has set a new precedent of storing these configs
+ # nova/<service>/api.py.
+ #
+ # We should update the existing services to use this new approach so we
+ # don't have to treat conductor differently here.
+ if binary == 'nova-conductor':
+ topic = CONF.conductor.topic
+ manager = CONF.conductor.manager
+ else:
+ topic = None
+ manager = None
+
+ try:
+ launcher.launch_server(service.Service.create(binary=binary,
+ topic=topic,
+ manager=manager))
+ except (Exception, SystemExit):
+ LOG.exception(_('Failed to load %s'), binary)
+ launcher.wait()
diff --git a/nova/cmd/api.py b/nova/cmd/api.py
new file mode 100644
index 000000000..811171afc
--- /dev/null
+++ b/nova/cmd/api.py
@@ -0,0 +1,54 @@
+#!/usr/bin/env python
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2010 United States Government as represented by the
+# Administrator of the National Aeronautics and Space Administration.
+# 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
+#
+# Unless required by applicable law or agreed to in writing, software
+# 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.
+
+"""Starter script for Nova API.
+
+Starts both the EC2 and OpenStack APIs in separate greenthreads.
+
+"""
+
+import sys
+
+from oslo.config import cfg
+
+from nova import config
+from nova.openstack.common import log as logging
+from nova import service
+from nova import utils
+
+CONF = cfg.CONF
+CONF.import_opt('enabled_apis', 'nova.service')
+CONF.import_opt('enabled_ssl_apis', 'nova.service')
+
+
+def main():
+ config.parse_args(sys.argv)
+ logging.setup("nova")
+ utils.monkey_patch()
+
+ launcher = service.ProcessLauncher()
+ for api in CONF.enabled_apis:
+ should_use_ssl = api in CONF.enabled_ssl_apis
+ if api == 'ec2':
+ server = service.WSGIService(api, use_ssl=should_use_ssl,
+ max_url_len=16384)
+ else:
+ server = service.WSGIService(api, use_ssl=should_use_ssl)
+ launcher.launch_server(server, workers=server.workers or 1)
+ launcher.wait()
diff --git a/nova/cmd/api_ec2.py b/nova/cmd/api_ec2.py
new file mode 100644
index 000000000..2d78c58e5
--- /dev/null
+++ b/nova/cmd/api_ec2.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2010 United States Government as represented by the
+# Administrator of the National Aeronautics and Space Administration.
+# 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
+#
+# Unless required by applicable law or agreed to in writing, software
+# 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.
+
+"""Starter script for Nova EC2 API."""
+
+import sys
+
+from nova import config
+from nova.openstack.common import log as logging
+from nova import service
+from nova import utils
+
+
+def main():
+ config.parse_args(sys.argv)
+ logging.setup("nova")
+ utils.monkey_patch()
+ server = service.WSGIService('ec2', max_url_len=16384)
+ service.serve(server, workers=server.workers)
+ service.wait()
diff --git a/nova/cmd/api_metadata.py b/nova/cmd/api_metadata.py
new file mode 100644
index 000000000..b2acee33c
--- /dev/null
+++ b/nova/cmd/api_metadata.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2010 United States Government as represented by the
+# Administrator of the National Aeronautics and Space Administration.
+# 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
+#
+# Unless required by applicable law or agreed to in writing, software
+# 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.
+
+"""Starter script for Nova Metadata API."""
+
+import sys
+
+from nova import config
+from nova.openstack.common import log as logging
+from nova import service
+from nova import utils
+
+
+def main():
+ config.parse_args(sys.argv)
+ logging.setup("nova")
+ utils.monkey_patch()
+ server = service.WSGIService('metadata')
+ service.serve(server, workers=server.workers)
+ service.wait()
diff --git a/nova/cmd/api_os_compute.py b/nova/cmd/api_os_compute.py
new file mode 100644
index 000000000..c4cb7982e
--- /dev/null
+++ b/nova/cmd/api_os_compute.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2010 United States Government as represented by the
+# Administrator of the National Aeronautics and Space Administration.
+# 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
+#
+# Unless required by applicable law or agreed to in writing, software
+# 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.
+
+"""Starter script for Nova OS API."""
+
+import sys
+
+from nova import config
+from nova.openstack.common import log as logging
+from nova import service
+from nova import utils
+
+
+def main():
+ config.parse_args(sys.argv)
+ logging.setup("nova")
+ utils.monkey_patch()
+ server = service.WSGIService('osapi_compute')
+ service.serve(server, workers=server.workers)
+ service.wait()
diff --git a/nova/cmd/baremetal_deploy_helper.py b/nova/cmd/baremetal_deploy_helper.py
new file mode 100644
index 000000000..2a5252694
--- /dev/null
+++ b/nova/cmd/baremetal_deploy_helper.py
@@ -0,0 +1,320 @@
+#!/usr/bin/env python
+
+# Copyright (c) 2012 NTT DOCOMO, INC.
+# 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
+#
+# Unless required by applicable law or agreed to in writing, software
+# 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.
+
+"""Starter script for Bare-Metal Deployment Service."""
+
+
+import os
+import sys
+import threading
+import time
+
+import cgi
+import Queue
+import re
+import socket
+import stat
+from wsgiref import simple_server
+
+from nova import config
+from nova import context as nova_context
+from nova import exception
+from nova.openstack.common import log as logging
+from nova import utils
+from nova.virt.baremetal import baremetal_states
+from nova.virt.baremetal import db
+
+
+LOG = logging.getLogger('nova.virt.baremetal.deploy_helper')
+
+QUEUE = Queue.Queue()
+
+
+# All functions are called from deploy() directly or indirectly.
+# They are split for stub-out.
+
+def discovery(portal_address, portal_port):
+ """Do iSCSI discovery on portal."""
+ utils.execute('iscsiadm',
+ '-m', 'discovery',
+ '-t', 'st',
+ '-p', '%s:%s' % (portal_address, portal_port),
+ run_as_root=True,
+ check_exit_code=[0])
+
+
+def login_iscsi(portal_address, portal_port, target_iqn):
+ """Login to an iSCSI target."""
+ utils.execute('iscsiadm',
+ '-m', 'node',
+ '-p', '%s:%s' % (portal_address, portal_port),
+ '-T', target_iqn,
+ '--login',
+ run_as_root=True,
+ check_exit_code=[0])
+ # Ensure the login complete
+ time.sleep(3)
+
+
+def logout_iscsi(portal_address, portal_port, target_iqn):
+ """Logout from an iSCSI target."""
+ utils.execute('iscsiadm',
+ '-m', 'node',
+ '-p', '%s:%s' % (portal_address, portal_port),
+ '-T', target_iqn,
+ '--logout',
+ run_as_root=True,
+ check_exit_code=[0])
+
+
+def make_partitions(dev, root_mb, swap_mb):
+ """Create partitions for root and swap on a disk device."""
+ # Lead in with 1MB to allow room for the partition table itself, otherwise
+ # the way sfdisk adjusts doesn't shift the partition up to compensate, and
+ # we lose the space.
+ # http://bazaar.launchpad.net/~ubuntu-branches/ubuntu/raring/util-linux/
+ # raring/view/head:/fdisk/sfdisk.c#L1940
+ stdin_command = ('1 %d 83;\n- %d 82;\n0 0;\n0 0;\n' % (root_mb, swap_mb))
+ utils.execute('sfdisk', '-uM', dev, process_input=stdin_command,
+ run_as_root=True,
+ check_exit_code=[0])
+ # avoid "device is busy"
+ time.sleep(3)
+
+
+def is_block_device(dev):
+ """Check whether a device is block or not."""
+ s = os.stat(dev)
+ return stat.S_ISBLK(s.st_mode)
+
+
+def dd(src, dst):
+ """Execute dd from src to dst."""
+ utils.execute('dd',
+ 'if=%s' % src,
+ 'of=%s' % dst,
+ 'bs=1M',
+ 'oflag=direct',
+ run_as_root=True,
+ check_exit_code=[0])
+
+
+def mkswap(dev, label='swap1'):
+ """Execute mkswap on a device."""
+ utils.execute('mkswap',
+ '-L', label,
+ dev,
+ run_as_root=True,
+ check_exit_code=[0])
+
+
+def block_uuid(dev):
+ """Get UUID of a block device."""
+ out, _ = utils.execute('blkid', '-s', 'UUID', '-o', 'value', dev,
+ run_as_root=True,
+ check_exit_code=[0])
+ return out.strip()
+
+
+def switch_pxe_config(path, root_uuid):
+ """Switch a pxe config from deployment mode to service mode."""
+ with open(path) as f:
+ lines = f.readlines()
+ root = 'UUID=%s' % root_uuid
+ rre = re.compile(r'\$\{ROOT\}')
+ dre = re.compile('^default .*$')
+ with open(path, 'w') as f:
+ for line in lines:
+ line = rre.sub(root, line)
+ line = dre.sub('default boot', line)
+ f.write(line)
+
+
+def notify(address, port):
+ """Notify a node that it becomes ready to reboot."""
+ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ try:
+ s.connect((address, port))
+ s.send('done')
+ finally:
+ s.close()
+
+
+def get_dev(address, port, iqn, lun):
+ """Returns a device path for given parameters."""
+ dev = "/dev/disk/by-path/ip-%s:%s-iscsi-%s-lun-%s" \
+ % (address, port, iqn, lun)
+ return dev
+
+
+def get_image_mb(image_path):
+ """Get size of an image in Megabyte."""
+ mb = 1024 * 1024
+ image_byte = os.path.getsize(image_path)
+ # round up size to MB
+ image_mb = int((image_byte + mb - 1) / mb)
+ return image_mb
+
+
+def work_on_disk(dev, root_mb, swap_mb, image_path):
+ """Creates partitions and write an image to the root partition."""
+ root_part = "%s-part1" % dev
+ swap_part = "%s-part2" % dev
+
+ if not is_block_device(dev):
+ LOG.warn("parent device '%s' not found", dev)
+ return
+ make_partitions(dev, root_mb, swap_mb)
+ if not is_block_device(root_part):
+ LOG.warn("root device '%s' not found", root_part)
+ return
+ if not is_block_device(swap_part):
+ LOG.warn("swap device '%s' not found", swap_part)
+ return
+ dd(image_path, root_part)
+ mkswap(swap_part)
+ root_uuid = block_uuid(root_part)
+ return root_uuid
+
+
+def deploy(address, port, iqn, lun, image_path, pxe_config_path,
+ root_mb, swap_mb):
+ """All-in-one function to deploy a node."""
+ dev = get_dev(address, port, iqn, lun)
+ image_mb = get_image_mb(image_path)
+ if image_mb > root_mb:
+ root_mb = image_mb
+ discovery(address, port)
+ login_iscsi(address, port, iqn)
+ try:
+ root_uuid = work_on_disk(dev, root_mb, swap_mb, image_path)
+ except exception.ProcessExecutionError, err:
+ # Log output if there was a error
+ LOG.error("Cmd : %s" % err.cmd)
+ LOG.error("StdOut : %s" % err.stdout)
+ LOG.error("StdErr : %s" % err.stderr)
+ finally:
+ logout_iscsi(address, port, iqn)
+ switch_pxe_config(pxe_config_path, root_uuid)
+ # Ensure the node started netcat on the port after POST the request.
+ time.sleep(3)
+ notify(address, 10000)
+
+
+class Worker(threading.Thread):
+ """Thread that handles requests in queue."""
+
+ def __init__(self):
+ super(Worker, self).__init__()
+ self.setDaemon(True)
+ self.stop = False
+ self.queue_timeout = 1
+
+ def run(self):
+ while not self.stop:
+ try:
+ # Set timeout to check self.stop periodically
+ (node_id, params) = QUEUE.get(block=True,
+ timeout=self.queue_timeout)
+ except Queue.Empty:
+ pass
+ else:
+ # Requests comes here from BareMetalDeploy.post()
+ LOG.info(_('start deployment for node %(node_id)s, '
+ 'params %(params)s') % locals())
+ context = nova_context.get_admin_context()
+ try:
+ db.bm_node_update(context, node_id,
+ {'task_state': baremetal_states.DEPLOYING})
+ deploy(**params)
+ except Exception:
+ LOG.error(_('deployment to node %s failed') % node_id)
+ db.bm_node_update(context, node_id,
+ {'task_state': baremetal_states.DEPLOYFAIL})
+ else:
+ LOG.info(_('deployment to node %s done') % node_id)
+ db.bm_node_update(context, node_id,
+ {'task_state': baremetal_states.DEPLOYDONE})
+
+
+class BareMetalDeploy(object):
+ """WSGI server for bare-metal deployment."""
+
+ def __init__(self):
+ self.worker = Worker()
+ self.worker.start()
+
+ def __call__(self, environ, start_response):
+ method = environ['REQUEST_METHOD']
+ if method == 'POST':
+ return self.post(environ, start_response)
+ else:
+ start_response('501 Not Implemented',
+ [('Content-type', 'text/plain')])
+ return 'Not Implemented'
+
+ def post(self, environ, start_response):
+ LOG.info("post: environ=%s", environ)
+ inpt = environ['wsgi.input']
+ length = int(environ.get('CONTENT_LENGTH', 0))
+
+ x = inpt.read(length)
+ q = dict(cgi.parse_qsl(x))
+ try:
+ node_id = q['i']
+ deploy_key = q['k']
+ address = q['a']
+ port = q.get('p', '3260')
+ iqn = q['n']
+ lun = q.get('l', '1')
+ except KeyError as e:
+ start_response('400 Bad Request', [('Content-type', 'text/plain')])
+ return "parameter '%s' is not defined" % e
+
+ context = nova_context.get_admin_context()
+ d = db.bm_node_get(context, node_id)
+
+ if d['deploy_key'] != deploy_key:
+ start_response('400 Bad Request', [('Content-type', 'text/plain')])
+ return 'key is not match'
+
+ params = {'address': address,
+ 'port': port,
+ 'iqn': iqn,
+ 'lun': lun,
+ 'image_path': d['image_path'],
+ 'pxe_config_path': d['pxe_config_path'],
+ 'root_mb': int(d['root_mb']),
+ 'swap_mb': int(d['swap_mb']),
+ }
+ # Restart worker, if needed
+ if not self.worker.isAlive():
+ self.worker = Worker()
+ self.worker.start()
+ LOG.info("request is queued: node %s, params %s", node_id, params)
+ QUEUE.put((node_id, params))
+ # Requests go to Worker.run()
+ start_response('200 OK', [('Content-type', 'text/plain')])
+ return ''
+
+
+def main():
+ config.parse_args(sys.argv)
+ logging.setup("nova")
+ app = BareMetalDeploy()
+ srv = simple_server.make_server('', 10000, app)
+ srv.serve_forever()
diff --git a/nova/cmd/baremetal_manage.py b/nova/cmd/baremetal_manage.py
new file mode 100644
index 000000000..881f2f60c
--- /dev/null
+++ b/nova/cmd/baremetal_manage.py
@@ -0,0 +1,208 @@
+#!/usr/bin/env python
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright (c) 2011 X.commerce, a business unit of eBay Inc.
+# Copyright 2010 United States Government as represented by the
+# Administrator of the National Aeronautics and Space Administration.
+# 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
+#
+# Unless required by applicable law or agreed to in writing, software
+# 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.
+
+# Interactive shell based on Django:
+#
+# Copyright (c) 2005, the Lawrence Journal-World
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# 1. Redistributions of source code must retain the above copyright notice,
+# this list of conditions and the following disclaimer.
+#
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# 3. Neither the name of Django nor the names of its contributors may be
+# used to endorse or promote products derived from this software without
+# specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+
+"""
+ CLI interface for nova bare-metal management.
+"""
+
+import gettext
+import os
+import sys
+
+from oslo.config import cfg
+
+gettext.install('nova', unicode=1)
+
+from nova import config
+from nova.openstack.common import cliutils
+from nova.openstack.common import log as logging
+from nova import version
+from nova.virt.baremetal.db import migration as bmdb_migration
+
+CONF = cfg.CONF
+
+
+# Decorators for actions
+def args(*args, **kwargs):
+ def _decorator(func):
+ func.__dict__.setdefault('args', []).insert(0, (args, kwargs))
+ return func
+ return _decorator
+
+
+class BareMetalDbCommands(object):
+ """Class for managing the bare-metal database."""
+
+ def __init__(self):
+ pass
+
+ @args('--version', dest='version', metavar='<version>',
+ help='Bare-metal Database version')
+ def sync(self, version=None):
+ """Sync the database up to the most recent version."""
+ bmdb_migration.db_sync(version)
+
+ def version(self):
+ """Print the current database version."""
+ v = bmdb_migration.db_version()
+ print(v)
+ # return for unittest
+ return v
+
+
+CATEGORIES = {
+ 'db': BareMetalDbCommands,
+}
+
+
+def methods_of(obj):
+ """Get all callable methods of an object that don't start with underscore
+ returns a list of tuples of the form (method_name, method)"""
+ result = []
+ for i in dir(obj):
+ if callable(getattr(obj, i)) and not i.startswith('_'):
+ result.append((i, getattr(obj, i)))
+ return result
+
+
+def add_command_parsers(subparsers):
+ parser = subparsers.add_parser('bash-completion')
+ parser.add_argument('query_category', nargs='?')
+
+ for category in CATEGORIES:
+ command_object = CATEGORIES[category]()
+
+ parser = subparsers.add_parser(category)
+ parser.set_defaults(command_object=command_object)
+
+ category_subparsers = parser.add_subparsers(dest='action')
+
+ for (action, action_fn) in methods_of(command_object):
+ parser = category_subparsers.add_parser(action)
+
+ action_kwargs = []
+ for args, kwargs in getattr(action_fn, 'args', []):
+ action_kwargs.append(kwargs['dest'])
+ kwargs['dest'] = 'action_kwarg_' + kwargs['dest']
+ parser.add_argument(*args, **kwargs)
+
+ parser.set_defaults(action_fn=action_fn)
+ parser.set_defaults(action_kwargs=action_kwargs)
+
+ parser.add_argument('action_args', nargs='*')
+
+
+category_opt = cfg.SubCommandOpt('category',
+ title='Command categories',
+ help='Available categories',
+ handler=add_command_parsers)
+
+
+def main():
+ """Parse options and call the appropriate class/method."""
+ CONF.register_cli_opt(category_opt)
+ try:
+ config.parse_args(sys.argv)
+ logging.setup("nova")
+ except cfg.ConfigFilesNotFoundError:
+ cfgfile = CONF.config_file[-1] if CONF.config_file else None
+ if cfgfile and not os.access(cfgfile, os.R_OK):
+ st = os.stat(cfgfile)
+ print(_("Could not read %s. Re-running with sudo") % cfgfile)
+ try:
+ os.execvp('sudo', ['sudo', '-u', '#%s' % st.st_uid] + sys.argv)
+ except Exception:
+ print(_('sudo failed, continuing as if nothing happened'))
+
+ print(_('Please re-run nova-manage as root.'))
+ return(2)
+
+ if CONF.category.name == "version":
+ print(version.version_string_with_package())
+ return(0)
+
+ if CONF.category.name == "bash-completion":
+ if not CONF.category.query_category:
+ print(" ".join(CATEGORIES.keys()))
+ elif CONF.category.query_category in CATEGORIES:
+ fn = CATEGORIES[CONF.category.query_category]
+ command_object = fn()
+ actions = methods_of(command_object)
+ print(" ".join([k for (k, v) in actions]))
+ return(0)
+
+ fn = CONF.category.action_fn
+ fn_args = [arg.decode('utf-8') for arg in CONF.category.action_args]
+ fn_kwargs = {}
+ for k in CONF.category.action_kwargs:
+ v = getattr(CONF.category, 'action_kwarg_' + k)
+ if v is None:
+ continue
+ if isinstance(v, basestring):
+ v = v.decode('utf-8')
+ fn_kwargs[k] = v
+
+ # call the action with the remaining arguments
+ # check arguments
+ try:
+ cliutils.validate_args(fn, *fn_args, **fn_kwargs)
+ except cliutils.MissingArgs as e:
+ print(fn.__doc__)
+ parser.print_help()
+ print(e)
+ return(1)
+ try:
+ fn(*fn_args, **fn_kwargs)
+ return(0)
+ except Exception:
+ print(_("Command failed, please check log for more info"))
+ raise
diff --git a/nova/cmd/cells.py b/nova/cmd/cells.py
new file mode 100644
index 000000000..9e6fae402
--- /dev/null
+++ b/nova/cmd/cells.py
@@ -0,0 +1,43 @@
+#!/usr/bin/env python
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+#
+# Copyright (c) 2012 Rackspace Hosting
+# 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
+#
+# Unless required by applicable law or agreed to in writing, software
+# 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.
+
+"""Starter script for Nova Cells Service."""
+
+import sys
+
+from oslo.config import cfg
+
+from nova import config
+from nova.openstack.common import log as logging
+from nova import service
+from nova import utils
+
+CONF = cfg.CONF
+CONF.import_opt('topic', 'nova.cells.opts', group='cells')
+CONF.import_opt('manager', 'nova.cells.opts', group='cells')
+
+
+def main():
+ config.parse_args(sys.argv)
+ logging.setup('nova')
+ utils.monkey_patch()
+ server = service.Service.create(binary='nova-cells',
+ topic=CONF.cells.topic,
+ manager=CONF.cells.manager)
+ service.serve(server)
+ service.wait()
diff --git a/nova/cmd/cert.py b/nova/cmd/cert.py
new file mode 100644
index 000000000..668d8b0a4
--- /dev/null
+++ b/nova/cmd/cert.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2012 OpenStack Foundation
+#
+# 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
+#
+# Unless required by applicable law or agreed to in writing, software
+# 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.
+
+"""Starter script for Nova Cert."""
+
+import sys
+
+from oslo.config import cfg
+
+from nova import config
+from nova.openstack.common import log as logging
+from nova import service
+from nova import utils
+
+CONF = cfg.CONF
+CONF.import_opt('cert_topic', 'nova.cert.rpcapi')
+
+
+def main():
+ config.parse_args(sys.argv)
+ logging.setup("nova")
+ utils.monkey_patch()
+ server = service.Service.create(binary='nova-cert', topic=CONF.cert_topic)
+ service.serve(server)
+ service.wait()
diff --git a/nova/cmd/clear_rabbit_queues.py b/nova/cmd/clear_rabbit_queues.py
new file mode 100644
index 000000000..b1ec0b92e
--- /dev/null
+++ b/nova/cmd/clear_rabbit_queues.py
@@ -0,0 +1,71 @@
+#!/usr/bin/env python
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright (c) 2011 OpenStack Foundation
+# 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
+#
+# Unless required by applicable law or agreed to in writing, software
+# 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.
+
+"""Admin/debug script to wipe rabbitMQ (AMQP) queues nova uses.
+ This can be used if you need to change durable options on queues,
+ or to wipe all messages in the queue system if things are in a
+ serious bad way.
+
+"""
+
+import gettext
+import sys
+
+from oslo.config import cfg
+
+gettext.install('nova', unicode=1)
+
+
+from nova import config
+from nova.openstack.common import log as logging
+from nova.openstack.common import rpc
+
+
+opts = [
+ cfg.MultiStrOpt('queues',
+ default=[],
+ positional=True,
+ help='Queues to delete'),
+ cfg.BoolOpt('delete_exchange',
+ default=False,
+ help='delete nova exchange too.'),
+]
+
+CONF = cfg.CONF
+CONF.register_cli_opts(opts)
+
+
+def delete_exchange(exch):
+ conn = rpc.create_connection()
+ x = conn.get_channel()
+ x.exchange_delete(exch)
+
+
+def delete_queues(queues):
+ conn = rpc.create_connection()
+ x = conn.get_channel()
+ for q in queues:
+ x.queue_delete(q)
+
+
+def main():
+ config.parse_args(sys.argv)
+ logging.setup("nova")
+ delete_queues(CONF.queues)
+ if CONF.delete_exchange:
+ delete_exchange(CONF.control_exchange)
diff --git a/nova/cmd/compute.py b/nova/cmd/compute.py
new file mode 100644
index 000000000..07caa17b1
--- /dev/null
+++ b/nova/cmd/compute.py
@@ -0,0 +1,65 @@
+#!/usr/bin/env python
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2010 United States Government as represented by the
+# Administrator of the National Aeronautics and Space Administration.
+# 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
+#
+# Unless required by applicable law or agreed to in writing, software
+# 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.
+
+"""Starter script for Nova Compute."""
+
+import sys
+import traceback
+
+from oslo.config import cfg
+
+from nova import config
+import nova.db.api
+from nova import exception
+from nova.openstack.common import log as logging
+from nova import service
+from nova import utils
+
+CONF = cfg.CONF
+CONF.import_opt('compute_topic', 'nova.compute.rpcapi')
+CONF.import_opt('use_local', 'nova.conductor.api', group='conductor')
+LOG = logging.getLogger('nova.compute')
+
+
+def block_db_access():
+ class NoDB(object):
+ def __getattr__(self, attr):
+ return self
+
+ def __call__(self, *args, **kwargs):
+ stacktrace = "".join(traceback.format_stack())
+ LOG.error('No db access allowed in nova-compute: %s' % stacktrace)
+ raise exception.DBNotAllowed('nova-compute')
+
+ nova.db.api.IMPL = NoDB()
+
+
+def main():
+ config.parse_args(sys.argv)
+ logging.setup('nova')
+ utils.monkey_patch()
+
+ if not CONF.conductor.use_local:
+ block_db_access()
+
+ server = service.Service.create(binary='nova-compute',
+ topic=CONF.compute_topic,
+ db_allowed=False)
+ service.serve(server)
+ service.wait()
diff --git a/nova/cmd/conductor.py b/nova/cmd/conductor.py
new file mode 100644
index 000000000..ba1ef2032
--- /dev/null
+++ b/nova/cmd/conductor.py
@@ -0,0 +1,41 @@
+#!/usr/bin/env python
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2012 IBM Corp.
+#
+# 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
+#
+# Unless required by applicable law or agreed to in writing, software
+# 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.
+
+"""Starter script for Nova Conductor."""
+
+import sys
+
+from oslo.config import cfg
+
+from nova import config
+from nova.openstack.common import log as logging
+from nova import service
+from nova import utils
+
+CONF = cfg.CONF
+CONF.import_opt('topic', 'nova.conductor.api', group='conductor')
+
+
+def main():
+ config.parse_args(sys.argv)
+ logging.setup("nova")
+ utils.monkey_patch()
+ server = service.Service.create(binary='nova-conductor',
+ topic=CONF.conductor.topic,
+ manager=CONF.conductor.manager)
+ service.serve(server)
+ service.wait()
diff --git a/nova/cmd/console.py b/nova/cmd/console.py
new file mode 100644
index 000000000..2aa099f0c
--- /dev/null
+++ b/nova/cmd/console.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright (c) 2010 OpenStack Foundation
+# 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
+#
+# Unless required by applicable law or agreed to in writing, software
+# 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.
+
+"""Starter script for Nova Console Proxy."""
+
+import sys
+
+from oslo.config import cfg
+
+from nova import config
+from nova.openstack.common import log as logging
+from nova import service
+
+CONF = cfg.CONF
+CONF.import_opt('console_topic', 'nova.console.rpcapi')
+
+
+def main():
+ config.parse_args(sys.argv)
+ logging.setup("nova")
+ server = service.Service.create(binary='nova-console',
+ topic=CONF.console_topic)
+ service.serve(server)
+ service.wait()
diff --git a/nova/cmd/consoleauth.py b/nova/cmd/consoleauth.py
new file mode 100644
index 000000000..d21b9b3f5
--- /dev/null
+++ b/nova/cmd/consoleauth.py
@@ -0,0 +1,38 @@
+#!/usr/bin/env python
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright (c) 2012 OpenStack Foundation
+# 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
+#
+# Unless required by applicable law or agreed to in writing, software
+# 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.
+
+"""VNC Console Proxy Server."""
+
+import sys
+
+from oslo.config import cfg
+
+from nova import config
+from nova.openstack.common import log as logging
+from nova import service
+
+CONF = cfg.CONF
+
+
+def main():
+ config.parse_args(sys.argv)
+ logging.setup("nova")
+ server = service.Service.create(binary='nova-consoleauth',
+ topic=CONF.consoleauth_topic)
+ service.serve(server)
+ service.wait()
diff --git a/nova/cmd/dhcpbridge.py b/nova/cmd/dhcpbridge.py
new file mode 100644
index 000000000..436039af6
--- /dev/null
+++ b/nova/cmd/dhcpbridge.py
@@ -0,0 +1,138 @@
+#!/usr/bin/env python
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2010 United States Government as represented by the
+# Administrator of the National Aeronautics and Space Administration.
+# 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
+#
+# Unless required by applicable law or agreed to in writing, software
+# 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.
+
+"""
+Handle lease database updates from DHCP servers.
+"""
+
+import gettext
+import os
+import sys
+
+from oslo.config import cfg
+
+gettext.install('nova', unicode=1)
+
+from nova import config
+from nova import context
+from nova import db
+from nova.network import rpcapi as network_rpcapi
+from nova.openstack.common import importutils
+from nova.openstack.common import jsonutils
+from nova.openstack.common import log as logging
+from nova.openstack.common import rpc
+
+CONF = cfg.CONF
+CONF.import_opt('host', 'nova.netconf')
+CONF.import_opt('network_manager', 'nova.service')
+LOG = logging.getLogger('nova.dhcpbridge')
+
+
+def add_lease(mac, ip_address):
+ """Set the IP that was assigned by the DHCP server."""
+ if CONF.fake_rabbit:
+ LOG.debug(_("leasing ip"))
+ network_manager = importutils.import_object(CONF.network_manager)
+ network_manager.lease_fixed_ip(context.get_admin_context(),
+ ip_address)
+ else:
+ api = network_rpcapi.NetworkAPI()
+ api.lease_fixed_ip(context.get_admin_context(), ip_address, CONF.host)
+
+
+def old_lease(mac, ip_address):
+ """Called when an old lease is recognized."""
+ # NOTE(vish): We assume we heard about this lease the first time.
+ # If not, we will get it the next time the lease is
+ # renewed.
+ pass
+
+
+def del_lease(mac, ip_address):
+ """Called when a lease expires."""
+ if CONF.fake_rabbit:
+ LOG.debug(_("releasing ip"))
+ network_manager = importutils.import_object(CONF.network_manager)
+ network_manager.release_fixed_ip(context.get_admin_context(),
+ ip_address)
+ else:
+ api = network_rpcapi.NetworkAPI()
+ api.release_fixed_ip(context.get_admin_context(), ip_address,
+ CONF.host)
+
+
+def init_leases(network_id):
+ """Get the list of hosts for a network."""
+ ctxt = context.get_admin_context()
+ network_ref = db.network_get(ctxt, network_id)
+ network_manager = importutils.import_object(CONF.network_manager)
+ return network_manager.get_dhcp_leases(ctxt, network_ref)
+
+
+def add_action_parsers(subparsers):
+ parser = subparsers.add_parser('init')
+
+ # NOTE(cfb): dnsmasq always passes mac, and ip. hostname
+ # is passed if known. We don't care about
+ # hostname, but argparse will complain if we
+ # do not accept it.
+ for action in ['add', 'del', 'old']:
+ parser = subparsers.add_parser(action)
+ parser.add_argument('mac')
+ parser.add_argument('ip')
+ parser.add_argument('hostname', nargs='?', default='')
+ parser.set_defaults(func=globals()[action + '_lease'])
+
+
+CONF.register_cli_opt(
+ cfg.SubCommandOpt('action',
+ title='Action options',
+ help='Available dhcpbridge options',
+ handler=add_action_parsers))
+
+
+def main():
+ """Parse environment and arguments and call the appropriate action."""
+ try:
+ config_file = os.environ['CONFIG_FILE']
+ except KeyError:
+ config_file = os.environ['FLAGFILE']
+
+ config.parse_args(sys.argv,
+ default_config_files=jsonutils.loads(config_file))
+
+ logging.setup("nova")
+
+ if CONF.action.name in ['add', 'del', 'old']:
+ msg = (_("Called '%(action)s' for mac '%(mac)s' with ip '%(ip)s'") %
+ {"action": CONF.action.name,
+ "mac": CONF.action.mac,
+ "ip": CONF.action.ip})
+ LOG.debug(msg)
+ CONF.action.func(CONF.action.mac, CONF.action.ip)
+ else:
+ try:
+ network_id = int(os.environ.get('NETWORK_ID'))
+ except TypeError:
+ LOG.error(_("Environment variable 'NETWORK_ID' must be set."))
+ return(1)
+
+ print init_leases(network_id)
+
+ rpc.cleanup()
diff --git a/nova/cmd/manage.py b/nova/cmd/manage.py
new file mode 100644
index 000000000..2482e0be2
--- /dev/null
+++ b/nova/cmd/manage.py
@@ -0,0 +1,1244 @@
+#!/usr/bin/env python
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright (c) 2011 X.commerce, a business unit of eBay Inc.
+# Copyright 2010 United States Government as represented by the
+# Administrator of the National Aeronautics and Space Administration.
+# 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
+#
+# Unless required by applicable law or agreed to in writing, software
+# 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.
+
+# Interactive shell based on Django:
+#
+# Copyright (c) 2005, the Lawrence Journal-World
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# 1. Redistributions of source code must retain the above copyright notice,
+# this list of conditions and the following disclaimer.
+#
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# 3. Neither the name of Django nor the names of its contributors may be
+# used to endorse or promote products derived from this software without
+# specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+
+"""
+ CLI interface for nova management.
+"""
+
+import gettext
+import netaddr
+import os
+import sys
+
+from oslo.config import cfg
+
+gettext.install('nova', unicode=1)
+
+from nova.api.ec2 import ec2utils
+from nova import availability_zones
+from nova.compute import instance_types
+from nova import config
+from nova import context
+from nova import db
+from nova.db import migration
+from nova import exception
+from nova.openstack.common import cliutils
+from nova.openstack.common.db import exception as db_exc
+from nova.openstack.common import importutils
+from nova.openstack.common import log as logging
+from nova.openstack.common import rpc
+from nova.openstack.common import timeutils
+from nova import quota
+from nova.scheduler import rpcapi as scheduler_rpcapi
+from nova import servicegroup
+from nova import version
+
+CONF = cfg.CONF
+CONF.import_opt('network_manager', 'nova.service')
+CONF.import_opt('service_down_time', 'nova.service')
+CONF.import_opt('flat_network_bridge', 'nova.network.manager')
+CONF.import_opt('num_networks', 'nova.network.manager')
+CONF.import_opt('multi_host', 'nova.network.manager')
+CONF.import_opt('network_size', 'nova.network.manager')
+CONF.import_opt('vlan_start', 'nova.network.manager')
+CONF.import_opt('vpn_start', 'nova.network.manager')
+CONF.import_opt('default_floating_pool', 'nova.network.floating_ips')
+CONF.import_opt('public_interface', 'nova.network.linux_net')
+
+QUOTAS = quota.QUOTAS
+
+
+# Decorators for actions
+def args(*args, **kwargs):
+ def _decorator(func):
+ func.__dict__.setdefault('args', []).insert(0, (args, kwargs))
+ return func
+ return _decorator
+
+
+def param2id(object_id):
+ """Helper function to convert various volume id types to internal id.
+ args: [object_id], e.g. 'vol-0000000a' or 'volume-0000000a' or '10'
+ """
+ if '-' in object_id:
+ return ec2utils.ec2_vol_id_to_uuid(object_id)
+ else:
+ return object_id
+
+
+class VpnCommands(object):
+ """Class for managing VPNs."""
+
+ @args('--project', dest='project_id', metavar='<Project name>',
+ help='Project name')
+ @args('--ip', metavar='<IP Address>', help='IP Address')
+ @args('--port', metavar='<Port>', help='Port')
+ def change(self, project_id, ip, port):
+ """Change the ip and port for a vpn.
+
+ this will update all networks associated with a project
+ not sure if that's the desired behavior or not, patches accepted
+
+ """
+ # TODO(tr3buchet): perhaps this shouldn't update all networks
+ # associated with a project in the future
+ admin_context = context.get_admin_context()
+ networks = db.project_get_networks(admin_context, project_id)
+ for network in networks:
+ db.network_update(admin_context,
+ network['id'],
+ {'vpn_public_address': ip,
+ 'vpn_public_port': int(port)})
+
+
+class ShellCommands(object):
+ def bpython(self):
+ """Runs a bpython shell.
+
+ Falls back to Ipython/python shell if unavailable"""
+ self.run('bpython')
+
+ def ipython(self):
+ """Runs an Ipython shell.
+
+ Falls back to Python shell if unavailable"""
+ self.run('ipython')
+
+ def python(self):
+ """Runs a python shell.
+
+ Falls back to Python shell if unavailable"""
+ self.run('python')
+
+ @args('--shell', metavar='<bpython|ipython|python >',
+ help='Python shell')
+ def run(self, shell=None):
+ """Runs a Python interactive interpreter."""
+ if not shell:
+ shell = 'bpython'
+
+ if shell == 'bpython':
+ try:
+ import bpython
+ bpython.embed()
+ except ImportError:
+ shell = 'ipython'
+ if shell == 'ipython':
+ try:
+ import IPython
+ # Explicitly pass an empty list as arguments, because
+ # otherwise IPython would use sys.argv from this script.
+ shell = IPython.Shell.IPShell(argv=[])
+ shell.mainloop()
+ except ImportError:
+ shell = 'python'
+
+ if shell == 'python':
+ import code
+ try:
+ # Try activating rlcompleter, because it's handy.
+ import readline
+ except ImportError:
+ pass
+ else:
+ # We don't have to wrap the following import in a 'try',
+ # because we already know 'readline' was imported successfully.
+ readline.parse_and_bind("tab:complete")
+ code.interact()
+
+ @args('--path', metavar='<path>', help='Script path')
+ def script(self, path):
+ """Runs the script from the specified path with flags set properly.
+ arguments: path"""
+ exec(compile(open(path).read(), path, 'exec'), locals(), globals())
+
+
+def _db_error(caught_exception):
+ print caught_exception
+ print _("The above error may show that the database has not "
+ "been created.\nPlease create a database using "
+ "'nova-manage db sync' before running this command.")
+ exit(1)
+
+
+class ProjectCommands(object):
+ """Class for managing projects."""
+
+ @args('--project', dest='project_id', metavar='<Project name>',
+ help='Project name')
+ @args('--key', metavar='<key>', help='Key')
+ @args('--value', metavar='<value>', help='Value')
+ def quota(self, project_id, key=None, value=None):
+ """
+ Create, update or display quotas for project
+
+ If no quota key is provided, the quota will be displayed.
+ If a valid quota key is provided and it does not exist,
+ it will be created. Otherwise, it will be updated.
+ """
+
+ ctxt = context.get_admin_context()
+ project_quota = QUOTAS.get_project_quotas(ctxt, project_id)
+ # if key is None, that means we need to show the quotas instead
+ # of updating them
+ if key:
+ if key in project_quota:
+ if value.lower() == 'unlimited':
+ value = -1
+ try:
+ db.quota_update(ctxt, project_id, key, value)
+ except exception.ProjectQuotaNotFound:
+ db.quota_create(ctxt, project_id, key, value)
+ else:
+ print _('%(key)s is not a valid quota key. Valid options are: '
+ '%(options)s.') % {'key': key,
+ 'options': ', '.join(project_quota)}
+ return(2)
+ print_format = "%-36s %-10s %-10s %-10s"
+ print print_format % (
+ _('Quota'),
+ _('Limit'),
+ _('In Use'),
+ _('Reserved'))
+ # Retrieve the quota after update
+ project_quota = QUOTAS.get_project_quotas(ctxt, project_id)
+ for key, value in project_quota.iteritems():
+ if value['limit'] < 0 or value['limit'] is None:
+ value['limit'] = 'unlimited'
+ print print_format % (key, value['limit'], value['in_use'],
+ value['reserved'])
+
+ @args('--project', dest='project_id', metavar='<Project name>',
+ help='Project name')
+ def scrub(self, project_id):
+ """Deletes data associated with project."""
+ admin_context = context.get_admin_context()
+ networks = db.project_get_networks(admin_context, project_id)
+ for network in networks:
+ db.network_disassociate(admin_context, network['id'])
+ groups = db.security_group_get_by_project(admin_context, project_id)
+ for group in groups:
+ db.security_group_destroy(admin_context, group['id'])
+
+
+AccountCommands = ProjectCommands
+
+
+class FixedIpCommands(object):
+ """Class for managing fixed ip."""
+
+ @args('--host', metavar='<host>', help='Host')
+ def list(self, host=None):
+ """Lists all fixed ips (optionally by host)."""
+ ctxt = context.get_admin_context()
+
+ try:
+ if host is None:
+ fixed_ips = db.fixed_ip_get_all(ctxt)
+ else:
+ fixed_ips = db.fixed_ip_get_all_by_instance_host(ctxt, host)
+ except exception.NotFound as ex:
+ print _("error: %s") % ex
+ return(2)
+
+ instances = db.instance_get_all(context.get_admin_context())
+ instances_by_uuid = {}
+ for instance in instances:
+ instances_by_uuid[instance['uuid']] = instance
+
+ print "%-18s\t%-15s\t%-15s\t%s" % (_('network'),
+ _('IP address'),
+ _('hostname'),
+ _('host'))
+
+ all_networks = {}
+ try:
+ # use network_get_all to retrieve all existing networks
+ # this is to ensure that IPs associated with deleted networks
+ # will not throw exceptions.
+ for network in db.network_get_all(context.get_admin_context()):
+ all_networks[network.id] = network
+ except exception.NoNetworksFound:
+ # do not have any networks, so even if there are IPs, these
+ # IPs should have been deleted ones, so return.
+ print _('No fixed IP found.')
+ return
+
+ has_ip = False
+ for fixed_ip in fixed_ips:
+ hostname = None
+ host = None
+ network = all_networks.get(fixed_ip['network_id'])
+ if network:
+ has_ip = True
+ if fixed_ip.get('instance_uuid'):
+ instance = instances_by_uuid.get(fixed_ip['instance_uuid'])
+ if instance:
+ hostname = instance['hostname']
+ host = instance['host']
+ else:
+ print _('WARNING: fixed ip %s allocated to missing'
+ ' instance') % str(fixed_ip['address'])
+ print "%-18s\t%-15s\t%-15s\t%s" % (
+ network['cidr'],
+ fixed_ip['address'],
+ hostname, host)
+
+ if not has_ip:
+ print _('No fixed IP found.')
+
+ @args('--address', metavar='<ip address>', help='IP address')
+ def reserve(self, address):
+ """Mark fixed ip as reserved
+ arguments: address"""
+ return self._set_reserved(address, True)
+
+ @args('--address', metavar='<ip address>', help='IP address')
+ def unreserve(self, address):
+ """Mark fixed ip as free to use
+ arguments: address"""
+ return self._set_reserved(address, False)
+
+ def _set_reserved(self, address, reserved):
+ ctxt = context.get_admin_context()
+
+ try:
+ fixed_ip = db.fixed_ip_get_by_address(ctxt, address)
+ if fixed_ip is None:
+ raise exception.NotFound('Could not find address')
+ db.fixed_ip_update(ctxt, fixed_ip['address'],
+ {'reserved': reserved})
+ except exception.NotFound as ex:
+ print _("error: %s") % ex
+ return(2)
+
+
+class FloatingIpCommands(object):
+ """Class for managing floating ip."""
+
+ @staticmethod
+ def address_to_hosts(addresses):
+ """
+ Iterate over hosts within an address range.
+
+ If an explicit range specifier is missing, the parameter is
+ interpreted as a specific individual address.
+ """
+ try:
+ return [netaddr.IPAddress(addresses)]
+ except ValueError:
+ net = netaddr.IPNetwork(addresses)
+ if net.size < 4:
+ reason = _("/%s should be specified as single address(es) "
+ "not in cidr format") % net.prefixlen
+ raise exception.InvalidInput(reason=reason)
+ elif net.size >= 1000000:
+ # NOTE(dripton): If we generate a million IPs and put them in
+ # the database, the system will slow to a crawl and/or run
+ # out of memory and crash. This is clearly a misconfiguration.
+ reason = _("Too many IP addresses will be generated. Please "
+ "increase /%s to reduce the number generated."
+ ) % net.prefixlen
+ raise exception.InvalidInput(reason=reason)
+ else:
+ return net.iter_hosts()
+
+ @args('--ip_range', metavar='<range>', help='IP range')
+ @args('--pool', metavar='<pool>', help='Optional pool')
+ @args('--interface', metavar='<interface>', help='Optional interface')
+ def create(self, ip_range, pool=None, interface=None):
+ """Creates floating ips for zone by range."""
+ admin_context = context.get_admin_context()
+ if not pool:
+ pool = CONF.default_floating_pool
+ if not interface:
+ interface = CONF.public_interface
+
+ ips = ({'address': str(address), 'pool': pool, 'interface': interface}
+ for address in self.address_to_hosts(ip_range))
+ try:
+ db.floating_ip_bulk_create(admin_context, ips)
+ except exception.FloatingIpExists as exc:
+ # NOTE(simplylizz): Maybe logging would be better here
+ # instead of printing, but logging isn't used here and I
+ # don't know why.
+ print('error: %s' % exc)
+ return(1)
+
+ @args('--ip_range', metavar='<range>', help='IP range')
+ def delete(self, ip_range):
+ """Deletes floating ips by range."""
+ admin_context = context.get_admin_context()
+
+ ips = ({'address': str(address)}
+ for address in self.address_to_hosts(ip_range))
+ db.floating_ip_bulk_destroy(admin_context, ips)
+
+ @args('--host', metavar='<host>', help='Host')
+ def list(self, host=None):
+ """Lists all floating ips (optionally by host)
+ Note: if host is given, only active floating IPs are returned"""
+ ctxt = context.get_admin_context()
+ try:
+ if host is None:
+ floating_ips = db.floating_ip_get_all(ctxt)
+ else:
+ floating_ips = db.floating_ip_get_all_by_host(ctxt, host)
+ except exception.NoFloatingIpsDefined:
+ print _("No floating IP addresses have been defined.")
+ return
+ for floating_ip in floating_ips:
+ instance_uuid = None
+ if floating_ip['fixed_ip_id']:
+ fixed_ip = db.fixed_ip_get(ctxt, floating_ip['fixed_ip_id'])
+ instance_uuid = fixed_ip['instance_uuid']
+
+ print "%s\t%s\t%s\t%s\t%s" % (floating_ip['project_id'],
+ floating_ip['address'],
+ instance_uuid,
+ floating_ip['pool'],
+ floating_ip['interface'])
+
+
+class NetworkCommands(object):
+ """Class for managing networks."""
+
+ @args('--label', metavar='<label>', help='Label for network (ex: public)')
+ @args('--fixed_range_v4', dest='cidr', metavar='<x.x.x.x/yy>',
+ help='IPv4 subnet (ex: 10.0.0.0/8)')
+ @args('--num_networks', metavar='<number>',
+ help='Number of networks to create')
+ @args('--network_size', metavar='<number>',
+ help='Number of IPs per network')
+ @args('--vlan', dest='vlan_start', metavar='<vlan id>', help='vlan id')
+ @args('--vpn', dest='vpn_start', help='vpn start')
+ @args('--fixed_range_v6', dest='cidr_v6',
+ help='IPv6 subnet (ex: fe80::/64')
+ @args('--gateway', help='gateway')
+ @args('--gateway_v6', help='ipv6 gateway')
+ @args('--bridge', metavar='<bridge>',
+ help='VIFs on this network are connected to this bridge')
+ @args('--bridge_interface', metavar='<bridge interface>',
+ help='the bridge is connected to this interface')
+ @args('--multi_host', metavar="<'T'|'F'>",
+ help='Multi host')
+ @args('--dns1', metavar="<DNS Address>", help='First DNS')
+ @args('--dns2', metavar="<DNS Address>", help='Second DNS')
+ @args('--uuid', metavar="<network uuid>", help='Network UUID')
+ @args('--fixed_cidr', metavar='<x.x.x.x/yy>',
+ help='IPv4 subnet for fixed IPS (ex: 10.20.0.0/16)')
+ @args('--project_id', metavar="<project id>",
+ help='Project id')
+ @args('--priority', metavar="<number>", help='Network interface priority')
+ def create(self, label=None, cidr=None, num_networks=None,
+ network_size=None, multi_host=None, vlan_start=None,
+ vpn_start=None, cidr_v6=None, gateway=None,
+ gateway_v6=None, bridge=None, bridge_interface=None,
+ dns1=None, dns2=None, project_id=None, priority=None,
+ uuid=None, fixed_cidr=None):
+ """Creates fixed ips for host by range."""
+ kwargs = dict(((k, v) for k, v in locals().iteritems()
+ if v and k != "self"))
+ if multi_host is not None:
+ kwargs['multi_host'] = multi_host == 'T'
+ net_manager = importutils.import_object(CONF.network_manager)
+ net_manager.create_networks(context.get_admin_context(), **kwargs)
+
+ def list(self):
+ """List all created networks."""
+ _fmt = "%-5s\t%-18s\t%-15s\t%-15s\t%-15s\t%-15s\t%-15s\t%-15s\t%-15s"
+ print _fmt % (_('id'),
+ _('IPv4'),
+ _('IPv6'),
+ _('start address'),
+ _('DNS1'),
+ _('DNS2'),
+ _('VlanID'),
+ _('project'),
+ _("uuid"))
+ try:
+ # Since network_get_all can throw exception.NoNetworksFound
+ # for this command to show a nice result, this exception
+ # should be caught and handled as such.
+ networks = db.network_get_all(context.get_admin_context())
+ except exception.NoNetworksFound:
+ print _('No networks found')
+ else:
+ for network in networks:
+ print _fmt % (network.id,
+ network.cidr,
+ network.cidr_v6,
+ network.dhcp_start,
+ network.dns1,
+ network.dns2,
+ network.vlan,
+ network.project_id,
+ network.uuid)
+
+ @args('--fixed_range', metavar='<x.x.x.x/yy>', help='Network to delete')
+ @args('--uuid', metavar='<uuid>', help='UUID of network to delete')
+ def delete(self, fixed_range=None, uuid=None):
+ """Deletes a network."""
+
+ if fixed_range is None and uuid is None:
+ raise Exception(_("Please specify either fixed_range or uuid"))
+
+ net_manager = importutils.import_object(CONF.network_manager)
+ if "QuantumManager" in CONF.network_manager:
+ if uuid is None:
+ raise Exception(_("UUID is required to delete "
+ "Quantum Networks"))
+ if fixed_range:
+ raise Exception(_("Deleting by fixed_range is not supported "
+ "with the QuantumManager"))
+ # delete the network
+ net_manager.delete_network(context.get_admin_context(),
+ fixed_range, uuid)
+
+ @args('--fixed_range', metavar='<x.x.x.x/yy>', help='Network to modify')
+ @args('--project', metavar='<project name>',
+ help='Project name to associate')
+ @args('--host', metavar='<host>', help='Host to associate')
+ @args('--disassociate-project', action="store_true", dest='dis_project',
+ default=False, help='Disassociate Network from Project')
+ @args('--disassociate-host', action="store_true", dest='dis_host',
+ default=False, help='Disassociate Host from Project')
+ def modify(self, fixed_range, project=None, host=None,
+ dis_project=None, dis_host=None):
+ """Associate/Disassociate Network with Project and/or Host
+ arguments: network project host
+ leave any field blank to ignore it
+ """
+ admin_context = context.get_admin_context()
+ network = db.network_get_by_cidr(admin_context, fixed_range)
+ net = {}
+ #User can choose the following actions each for project and host.
+ #1) Associate (set not None value given by project/host parameter)
+ #2) Disassociate (set None by disassociate parameter)
+ #3) Keep unchanged (project/host key is not added to 'net')
+ if dis_project:
+ net['project_id'] = None
+ if dis_host:
+ net['host'] = None
+
+ # The --disassociate-X are boolean options, but if they user
+ # mistakenly provides a value, it will be used as a positional argument
+ # and be erroneously interepreted as some other parameter (e.g.
+ # a project instead of host value). The safest thing to do is error-out
+ # with a message indicating that there is probably a problem with
+ # how the disassociate modifications are being used.
+ if dis_project or dis_host:
+ if project or host:
+ error_msg = "ERROR: Unexpected arguments provided. Please " \
+ "use separate commands."
+ print(error_msg)
+ return(1)
+ db.network_update(admin_context, network['id'], net)
+ return
+
+ if project:
+ net['project_id'] = project
+ if host:
+ net['host'] = host
+
+ db.network_update(admin_context, network['id'], net)
+
+
+class VmCommands(object):
+ """Class for mangaging VM instances."""
+
+ @args('--host', metavar='<host>', help='Host')
+ def list(self, host=None):
+ """Show a list of all instances."""
+
+ print ("%-10s %-15s %-10s %-10s %-26s %-9s %-9s %-9s"
+ " %-10s %-10s %-10s %-5s" % (_('instance'),
+ _('node'),
+ _('type'),
+ _('state'),
+ _('launched'),
+ _('image'),
+ _('kernel'),
+ _('ramdisk'),
+ _('project'),
+ _('user'),
+ _('zone'),
+ _('index')))
+
+ if host is None:
+ instances = db.instance_get_all(context.get_admin_context())
+ else:
+ instances = db.instance_get_all_by_host(
+ context.get_admin_context(), host)
+
+ for instance in instances:
+ instance_type = instance_types.extract_instance_type(instance)
+ print ("%-10s %-15s %-10s %-10s %-26s %-9s %-9s %-9s"
+ " %-10s %-10s %-10s %-5d" % (instance['display_name'],
+ instance['host'],
+ instance_type['name'],
+ instance['vm_state'],
+ instance['launched_at'],
+ instance['image_ref'],
+ instance['kernel_id'],
+ instance['ramdisk_id'],
+ instance['project_id'],
+ instance['user_id'],
+ instance['availability_zone'],
+ instance['launch_index']))
+
+
+class ServiceCommands(object):
+ """Enable and disable running services."""
+
+ @args('--host', metavar='<host>', help='Host')
+ @args('--service', metavar='<service>', help='Nova service')
+ def list(self, host=None, service=None):
+ """
+ Show a list of all running services. Filter by host & service name.
+ """
+ servicegroup_api = servicegroup.API()
+ ctxt = context.get_admin_context()
+ now = timeutils.utcnow()
+ services = db.service_get_all(ctxt)
+ services = availability_zones.set_availability_zones(ctxt, services)
+ if host:
+ services = [s for s in services if s['host'] == host]
+ if service:
+ services = [s for s in services if s['binary'] == service]
+ print_format = "%-16s %-36s %-16s %-10s %-5s %-10s"
+ print print_format % (
+ _('Binary'),
+ _('Host'),
+ _('Zone'),
+ _('Status'),
+ _('State'),
+ _('Updated_At'))
+ for svc in services:
+ alive = servicegroup_api.service_is_up(svc)
+ art = (alive and ":-)") or "XXX"
+ active = 'enabled'
+ if svc['disabled']:
+ active = 'disabled'
+ print print_format % (svc['binary'], svc['host'],
+ svc['availability_zone'], active, art,
+ svc['updated_at'])
+
+ @args('--host', metavar='<host>', help='Host')
+ @args('--service', metavar='<service>', help='Nova service')
+ def enable(self, host, service):
+ """Enable scheduling for a service."""
+ ctxt = context.get_admin_context()
+ try:
+ svc = db.service_get_by_args(ctxt, host, service)
+ db.service_update(ctxt, svc['id'], {'disabled': False})
+ except exception.NotFound as ex:
+ print _("error: %s") % ex
+ return(2)
+ print _("Service %(service)s on host %(host)s enabled.") % locals()
+
+ @args('--host', metavar='<host>', help='Host')
+ @args('--service', metavar='<service>', help='Nova service')
+ def disable(self, host, service):
+ """Disable scheduling for a service."""
+ ctxt = context.get_admin_context()
+ try:
+ svc = db.service_get_by_args(ctxt, host, service)
+ db.service_update(ctxt, svc['id'], {'disabled': True})
+ except exception.NotFound as ex:
+ print _("error: %s") % ex
+ return(2)
+ print _("Service %(service)s on host %(host)s disabled.") % locals()
+
+ @args('--host', metavar='<host>', help='Host')
+ def describe_resource(self, host):
+ """Describes cpu/memory/hdd info for host.
+
+ :param host: hostname.
+
+ """
+ rpcapi = scheduler_rpcapi.SchedulerAPI()
+ result = rpcapi.show_host_resources(context.get_admin_context(),
+ host=host)
+
+ if not isinstance(result, dict):
+ print _('An unexpected error has occurred.')
+ print _('[Result]'), result
+ else:
+ # Printing a total and used_now
+ # (NOTE)The host name width 16 characters
+ print '%(a)-25s%(b)16s%(c)8s%(d)8s%(e)8s' % {"a": _('HOST'),
+ "b": _('PROJECT'),
+ "c": _('cpu'),
+ "d": _('mem(mb)'),
+ "e": _('hdd')}
+ print ('%(a)-16s(total)%(b)26s%(c)8s%(d)8s' %
+ {"a": host,
+ "b": result['resource']['vcpus'],
+ "c": result['resource']['memory_mb'],
+ "d": result['resource']['local_gb']})
+
+ print ('%(a)-16s(used_now)%(b)23s%(c)8s%(d)8s' %
+ {"a": host,
+ "b": result['resource']['vcpus_used'],
+ "c": result['resource']['memory_mb_used'],
+ "d": result['resource']['local_gb_used']})
+
+ # Printing a used_max
+ cpu_sum = 0
+ mem_sum = 0
+ hdd_sum = 0
+ for p_id, val in result['usage'].items():
+ cpu_sum += val['vcpus']
+ mem_sum += val['memory_mb']
+ hdd_sum += val['root_gb']
+ hdd_sum += val['ephemeral_gb']
+ print '%(a)-16s(used_max)%(b)23s%(c)8s%(d)8s' % {"a": host,
+ "b": cpu_sum,
+ "c": mem_sum,
+ "d": hdd_sum}
+
+ for p_id, val in result['usage'].items():
+ print '%(a)-25s%(b)16s%(c)8s%(d)8s%(e)8s' % {
+ "a": host,
+ "b": p_id,
+ "c": val['vcpus'],
+ "d": val['memory_mb'],
+ "e": val['root_gb'] + val['ephemeral_gb']}
+
+
+class HostCommands(object):
+ """List hosts."""
+
+ def list(self, zone=None):
+ """Show a list of all physical hosts. Filter by zone.
+ args: [zone]"""
+ print "%-25s\t%-15s" % (_('host'),
+ _('zone'))
+ ctxt = context.get_admin_context()
+ services = db.service_get_all(ctxt)
+ services = availability_zones.set_availability_zones(ctxt, services)
+ if zone:
+ services = [s for s in services if s['availability_zone'] == zone]
+ hosts = []
+ for srv in services:
+ if not [h for h in hosts if h['host'] == srv['host']]:
+ hosts.append(srv)
+
+ for h in hosts:
+ print "%-25s\t%-15s" % (h['host'], h['availability_zone'])
+
+
+class DbCommands(object):
+ """Class for managing the database."""
+
+ def __init__(self):
+ pass
+
+ @args('--version', metavar='<version>', help='Database version')
+ def sync(self, version=None):
+ """Sync the database up to the most recent version."""
+ return migration.db_sync(version)
+
+ def version(self):
+ """Print the current database version."""
+ print migration.db_version()
+
+ @args('--max_rows', metavar='<number>',
+ help='Maximum number of deleted rows to archive')
+ def archive_deleted_rows(self, max_rows):
+ """Move up to max_rows deleted rows from production tables to shadow
+ tables.
+ """
+ if max_rows is not None:
+ max_rows = int(max_rows)
+ if max_rows < 0:
+ print _("Must supply a positive value for max_rows")
+ return(1)
+ admin_context = context.get_admin_context()
+ db.archive_deleted_rows(admin_context, max_rows)
+
+
+class InstanceTypeCommands(object):
+ """Class for managing instance types / flavors."""
+
+ def _print_instance_types(self, name, val):
+ is_public = ('private', 'public')[val["is_public"] == 1]
+ print ("%s: Memory: %sMB, VCPUS: %s, Root: %sGB, Ephemeral: %sGb, "
+ "FlavorID: %s, Swap: %sMB, RXTX Factor: %s, %s, ExtraSpecs %s") % (
+ name, val["memory_mb"], val["vcpus"], val["root_gb"],
+ val["ephemeral_gb"], val["flavorid"], val["swap"],
+ val["rxtx_factor"], is_public, val["extra_specs"])
+
+ @args('--name', metavar='<name>',
+ help='Name of instance type/flavor')
+ @args('--memory', metavar='<memory size>', help='Memory size')
+ @args('--cpu', dest='vcpus', metavar='<num cores>', help='Number cpus')
+ @args('--root_gb', metavar='<root_gb>', help='Root disk size')
+ @args('--ephemeral_gb', metavar='<ephemeral_gb>',
+ help='Ephemeral disk size')
+ @args('--flavor', dest='flavorid', metavar='<flavor id>',
+ help='Flavor ID')
+ @args('--swap', metavar='<swap>', help='Swap')
+ @args('--rxtx_factor', metavar='<rxtx_factor>', help='rxtx_factor')
+ @args('--is_public', metavar='<is_public>',
+ help='Make flavor accessible to the public')
+ def create(self, name, memory, vcpus, root_gb, ephemeral_gb=0,
+ flavorid=None, swap=0, rxtx_factor=1.0, is_public=True):
+ """Creates instance types / flavors."""
+ try:
+ instance_types.create(name, memory, vcpus, root_gb,
+ ephemeral_gb, flavorid, swap, rxtx_factor,
+ is_public)
+ except exception.InvalidInput, e:
+ print _("Must supply valid parameters to create instance_type")
+ print e
+ return(1)
+ except exception.InstanceTypeExists:
+ print _("Instance Type exists.")
+ print _("Please ensure instance_type name and flavorid are "
+ "unique.")
+ print _("Currently defined instance_type names and flavorids:")
+ print
+ self.list()
+ return(2)
+ except Exception:
+ print _("Unknown error")
+ return(3)
+ else:
+ print _("%s created") % name
+
+ @args('--name', metavar='<name>', help='Name of instance type/flavor')
+ def delete(self, name):
+ """Marks instance types / flavors as deleted."""
+ try:
+ instance_types.destroy(name)
+ except exception.InstanceTypeNotFound:
+ print _("Valid instance type name is required")
+ return(1)
+ except db_exc.DBError, e:
+ print _("DB Error: %s") % e
+ return(2)
+ except Exception:
+ return(3)
+ else:
+ print _("%s deleted") % name
+
+ @args('--name', metavar='<name>', help='Name of instance type/flavor')
+ def list(self, name=None):
+ """Lists all active or specific instance types / flavors."""
+ try:
+ if name is None:
+ inst_types = instance_types.get_all_types()
+ else:
+ inst_types = instance_types.get_instance_type_by_name(name)
+ except db_exc.DBError, e:
+ _db_error(e)
+ if isinstance(inst_types.values()[0], dict):
+ for k, v in inst_types.iteritems():
+ self._print_instance_types(k, v)
+ else:
+ self._print_instance_types(name, inst_types)
+
+ @args('--name', metavar='<name>', help='Name of instance type/flavor')
+ @args('--key', metavar='<key>', help='The key of the key/value pair')
+ @args('--value', metavar='<value>', help='The value of the key/value pair')
+ def set_key(self, name, key, value=None):
+ """Add key/value pair to specified instance type's extra_specs."""
+ try:
+ try:
+ inst_type = instance_types.get_instance_type_by_name(name)
+ except exception.InstanceTypeNotFoundByName, e:
+ print e
+ return(2)
+
+ ctxt = context.get_admin_context()
+ ext_spec = {key: value}
+ db.instance_type_extra_specs_update_or_create(
+ ctxt,
+ inst_type["flavorid"],
+ ext_spec)
+ print _("Key %(key)s set to %(value)s on instance"
+ " type %(name)s") % locals()
+ except db_exc.DBError, e:
+ _db_error(e)
+
+ @args('--name', metavar='<name>', help='Name of instance type/flavor')
+ @args('--key', metavar='<key>', help='The key to be deleted')
+ def unset_key(self, name, key):
+ """Delete the specified extra spec for instance type."""
+ try:
+ try:
+ inst_type = instance_types.get_instance_type_by_name(name)
+ except exception.InstanceTypeNotFoundByName, e:
+ print e
+ return(2)
+
+ ctxt = context.get_admin_context()
+ db.instance_type_extra_specs_delete(
+ ctxt,
+ inst_type["flavorid"],
+ key)
+
+ print _("Key %(key)s on instance type %(name)s unset") % locals()
+ except db_exc.DBError, e:
+ _db_error(e)
+
+
+class AgentBuildCommands(object):
+ """Class for managing agent builds."""
+
+ def create(self, os, architecture, version, url, md5hash,
+ hypervisor='xen'):
+ """Creates a new agent build."""
+ ctxt = context.get_admin_context()
+ agent_build = db.agent_build_create(ctxt,
+ {'hypervisor': hypervisor,
+ 'os': os,
+ 'architecture': architecture,
+ 'version': version,
+ 'url': url,
+ 'md5hash': md5hash})
+
+ def delete(self, os, architecture, hypervisor='xen'):
+ """Deletes an existing agent build."""
+ ctxt = context.get_admin_context()
+ agent_build_ref = db.agent_build_get_by_triple(ctxt,
+ hypervisor, os, architecture)
+ db.agent_build_destroy(ctxt, agent_build_ref['id'])
+
+ def list(self, hypervisor=None):
+ """Lists all agent builds.
+ arguments: <none>"""
+ fmt = "%-10s %-8s %12s %s"
+ ctxt = context.get_admin_context()
+ by_hypervisor = {}
+ for agent_build in db.agent_build_get_all(ctxt):
+ buildlist = by_hypervisor.get(agent_build.hypervisor)
+ if not buildlist:
+ buildlist = by_hypervisor[agent_build.hypervisor] = []
+
+ buildlist.append(agent_build)
+
+ for key, buildlist in by_hypervisor.iteritems():
+ if hypervisor and key != hypervisor:
+ continue
+
+ print _('Hypervisor: %s') % key
+ print fmt % ('-' * 10, '-' * 8, '-' * 12, '-' * 32)
+ for agent_build in buildlist:
+ print fmt % (agent_build.os, agent_build.architecture,
+ agent_build.version, agent_build.md5hash)
+ print ' %s' % agent_build.url
+
+ print
+
+ def modify(self, os, architecture, version, url, md5hash,
+ hypervisor='xen'):
+ """Update an existing agent build."""
+ ctxt = context.get_admin_context()
+ agent_build_ref = db.agent_build_get_by_triple(ctxt,
+ hypervisor, os, architecture)
+ db.agent_build_update(ctxt, agent_build_ref['id'],
+ {'version': version,
+ 'url': url,
+ 'md5hash': md5hash})
+
+
+class GetLogCommands(object):
+ """Get logging information."""
+
+ def errors(self):
+ """Get all of the errors from the log files."""
+ error_found = 0
+ if CONF.log_dir:
+ logs = [x for x in os.listdir(CONF.log_dir) if x.endswith('.log')]
+ for file in logs:
+ log_file = os.path.join(CONF.log_dir, file)
+ lines = [line.strip() for line in open(log_file, "r")]
+ lines.reverse()
+ print_name = 0
+ for index, line in enumerate(lines):
+ if line.find(" ERROR ") > 0:
+ error_found += 1
+ if print_name == 0:
+ print log_file + ":-"
+ print_name = 1
+ linenum = len(lines) - index
+ print _('Line %(linenum)d : %(line)s') % locals()
+ if error_found == 0:
+ print _('No errors in logfiles!')
+
+ def syslog(self, num_entries=10):
+ """Get <num_entries> of the nova syslog events."""
+ entries = int(num_entries)
+ count = 0
+ log_file = ''
+ if os.path.exists('/var/log/syslog'):
+ log_file = '/var/log/syslog'
+ elif os.path.exists('/var/log/messages'):
+ log_file = '/var/log/messages'
+ else:
+ print _('Unable to find system log file!')
+ return(1)
+ lines = [line.strip() for line in open(log_file, "r")]
+ lines.reverse()
+ print _('Last %s nova syslog entries:-') % (entries)
+ for line in lines:
+ if line.find("nova") > 0:
+ count += 1
+ print "%s" % (line)
+ if count == entries:
+ break
+
+ if count == 0:
+ print _('No nova entries in syslog!')
+
+
+class CellCommands(object):
+ """Commands for managing cells."""
+
+ @args('--name', metavar='<name>', help='Name for the new cell')
+ @args('--cell_type', metavar='<parent|child>',
+ help='Whether the cell is a parent or child')
+ @args('--username', metavar='<username>',
+ help='Username for the message broker in this cell')
+ @args('--password', metavar='<password>',
+ help='Password for the message broker in this cell')
+ @args('--hostname', metavar='<hostname>',
+ help='Address of the message broker in this cell')
+ @args('--port', metavar='<number>',
+ help='Port number of the message broker in this cell')
+ @args('--virtual_host', metavar='<virtual_host>',
+ help='The virtual host of the message broker in this cell')
+ @args('--woffset', metavar='<float>')
+ @args('--wscale', metavar='<float>')
+ def create(self, name, cell_type='child', username=None, password=None,
+ hostname=None, port=None, virtual_host=None,
+ woffset=None, wscale=None):
+
+ if cell_type not in ['parent', 'child']:
+ print "Error: cell type must be 'parent' or 'child'"
+ return(2)
+
+ is_parent = cell_type == 'parent'
+ values = {'name': name,
+ 'is_parent': is_parent,
+ 'username': username,
+ 'password': password,
+ 'rpc_host': hostname,
+ 'rpc_port': int(port),
+ 'rpc_virtual_host': virtual_host,
+ 'weight_offset': float(woffset),
+ 'weight_scale': float(wscale)}
+ ctxt = context.get_admin_context()
+ db.cell_create(ctxt, values)
+
+ @args('--cell_name', metavar='<cell_name>',
+ help='Name of the cell to delete')
+ def delete(self, cell_name):
+ ctxt = context.get_admin_context()
+ db.cell_delete(ctxt, cell_name)
+
+ def list(self):
+ ctxt = context.get_admin_context()
+ cells = db.cell_get_all(ctxt)
+ fmt = "%3s %-10s %-6s %-10s %-15s %-5s %-10s"
+ print fmt % ('Id', 'Name', 'Type', 'Username', 'Hostname',
+ 'Port', 'VHost')
+ print fmt % ('-' * 3, '-' * 10, '-' * 6, '-' * 10, '-' * 15,
+ '-' * 5, '-' * 10)
+ for cell in cells:
+ print fmt % (cell.id, cell.name,
+ 'parent' if cell.is_parent else 'child',
+ cell.username, cell.rpc_host,
+ cell.rpc_port, cell.rpc_virtual_host)
+ print fmt % ('-' * 3, '-' * 10, '-' * 6, '-' * 10, '-' * 15,
+ '-' * 5, '-' * 10)
+
+
+CATEGORIES = {
+ 'account': AccountCommands,
+ 'agent': AgentBuildCommands,
+ 'cell': CellCommands,
+ 'db': DbCommands,
+ 'fixed': FixedIpCommands,
+ 'flavor': InstanceTypeCommands,
+ 'floating': FloatingIpCommands,
+ 'host': HostCommands,
+ 'instance_type': InstanceTypeCommands,
+ 'logs': GetLogCommands,
+ 'network': NetworkCommands,
+ 'project': ProjectCommands,
+ 'service': ServiceCommands,
+ 'shell': ShellCommands,
+ 'vm': VmCommands,
+ 'vpn': VpnCommands,
+}
+
+
+def methods_of(obj):
+ """Get all callable methods of an object that don't start with underscore
+ returns a list of tuples of the form (method_name, method)"""
+ result = []
+ for i in dir(obj):
+ if callable(getattr(obj, i)) and not i.startswith('_'):
+ result.append((i, getattr(obj, i)))
+ return result
+
+
+def add_command_parsers(subparsers):
+ parser = subparsers.add_parser('version')
+
+ parser = subparsers.add_parser('bash-completion')
+ parser.add_argument('query_category', nargs='?')
+
+ for category in CATEGORIES:
+ command_object = CATEGORIES[category]()
+
+ parser = subparsers.add_parser(category)
+ parser.set_defaults(command_object=command_object)
+
+ category_subparsers = parser.add_subparsers(dest='action')
+
+ for (action, action_fn) in methods_of(command_object):
+ parser = category_subparsers.add_parser(action)
+
+ action_kwargs = []
+ for args, kwargs in getattr(action_fn, 'args', []):
+ # FIXME(markmc): hack to assume dest is the arg name without
+ # the leading hyphens if no dest is supplied
+ kwargs.setdefault('dest', args[0][2:])
+ if kwargs['dest'].startswith('action_kwarg_'):
+ action_kwargs.append(
+ kwargs['dest'][len('action_kwarg_'):])
+ else:
+ action_kwargs.append(kwargs['dest'])
+ kwargs['dest'] = 'action_kwarg_' + kwargs['dest']
+
+ parser.add_argument(*args, **kwargs)
+
+ parser.set_defaults(action_fn=action_fn)
+ parser.set_defaults(action_kwargs=action_kwargs)
+
+ parser.add_argument('action_args', nargs='*')
+
+
+category_opt = cfg.SubCommandOpt('category',
+ title='Command categories',
+ help='Available categories',
+ handler=add_command_parsers)
+
+
+def main():
+ """Parse options and call the appropriate class/method."""
+ CONF.register_cli_opt(category_opt)
+ try:
+ config.parse_args(sys.argv)
+ logging.setup("nova")
+ except cfg.ConfigFilesNotFoundError:
+ cfgfile = CONF.config_file[-1] if CONF.config_file else None
+ if cfgfile and not os.access(cfgfile, os.R_OK):
+ st = os.stat(cfgfile)
+ print _("Could not read %s. Re-running with sudo") % cfgfile
+ try:
+ os.execvp('sudo', ['sudo', '-u', '#%s' % st.st_uid] + sys.argv)
+ except Exception:
+ print _('sudo failed, continuing as if nothing happened')
+
+ print _('Please re-run nova-manage as root.')
+ return(2)
+
+ if CONF.category.name == "version":
+ print version.version_string_with_package()
+ return(0)
+
+ if CONF.category.name == "bash-completion":
+ if not CONF.category.query_category:
+ print " ".join(CATEGORIES.keys())
+ elif CONF.category.query_category in CATEGORIES:
+ fn = CATEGORIES[CONF.category.query_category]
+ command_object = fn()
+ actions = methods_of(command_object)
+ print " ".join([k for (k, v) in actions])
+ return(0)
+
+ fn = CONF.category.action_fn
+ fn_args = [arg.decode('utf-8') for arg in CONF.category.action_args]
+ fn_kwargs = {}
+ for k in CONF.category.action_kwargs:
+ v = getattr(CONF.category, 'action_kwarg_' + k)
+ if v is None:
+ continue
+ if isinstance(v, basestring):
+ v = v.decode('utf-8')
+ fn_kwargs[k] = v
+
+ # call the action with the remaining arguments
+ # check arguments
+ try:
+ cliutils.validate_args(fn, *fn_args, **fn_kwargs)
+ except cliutils.MissingArgs as e:
+ # NOTE(mikal): this isn't the most helpful error message ever. It is
+ # long, and tells you a lot of things you probably don't want to know
+ # if you just got a single arg wrong.
+ print fn.__doc__
+ CONF.print_help()
+ print e
+ return(1)
+ try:
+ ret = fn(*fn_args, **fn_kwargs)
+ rpc.cleanup()
+ return(ret)
+ except Exception:
+ print _("Command failed, please check log for more info")
+ raise
diff --git a/nova/cmd/network.py b/nova/cmd/network.py
new file mode 100644
index 000000000..72eac2a19
--- /dev/null
+++ b/nova/cmd/network.py
@@ -0,0 +1,42 @@
+#!/usr/bin/env python
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2010 United States Government as represented by the
+# Administrator of the National Aeronautics and Space Administration.
+# 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
+#
+# Unless required by applicable law or agreed to in writing, software
+# 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.
+
+"""Starter script for Nova Network."""
+
+import sys
+
+from oslo.config import cfg
+
+from nova import config
+from nova.openstack.common import log as logging
+from nova import service
+from nova import utils
+
+CONF = cfg.CONF
+CONF.import_opt('network_topic', 'nova.network.rpcapi')
+
+
+def main():
+ config.parse_args(sys.argv)
+ logging.setup("nova")
+ utils.monkey_patch()
+ server = service.Service.create(binary='nova-network',
+ topic=CONF.network_topic)
+ service.serve(server)
+ service.wait()
diff --git a/nova/cmd/novncproxy.py b/nova/cmd/novncproxy.py
new file mode 100644
index 000000000..cf70b83c5
--- /dev/null
+++ b/nova/cmd/novncproxy.py
@@ -0,0 +1,97 @@
+#!/usr/bin/env python
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright (c) 2012 OpenStack Foundation
+# 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
+#
+# Unless required by applicable law or agreed to in writing, software
+# 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.
+
+"""
+Websocket proxy that is compatible with OpenStack Nova
+noVNC consoles. Leverages websockify.py by Joel Martin
+"""
+
+import os
+import sys
+
+from oslo.config import cfg
+
+from nova import config
+from nova.console import websocketproxy
+
+
+opts = [
+ cfg.BoolOpt('record',
+ default=False,
+ help='Record sessions to FILE.[session_number]'),
+ cfg.BoolOpt('daemon',
+ default=False,
+ help='Become a daemon (background process)'),
+ cfg.BoolOpt('ssl_only',
+ default=False,
+ help='Disallow non-encrypted connections'),
+ cfg.BoolOpt('source_is_ipv6',
+ default=False,
+ help='Source is ipv6'),
+ cfg.StrOpt('cert',
+ default='self.pem',
+ help='SSL certificate file'),
+ cfg.StrOpt('key',
+ default=None,
+ help='SSL key file (if separate from cert)'),
+ cfg.StrOpt('web',
+ default='/usr/share/novnc',
+ help='Run webserver on same port. Serve files from DIR.'),
+ cfg.StrOpt('novncproxy_host',
+ default='0.0.0.0',
+ help='Host on which to listen for incoming requests'),
+ cfg.IntOpt('novncproxy_port',
+ default=6080,
+ help='Port on which to listen for incoming requests'),
+ ]
+
+CONF = cfg.CONF
+CONF.register_cli_opts(opts)
+CONF.import_opt('debug', 'nova.openstack.common.log')
+
+
+def main():
+ # Setup flags
+ config.parse_args(sys.argv)
+
+ if CONF.ssl_only and not os.path.exists(CONF.cert):
+ print "SSL only and %s not found" % CONF.cert
+ return(-1)
+
+ # Check to see if novnc html/js/css files are present
+ if not os.path.exists(CONF.web):
+ print "Can not find novnc html/js/css files at %s." % CONF.web
+ return(-1)
+
+ # Create and start the NovaWebSockets proxy
+ server = websocketproxy.NovaWebSocketProxy(
+ listen_host=CONF.novncproxy_host,
+ listen_port=CONF.novncproxy_port,
+ source_is_ipv6=CONF.source_is_ipv6,
+ verbose=CONF.verbose,
+ cert=CONF.cert,
+ key=CONF.key,
+ ssl_only=CONF.ssl_only,
+ daemon=CONF.daemon,
+ record=CONF.record,
+ web=CONF.web,
+ target_host='ignore',
+ target_port='ignore',
+ wrap_mode='exit',
+ wrap_cmd=None)
+ server.start_server()
diff --git a/nova/cmd/objectstore.py b/nova/cmd/objectstore.py
new file mode 100644
index 000000000..eb8257f9c
--- /dev/null
+++ b/nova/cmd/objectstore.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2010 United States Government as represented by the
+# Administrator of the National Aeronautics and Space Administration.
+# 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
+#
+# Unless required by applicable law or agreed to in writing, software
+# 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.
+
+"""Daemon for nova objectstore. Supports S3 API."""
+
+import sys
+
+from nova import config
+from nova.objectstore import s3server
+from nova.openstack.common import log as logging
+from nova import service
+from nova import utils
+
+
+def main():
+ config.parse_args(sys.argv)
+ logging.setup("nova")
+ utils.monkey_patch()
+ server = s3server.get_wsgi_server()
+ service.serve(server)
+ service.wait()
diff --git a/nova/cmd/rootwrap.py b/nova/cmd/rootwrap.py
new file mode 100644
index 000000000..0b7d0064f
--- /dev/null
+++ b/nova/cmd/rootwrap.py
@@ -0,0 +1,128 @@
+#!/usr/bin/env python
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright (c) 2011 OpenStack Foundation.
+# 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
+#
+# Unless required by applicable law or agreed to in writing, software
+# 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.
+
+"""Root wrapper for OpenStack services
+
+ Filters which commands a service is allowed to run as another user.
+
+ To use this with nova, you should set the following in
+ nova.conf:
+ rootwrap_config=/etc/nova/rootwrap.conf
+
+ You also need to let the nova user run nova-rootwrap
+ as root in sudoers:
+ nova ALL = (root) NOPASSWD: /usr/bin/nova-rootwrap
+ /etc/nova/rootwrap.conf *
+
+ Service packaging should deploy .filters files only on nodes where
+ they are needed, to avoid allowing more than is necessary.
+"""
+
+import ConfigParser
+import logging
+import os
+import pwd
+import signal
+import subprocess
+import sys
+
+
+RC_UNAUTHORIZED = 99
+RC_NOCOMMAND = 98
+RC_BADCONFIG = 97
+RC_NOEXECFOUND = 96
+
+
+def _subprocess_setup():
+ # Python installs a SIGPIPE handler by default. This is usually not what
+ # non-Python subprocesses expect.
+ signal.signal(signal.SIGPIPE, signal.SIG_DFL)
+
+
+def _exit_error(execname, message, errorcode, log=True):
+ print "%s: %s" % (execname, message)
+ if log:
+ logging.error(message)
+ return(errorcode)
+
+
+def main():
+ # Split arguments, require at least a command
+ execname = sys.argv.pop(0)
+ if len(sys.argv) < 2:
+ _exit_error(execname, "No command specified", RC_NOCOMMAND, log=False)
+
+ configfile = sys.argv.pop(0)
+ userargs = sys.argv[:]
+
+ # Add ../ to sys.path to allow running from branch
+ possible_topdir = os.path.normpath(os.path.join(os.path.abspath(execname),
+ os.pardir, os.pardir))
+ if os.path.exists(os.path.join(possible_topdir, "nova", "__init__.py")):
+ sys.path.insert(0, possible_topdir)
+
+ from nova.openstack.common.rootwrap import wrapper
+
+ # Load configuration
+ try:
+ rawconfig = ConfigParser.RawConfigParser()
+ rawconfig.read(configfile)
+ config = wrapper.RootwrapConfig(rawconfig)
+ except ValueError as exc:
+ msg = "Incorrect value in %s: %s" % (configfile, exc.message)
+ _exit_error(execname, msg, RC_BADCONFIG, log=False)
+ except ConfigParser.Error:
+ _exit_error(execname, "Incorrect configuration file: %s" % configfile,
+ RC_BADCONFIG, log=False)
+
+ if config.use_syslog:
+ wrapper.setup_syslog(execname,
+ config.syslog_log_facility,
+ config.syslog_log_level)
+
+ # Execute command if it matches any of the loaded filters
+ filters = wrapper.load_filters(config.filters_path)
+ try:
+ filtermatch = wrapper.match_filter(filters, userargs,
+ exec_dirs=config.exec_dirs)
+ if filtermatch:
+ command = filtermatch.get_command(userargs,
+ exec_dirs=config.exec_dirs)
+ if config.use_syslog:
+ logging.info("(%s > %s) Executing %s (filter match = %s)" % (
+ os.getlogin(), pwd.getpwuid(os.getuid())[0],
+ command, filtermatch.name))
+
+ obj = subprocess.Popen(command,
+ stdin=sys.stdin,
+ stdout=sys.stdout,
+ stderr=sys.stderr,
+ preexec_fn=_subprocess_setup,
+ env=filtermatch.get_environment(userargs))
+ obj.wait()
+ return(obj.returncode)
+
+ except wrapper.FilterMatchNotExecutable as exc:
+ msg = ("Executable not found: %s (filter match = %s)"
+ % (exc.match.exec_path, exc.match.name))
+ _exit_error(execname, msg, RC_NOEXECFOUND, log=config.use_syslog)
+
+ except wrapper.NoFilterMatched:
+ msg = ("Unauthorized command: %s (no filter matched)"
+ % ' '.join(userargs))
+ _exit_error(execname, msg, RC_UNAUTHORIZED, log=config.use_syslog)
diff --git a/nova/cmd/rpc_zmq_receiver.py b/nova/cmd/rpc_zmq_receiver.py
new file mode 100644
index 000000000..a587af689
--- /dev/null
+++ b/nova/cmd/rpc_zmq_receiver.py
@@ -0,0 +1,38 @@
+#!/usr/bin/env python
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2011 OpenStack Foundation
+#
+# 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
+#
+# Unless required by applicable law or agreed to in writing, software
+# 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.
+
+import contextlib
+import sys
+
+from oslo.config import cfg
+
+from nova.openstack.common import log as logging
+from nova.openstack.common import rpc
+from nova.openstack.common.rpc import impl_zmq
+
+CONF = cfg.CONF
+CONF.register_opts(rpc.rpc_opts)
+CONF.register_opts(impl_zmq.zmq_opts)
+
+
+def main():
+ CONF(sys.argv[1:], project='nova')
+ logging.setup("nova")
+
+ with contextlib.closing(impl_zmq.ZmqProxy(CONF)) as reactor:
+ reactor.consume_in_thread()
+ reactor.wait()
diff --git a/nova/cmd/scheduler.py b/nova/cmd/scheduler.py
new file mode 100644
index 000000000..5bf459b97
--- /dev/null
+++ b/nova/cmd/scheduler.py
@@ -0,0 +1,45 @@
+#!/usr/bin/env python
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2010 United States Government as represented by the
+# Administrator of the National Aeronautics and Space Administration.
+# 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
+#
+# Unless required by applicable law or agreed to in writing, software
+# 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.
+
+"""Starter script for Nova Scheduler."""
+
+import gettext
+import sys
+
+from oslo.config import cfg
+
+gettext.install('nova', unicode=1)
+
+from nova import config
+from nova.openstack.common import log as logging
+from nova import service
+from nova import utils
+
+CONF = cfg.CONF
+CONF.import_opt('scheduler_topic', 'nova.scheduler.rpcapi')
+
+
+def main():
+ config.parse_args(sys.argv)
+ logging.setup("nova")
+ utils.monkey_patch()
+ server = service.Service.create(binary='nova-scheduler',
+ topic=CONF.scheduler_topic)
+ service.serve(server)
+ service.wait()
diff --git a/nova/cmd/spicehtml5proxy.py b/nova/cmd/spicehtml5proxy.py
new file mode 100644
index 000000000..190e7e77a
--- /dev/null
+++ b/nova/cmd/spicehtml5proxy.py
@@ -0,0 +1,97 @@
+#!/usr/bin/env python
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright (c) 2012 OpenStack Foundation
+# 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
+#
+# Unless required by applicable law or agreed to in writing, software
+# 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.
+
+"""
+Websocket proxy that is compatible with OpenStack Nova
+SPICE HTML5 consoles. Leverages websockify.py by Joel Martin
+"""
+
+import os
+import sys
+
+from oslo.config import cfg
+
+from nova import config
+from nova.console import websocketproxy
+
+
+opts = [
+ cfg.BoolOpt('record',
+ default=False,
+ help='Record sessions to FILE.[session_number]'),
+ cfg.BoolOpt('daemon',
+ default=False,
+ help='Become a daemon (background process)'),
+ cfg.BoolOpt('ssl_only',
+ default=False,
+ help='Disallow non-encrypted connections'),
+ cfg.BoolOpt('source_is_ipv6',
+ default=False,
+ help='Source is ipv6'),
+ cfg.StrOpt('cert',
+ default='self.pem',
+ help='SSL certificate file'),
+ cfg.StrOpt('key',
+ default=None,
+ help='SSL key file (if separate from cert)'),
+ cfg.StrOpt('web',
+ default='/usr/share/spice-html5',
+ help='Run webserver on same port. Serve files from DIR.'),
+ cfg.StrOpt('spicehtml5proxy_host',
+ default='0.0.0.0',
+ help='Host on which to listen for incoming requests'),
+ cfg.IntOpt('spicehtml5proxy_port',
+ default=6082,
+ help='Port on which to listen for incoming requests'),
+ ]
+
+CONF = cfg.CONF
+CONF.register_cli_opts(opts)
+CONF.import_opt('debug', 'nova.openstack.common.log')
+
+
+def main():
+ # Setup flags
+ config.parse_args(sys.argv)
+
+ if CONF.ssl_only and not os.path.exists(CONF.cert):
+ print "SSL only and %s not found." % CONF.cert
+ return(-1)
+
+ # Check to see if spice html/js/css files are present
+ if not os.path.exists(CONF.web):
+ print "Can not find spice html/js/css files at %s." % CONF.web
+ return(-1)
+
+ # Create and start the NovaWebSockets proxy
+ server = websocketproxy.NovaWebSocketProxy(
+ listen_host=CONF.spicehtml5proxy_host,
+ listen_port=CONF.spicehtml5proxy_port,
+ source_is_ipv6=CONF.source_is_ipv6,
+ verbose=CONF.verbose,
+ cert=CONF.cert,
+ key=CONF.key,
+ ssl_only=CONF.ssl_only,
+ daemon=CONF.daemon,
+ record=CONF.record,
+ web=CONF.web,
+ target_host='ignore',
+ target_port='ignore',
+ wrap_mode='exit',
+ wrap_cmd=None)
+ server.start_server()
diff --git a/nova/cmd/xvpvncproxy.py b/nova/cmd/xvpvncproxy.py
new file mode 100644
index 000000000..0f62e2083
--- /dev/null
+++ b/nova/cmd/xvpvncproxy.py
@@ -0,0 +1,35 @@
+#!/usr/bin/env python
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright (c) 2010 OpenStack Foundation
+# 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
+#
+# Unless required by applicable law or agreed to in writing, software
+# 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.
+
+"""XVP VNC Console Proxy Server."""
+
+import sys
+
+from nova import config
+from nova.openstack.common import log as logging
+from nova import service
+from nova.vnc import xvp_proxy
+
+
+def main():
+ config.parse_args(sys.argv)
+ logging.setup("nova")
+
+ wsgi_server = xvp_proxy.get_wsgi_server()
+ service.serve(wsgi_server)
+ service.wait()