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