1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright (c) 2010 Openstack, LLC.
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may
7
# not use this file except in compliance with the License. You may obtain
8
# a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
# License for the specific language governing permissions and limitations
26
from nova import exception
27
from nova import flags
28
from nova import manager
30
from nova import utils
33
flags.DEFINE_string('console_driver',
34
'nova.console.xvp.XVPConsoleProxy',
35
'Driver to use for the console proxy')
36
flags.DEFINE_boolean('stub_compute', False,
37
'Stub calls to compute worker for tests')
38
flags.DEFINE_string('console_public_hostname',
40
'Publicly visable name for this console host')
43
class ConsoleProxyManager(manager.Manager):
45
""" Sets up and tears down any proxy connections needed for accessing
46
instance consoles securely"""
48
def __init__(self, console_driver=None, *args, **kwargs):
49
if not console_driver:
50
console_driver = FLAGS.console_driver
51
self.driver = utils.import_object(console_driver)
52
super(ConsoleProxyManager, self).__init__(*args, **kwargs)
53
self.driver.host = self.host
56
self.driver.init_host()
58
@exception.wrap_exception
59
def add_console(self, context, instance_id, password=None,
61
instance = self.db.instance_get(context, instance_id)
62
host = instance['host']
63
name = instance['name']
64
pool = self.get_pool_for_instance_host(context, host)
66
console = self.db.console_get_by_pool_instance(context,
69
except exception.NotFound:
70
logging.debug(_("Adding console"))
72
password = self.driver.generate_password()
74
port = self.driver.get_port(context)
75
console_data = {'instance_name': name,
76
'instance_id': instance_id,
78
'pool_id': pool['id']}
80
console_data['port'] = port
81
console = self.db.console_create(context, console_data)
82
self.driver.setup_console(context, console)
85
@exception.wrap_exception
86
def remove_console(self, context, console_id, **_kwargs):
88
console = self.db.console_get(context, console_id)
89
except exception.NotFound:
90
logging.debug(_('Tried to remove non-existant console '
92
{'console_id': console_id})
94
self.db.console_delete(context, console_id)
95
self.driver.teardown_console(context, console)
97
def get_pool_for_instance_host(self, context, instance_host):
98
context = context.elevated()
99
console_type = self.driver.console_type
101
pool = self.db.console_pool_get_by_host_type(context,
105
except exception.NotFound:
106
#NOTE(mdragon): Right now, the only place this info exists is the
107
# compute worker's flagfile, at least for
108
# xenserver. Thus we ned to ask.
109
if FLAGS.stub_compute:
110
pool_info = {'address': '127.0.0.1',
112
'password': '1234pass'}
114
pool_info = rpc.call(context,
115
self.db.queue_get_for(context,
118
{"method": "get_console_pool_info",
119
"args": {"console_type": console_type}})
120
pool_info['password'] = self.driver.fix_pool_password(
121
pool_info['password'])
122
pool_info['host'] = self.host
123
pool_info['public_hostname'] = FLAGS.console_public_hostname
124
pool_info['console_type'] = self.driver.console_type
125
pool_info['compute_host'] = instance_host
126
pool = self.db.console_pool_create(context, pool_info)