Tony Mack | cafd0b0 | 2013-04-17 10:45:12 -0400 | [diff] [blame] | 1 | import os |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 2 | import sys |
Scott Baker | 76a840e | 2015-02-11 21:38:09 -0800 | [diff] [blame] | 3 | #os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xos.settings") |
Tony Mack | cafd0b0 | 2013-04-17 10:45:12 -0400 | [diff] [blame] | 4 | import time |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 5 | from core.models.sliver import Sliver |
| 6 | from openstack.manager import OpenStackManager |
Tony Mack | cafd0b0 | 2013-04-17 10:45:12 -0400 | [diff] [blame] | 7 | |
| 8 | class SliverAgent: |
| 9 | |
| 10 | def run(self): |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 11 | manager = OpenStackManager() |
| 12 | # exit if openstack is disable or unavailable |
| 13 | if not manager.enabled or not manager.has_openstack: |
| 14 | sys.exit() |
| 15 | |
| 16 | while True : |
Tony Mack | cafd0b0 | 2013-04-17 10:45:12 -0400 | [diff] [blame] | 17 | # fill in null ip addresses |
| 18 | slivers = Sliver.objects.filter(ip=None) |
| 19 | for sliver in slivers: |
Tony Mack | ffab0f2 | 2013-04-29 19:42:20 -0400 | [diff] [blame] | 20 | # update connection |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 21 | manager.client.connect(username=manager.client.keystone.username, |
| 22 | password=manager.client.keystone.password, |
Tony Mack | ffab0f2 | 2013-04-29 19:42:20 -0400 | [diff] [blame] | 23 | tenant=sliver.slice.name) |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 24 | sliver.os_manager = manager |
| 25 | servers = manager.client.nova.servers.findall(id=sliver.instance_id) |
Tony Mack | cafd0b0 | 2013-04-17 10:45:12 -0400 | [diff] [blame] | 26 | if not servers: |
| 27 | continue |
| 28 | server = servers[0] |
| 29 | ips = server.addresses.get(sliver.slice.name, []) |
| 30 | if not ips: |
| 31 | continue |
| 32 | sliver.ip = ips[0]['addr'] |
| 33 | sliver.save() |
| 34 | time.sleep(7) |
| 35 | |
| 36 | |
| 37 | if __name__ == '__main__': |
| 38 | SliverAgent().run() |
| 39 | |