blob: e2c63bc6b57ccf4f780afdcc40100368901a6c9c [file] [log] [blame]
Tony Macke1509e82013-10-09 12:38:04 -04001import os
2import base64
3import random
Scott Baker1fd5d222014-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 Mack3066a952015-01-05 22:48:11 -05009from core.models.site import SiteDeployment, Controller
Scott Baker1fd5d222014-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 Bhatiaafa037d2014-12-22 01:43:05 -050025 controllers = Controller.objects.all()
Tony Macke1509e82013-10-09 12:38:04 -040026 nodes = Node.objects.all()
Sapan Bhatia14356b72014-11-05 10:32:41 -050027 node_hostnames = [node.name for node in nodes]
Tony Macke1509e82013-10-09 12:38:04 -040028
Tony Mack06c8e472014-11-30 15:53:08 -050029 # fetch all nodes from each controller
Tony Mack026ffbe2014-05-07 13:31:55 -040030 new_nodes = []
Tony Mack06c8e472014-11-30 15:53:08 -050031 for controller in controllers:
Scott Baker1fd5d222014-09-04 11:04:27 -070032 try:
Tony Mack3066a952015-01-05 22:48:11 -050033 controller_site_deployments = SiteDeployment.objects.filter(controller=controller)[0]
Sapan Bhatiaafa037d2014-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 Baker1fd5d222014-09-04 11:04:27 -070043 compute_nodes = driver.shell.nova.hypervisors.list()
44 except:
Tony Mack06c8e472014-11-30 15:53:08 -050045 logger.log_exc("Failed to get nodes from controller %s" % str(controller))
Scott Baker1fd5d222014-09-04 11:04:27 -070046 continue
47
Tony Mack6d2a98c2014-06-03 21:08:39 -040048 for compute_node in compute_nodes:
Scott Bakerc217c0f2014-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 Bhatiaafa037d2014-12-22 01:43:05 -050051 # XX pick the first one
Scott Bakerc217c0f2014-09-22 17:46:35 -070052 node = Node(name=compute_node.hypervisor_hostname,
Sapan Bhatiaafa037d2014-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