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 |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 6 | from core.models.site import * |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 7 | from ec2_observer.awslib import * |
| 8 | import pdb |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 9 | |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 10 | class SyncSiteDeployments(SyncStep): |
| 11 | requested_interval=86400 |
| 12 | provides=[SiteDeployments] |
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 | zone_ret = aws_run('ec2 describe-availability-zones') |
| 16 | zones = zone_ret['AvailabilityZones'] |
| 17 | available_sites = [zone['ZoneName'] for zone in zones] |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 18 | |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 19 | current_sites = [] |
| 20 | for s in available_sites: |
| 21 | site = Site.objects.filter(Q(name=s)) |
| 22 | if (site): |
| 23 | current_sites.append(site[0]) |
| 24 | |
| 25 | # OK not to catch exception |
| 26 | # The syncstep should catch it |
| 27 | # At any rate, we should not run if there are no deployments |
| 28 | deployment = Deployment.objects.filter(Q(name="Amazon EC2"))[0] |
| 29 | current_site_deployments = SiteDeployments.objects.filter(Q(deployment=deployment)) |
| 30 | site_dict = {} |
| 31 | |
| 32 | for sd in current_site_deployments: |
| 33 | site_dict[sd.site]=sd |
| 34 | |
| 35 | updated_site_deployments = [] |
| 36 | for site in current_sites: |
| 37 | try: |
| 38 | site_record = site_dict[site] |
| 39 | except KeyError: |
| 40 | sd = SiteDeployments(site=site,deployment=deployment,tenant_id=base64.urlsafe_b64encode(os.urandom(12))) |
| 41 | updated_site_deployments.append(sd) |
| 42 | |
| 43 | return updated_site_deployments |
| 44 | |
| 45 | |
| 46 | def sync_record(self, site_deployment): |
| 47 | site_deployment.save() |