def launch_instance(self, ctx, instance):
     for device in instance['devices']:
         device['instance_id'] = 'nova-123'
         device['status'] = 'building'
     for wire in instance['wires']:
         wire['quantum_id'] = 'quantum-123'
     db.create_instance(instance)
     return instance
    def launch_instance(self, ctx, instance):
        # TODO: validate
        nova = self._get_nova_client(ctx)
        quantum = self._get_quantum_client(ctx)
        instance['_id'] = ObjectId()
        conn_info = {} # [device_id] = [(net,port)]

        def add_conn_info(device, net, port):
            conn = conn_info.get(device)
            if conn is None:
                conn = []
                conn_info[device] = conn
            conn.append({"net-id": net, "v4-fixed-ip": "", "port-id": port})

        def create_port(instance_id, net_id, wire_id, side):
            return quantum.create_port({
                'port': {
                    'tenant_id': ctx.tenant,
                    'network_id': net_id,
                    'name': "%s-%s-%s" % (
                        instance_id, wire_id, side)}})['port']

        devices_dict = dict(map(lambda d: (d['id'], d), instance['devices']))
        def port_attrs(wire_side):
            device = devices_dict[wire_side['device']]
            slot = wire_side['slot']
            slot_model = device['slots'][slot]['model']
            return {
                'slot-id': slot,
                'port-id': wire_side['port'],
                'slot-model': slot_model}

        for wire in instance['wires']:
            print wire
            net = quantum.create_network({
                'network': {
                    'name': "%s-%s" % (instance['_id'], wire['id']),
                    'tenant_id': ctx.tenant}})['network']
            left = create_port(instance['_id'], net['id'], wire['id'], 'left')
            wire['left']['quantum_id'] = left['id']
            right = create_port(instance['_id'], net['id'], wire['id'], 'right')
            wire['right']['quantum_id'] = right['id']
            left_attrs = port_attrs(wire['left'])
            right_attrs = port_attrs(wire['right'])
            quantum.set_port_attrs(ctx.tenant, net['id'], left['id'], left_attrs)
            quantum.set_port_attrs(ctx.tenant, net['id'], right['id'], right_attrs)
            add_conn_info(wire['left']['device'], net['id'], left['id'])
            add_conn_info(wire['right']['device'], net['id'], right['id'])
            wire['quantum_id'] = net['id']
        print conn_info
        for device in instance['devices']:
            print "Launching %s" % device
            conn = conn_info.get(device['id'])
            logging.debug("Connection info for instance: %s" % conn)
            server = nova.servers.create(
                name="%s-%s-%s" % (instance['_id'], device['id'], device['name']),
                image=device['software_id'],
                flavor=self._flavor_for_device(nova, device),
                nics=conn,
                scheduler_hints=[]) # TODO: use instance type
            device['instance_id'] = server.id
            device['status'] = server.status.lower()
        db.create_instance(instance)
        return instance
Beispiel #3
0
    def launch_instance(self, ctx, instance):
        # TODO: validate
        nova = self._get_nova_client(ctx)
        quantum = self._get_quantum_client(ctx)
        instance['_id'] = ObjectId()
        conn_info = {}  # [device_id] = [(net,port)]

        def add_conn_info(device, net, port):
            conn = conn_info.get(device)
            if conn is None:
                conn = []
                conn_info[device] = conn
            conn.append({"net-id": net, "v4-fixed-ip": "", "port-id": port})

        def create_port(instance_id, net_id, wire_id, side):
            return quantum.create_port({
                'port': {
                    'tenant_id': ctx.tenant,
                    'network_id': net_id,
                    'name': "%s-%s-%s" % (instance_id, wire_id, side)
                }
            })['port']

        devices_dict = dict(map(lambda d: (d['id'], d), instance['devices']))

        def port_attrs(wire_side):
            device = devices_dict[wire_side['device']]
            slot = wire_side['slot']
            slot_model = device['slots'][slot]['model']
            return {
                'slot-id': slot,
                'port-id': wire_side['port'],
                'slot-model': slot_model
            }

        for wire in instance['wires']:
            print wire
            net = quantum.create_network({
                'network': {
                    'name': "%s-%s" % (instance['_id'], wire['id']),
                    'tenant_id': ctx.tenant
                }
            })['network']
            left = create_port(instance['_id'], net['id'], wire['id'], 'left')
            wire['left']['quantum_id'] = left['id']
            right = create_port(instance['_id'], net['id'], wire['id'],
                                'right')
            wire['right']['quantum_id'] = right['id']
            left_attrs = port_attrs(wire['left'])
            right_attrs = port_attrs(wire['right'])
            quantum.set_port_attrs(ctx.tenant, net['id'], left['id'],
                                   left_attrs)
            quantum.set_port_attrs(ctx.tenant, net['id'], right['id'],
                                   right_attrs)
            add_conn_info(wire['left']['device'], net['id'], left['id'])
            add_conn_info(wire['right']['device'], net['id'], right['id'])
            wire['quantum_id'] = net['id']
        print conn_info
        for device in instance['devices']:
            print "Launching %s" % device
            conn = conn_info.get(device['id'])
            logging.debug("Connection info for instance: %s" % conn)
            server = nova.servers.create(
                name="%s-%s-%s" %
                (instance['_id'], device['id'], device['name']),
                image=device['software_id'],
                flavor=self._flavor_for_device(nova, device),
                nics=conn,
                scheduler_hints=[])  # TODO: use instance type
            device['instance_id'] = server.id
            device['status'] = server.status.lower()
        db.create_instance(instance)
        return instance