~jk0/nova/xs-rescue

« back to all changes in this revision

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

  • Committer: Josh Kearney
  • Date: 2011-02-25 16:54:37 UTC
  • Revision ID: josh@jk0.org-20110225165437-4jdw41n3x6ntuv2l
Create rescue instance

Show diffs side-by-side

added added

removed removed

Lines of Context:
65
65
 
66
66
    def spawn(self, instance):
67
67
        """Create VM instance"""
68
 
        vm = VMHelper.lookup(self._session, instance.name)
 
68
        instance_name = instance.name
 
69
        vm = VMHelper.lookup(self._session, instance_name)
69
70
        if vm is not None:
70
71
            raise exception.Duplicate(_('Attempted to create'
71
 
            ' non-unique name %s') % instance.name)
 
72
            ' non-unique name %s') % instance_name)
72
73
 
73
74
        #ensure enough free memory is available
74
75
        if not VMHelper.ensure_free_mem(self._session, instance):
75
 
                name = instance['name']
76
 
                LOG.exception(_('instance %(name)s: not enough free memory')
77
 
                              % locals())
78
 
                db.instance_set_state(context.get_admin_context(),
79
 
                                      instance['id'],
80
 
                                      power_state.SHUTDOWN)
81
 
                return
 
76
            LOG.exception(_('instance %(instance_name)s: not enough free '
 
77
                          'memory') % locals())
 
78
            db.instance_set_state(context.get_admin_context(),
 
79
                                  instance['id'],
 
80
                                  power_state.SHUTDOWN)
 
81
            return
82
82
 
83
83
        user = AuthManager().get_user(instance.user_id)
84
84
        project = AuthManager().get_project(instance.project_id)
150
150
 
151
151
        LOG.debug(_('Starting VM %s...'), vm_ref)
152
152
        self._session.call_xenapi('VM.start', vm_ref, False, False)
153
 
        instance_name = instance.name
154
153
        LOG.info(_('Spawning VM %(instance_name)s created %(vm_ref)s.')
155
 
                % locals())
 
154
                 % locals())
156
155
 
157
156
        def _inject_onset_files():
158
157
            onset_files = instance.onset_files
176
175
 
177
176
        def _wait_for_boot():
178
177
            try:
179
 
                state = self.get_info(instance['name'])['state']
 
178
                state = self.get_info(instance_name)['state']
180
179
                db.instance_set_state(context.get_admin_context(),
181
180
                                      instance['id'], state)
182
181
                if state == power_state.RUNNING:
183
 
                    LOG.debug(_('Instance %s: booted'), instance['name'])
 
182
                    LOG.debug(_('Instance %s: booted'), instance_name)
184
183
                    timer.stop()
185
184
                    _inject_onset_files()
186
185
                    return True
187
186
            except Exception, exc:
188
187
                LOG.warn(exc)
189
188
                LOG.exception(_('instance %s: failed to boot'),
190
 
                              instance['name'])
 
189
                              instance_name)
191
190
                db.instance_set_state(context.get_admin_context(),
192
191
                                      instance['id'],
193
192
                                      power_state.SHUTDOWN)
475
474
 
476
475
    def rescue(self, instance, callback):
477
476
        """Rescue the specified instance"""
478
 
        #vm = self._get_vm_opaque_ref(instance)
479
 
        #self._shutdown(instance, vm)
480
 
 
481
 
        print instance.__dict__
482
 
        print instance.name
483
 
        print "%s-rescue" % instance.name
484
 
        #rescue_vm = self.spawn(instance)
485
 
 
486
 
        #vbd = self._session.get_xenapi().VM.get_VBDs(vm)[0]
487
 
        #vdi_ref = self._session.get_xenapi().VBD.get_record(vbd)["VDI"]
488
 
        #vbd_ref = VMHelper.create_vbd(
489
 
        #    self._session,
490
 
        #    rescue_vm,
491
 
        #    vdi_ref,
492
 
        #    1,
493
 
        #    False)
494
 
 
495
 
        #self._session.call_xenapi("Async.VBD.plug", vbd_ref)
 
477
        vm = self._get_vm_opaque_ref(instance)
 
478
        self._shutdown(instance, vm)
 
479
 
 
480
        # log old instance
 
481
        # log new instance
 
482
 
 
483
        instance._rescue = True
 
484
        self.spawn(instance)
 
485
        rescue_vm = self._get_vm_opaque_ref(instance)
 
486
 
 
487
        vbd = self._session.get_xenapi().VM.get_VBDs(vm)[0]
 
488
        vdi_ref = self._session.get_xenapi().VBD.get_record(vbd)["VDI"]
 
489
        vbd_ref = VMHelper.create_vbd(
 
490
            self._session,
 
491
            rescue_vm,
 
492
            vdi_ref,
 
493
            1,
 
494
            False)
 
495
 
 
496
        self._session.call_xenapi("Async.VBD.plug", vbd_ref)
496
497
 
497
498
    def unrescue(self, instance, callback):
498
499
        """Unrescue the specified instance"""
505
506
                VMHelper.unplug_vbd(self._session, vbd_ref)
506
507
                VMHelper.destroy_vbd(self._session, vbd_ref)
507
508
 
508
 
        self.reboot(instance)
 
509
        # fetch old instance
 
510
        # fetch new instance
 
511
        # destroy new instance
 
512
        # start old instance
 
513
 
 
514
        self.destroy(instance)
 
515
        instance._rescue = False
 
516
 
 
517
        self._start(instance, vm)
509
518
 
510
519
    def get_info(self, instance):
511
520
        """Return data about VM instance"""