~sandy-walsh/nova/zones

« back to all changes in this revision

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

  • Committer: Sandy Walsh
  • Date: 2011-02-17 21:39:03 UTC
  • mfrom: (635.1.60 nova)
  • Revision ID: sandy.walsh@rackspace.com-20110217213903-swehe88wea8inxow
changed from 003-004 migration

Show diffs side-by-side

added added

removed removed

Lines of Context:
67
67
            raise exception.Duplicate(_('Attempted to create'
68
68
            ' non-unique name %s') % instance.name)
69
69
 
70
 
        bridge = db.network_get_by_instance(context.get_admin_context(),
71
 
                                            instance['id'])['bridge']
72
 
        network_ref = \
73
 
            NetworkHelper.find_network_with_bridge(self._session, bridge)
 
70
        #ensure enough free memory is available
 
71
        if not VMHelper.ensure_free_mem(self._session, instance):
 
72
                name = instance['name']
 
73
                LOG.exception(_('instance %(name)s: not enough free memory')
 
74
                              % locals())
 
75
                db.instance_set_state(context.get_admin_context(),
 
76
                                      instance['id'],
 
77
                                      power_state.SHUTDOWN)
 
78
                return
74
79
 
75
80
        user = AuthManager().get_user(instance.user_id)
76
81
        project = AuthManager().get_project(instance.project_id)
99
104
                                          instance, kernel, ramdisk, pv_kernel)
100
105
        VMHelper.create_vbd(self._session, vm_ref, vdi_ref, 0, True)
101
106
 
102
 
        if network_ref:
103
 
            VMHelper.create_vif(self._session, vm_ref,
104
 
                                network_ref, instance.mac_address)
 
107
        # write network info
 
108
        admin_context = context.get_admin_context()
 
109
 
 
110
        # TODO(tr3buchet) - remove comment in multi-nic
 
111
        # I've decided to go ahead and consider multiple IPs and networks
 
112
        # at this stage even though they aren't implemented because these will
 
113
        # be needed for multi-nic and there was no sense writing it for single
 
114
        # network/single IP and then having to turn around and re-write it
 
115
        IPs = db.fixed_ip_get_all_by_instance(admin_context, instance['id'])
 
116
        for network in db.network_get_all_by_instance(admin_context,
 
117
                                                      instance['id']):
 
118
            network_IPs = [ip for ip in IPs if ip.network_id == network.id]
 
119
 
 
120
            def ip_dict(ip):
 
121
                return {'netmask': network['netmask'],
 
122
                        'enabled': '1',
 
123
                        'ip': ip.address}
 
124
 
 
125
            mac_id = instance.mac_address.replace(':', '')
 
126
            location = 'vm-data/networking/%s' % mac_id
 
127
            mapping = {'label': network['label'],
 
128
                       'gateway': network['gateway'],
 
129
                       'mac': instance.mac_address,
 
130
                       'dns': [network['dns']],
 
131
                       'ips': [ip_dict(ip) for ip in network_IPs]}
 
132
            self.write_to_param_xenstore(vm_ref, {location: mapping})
 
133
 
 
134
            # TODO(tr3buchet) - remove comment in multi-nic
 
135
            # this bit here about creating the vifs will be updated
 
136
            # in multi-nic to handle multiple IPs on the same network
 
137
            # and multiple networks
 
138
            # for now it works as there is only one of each
 
139
            bridge = network['bridge']
 
140
            network_ref = \
 
141
                NetworkHelper.find_network_with_bridge(self._session, bridge)
 
142
 
 
143
            if network_ref:
 
144
                VMHelper.create_vif(self._session, vm_ref,
 
145
                                    network_ref, instance.mac_address)
 
146
 
105
147
        LOG.debug(_('Starting VM %s...'), vm_ref)
106
148
        self._session.call_xenapi('VM.start', vm_ref, False, False)
107
149
        instance_name = instance.name
109
151
                % locals())
110
152
 
111
153
        # NOTE(armando): Do we really need to do this in virt?
 
154
        # NOTE(tr3buchet): not sure but wherever we do it, we need to call
 
155
        #                  reset_network afterwards
112
156
        timer = utils.LoopingCall(f=None)
113
157
 
114
158
        def _wait_for_boot():
129
173
                timer.stop()
130
174
 
131
175
        timer.f = _wait_for_boot
 
176
 
 
177
        # call reset networking
 
178
        self.reset_network(instance)
 
179
 
132
180
        return timer.start(interval=0.5, now=True)
133
181
 
134
182
    def _get_vm_opaque_ref(self, instance_or_vm):
161
209
                instance_name = instance_or_vm.name
162
210
        vm = VMHelper.lookup(self._session, instance_name)
163
211
        if vm is None:
164
 
            raise Exception(_('Instance not present %s') % instance_name)
 
212
            raise exception.NotFound(
 
213
                            _('Instance not present %s') % instance_name)
165
214
        return vm
166
215
 
167
216
    def snapshot(self, instance, image_id):
389
438
        # TODO: implement this!
390
439
        return 'http://fakeajaxconsole/fake_url'
391
440
 
 
441
    def reset_network(self, instance):
 
442
        """
 
443
        Creates uuid arg to pass to make_agent_call and calls it.
 
444
 
 
445
        """
 
446
        args = {'id': str(uuid.uuid4())}
 
447
        resp = self._make_agent_call('resetnetwork', instance, '', args)
 
448
 
392
449
    def list_from_xenstore(self, vm, path):
393
450
        """Runs the xenstore-ls command to get a listing of all records
394
451
        from 'path' downward. Returns a dict with the sub-paths as keys,