blob: d5edb0254757cceee5ec54a9efa3d6c830937fc7 [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 Mack06c8e472014-11-30 15:53:08 -05009from core.models.site import SiteDeployments, 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
Tony Mack06c8e472014-11-30 15:53:08 -050025 site_deployments = SiteDeployments.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
30 controllers = Controller.objects.all()
Tony Mack026ffbe2014-05-07 13:31:55 -040031 new_nodes = []
Tony Mack06c8e472014-11-30 15:53:08 -050032 for controller in controllers:
Scott Baker1fd5d222014-09-04 11:04:27 -070033 try:
Tony Mackd14d48f2014-12-05 17:13:08 -050034 driver = self.driver.admin_driver(controller=controller)
Scott Baker1fd5d222014-09-04 11:04:27 -070035 compute_nodes = driver.shell.nova.hypervisors.list()
36 except:
Tony Mack06c8e472014-11-30 15:53:08 -050037 logger.log_exc("Failed to get nodes from controller %s" % str(controller))
Scott Baker1fd5d222014-09-04 11:04:27 -070038 continue
39
Tony Mack6d2a98c2014-06-03 21:08:39 -040040 for compute_node in compute_nodes:
Scott Bakerc217c0f2014-09-22 17:46:35 -070041 if compute_node.hypervisor_hostname not in node_hostnames:
Tony Mack6d2a98c2014-06-03 21:08:39 -040042 # 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 Mack06c8e472014-11-30 15:53:08 -050044 site_index = random.randint(0, len(site_deployments))
Scott Bakerc217c0f2014-09-22 17:46:35 -070045 node = Node(name=compute_node.hypervisor_hostname,
Tony Mackd14d48f2014-12-05 17:13:08 -050046 site_deployment=site_deployments[site_index])
Tony Mack6d2a98c2014-06-03 21:08:39 -040047 new_nodes.append(node)
Tony Macke4be32f2014-03-11 20:45:25 -040048
49 return new_nodes
50
Tony Macke1509e82013-10-09 12:38:04 -040051
52 def sync_record(self, node):
53 node.save()
54