blob: d648b7da7119915a607ef9baf57675cd3f0949c7 [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 Mack91463d92014-06-19 20:42:03 -04009from core.models.site import Site, Deployment
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 Macke4be32f2014-03-11 20:45:25 -040025 sites = Site.objects.all()
Tony Macke1509e82013-10-09 12:38:04 -040026 nodes = Node.objects.all()
27 node_hostnames = [node.name for node in nodes]
28
Scott Baker1fd5d222014-09-04 11:04:27 -070029 # fetch all nodes from each deployment
Tony Macke4be32f2014-03-11 20:45:25 -040030 deployments = Deployment.objects.all()
Tony Mack026ffbe2014-05-07 13:31:55 -040031 new_nodes = []
Tony Macke4be32f2014-03-11 20:45:25 -040032 for deployment in deployments:
Scott Baker1fd5d222014-09-04 11:04:27 -070033 try:
34 driver = self.driver.admin_driver(deployment=deployment.name)
35 compute_nodes = driver.shell.nova.hypervisors.list()
36 except:
37 logger.log_exc("Failed to get nodes from deployment %s" % str(deployment))
38 continue
39
Tony Mack6d2a98c2014-06-03 21:08:39 -040040 for compute_node in compute_nodes:
41 if compute_node.hypervisor_hostname not in node_hostnames:
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
44 site_index = random.randint(0, len(sites))
45 node = Node(name=compute_node.hypervisor_hostname,
46 site=sites[site_index], deployment=deployment)
47 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