blob: 0fa8b61d616c05e8640aa1b544487b02a21a3473 [file] [log] [blame]
Sapan Bhatia511ea5f2014-07-21 22:53:58 -04001import os
2import base64
3import random
4import time
5from datetime import datetime
6from django.db.models import F, Q
7from planetstack.config import Config
8from ec2_observer.syncstep import SyncStep
9from core.models.node import Node
10from core.models.site import *
11from ec2_observer.awslib import *
12import pdb
13
14class SyncNodes(SyncStep):
Sapan Bhatia7b130102014-07-23 16:39:41 -040015 provides=[Node]
16 requested_interval=0
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040017
Sapan Bhatia7b130102014-07-23 16:39:41 -040018 def fetch_pending(self, deletion):
Sapan Bhatiae7e4ca12014-07-22 01:27:02 -040019 if (deletion):
20 return []
21
Sapan Bhatia7b130102014-07-23 16:39:41 -040022 deployment = Deployment.objects.filter(Q(name="Amazon EC2"))[0]
Tony Mack3066a952015-01-05 22:48:11 -050023 current_site_deployments = SiteDeployment.objects.filter(Q(deployment=deployment))
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040024
Sapan Bhatia7b130102014-07-23 16:39:41 -040025 zone_ret = aws_run('ec2 describe-availability-zones')
26 zones = zone_ret['AvailabilityZones']
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040027
Sapan Bhatia7b130102014-07-23 16:39:41 -040028 # collect local nodes
29 instance_types = 'm1.small | m1.medium | m1.large | m1.xlarge | m3.medium | m3.large | m3.xlarge | m3.2xlarge'.split(' | ')
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040030
Sapan Bhatia7b130102014-07-23 16:39:41 -040031 all_new_nodes = []
32 for sd in current_site_deployments:
33 s = sd.site
34 current_fqns = [n.name for n in s.nodes.all()]
35 all_fqns = ['.'.join([n,s.name]) for n in instance_types]
36 new_node_names = list(set(all_fqns) - set(current_fqns))
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040037
Sapan Bhatia7b130102014-07-23 16:39:41 -040038 new_nodes = []
39 for node_name in new_node_names:
40 node = Node(name=node_name,
41 site=s,deployment=deployment)
42 new_nodes.append(node)
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040043
Sapan Bhatia7b130102014-07-23 16:39:41 -040044 all_new_nodes.extend(new_nodes)
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040045
Sapan Bhatia7b130102014-07-23 16:39:41 -040046 return all_new_nodes
47
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040048
Sapan Bhatia7b130102014-07-23 16:39:41 -040049 def sync_record(self, node):
50 node.save()
51