93
93
context = context.elevated()
94
94
instance_ref = self.db.instance_get(context, instance_id)
95
95
if instance_ref['name'] in self.driver.list_instances():
96
raise exception.Error("Instance has already been created")
97
logging.debug("instance %s: starting...", instance_id)
96
raise exception.Error(_("Instance has already been created"))
97
logging.debug(_("instance %s: starting..."), instance_id)
98
98
self.network_manager.setup_compute_network(context, instance_id)
99
99
self.db.instance_update(context,
114
114
{'launched_at': now})
115
115
except Exception: # pylint: disable-msg=W0702
116
logging.exception("instance %s: Failed to spawn",
116
logging.exception(_("instance %s: Failed to spawn"),
117
117
instance_ref['name'])
118
118
self.db.instance_set_state(context,
125
125
def terminate_instance(self, context, instance_id):
126
126
"""Terminate an instance on this machine."""
127
127
context = context.elevated()
128
logging.debug("instance %s: terminating", instance_id)
128
logging.debug(_("instance %s: terminating"), instance_id)
130
130
instance_ref = self.db.instance_get(context, instance_id)
131
131
volumes = instance_ref.get('volumes', []) or []
133
133
self.detach_volume(context, instance_id, volume['id'])
134
134
if instance_ref['state'] == power_state.SHUTOFF:
135
135
self.db.instance_destroy(context, instance_id)
136
raise exception.Error('trying to destroy already destroyed'
137
' instance: %s' % instance_id)
136
raise exception.Error(_('trying to destroy already destroyed'
137
' instance: %s') % instance_id)
138
138
self.driver.destroy(instance_ref)
140
140
# TODO(ja): should we keep it in a terminated state for a bit?
148
148
self._update_state(context, instance_id)
150
150
if instance_ref['state'] != power_state.RUNNING:
151
logging.warn('trying to reboot a non-running '
152
'instance: %s (state: %s excepted: %s)',
151
logging.warn(_('trying to reboot a non-running '
152
'instance: %s (state: %s excepted: %s)'),
153
153
instance_ref['internal_id'],
154
154
instance_ref['state'],
155
155
power_state.RUNNING)
157
logging.debug('instance %s: rebooting', instance_ref['name'])
157
logging.debug(_('instance %s: rebooting'), instance_ref['name'])
158
158
self.db.instance_set_state(context,
160
160
power_state.NOSTATE,
168
168
context = context.elevated()
169
169
instance_ref = self.db.instance_get(context, instance_id)
171
logging.debug('instance %s: rescuing',
171
logging.debug(_('instance %s: rescuing'),
172
172
instance_ref['internal_id'])
173
173
self.db.instance_set_state(context,
183
183
context = context.elevated()
184
184
instance_ref = self.db.instance_get(context, instance_id)
186
logging.debug('instance %s: unrescuing',
186
logging.debug(_('instance %s: unrescuing'),
187
187
instance_ref['internal_id'])
188
188
self.db.instance_set_state(context,
192
192
self.driver.unrescue(instance_ref)
193
193
self._update_state(context, instance_id)
196
def _update_state_callback(self, context, instance_id, result):
197
"""Update instance state when async task completes."""
198
self._update_state(context, instance_id)
200
@exception.wrap_exception
201
def pause_instance(self, context, instance_id):
202
"""Pause an instance on this server."""
203
context = context.elevated()
204
instance_ref = self.db.instance_get(context, instance_id)
206
logging.debug('instance %s: pausing',
207
instance_ref['internal_id'])
208
self.db.instance_set_state(context,
212
self.driver.pause(instance_ref,
213
lambda result: self._update_state_callback(self,
218
@exception.wrap_exception
219
def unpause_instance(self, context, instance_id):
220
"""Unpause a paused instance on this server."""
221
context = context.elevated()
222
instance_ref = self.db.instance_get(context, instance_id)
224
logging.debug('instance %s: unpausing',
225
instance_ref['internal_id'])
226
self.db.instance_set_state(context,
230
self.driver.unpause(instance_ref,
231
lambda result: self._update_state_callback(self,
195
236
@exception.wrap_exception
196
237
def get_console_output(self, context, instance_id):
197
238
"""Send the console output for an instance."""
198
239
context = context.elevated()
199
logging.debug("instance %s: getting console output", instance_id)
240
logging.debug(_("instance %s: getting console output"), instance_id)
200
241
instance_ref = self.db.instance_get(context, instance_id)
202
243
return self.driver.get_console_output(instance_ref)
205
246
def attach_volume(self, context, instance_id, volume_id, mountpoint):
206
247
"""Attach a volume to an instance."""
207
248
context = context.elevated()
208
logging.debug("instance %s: attaching volume %s to %s", instance_id,
249
logging.debug(_("instance %s: attaching volume %s to %s"), instance_id,
209
250
volume_id, mountpoint)
210
251
instance_ref = self.db.instance_get(context, instance_id)
211
252
dev_path = self.volume_manager.setup_compute_volume(context,
222
263
# NOTE(vish): The inline callback eats the exception info so we
223
264
# log the traceback here and reraise the same
224
265
# ecxception below.
225
logging.exception("instance %s: attach failed %s, removing",
266
logging.exception(_("instance %s: attach failed %s, removing"),
226
267
instance_id, mountpoint)
227
268
self.volume_manager.remove_compute_volume(context,
234
275
def detach_volume(self, context, instance_id, volume_id):
235
276
"""Detach a volume from an instance."""
236
277
context = context.elevated()
237
logging.debug("instance %s: detaching volume %s",
278
logging.debug(_("instance %s: detaching volume %s"),
240
281
instance_ref = self.db.instance_get(context, instance_id)
241
282
volume_ref = self.db.volume_get(context, volume_id)
242
283
if instance_ref['name'] not in self.driver.list_instances():
243
logging.warn("Detaching volume from unknown instance %s",
284
logging.warn(_("Detaching volume from unknown instance %s"),
244
285
instance_ref['name'])
246
287
self.driver.detach_volume(instance_ref['name'],