~ubuntu-branches/ubuntu/quantal/nova/quantal-proposed

« back to all changes in this revision

Viewing changes to nova/console/manager.py

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2011-01-21 11:48:06 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: james.westby@ubuntu.com-20110121114806-v8fvnnl6az4m4ohv
Tags: upstream-2011.1~bzr597
ImportĀ upstreamĀ versionĀ 2011.1~bzr597

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright (c) 2010 Openstack, LLC.
 
4
# All Rights Reserved.
 
5
#
 
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
 
9
#
 
10
#         http://www.apache.org/licenses/LICENSE-2.0
 
11
#
 
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
 
16
#    under the License.
 
17
 
 
18
"""
 
19
Console Proxy Service
 
20
"""
 
21
 
 
22
import functools
 
23
import logging
 
24
import socket
 
25
 
 
26
from nova import exception
 
27
from nova import flags
 
28
from nova import manager
 
29
from nova import rpc
 
30
from nova import utils
 
31
 
 
32
FLAGS = flags.FLAGS
 
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',
 
39
                    socket.gethostname(),
 
40
                    'Publicly visable name for this console host')
 
41
 
 
42
 
 
43
class ConsoleProxyManager(manager.Manager):
 
44
 
 
45
    """ Sets up and tears down any proxy connections needed for accessing
 
46
        instance consoles securely"""
 
47
 
 
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
 
54
 
 
55
    def init_host(self):
 
56
        self.driver.init_host()
 
57
 
 
58
    @exception.wrap_exception
 
59
    def add_console(self, context, instance_id, password=None,
 
60
                    port=None, **kwargs):
 
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)
 
65
        try:
 
66
            console = self.db.console_get_by_pool_instance(context,
 
67
                                                      pool['id'],
 
68
                                                      instance_id)
 
69
        except exception.NotFound:
 
70
            logging.debug(_("Adding console"))
 
71
            if not password:
 
72
                password = self.driver.generate_password()
 
73
            if not port:
 
74
                port = self.driver.get_port(context)
 
75
            console_data = {'instance_name': name,
 
76
                            'instance_id': instance_id,
 
77
                            'password': password,
 
78
                            'pool_id': pool['id']}
 
79
            if port:
 
80
                console_data['port'] = port
 
81
            console = self.db.console_create(context, console_data)
 
82
            self.driver.setup_console(context, console)
 
83
        return console['id']
 
84
 
 
85
    @exception.wrap_exception
 
86
    def remove_console(self, context, console_id, **_kwargs):
 
87
        try:
 
88
            console = self.db.console_get(context, console_id)
 
89
        except exception.NotFound:
 
90
            logging.debug(_('Tried to remove non-existant console '
 
91
                            '%(console_id)s.') %
 
92
                            {'console_id': console_id})
 
93
            return
 
94
        self.db.console_delete(context, console_id)
 
95
        self.driver.teardown_console(context, console)
 
96
 
 
97
    def get_pool_for_instance_host(self, context, instance_host):
 
98
        context = context.elevated()
 
99
        console_type = self.driver.console_type
 
100
        try:
 
101
            pool = self.db.console_pool_get_by_host_type(context,
 
102
                                                         instance_host,
 
103
                                                         self.host,
 
104
                                                         console_type)
 
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',
 
111
                             'username': 'test',
 
112
                             'password': '1234pass'}
 
113
            else:
 
114
                pool_info = rpc.call(context,
 
115
                                 self.db.queue_get_for(context,
 
116
                                                   FLAGS.compute_topic,
 
117
                                                   instance_host),
 
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)
 
127
        return pool