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): |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame] | 15 | provides=[Node] |
| 16 | requested_interval=0 |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 17 | |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame] | 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 | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame] | 22 | deployment = Deployment.objects.filter(Q(name="Amazon EC2"))[0] |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 23 | current_site_deployments = SiteDeployment.objects.filter(Q(deployment=deployment)) |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 24 | |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame] | 25 | zone_ret = aws_run('ec2 describe-availability-zones') |
| 26 | zones = zone_ret['AvailabilityZones'] |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 27 | |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame] | 28 | # collect local nodes |
| 29 | instance_types = 'm1.small | m1.medium | m1.large | m1.xlarge | m3.medium | m3.large | m3.xlarge | m3.2xlarge'.split(' | ') |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 30 | |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame] | 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)) |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 37 | |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame] | 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) |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 43 | |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame] | 44 | all_new_nodes.extend(new_nodes) |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 45 | |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame] | 46 | return all_new_nodes |
| 47 | |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 48 | |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame] | 49 | def sync_record(self, node): |
| 50 | node.save() |
| 51 | |