Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
| 3 | from django.db.models import F, Q |
| 4 | from planetstack.config import Config |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 5 | from ec2_observer.syncstep import SyncStep |
| 6 | from core.models.site import * |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 7 | from ec2_observer.awslib import * |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 8 | import pdb |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 9 | |
| 10 | class SyncSites(SyncStep): |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame] | 11 | provides=[Site] |
| 12 | requested_interval=3600 |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 13 | |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame] | 14 | def fetch_pending(self, deletion): |
Sapan Bhatia | e7e4ca1 | 2014-07-22 01:27:02 -0400 | [diff] [blame] | 15 | if (deletion): |
| 16 | return [] |
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 | deployment = Deployment.objects.filter(Q(name="Amazon EC2"))[0] |
Sapan Bhatia | b55f4a3 | 2014-11-19 15:07:36 -0500 | [diff] [blame] | 19 | current_site_deployments = SiteDeployments.objects.filter(Q(deployment=deployment)) |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 20 | |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame] | 21 | zone_ret = aws_run('ec2 describe-availability-zones') |
| 22 | zones = zone_ret['AvailabilityZones'] |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 23 | |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame] | 24 | available_sites = [zone['ZoneName'] for zone in zones] |
| 25 | site_names = [sd.site.name for sd in current_site_deployments] |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 26 | |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame] | 27 | new_site_names = list(set(available_sites) - set(site_names)) |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 28 | |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame] | 29 | new_sites = [] |
| 30 | for s in new_site_names: |
| 31 | site = Site(name=s, |
| 32 | login_base=s, |
| 33 | site_url="www.amazon.com", |
| 34 | enabled=True, |
| 35 | is_public=True, |
| 36 | abbreviated_name=s) |
| 37 | new_sites.append(site) |
| 38 | |
| 39 | return new_sites |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 40 | |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame] | 41 | def sync_record(self, site): |
| 42 | site.save() |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 43 | |