Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
| 3 | import random |
Scott Baker | 1fd5d22 | 2014-09-04 11:04:27 -0700 | [diff] [blame] | 4 | from datetime import datetime |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 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 |
Scott Baker | 1fd5d22 | 2014-09-04 11:04:27 -0700 | [diff] [blame] | 10 | from util.logger import Logger, logging |
| 11 | |
| 12 | logger = Logger(level=logging.INFO) |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 13 | |
| 14 | class SyncNodes(OpenStackSyncStep): |
| 15 | provides=[Node] |
| 16 | requested_interval=0 |
| 17 | |
Sapan Bhatia | b83643c | 2014-07-23 09:59:32 -0400 | [diff] [blame] | 18 | def fetch_pending(self, deleted): |
| 19 | # Nodes come from the back end |
| 20 | # You can't delete them |
| 21 | if (deleted): |
| 22 | return [] |
| 23 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 24 | # collect local nodes |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 25 | sites = Site.objects.all() |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 26 | nodes = Node.objects.all() |
| 27 | node_hostnames = [node.name for node in nodes] |
| 28 | |
Scott Baker | 1fd5d22 | 2014-09-04 11:04:27 -0700 | [diff] [blame] | 29 | # fetch all nodes from each deployment |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 30 | deployments = Deployment.objects.all() |
Tony Mack | 026ffbe | 2014-05-07 13:31:55 -0400 | [diff] [blame] | 31 | new_nodes = [] |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 32 | for deployment in deployments: |
Scott Baker | 1fd5d22 | 2014-09-04 11:04:27 -0700 | [diff] [blame] | 33 | try: |
| 34 | driver = self.driver.admin_driver(deployment=deployment.name) |
| 35 | compute_nodes = driver.shell.nova.hypervisors.list() |
| 36 | except: |
| 37 | logger.log_exc("Failed to get nodes from deployment %s" % str(deployment)) |
| 38 | continue |
| 39 | |
Tony Mack | 6d2a98c | 2014-06-03 21:08:39 -0400 | [diff] [blame] | 40 | for compute_node in compute_nodes: |
| 41 | if compute_node.hypervisor_hostname not in node_hostnames: |
| 42 | # XX TODO:figure out how to correctly identify a node's site. |
| 43 | # XX pick a random site to add the node to for now |
| 44 | site_index = random.randint(0, len(sites)) |
| 45 | node = Node(name=compute_node.hypervisor_hostname, |
| 46 | site=sites[site_index], deployment=deployment) |
| 47 | new_nodes.append(node) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 48 | |
| 49 | return new_nodes |
| 50 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 51 | |
| 52 | def sync_record(self, node): |
| 53 | node.save() |
| 54 | |