Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
| 3 | from planetstack.config import Config |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 4 | from observer.openstacksyncstep import OpenStackSyncStep |
| 5 | from core.models.network import * |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 6 | |
| 7 | class SyncNetworkSlivers(OpenStackSyncStep): |
Sapan Bhatia | 2ef36c8 | 2013-09-02 14:30:37 -0400 | [diff] [blame] | 8 | requested_interval = 3600 |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 9 | provides=[NetworkSliver] |
| 10 | |
| 11 | def call(self): |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 12 | 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 Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 18 | |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 19 | networks = Network.objects.all() |
| 20 | networks_by_id = {} |
| 21 | for network in networks: |
| 22 | networks_by_id[network.network_id] = network |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 23 | |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 24 | slivers = Sliver.objects.all() |
| 25 | slivers_by_instance_id = {} |
| 26 | for sliver in slivers: |
| 27 | slivers_by_instance_id[sliver.instance_id] = sliver |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 28 | |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 29 | 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 Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 35 | |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 36 | if port["device_owner"] != "compute:nova": |
| 37 | # we only want the ports that connect to instances |
| 38 | continue |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 39 | |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 40 | 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 Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 44 | |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 45 | 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 Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 49 | |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 50 | 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 Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 60 | |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 61 | 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 Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 64 | |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 65 | if not port["fixed_ips"]: |
| 66 | print "port", port["id"], "has no fixed_ips" |
| 67 | continue |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 68 | |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 69 | # print "XXX", port |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 70 | |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 71 | ns = NetworkSliver(network=network, |
| 72 | sliver=sliver, |
| 73 | ip=port["fixed_ips"][0]["ip_address"], |
| 74 | port_id=port["id"]) |
| 75 | ns.save() |