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

« back to all changes in this revision

Viewing changes to nova/virt/xenapi/agent.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-08-16 14:04:11 UTC
  • mto: This revision was merged to the branch mainline in revision 84.
  • Revision ID: package-import@ubuntu.com-20120816140411-0mr4n241wmk30t9l
Tags: upstream-2012.2~f3
ImportĀ upstreamĀ versionĀ 2012.2~f3

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 Citrix Systems, Inc.
 
4
# Copyright 2010-2012 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
 
 
18
import base64
 
19
import binascii
 
20
import os
 
21
import time
 
22
import uuid
 
23
 
 
24
from nova import flags
 
25
from nova.openstack.common import cfg
 
26
from nova.openstack.common import jsonutils
 
27
from nova.openstack.common import log as logging
 
28
from nova import utils
 
29
 
 
30
 
 
31
LOG = logging.getLogger(__name__)
 
32
 
 
33
xenapi_agent_opts = [
 
34
    cfg.IntOpt('agent_version_timeout',
 
35
               default=300,
 
36
               help='number of seconds to wait for agent '
 
37
                    'to be fully operational'),
 
38
]
 
39
 
 
40
FLAGS = flags.FLAGS
 
41
FLAGS.register_opts(xenapi_agent_opts)
 
42
 
 
43
 
 
44
def _call_agent(session, instance, vm_ref, method, addl_args=None):
 
45
    """Abstracts out the interaction with the agent xenapi plugin."""
 
46
    if addl_args is None:
 
47
        addl_args = {}
 
48
 
 
49
    vm_rec = session.call_xenapi("VM.get_record", vm_ref)
 
50
 
 
51
    args = {
 
52
        'id': str(uuid.uuid4()),
 
53
        'dom_id': vm_rec['domid'],
 
54
    }
 
55
    args.update(addl_args)
 
56
 
 
57
    try:
 
58
        ret = session.call_plugin('agent', method, args)
 
59
    except session.XenAPI.Failure, e:
 
60
        err_msg = e.details[-1].splitlines()[-1]
 
61
        if 'TIMEOUT:' in err_msg:
 
62
            LOG.error(_('TIMEOUT: The call to %(method)s timed out. '
 
63
                        'args=%(args)r'), locals(), instance=instance)
 
64
            return {'returncode': 'timeout', 'message': err_msg}
 
65
        elif 'NOT IMPLEMENTED:' in err_msg:
 
66
            LOG.error(_('NOT IMPLEMENTED: The call to %(method)s is not'
 
67
                        ' supported by the agent. args=%(args)r'),
 
68
                      locals(), instance=instance)
 
69
            return {'returncode': 'notimplemented', 'message': err_msg}
 
70
        else:
 
71
            LOG.error(_('The call to %(method)s returned an error: %(e)s. '
 
72
                        'args=%(args)r'), locals(), instance=instance)
 
73
            return {'returncode': 'error', 'message': err_msg}
 
74
        return None
 
75
 
 
76
    if isinstance(ret, dict):
 
77
        return ret
 
78
    try:
 
79
        return jsonutils.loads(ret)
 
80
    except TypeError:
 
81
        LOG.error(_('The agent call to %(method)s returned an invalid'
 
82
                    ' response: %(ret)r. path=%(path)s; args=%(args)r'),
 
83
                  locals(), instance=instance)
 
84
        return {'returncode': 'error',
 
85
                'message': 'unable to deserialize response'}
 
86
 
 
87
 
 
88
def _get_agent_version(session, instance, vm_ref):
 
89
    resp = _call_agent(session, instance, vm_ref, 'version')
 
90
    if resp['returncode'] != '0':
 
91
        LOG.error(_('Failed to query agent version: %(resp)r'),
 
92
                  locals(), instance=instance)
 
93
        return None
 
94
 
 
95
    # Some old versions of the Windows agent have a trailing \\r\\n
 
