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 | 39e8558 | 2014-11-19 15:07:36 -0500 | [diff] [blame^] | 10 | class SyncSiteDeployments(SyncStep): |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame] | 11 | requested_interval=86400 |
Sapan Bhatia | 39e8558 | 2014-11-19 15:07:36 -0500 | [diff] [blame^] | 12 | provides=[SiteDeployments] |
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 [] |
| 17 | |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -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 | 7b13010 | 2014-07-23 16:39:41 -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]) |
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 | # 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] |
Sapan Bhatia | 39e8558 | 2014-11-19 15:07:36 -0500 | [diff] [blame^] | 32 | current_site_deployments = SiteDeployments.objects.filter(Q(deployment=deployment)) |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame] | 33 | site_dict = {} |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 34 | |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame] | 35 | for sd in current_site_deployments: |
| 36 | site_dict[sd.site]=sd |
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 | updated_site_deployments = [] |
| 39 | for site in current_sites: |
| 40 | try: |
| 41 | site_record = site_dict[site] |
| 42 | except KeyError: |
Sapan Bhatia | 39e8558 | 2014-11-19 15:07:36 -0500 | [diff] [blame^] | 43 | sd = SiteDeployments(site=site,deployment=deployment,tenant_id=base64.urlsafe_b64encode(os.urandom(12))) |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame] | 44 | updated_site_deployments.append(sd) |
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 updated_site_deployments |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 47 | |
| 48 | |
Sapan Bhatia | 7b13010 | 2014-07-23 16:39:41 -0400 | [diff] [blame] | 49 | def sync_record(self, site_deployment): |
| 50 | site_deployment.save() |