~ubuntu-branches/ubuntu/raring/nova/raring-proposed

1.1.14 by Chuck Short
Import upstream version 2011.2~bzr925
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3
# Copyright (c) 2011 Citrix Systems, Inc.
4
# Copyright 2011 OpenStack LLC.
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
1.1.17 by Soren Hansen
Import upstream version 2011.3~bzr1108
18
"""VMRC Console Manager."""
1.1.14 by Chuck Short
Import upstream version 2011.2~bzr925
19
1.1.57 by Chuck Short
Import upstream version 2012.2~f2~20120621.14517
20
from nova.compute import rpcapi as compute_rpcapi
1.1.14 by Chuck Short
Import upstream version 2011.2~bzr925
21
from nova import exception
22
from nova import manager
1.1.44 by Chuck Short
Import upstream version 2012.1~e4~20120210.12574
23
from nova.openstack.common import cfg
1.1.55 by Chuck Short
Import upstream version 2012.2~f1
24
from nova.openstack.common import importutils
1.1.58 by Chuck Short
Import upstream version 2012.2~f2
25
from nova.openstack.common import log as logging
1.1.59 by Chuck Short
Import upstream version 2012.2~f3
26
from nova.virt.vmwareapi import driver as vmwareapi_conn
1.1.17 by Soren Hansen
Import upstream version 2011.3~bzr1108
27
1.1.14 by Chuck Short
Import upstream version 2011.2~bzr925
28
1.1.45 by Chuck Short
Import upstream version 2012.1~e4~20120217.12709
29
LOG = logging.getLogger(__name__)
1.1.14 by Chuck Short
Import upstream version 2011.2~bzr925
30
1.1.66 by Chuck Short
Import upstream version 2013.1~g1
31
CONF = cfg.CONF
32
CONF.import_opt('console_driver', 'nova.console.manager')
33
CONF.import_opt('console_public_hostname', 'nova.console.manager')
1.1.14 by Chuck Short
Import upstream version 2011.2~bzr925
34
35
36
class ConsoleVMRCManager(manager.Manager):
1.1.17 by Soren Hansen
Import upstream version 2011.3~bzr1108
37
    """Manager to handle VMRC connections for accessing instance consoles."""
1.1.14 by Chuck Short
Import upstream version 2011.2~bzr925
38
39
    def __init__(self, console_driver=None, *args, **kwargs):
1.1.66 by Chuck Short
Import upstream version 2013.1~g1
40
        self.driver = importutils.import_object(CONF.console_driver)
1.1.57 by Chuck Short
Import upstream version 2012.2~f2~20120621.14517
41
        self.compute_rpcapi = compute_rpcapi.ComputeAPI()
1.1.14 by Chuck Short
Import upstream version 2011.2~bzr925
42
        super(ConsoleVMRCManager, self).__init__(*args, **kwargs)
43
44
    def init_host(self):
45
        self.sessions = {}
46
        self.driver.init_host()
47
48
    def _get_vim_session(self, pool):
49
        """Get VIM session for the pool specified."""
50
        vim_session = None
51
        if pool['id'] not in self.sessions.keys():
1.1.17 by Soren Hansen
Import upstream version 2011.3~bzr1108
52
            vim_session = vmwareapi_conn.VMWareAPISession(
53
                    pool['address'],
54
                    pool['username'],
55
                    pool['password'],
1.1.66 by Chuck Short
Import upstream version 2013.1~g1
56
                    CONF.console_vmrc_error_retries)
1.1.14 by Chuck Short
Import upstream version 2011.2~bzr925
57
            self.sessions[pool['id']] = vim_session
58
        return self.sessions[pool['id']]
59
60
    def _generate_console(self, context, pool, name, instance_id, instance):
61
        """Sets up console for the instance."""
1.1.17 by Soren Hansen
Import upstream version 2011.3~bzr1108
62
        LOG.debug(_('Adding console'))
1.1.14 by Chuck Short
Import upstream version 2011.2~bzr925
63
64
        password = self.driver.generate_password(
65
                        self._get_vim_session(pool),
66
                        pool,
67
                        instance.name)
68
69
        console_data = {'instance_name': name,
70
                        'instance_id': instance_id,
71
                        'password': password,
72
                        'pool_id': pool['id']}
73
        console_data['port'] = self.driver.get_port(context)
