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() |
| 24 | for deployment in deployments: |
| 25 | driver = self.driver.admin_driver(deployment=deployment.name) |
| 26 | compute_nodes = driver.shell.nova.hypervisors.list() |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 27 | if compute_node.hypervisor_hostname not in node_hostnames: |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 28 | # XX TODO:figure out how to correctly identify a node's site. |
| 29 | # XX pick a random site to add the node to for now |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 30 | site_index = random.randint(0, len(sites)) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 31 | node = Node(name=compute_node.hypervisor_hostname, |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 32 | site=sites[site_index], deployment=deployment) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 33 | new_nodes.append(node) |
| 34 | |
| 35 | return new_nodes |
| 36 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 37 | |
| 38 | def sync_record(self, node): |
| 39 | node.save() |
| 40 | |