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