96
    # (ie CRLF escaped) for some reason. Strip that off.
 
97
    return resp['message'].replace('\\r\\n', '')
 
98
 
 
99
 
 
100
def get_agent_version(session, instance, vm_ref):
 
101
    """Get the version of the agent running on the VM instance."""
 
102
 
 
103
    LOG.debug(_('Querying agent version'), instance=instance)
 
104
 
 
105
    # The agent can be slow to start for a variety of reasons. On Windows,
 
106
    # it will generally perform a setup process on first boot that can
 
107
    # take a couple of minutes and then reboot. On Linux, the system can
 
108
    # also take a while to boot. So we need to be more patient than
 
109
    # normal as well as watch for domid changes
 
110
 
 
111
    expiration = time.time() + FLAGS.agent_version_timeout
 
112
    while time.time() < expiration:
 
113
        ret = _get_agent_version(session, instance, vm_ref)
 
114
        if ret:
 
115
            return ret
 
116
 
 
117
    LOG.info(_('Reached maximum time attempting to query agent version'),
 
118
             instance=instance)
 
119
 
 
120
    return None
 
121
 
 
122
 
 
123
def agent_update(session, instance, vm_ref, agent_build):
 
124
    """Update agent on the VM instance."""
 
125
 
 
126
    LOG.info(_('Updating agent to %s'), agent_build['version'],
 
127
             instance=instance)
 
128
 
 
129
    # Send the encrypted password
 
130
    args = {'url': agent_build['url'], 'md5sum': agent_build['md5hash']}
 
131
    resp = _call_agent(session, instance, vm_ref, 'agentupdate', args)
 
132
    if resp['returncode'] != '0':
 
133
        LOG.error(_('Failed to update agent: %(resp)r'), locals(),
 
134
                  instance=instance)
 
135
        return None
 
136
    return resp['message']
 
137
 
 
138
 
 
139
def set_admin_password(session, instance, vm_ref, new_pass):
 
140
    """Set the root/admin password on the VM instance.
 
141
 
 
142
    This is done via an agent running on the VM. Communication between nova
 
143
    and the agent is done via writing xenstore records. Since communication
 
144
    is done over the XenAPI RPC calls, we need to encrypt the password.
 
145
    We're using a simple Diffie-Hellman class instead of a more advanced
 
146
    library (such as M2Crypto) for compatibility with the agent code.
 
147
    """
 
148
    LOG.debug(_('Setting admin password'), instance=instance)
 
149
 
 
150
    dh = SimpleDH()
 
151
 
 
152
    # Exchange keys
 
153
    args = {'pub': str(dh.get_public())}
 
154
    resp = _call_agent(session, instance, vm_ref, 'key_init', args)
 
155
 
 
156
    # Successful return code from key_init is 'D0'
 
157
    if resp['returncode'] != 'D0':
 
158
        msg = _('Failed to exchange keys: %(resp)r') % locals()
 
159
        LOG.error(msg, instance=instance)
 
160
        raise Exception(msg)
 
161
 
 
162
    # Some old versions of the Windows agent have a trailing \\r\\n
 
163
    # (ie CRLF escaped) for some reason. Strip that off.
 
164
    agent_pub = int(resp['message'].replace('\\r\\n', ''))
 
165
    dh.compute_shared(agent_pub)
 
166
 
 
167
    # Some old versions of Linux and Windows agent expect trailing \n
 
168
    # on password to work correctly.
 
169
    enc_pass = dh.encrypt(new_pass + '\n')
 
170
 
 
171
    # Send the encrypted password
 
172
    args = {'enc_pass': enc_pass}
 
173
    resp = _call_agent(session, instance, vm_ref, 'password', args)
 
174
 
 
175
    # Successful return code from password is '0'
 
176
    if resp['returncode'] != '0':
 
177
        msg = _('Failed to update password: %(resp)r') % locals()
 
178
        LOG.error(msg, instance=instance)
 
