blob: d723da521d8993faf21aec3315cb92d6b579efc0 [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.sliver import Sliver
Scott Baker72c2e702014-08-11 18:58:52 -07007from util.logger import Logger, logging
Sapan Bhatia24836f12013-08-27 10:16:05 -04008
Scott Bakerf24b9272014-08-14 17:35:05 -07009logger = Logger(level=logging.INFO)
10
Sapan Bhatia24836f12013-08-27 10:16:05 -040011class SyncSliverIps(OpenStackSyncStep):
Tony Mackce79de02013-09-24 10:12:33 -040012 provides=[Sliver]
13 requested_interval=0
Tony Mack66646d52013-09-24 21:47:12 -040014
Sapan Bhatiab83643c2014-07-23 09:59:32 -040015 def fetch_pending(self, deleted):
Scott Bakerf24b9272014-08-14 17:35:05 -070016 return [] # XXX smbaker - disabling this sync_step, since sliver.ip is obsoleted by sync_network_slivers()
17
Sapan Bhatiab83643c2014-07-23 09:59:32 -040018 # Not supported yet
19 if (deleted):
20 return []
Tony Mackce79de02013-09-24 10:12:33 -040021 slivers = Sliver.objects.filter(ip=None)
22 return slivers
Sapan Bhatia24836f12013-08-27 10:16:05 -040023
Tony Mackce79de02013-09-24 10:12:33 -040024 def sync_record(self, sliver):
Scott Baker72c2e702014-08-11 18:58:52 -070025 driver = self.driver.client_driver(tenant=sliver.slice.name,
26 deployment=sliver.node.deployment.name)
Tony Mackce79de02013-09-24 10:12:33 -040027 servers = driver.shell.nova.servers.findall(id=sliver.instance_id)
28 if not servers:
29 return
30 server = servers[0]
Scott Baker72c2e702014-08-11 18:58:52 -070031
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 Mack261f8582014-04-21 17:08:31 -040049 sliver.save()
Scott Baker72c2e702014-08-11 18:58:52 -070050 logger.info("saved sliver ip: %s %s" % (sliver, ip))