blob: 9d51073c6830a6ccd28e3facbd8d89277eb1dc70 [file] [log] [blame]
Tony Macke1509e82013-10-09 12:38:04 -04001import os
2import base64
3import random
Scott Bakeraa574062014-09-04 11:04:27 -07004from datetime import datetime
Tony Macke1509e82013-10-09 12:38:04 -04005from django.db.models import F, Q
6from planetstack.config import Config
7from observer.openstacksyncstep import OpenStackSyncStep
8from core.models.node import Node
Tony Mackb469d242015-01-03 19:37:39 -05009from core.models.site import SiteDeployments, Controller, SiteDeployments
Scott Bakeraa574062014-09-04 11:04:27 -070010from util.logger import Logger, logging
11
12logger = Logger(level=logging.INFO)
Tony Macke1509e82013-10-09 12:38:04 -040013
14class SyncNodes(OpenStackSyncStep):
15 provides=[Node]
16 requested_interval=0
17
Sapan Bhatiab83643c2014-07-23 09:59:32 -040018 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 Macke1509e82013-10-09 12:38:04 -040024 # collect local nodes
Sapan Bhatiaba74c752014-12-22 01:43:05 -050025 controllers = Controller.objects.all()
Tony Macke1509e82013-10-09 12:38:04 -040026 nodes = Node.objects.all()
Sapan Bhatia475c5972014-11-05 10:32:41 -050027 node_hostnames = [node.name for node in nodes]
Tony Macke1509e82013-10-09 12:38:04 -040028
Tony Mack336e0f92014-11-30 15:53:08 -050029 # fetch all nodes from each controller
Tony Mack026ffbe2014-05-07 13:31:55 -040030 new_nodes = []
Tony Mack336e0f92014-11-30 15:53:08 -050031 for controller in controllers:
Scott Bakeraa574062014-09-04 11:04:27 -070032 try:
Tony Mackb469d242015-01-03 19:37:39 -050033 controller_site_deployments = SiteDeployments.objects.filter(controller=controller)[0]
Sapan Bhatiaba74c752014-12-22 01:43:05 -050034 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 Bakeraa574062014-09-04 11:04:27 -070043 compute_nodes = driver.shell.nova.hypervisors.list()
44 except:
Tony Mack336e0f92014-11-30 15:53:08 -050045 logger.log_exc("Failed to get nodes from controller %s" % str(controller))
Scott Bakeraa574062014-09-04 11:04:27 -070046 continue
47
Tony Mack6d2a98c2014-06-03 21:08:39 -040048 for compute_node in compute_nodes:
Scott Baker96e44072014-09-22 17:46:35 -070049 if compute_node.hypervisor_hostname not in node_hostnames:
Tony Mack6d2a98c2014-06-03 21:08:39 -040050 # XX TODO:figure out how to correctly identify a node's site.
Sapan Bhatiaba74c752014-12-22 01:43:05 -050051 # XX pick the first one
Scott Baker96e44072014-09-22 17:46:35 -070052 node = Node(name=compute_node.hypervisor_hostname,
Sapan Bhatiaba74c752014-12-22 01:43:05 -050053 site_deployment=site_deployment)
Tony Mack6d2a98c2014-06-03 21:08:39 -040054 new_nodes.append(node)
Tony Macke4be32f2014-03-11 20:45:25 -040055
56 return new_nodes
57
Tony Macke1509e82013-10-09 12:38:04 -040058
59 def sync_record(self, node):
60 node.save()
61