127
128
info = self.driver.get_info(instance_ref['name'])
128
129
state = info['state']
129
130
except exception.NotFound:
130
state = power_state.NOSTATE
131
state = power_state.FAILED
131
132
self.db.instance_set_state(context, instance_id, state)
133
def get_console_topic(self, context, **_kwargs):
134
def get_console_topic(self, context, **kwargs):
134
135
"""Retrieves the console host for a project on this host
135
136
Currently this is just set in the flags for each compute
139
140
FLAGS.console_topic,
140
141
FLAGS.console_host)
142
def get_network_topic(self, context, **_kwargs):
143
def get_network_topic(self, context, **kwargs):
143
144
"""Retrieves the network host for a project on this host"""
144
145
# TODO(vish): This method should be memoized. This will make
145
146
# the call to get_network_host cheaper, so that
159
160
@exception.wrap_exception
160
161
def refresh_security_group_rules(self, context,
161
security_group_id, **_kwargs):
162
security_group_id, **kwargs):
162
163
"""This call passes straight through to the virtualization driver."""
163
164
return self.driver.refresh_security_group_rules(security_group_id)
165
166
@exception.wrap_exception
166
167
def refresh_security_group_members(self, context,
167
security_group_id, **_kwargs):
168
security_group_id, **kwargs):
168
169
"""This call passes straight through to the virtualization driver."""
169
170
return self.driver.refresh_security_group_members(security_group_id)
171
172
@exception.wrap_exception
172
def run_instance(self, context, instance_id, **_kwargs):
173
def run_instance(self, context, instance_id, **kwargs):
173
174
"""Launch a new instance with specified options."""
174
175
context = context.elevated()
175
176
instance_ref = self.db.instance_get(context, instance_id)
177
instance_ref.onset_files = kwargs.get('onset_files', [])
176
178
if instance_ref['name'] in self.driver.list_instances():
177
179
raise exception.Error(_("Instance has already been created"))
178
180
LOG.audit(_("instance %s: starting..."), instance_id,
323
325
"""Set the root/admin password for an instance on this server."""
324
326
context = context.elevated()
325
327
instance_ref = self.db.instance_get(context, instance_id)
326
if instance_ref['state'] != power_state.RUNNING:
327
logging.warn('trying to reset the password on a non-running '
328
'instance: %s (state: %s expected: %s)',
330
instance_ref['state'],
333
logging.debug('instance %s: setting admin password',
328
instance_id = instance_ref['id']
329
instance_state = instance_ref['state']
330
expected_state = power_state.RUNNING
331
if instance_state != expected_state:
332
LOG.warn(_('trying to reset the password on a non-running '
333
'instance: %(instance_id)s (state: %(instance_state)s '
334
'expected: %(expected_state)s)') % locals())
335
LOG.audit(_('instance %s: setting admin password'),
334
336
instance_ref['name'])
335
337
if new_pass is None:
336
338
# Generate a random password
337
new_pass = self._generate_password(FLAGS.password_length)
339
new_pass = utils.generate_password(FLAGS.password_length)
339
340
self.driver.set_admin_password(instance_ref, new_pass)
340
341
self._update_state(context, instance_id)
342
def _generate_password(self, length=20):
343
"""Generate a random sequence of letters and digits
344
to be used as a password.
346
chrs = string.letters + string.digits
347
return "".join([random.choice(chrs) for i in xrange(length)])
343
@exception.wrap_exception
344
@checks_instance_lock
345
def inject_file(self, context, instance_id, path, file_contents):
346
"""Write a file to the specified path on an instance on this server"""
347
context = context.elevated()
348
instance_ref = self.db.instance_get(context, instance_id)
349
instance_id = instance_ref['id']
350
instance_state = instance_ref['state']
351
expected_state = power_state.RUNNING
352
if instance_state != expected_state:
353
LOG.warn(_('trying to inject a file into a non-running '
354
'instance: %(instance_id)s (state: %(instance_state)s '
355
'expected: %(expected_state)s)') % locals())
356
# Files/paths *should* be base64-encoded at this point, but
357
# double-check to make sure.
358
b64_path = utils.ensure_b64_encoding(path)
359
b64_contents = utils.ensure_b64_encoding(file_contents)
360
plain_path = base64.b64decode(b64_path)
361
nm = instance_ref['name']
362
msg = _('instance %(nm)s: injecting file to %(plain_path)s') % locals()
364
self.driver.inject_file(instance_ref, b64_path, b64_contents)
349
366
@exception.wrap_exception
350
367
@checks_instance_lock
498
515
instance_ref = self.db.instance_get(context, instance_id)
499
516
return instance_ref['locked']
518
@checks_instance_lock
519
def reset_network(self, context, instance_id):
521
Reset networking on the instance.
524
context = context.elevated()
525
instance_ref = self.db.instance_get(context, instance_id)
526
LOG.debug(_('instance %s: reset network'), instance_id,
528
self.driver.reset_network(instance_ref)
501
530
@exception.wrap_exception
502
531
def get_console_output(self, context, instance_id):
503
532
"""Send the console output for an instance."""
511
540
def get_ajax_console(self, context, instance_id):
512
541
"""Return connection information for an ajax console"""
513
542
context = context.elevated()
514
logging.debug(_("instance %s: getting ajax console"), instance_id)
543
LOG.debug(_("instance %s: getting ajax console"), instance_id)
515
544
instance_ref = self.db.instance_get(context, instance_id)
517
546
return self.driver.get_ajax_console(instance_ref)