blob: 09dc7edec9079278fb5b46114d43247cfac031a9 [file] [log] [blame]
Sapan Bhatia24836f12013-08-27 10:16:05 -04001import os
2import base64
Tony Mackae7f30c2013-09-25 12:46:50 -04003from django.db.models import F, Q
Sapan Bhatia24836f12013-08-27 10:16:05 -04004from planetstack.config import Config
Sapan Bhatia04c94ad2013-09-02 18:00:28 -04005from observer.openstacksyncstep import OpenStackSyncStep
6from core.models.network import *
Sapan Bhatia24836f12013-08-27 10:16:05 -04007
8class SyncNetworkSlivers(OpenStackSyncStep):
Tony Mackdacfb982013-09-24 21:57:16 -04009 requested_interval = 3600
10 provides=[NetworkSliver]
Sapan Bhatia24836f12013-08-27 10:16:05 -040011
Tony Mack31078dd2013-10-02 12:46:21 -040012 def fetch_pending(self):
13 return NetworkSliver.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
14
Tony Mack16f04742013-09-25 08:53:28 -040015 def call(self, failed=[]):
Tony Mackdacfb982013-09-24 21:57:16 -040016 networkSlivers = NetworkSliver.objects.all()
17 networkSlivers_by_id = {}
18 networkSlivers_by_port = {}
19 for networkSliver in networkSlivers:
20 networkSlivers_by_id[networkSliver.id] = networkSliver
21 networkSlivers_by_port[networkSliver.port_id] = networkSliver
Sapan Bhatia24836f12013-08-27 10:16:05 -040022
Tony Mackdacfb982013-09-24 21:57:16 -040023 networks = Network.objects.all()
24 networks_by_id = {}
25 for network in networks:
26 networks_by_id[network.network_id] = network
Sapan Bhatia24836f12013-08-27 10:16:05 -040027
Tony Mackdacfb982013-09-24 21:57:16 -040028 slivers = Sliver.objects.all()
29 slivers_by_instance_id = {}
30 for sliver in slivers:
31 slivers_by_instance_id[sliver.instance_id] = sliver
Sapan Bhatia24836f12013-08-27 10:16:05 -040032
Tony Mackdacfb982013-09-24 21:57:16 -040033 ports = self.driver.shell.quantum.list_ports()["ports"]
34 for port in ports:
35 if port["id"] in networkSlivers_by_port:
36 # we already have it
37 print "already accounted for port", port["id"]
38 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040039
Tony Mackdacfb982013-09-24 21:57:16 -040040 if port["device_owner"] != "compute:nova":
41 # we only want the ports that connect to instances
42 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040043
Tony Mackdacfb982013-09-24 21:57:16 -040044 network = networks_by_id.get(port['network_id'], None)
45 if not network:
46 #print "no network for port", port["id"], "network", port["network_id"]
47 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040048
Tony Mackdacfb982013-09-24 21:57:16 -040049 sliver = slivers_by_instance_id.get(port['device_id'], None)
50 if not sliver:
51 print "no sliver for port", port["id"], "device_id", port['device_id']
52 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040053
Tony Mackdacfb982013-09-24 21:57:16 -040054 if network.template.sharedNetworkId is not None:
55 # If it's a shared network template, then more than one network
56 # object maps to the quantum network. We have to do a whole bunch
57 # of extra work to find the right one.
58 networks = network.template.network_set.all()
59 network = None
60 for candidate_network in networks:
61 if (candidate_network.owner == sliver.slice):
62 print "found network", candidate_network
63 network = candidate_network
Sapan Bhatia24836f12013-08-27 10:16:05 -040064
Tony Mackdacfb982013-09-24 21:57:16 -040065 if not network:
66 print "failed to find the correct network for a shared template for port", port["id"], "network", port["network_id"]
67 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040068
Tony Mackdacfb982013-09-24 21:57:16 -040069 if not port["fixed_ips"]:
70 print "port", port["id"], "has no fixed_ips"
71 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040072
Tony Mackdacfb982013-09-24 21:57:16 -040073# print "XXX", port
Sapan Bhatia24836f12013-08-27 10:16:05 -040074
Tony Mackdacfb982013-09-24 21:57:16 -040075 ns = NetworkSliver(network=network,
76 sliver=sliver,
77 ip=port["fixed_ips"][0]["ip_address"],
78 port_id=port["id"])
79 ns.save()