179
        raise Exception(msg)
 
180
 
 
181
    return resp['message']
 
182
 
 
183
 
 
184
def inject_file(session, instance, vm_ref, path, contents):
 
185
    LOG.debug(_('Injecting file path: %r'), path, instance=instance)
 
186
 
 
187
    # Files/paths must be base64-encoded for transmission to agent
 
188
    b64_path = base64.b64encode(path)
 
189
    b64_contents = base64.b64encode(contents)
 
190
 
 
191
    args = {'b64_path': b64_path, 'b64_contents': b64_contents}
 
192
 
 
193
    # If the agent doesn't support file injection, a NotImplementedError
 
194
    # will be raised with the appropriate message.
 
195
    resp = _call_agent(session, instance, vm_ref, 'inject_file', args)
 
196
    if resp['returncode'] != '0':
 
197
        LOG.error(_('Failed to inject file: %(resp)r'), locals(),
 
198
                  instance=instance)
 
199
        return None
 
200
 
 
201
    return resp['message']
 
202
 
 
203
 
 
204
def resetnetwork(session, instance, vm_ref):
 
205
    LOG.debug(_('Resetting network'), instance=instance)
 
206
 
 
207
    resp = _call_agent(session, instance, vm_ref, 'resetnetwork')
 
208
    if resp['returncode'] != '0':
 
209
        LOG.error(_('Failed to reset network: %(resp)r'), locals(),
 
210
                  instance=instance)
 
211
        return None
 
212
 
 
213
    return resp['message']
 
214
 
 
215
 
 
216
class SimpleDH(object):
 
217
    """
 
218
    This class wraps all the functionality needed to implement
 
219
    basic Diffie-Hellman-Merkle key exchange in Python. It features
 
220
    intelligent defaults for the prime and base numbers needed for the
 
221
    calculation, while allowing you to supply your own. It requires that
 
222
    the openssl binary be installed on the system on which this is run,
 
223
    as it uses that to handle the encryption and decryption. If openssl
 
224
    is not available, a RuntimeError will be raised.
 
225
    """
 
226
    def __init__(self):
 
227
        self._prime = 162259276829213363391578010288127
 
228
        self._base = 5
 
229
        self._public = None
 
230
        self._shared = None
 
231
        self.generate_private()
 
232
 
 
233
    def generate_private(self):
 
234
        self._private = int(binascii.hexlify(os.urandom(10)), 16)
 
235
        return self._private
 
236
 
 
237
    def get_public(self):
 
238
        self._public = self.mod_exp(self._base, self._private, self._prime)
 
239
        return self._public
 
240
 
 
241
    def compute_shared(self, other):
 
242
        self._shared = self.mod_exp(other, self._private, self._prime)
 
243
        return self._shared
 
244
 
 
245
    @staticmethod
 
246
    def mod_exp(num, exp, mod):
 
247
        """Efficient implementation of (num ** exp) % mod"""
 
248
        result = 1
 
249
        while exp > 0:
 
250
            if (exp & 1) == 1:
 
251
                result = (result * num) % mod
 
252
            exp = exp >> 1
 
253
            num = (num * num) % mod
 
254
        return result
 
255
 
 
256
    def _run_ssl(self, text, decrypt=False):
 
257
        cmd = ['openssl', 'aes-128-cbc', '-A', '-a', '-pass',
 
258
               'pass:%s' % self._shared, '-nosalt']
 
259
        if decrypt:
 
260
            cmd.append('-d')
 
261
        out, err = utils.execute(*cmd, process_input=text)
 
262
        if err:
 
263
            raise RuntimeError(_('OpenSSL error: %s') % err)
 
264
        return out
 
265
 
 
266
    def encrypt(self, text):
 
267
        return self._run_ssl(text).strip('\n')
 
268
 
 
269
    def decrypt(self, text):
 
270
        return self._run_ssl(text, decrypt=True)