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): |
Sapan Bhatia | e7e4ca1 | 2014-07-22 01:27:02 -0400 | [diff] [blame] | 19 | if (deletion): |
| 20 | return [] |
| 21 | |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 22 | deployment = Deployment.objects.filter(Q(name="Amazon EC2"))[0] |
| 23 | current_site_deployments = SiteDeployments.objects.filter(Q(deployment=deployment)) |
| 24 | |
| 25 | zone_ret = aws_run('ec2 describe-availability-zones') |
| 26 | zones = zone_ret['AvailabilityZones'] |
| 27 | |
| 28 | # collect local nodes |
| 29 | instance_types = 'm1.small | m1.medium | m1.large | m1.xlarge | m3.medium | m3.large | m3.xlarge | m3.2xlarge'.split(' | ') |
| 30 | |
| 31 | 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)) |
| 37 | |
| 38 | 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) |
| 43 | |
| 44 | all_new_nodes.extend(new_nodes) |
| 45 | |
| 46 | return all_new_nodes |
| 47 | |
| 48 | |
| 49 | def sync_record(self, node): |
| 50 | node.save() |
| 51 | |