Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
| 3 | import random |
Scott Baker | aa57406 | 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 | b469d24 | 2015-01-03 19:37:39 -0500 | [diff] [blame] | 9 | from core.models.site import SiteDeployments, Controller, SiteDeployments |
Scott Baker | aa57406 | 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 |
Sapan Bhatia | ba74c75 | 2014-12-22 01:43:05 -0500 | [diff] [blame] | 25 | controllers = Controller.objects.all() |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 26 | nodes = Node.objects.all() |
Sapan Bhatia | 475c597 | 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 | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 29 | # fetch all nodes from each controller |
Tony Mack | 026ffbe | 2014-05-07 13:31:55 -0400 | [diff] [blame] | 30 | new_nodes = [] |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 31 | for controller in controllers: |
Scott Baker | aa57406 | 2014-09-04 11:04:27 -0700 | [diff] [blame] | 32 | try: |
Tony Mack | b469d24 | 2015-01-03 19:37:39 -0500 | [diff] [blame] | 33 | controller_site_deployments = SiteDeployments.objects.filter(controller=controller)[0] |
Sapan Bhatia | ba74c75 | 2014-12-22 01:43:05 -0500 | [diff] [blame] | 34 | except IndexError: |
| 35 | raise Exception("Controller %s not bound to any site deployments"%controller.name) |
| 36 | |
| 37 | site_deployment = controller_site_deployments.site_deployment |
| 38 | if (not site_deployment): |
| 39 | raise Exception('Controller without Site Deployment: %s'%controller.name) |
| 40 | |
| 41 | try: |
| 42 | driver = self.driver.admin_driver(controller=controller,tenant='admin') |
Scott Baker | aa57406 | 2014-09-04 11:04:27 -0700 | [diff] [blame] | 43 | compute_nodes = driver.shell.nova.hypervisors.list() |
| 44 | except: |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 45 | logger.log_exc("Failed to get nodes from controller %s" % str(controller)) |
Scott Baker | aa57406 | 2014-09-04 11:04:27 -0700 | [diff] [blame] | 46 | continue |
| 47 | |
Tony Mack | 6d2a98c | 2014-06-03 21:08:39 -0400 | [diff] [blame] | 48 | for compute_node in compute_nodes: |
Scott Baker | 96e4407 | 2014-09-22 17:46:35 -0700 | [diff] [blame] | 49 | if compute_node.hypervisor_hostname not in node_hostnames: |
Tony Mack | 6d2a98c | 2014-06-03 21:08:39 -0400 | [diff] [blame] | 50 | # XX TODO:figure out how to correctly identify a node's site. |
Sapan Bhatia | ba74c75 | 2014-12-22 01:43:05 -0500 | [diff] [blame] | 51 | # XX pick the first one |
Scott Baker | 96e4407 | 2014-09-22 17:46:35 -0700 | [diff] [blame] | 52 | node = Node(name=compute_node.hypervisor_hostname, |
Sapan Bhatia | ba74c75 | 2014-12-22 01:43:05 -0500 | [diff] [blame] | 53 | site_deployment=site_deployment) |
Tony Mack | 6d2a98c | 2014-06-03 21:08:39 -0400 | [diff] [blame] | 54 | new_nodes.append(node) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 55 | |
| 56 | return new_nodes |
| 57 | |
Tony Mack | e1509e8 | 2013-10-09 12:38:04 -0400 | [diff] [blame] | 58 | |
| 59 | def sync_record(self, node): |
| 60 | node.save() |
| 61 | |