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