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 | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame^] | 9 | from core.models.site import SiteDeployments, Controller |
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 | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame^] | 25 | site_deployments = SiteDeployments.objects.all() |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 26 | nodes = Node.objects.all() |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 27 | node_hostnames = [node.name for node in nodes] |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 28 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame^] | 29 | # fetch all nodes from each controller |
| 30 | controllers = Controller.objects.all() |
Tony Mack | 026ffbe | 2014-05-07 13:31:55 -0400 | [diff] [blame] | 31 | new_nodes = [] |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame^] | 32 | for controller in controllers: |
Scott Baker | 1fd5d22 | 2014-09-04 11:04:27 -0700 | [diff] [blame] | 33 | try: |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame^] | 34 | driver = self.driver.admin_driver(controller=controller.name) |
Scott Baker | 1fd5d22 | 2014-09-04 11:04:27 -0700 | [diff] [blame] | 35 | compute_nodes = driver.shell.nova.hypervisors.list() |
| 36 | except: |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame^] | 37 | logger.log_exc("Failed to get nodes from controller %s" % str(controller)) |
Scott Baker | 1fd5d22 | 2014-09-04 11:04:27 -0700 | [diff] [blame] | 38 | continue |
| 39 | |
Tony Mack | 6d2a98c | 2014-06-03 21:08:39 -0400 | [diff] [blame] | 40 | for compute_node in compute_nodes: |
Scott Baker | c217c0f | 2014-09-22 17:46:35 -0700 | [diff] [blame] | 41 | if compute_node.hypervisor_hostname not in node_hostnames: |
Tony Mack | 6d2a98c | 2014-06-03 21:08:39 -0400 | [diff] [blame] | 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 |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame^] | 44 | site_index = random.randint(0, len(site_deployments)) |
Scott Baker | c217c0f | 2014-09-22 17:46:35 -0700 | [diff] [blame] | 45 | node = Node(name=compute_node.hypervisor_hostname, |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame^] | 46 | site_deployment=site_deployments[site_index], controller=controller) |
Tony Mack | 6d2a98c | 2014-06-03 21:08:39 -0400 | [diff] [blame] | 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 | |