Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
Tony Mack | ae7f30c | 2013-09-25 12:46:50 -0400 | [diff] [blame] | 3 | from django.db.models import F, Q |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 4 | from planetstack.config import Config |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 5 | from observer.openstacksyncstep import OpenStackSyncStep |
| 6 | from core.models.sliver import Sliver |
Scott Baker | 72c2e70 | 2014-08-11 18:58:52 -0700 | [diff] [blame] | 7 | from util.logger import Logger, logging |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 8 | |
Scott Baker | f24b927 | 2014-08-14 17:35:05 -0700 | [diff] [blame] | 9 | logger = Logger(level=logging.INFO) |
| 10 | |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 11 | class SyncSliverIps(OpenStackSyncStep): |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 12 | provides=[Sliver] |
| 13 | requested_interval=0 |
Tony Mack | 66646d5 | 2013-09-24 21:47:12 -0400 | [diff] [blame] | 14 | |
Sapan Bhatia | b83643c | 2014-07-23 09:59:32 -0400 | [diff] [blame] | 15 | def fetch_pending(self, deleted): |
Scott Baker | f24b927 | 2014-08-14 17:35:05 -0700 | [diff] [blame] | 16 | return [] # XXX smbaker - disabling this sync_step, since sliver.ip is obsoleted by sync_network_slivers() |
| 17 | |
Sapan Bhatia | b83643c | 2014-07-23 09:59:32 -0400 | [diff] [blame] | 18 | # Not supported yet |
| 19 | if (deleted): |
| 20 | return [] |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 21 | slivers = Sliver.objects.filter(ip=None) |
| 22 | return slivers |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 23 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 24 | def sync_record(self, sliver): |
Scott Baker | 72c2e70 | 2014-08-11 18:58:52 -0700 | [diff] [blame] | 25 | driver = self.driver.client_driver(tenant=sliver.slice.name, |
| 26 | deployment=sliver.node.deployment.name) |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 27 | servers = driver.shell.nova.servers.findall(id=sliver.instance_id) |
| 28 | if not servers: |
| 29 | return |
| 30 | server = servers[0] |
Scott Baker | 72c2e70 | 2014-08-11 18:58:52 -0700 | [diff] [blame] | 31 | |
| 32 | # First try to grab the dedicated public address |
| 33 | # NOTE: "ext-net" is hardcoded here. |
| 34 | ip = None |
| 35 | ext_net_addrs = server.addresses.get("ext-net")
|
| 36 | if ext_net_addrs:
|
| 37 | ip = ext_net_addrs[0]["addr"]
|
| 38 |
|
| 39 | # If there was no public address, then grab the first address in the
|
| 40 | # list.
|
| 41 | if not ip:
|
| 42 | if server.addresses:
|
| 43 | addrs = server.addresses.values()[0]
|
| 44 | if addrs:
|
| 45 | ip = addrs[0]["addr"] |
| 46 | |
| 47 | if ip and ip!=sliver.ip: |
| 48 | sliver.ip = ip |
Tony Mack | 261f858 | 2014-04-21 17:08:31 -0400 | [diff] [blame] | 49 | sliver.save() |
Scott Baker | 72c2e70 | 2014-08-11 18:58:52 -0700 | [diff] [blame] | 50 | logger.info("saved sliver ip: %s %s" % (sliver, ip)) |