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 | 511ea5f | 2014-07-21 22:53:58 -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 | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 14 | def fetch_pending(self, deletion): |
| 15 | |
| 16 | deployment = Deployment.objects.filter(Q(name="Amazon EC2"))[0] |
| 17 | current_site_deployments = SiteDeployments.objects.filter(Q(deployment=deployment)) |
| 18 | |
| 19 | zone_ret = aws_run('ec2 describe-availability-zones') |
| 20 | zones = zone_ret['AvailabilityZones'] |
| 21 | |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 22 | available_sites = [zone['ZoneName'] for zone in zones] |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 23 | site_names = [sd.site.name for sd in current_site_deployments] |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 24 | |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 25 | new_site_names = list(set(available_sites) - set(site_names)) |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 26 | |
| 27 | new_sites = [] |
| 28 | for s in new_site_names: |
| 29 | site = Site(name=s, |
| 30 | login_base=s, |
| 31 | site_url="www.amazon.com", |
| 32 | enabled=True, |
| 33 | is_public=True, |
| 34 | abbreviated_name=s) |
| 35 | new_sites.append(site) |
| 36 | |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 37 | return new_sites |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 38 | |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 39 | def sync_record(self, site): |
| 40 | site.save() |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 41 | |