Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
| 3 | import random |
| 4 | import time |
| 5 | from datetime import datetime |
| 6 | from django.db.models import F, Q |
| 7 | from planetstack.config import Config |
| 8 | from ec2_observer.syncstep import SyncStep |
| 9 | from core.models.node import Node |
| 10 | from core.models.site import * |
| 11 | from ec2_observer.awslib import * |
| 12 | import pdb |
| 13 | |
| 14 | class SyncNodes(SyncStep): |
| 15 | provides=[Node] |
| 16 | requested_interval=0 |
| 17 | |
| 18 | def fetch_pending(self, deletion): |
| 19 | deployment = Deployment.objects.filter(Q(name="Amazon EC2"))[0] |
| 20 | current_site_deployments = SiteDeployments.objects.filter(Q(deployment=deployment)) |
| 21 | |
| 22 | zone_ret = aws_run('ec2 describe-availability-zones') |
| 23 | zones = zone_ret['AvailabilityZones'] |
| 24 | |
| 25 | # collect local nodes |
| 26 | instance_types = 'm1.small | m1.medium | m1.large | m1.xlarge | m3.medium | m3.large | m3.xlarge | m3.2xlarge'.split(' | ') |
| 27 | |
| 28 | all_new_nodes = [] |
| 29 | for sd in current_site_deployments: |
| 30 | s = sd.site |
| 31 | current_fqns = [n.name for n in s.nodes.all()] |
| 32 | all_fqns = ['.'.join([n,s.name]) for n in instance_types] |
| 33 | new_node_names = list(set(all_fqns) - set(current_fqns)) |
| 34 | |
| 35 | new_nodes = [] |
| 36 | for node_name in new_node_names: |
| 37 | node = Node(name=node_name, |
| 38 | site=s,deployment=deployment) |
| 39 | new_nodes.append(node) |
| 40 | |
| 41 | all_new_nodes.extend(new_nodes) |
| 42 | |
| 43 | return all_new_nodes |
| 44 | |
| 45 | |
| 46 | def sync_record(self, node): |
| 47 | node.save() |
| 48 | |