blob: 740b5e1193ddfccbfdadb2cd57d9f1e6af7cacbe [file] [log] [blame]
Tony Macke1509e82013-10-09 12:38:04 -04001import os
2import base64
3import random
4from datetime import datetime
5from 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
Tony Macke1509e82013-10-09 12:38:04 -040010
11class SyncNodes(OpenStackSyncStep):
12 provides=[Node]
13 requested_interval=0
14
Sapan Bhatiab83643c2014-07-23 09:59:32 -040015 def fetch_pending(self, deleted):
16 # Nodes come from the back end
17 # You can't delete them
18 if (deleted):
19 return []
20
Tony Macke1509e82013-10-09 12:38:04 -040021 # collect local nodes
Tony Macke4be32f2014-03-11 20:45:25 -040022 sites = Site.objects.all()
Tony Macke1509e82013-10-09 12:38:04 -040023 nodes = Node.objects.all()
24 node_hostnames = [node.name for node in nodes]
25
Tony Macke4be32f2014-03-11 20:45:25 -040026 # fetch all nodes from each deployment
27 deployments = Deployment.objects.all()
Tony Mack026ffbe2014-05-07 13:31:55 -040028 new_nodes = []
Tony Macke4be32f2014-03-11 20:45:25 -040029 for deployment in deployments:
30 driver = self.driver.admin_driver(deployment=deployment.name)
31 compute_nodes = driver.shell.nova.hypervisors.list()
Tony Mack6d2a98c2014-06-03 21:08:39 -040032 for compute_node in compute_nodes:
33 if compute_node.hypervisor_hostname not in node_hostnames:
34 # XX TODO:figure out how to correctly identify a node's site.
35 # XX pick a random site to add the node to for now
36 site_index = random.randint(0, len(sites))
37 node = Node(name=compute_node.hypervisor_hostname,
38 site=sites[site_index], deployment=deployment)
39 new_nodes.append(node)
Tony Macke4be32f2014-03-11 20:45:25 -040040
41 return new_nodes
42
Tony Macke1509e82013-10-09 12:38:04 -040043
44 def sync_record(self, node):
45 node.save()
46