74
        console = self.db.console_create(context, console_data)
75
        self.driver.setup_console(context, console)
76
        return console
77
1.1.23 by Chuck Short
Import upstream version 2011.3~d3~20110715.1279
78
    @exception.wrap_exception()
1.1.14 by Chuck Short
Import upstream version 2011.2~bzr925
79
    def add_console(self, context, instance_id, password=None,
80
                    port=None, **kwargs):
1.1.17 by Soren Hansen
Import upstream version 2011.3~bzr1108
81
        """Adds a console for the instance.
82
83
        If it is one time password, then we generate new console credentials.
84
1.1.14 by Chuck Short
Import upstream version 2011.2~bzr925
85
        """
86
        instance = self.db.instance_get(context, instance_id)
87
        host = instance['host']
88
        name = instance['name']
89
        pool = self.get_pool_for_instance_host(context, host)
90
        try:
91
            console = self.db.console_get_by_pool_instance(context,
92
                                                      pool['id'],
1.1.57 by Chuck Short
Import upstream version 2012.2~f2~20120621.14517
93
                                                      instance['uuid'])
1.1.14 by Chuck Short
Import upstream version 2011.2~bzr925
94
            if self.driver.is_otp():
1.1.17 by Soren Hansen
Import upstream version 2011.3~bzr1108
95
                console = self._generate_console(context,
96
                                                 pool,
97
                                                 name,
98
                                                 instance_id,
99
                                                 instance)
1.1.14 by Chuck Short
Import upstream version 2011.2~bzr925
100
        except exception.NotFound:
1.1.17 by Soren Hansen
Import upstream version 2011.3~bzr1108
101
            console = self._generate_console(context,
102
                                             pool,
103
                                             name,
104
                                             instance_id,
105
                                             instance)
1.1.14 by Chuck Short
Import upstream version 2011.2~bzr925
106
        return console['id']
107
1.1.23 by Chuck Short
Import upstream version 2011.3~d3~20110715.1279
108
    @exception.wrap_exception()
1.1.14 by Chuck Short
Import upstream version 2011.2~bzr925
109
    def remove_console(self, context, console_id, **_kwargs):
110
        """Removes a console entry."""
111
        try:
112
            console = self.db.console_get(context, console_id)
113
        except exception.NotFound:
1.1.17 by Soren Hansen
Import upstream version 2011.3~bzr1108
114
            LOG.debug(_('Tried to remove non-existent console '
115
                        '%(console_id)s.') % {'console_id': console_id})
1.1.14 by Chuck Short
Import upstream version 2011.2~bzr925
116
            return
1.1.17 by Soren Hansen
Import upstream version 2011.3~bzr1108
117
        LOG.debug(_('Removing console '
118
                    '%(console_id)s.') % {'console_id': console_id})
1.1.14 by Chuck Short
Import upstream version 2011.2~bzr925
119
        self.db.console_delete(context, console_id)
120
        self.driver.teardown_console(context, console)
121
122
    def get_pool_for_instance_host(self, context, instance_host):
123
        """Gets console pool info for the instance."""
124
        context = context.elevated()
125
        console_type = self.driver.console_type
126
        try:
127
            pool = self.db.console_pool_get_by_host_type(context,
128
                                                         instance_host,
129
                                                         self.host,
130
                                                         console_type)
131
        except exception.NotFound:
1.1.57 by Chuck Short
Import upstream version 2012.2~f2~20120621.14517
132
            pool_info = self.compute_rpcapi.get_console_pool_info(context,
133
                    console_type, instance_host)
1.1.14 by Chuck Short
Import upstream version 2011.2~bzr925
134
            pool_info['password'] = self.driver.fix_pool_password(
135
                                                    pool_info['password'])
136
            pool_info['host'] = self.host
137
            # ESX Address or Proxy Address
138
            public_host_name = pool_info['address']
1.1.66 by Chuck Short
Import upstream version 2013.1~g1
139
            if CONF.console_public_hostname:
140
                public_host_name = CONF.console_public_hostname
1.1.14 by Chuck Short
Import upstream version 2011.2~bzr925
141
            pool_info['public_hostname'] = public_host_name
142
            pool_info['console_type'] = console_type
143
            pool_info['compute_host'] = instance_host
144
            pool = self.db.console_pool_create(context, pool_info)
145
        return pool