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 | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 28 | if compute_node.hypervisor_hostname not in node_hostnames: |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 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 |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 31 | site_index = random.randint(0, len(sites)) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 32 | node = Node(name=compute_node.hypervisor_hostname, |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 33 | site=sites[site_index], deployment=deployment) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 34 | new_nodes.append(node) |
| 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 | |