Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
| 3 | import random |
| 4 | from datetime import datetime |
| 5 | from django.db.models import F, Q |
| 6 | from planetstack.config import Config |
| 7 | from observer.openstacksyncstep import OpenStackSyncStep |
| 8 | from core.models.node import Node |
Tony Mack | 91463d9 | 2014-06-19 20:42:03 -0400 | [diff] [blame^] | 9 | from core.models.site import Site, Deployment |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 10 | |
| 11 | class SyncNodes(OpenStackSyncStep): |
| 12 | provides=[Node] |
| 13 | requested_interval=0 |
| 14 | |
| 15 | def fetch_pending(self): |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 16 | # collect local nodes |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 17 | sites = Site.objects.all() |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 18 | nodes = Node.objects.all() |
| 19 | node_hostnames = [node.name for node in nodes] |
| 20 | |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 21 | # fetch all nodes from each deployment |
| 22 | deployments = Deployment.objects.all() |
Tony Mack | 026ffbe | 2014-05-07 13:31:55 -0400 | [diff] [blame] | 23 | new_nodes = [] |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 24 | for deployment in deployments: |
| 25 | driver = self.driver.admin_driver(deployment=deployment.name) |
| 26 | compute_nodes = driver.shell.nova.hypervisors.list() |
Tony Mack | 6d2a98c | 2014-06-03 21:08:39 -0400 | [diff] [blame] | 27 | for compute_node in compute_nodes: |
| 28 | if compute_node.hypervisor_hostname not in node_hostnames: |
| 29 | # XX TODO:figure out how to correctly identify a node's site. |
| 30 | # XX pick a random site to add the node to for now |
| 31 | site_index = random.randint(0, len(sites)) |
| 32 | node = Node(name=compute_node.hypervisor_hostname, |
| 33 | site=sites[site_index], deployment=deployment) |
| 34 | new_nodes.append(node) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 35 | |
| 36 | return new_nodes |
| 37 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 38 | |
| 39 | def sync_record(self, node): |
| 40 | node.save() |
| 41 | |