blob: 9e24faea4a26fff4e650a80aea2e06e5af6f1aef [file] [log] [blame]
Sapan Bhatia24836f12013-08-27 10:16:05 -04001import os
2import base64
3from planetstack.config import Config
Sapan Bhatia04c94ad2013-09-02 18:00:28 -04004from observer.openstacksyncstep import OpenStackSyncStep
5from core.models.network import *
Sapan Bhatia24836f12013-08-27 10:16:05 -04006
7class SyncNetworkSlivers(OpenStackSyncStep):
Sapan Bhatia2ef36c82013-09-02 14:30:37 -04008 requested_interval = 3600
Sapan Bhatia24836f12013-08-27 10:16:05 -04009 provides=[NetworkSliver]
10
11 def call(self):
Sapan Bhatia04c94ad2013-09-02 18:00:28 -040012 networkSlivers = NetworkSliver.objects.all()
13 networkSlivers_by_id = {}
14 networkSlivers_by_port = {}
15 for networkSliver in networkSlivers:
16 networkSlivers_by_id[networkSliver.id] = networkSliver
17 networkSlivers_by_port[networkSliver.port_id] = networkSliver
Sapan Bhatia24836f12013-08-27 10:16:05 -040018
Sapan Bhatia04c94ad2013-09-02 18:00:28 -040019 networks = Network.objects.all()
20 networks_by_id = {}
21 for network in networks:
22 networks_by_id[network.network_id] = network
Sapan Bhatia24836f12013-08-27 10:16:05 -040023
Sapan Bhatia04c94ad2013-09-02 18:00:28 -040024 slivers = Sliver.objects.all()
25 slivers_by_instance_id = {}
26 for sliver in slivers:
27 slivers_by_instance_id[sliver.instance_id] = sliver
Sapan Bhatia24836f12013-08-27 10:16:05 -040028
Sapan Bhatia04c94ad2013-09-02 18:00:28 -040029 ports = self.manager.driver.shell.quantum.list_ports()["ports"]
30 for port in ports:
31 if port["id"] in networkSlivers_by_port:
32 # we already have it
33 print "already accounted for port", port["id"]
34 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040035
Sapan Bhatia04c94ad2013-09-02 18:00:28 -040036 if port["device_owner"] != "compute:nova":
37 # we only want the ports that connect to instances
38 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040039
Sapan Bhatia04c94ad2013-09-02 18:00:28 -040040 network = networks_by_id.get(port['network_id'], None)
41 if not network:
42 #print "no network for port", port["id"], "network", port["network_id"]
43 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040044
Sapan Bhatia04c94ad2013-09-02 18:00:28 -040045 sliver = slivers_by_instance_id.get(port['device_id'], None)
46 if not sliver:
47 print "no sliver for port", port["id"], "device_id", port['device_id']
48 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040049
Sapan Bhatia04c94ad2013-09-02 18:00:28 -040050 if network.template.sharedNetworkId is not None:
51 # If it's a shared network template, then more than one network
52 # object maps to the quantum network. We have to do a whole bunch
53 # of extra work to find the right one.
54 networks = network.template.network_set.all()
55 network = None
56 for candidate_network in networks:
57 if (candidate_network.owner == sliver.slice):
58 print "found network", candidate_network
59 network = candidate_network
Sapan Bhatia24836f12013-08-27 10:16:05 -040060
Sapan Bhatia04c94ad2013-09-02 18:00:28 -040061 if not network:
62 print "failed to find the correct network for a shared template for port", port["id"], "network", port["network_id"]
63 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040064
Sapan Bhatia04c94ad2013-09-02 18:00:28 -040065 if not port["fixed_ips"]:
66 print "port", port["id"], "has no fixed_ips"
67 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040068
Sapan Bhatia04c94ad2013-09-02 18:00:28 -040069# print "XXX", port
Sapan Bhatia24836f12013-08-27 10:16:05 -040070
Sapan Bhatia04c94ad2013-09-02 18:00:28 -040071 ns = NetworkSliver(network=network,
72 sliver=sliver,
73 ip=port["fixed_ips"][0]["ip_address"],
74 port_id=port["id"])
75 ns.save()