blob: 54df732e784d0dfbb677c8bd08f3f9b227b358d3 [file] [log] [blame]
Sapan Bhatia26d40bc2014-05-12 15:28:02 -04001import os
2import base64
3from django.db.models import F, Q
4from planetstack.config import Config
Sapan Bhatia511ea5f2014-07-21 22:53:58 -04005from ec2_observer.syncstep import SyncStep
Sapan Bhatia26d40bc2014-05-12 15:28:02 -04006from core.models.site import *
Sapan Bhatia511ea5f2014-07-21 22:53:58 -04007from ec2_observer.awslib import *
8import pdb
Sapan Bhatia26d40bc2014-05-12 15:28:02 -04009
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040010class SyncSiteDeployments(SyncStep):
11 requested_interval=86400
12 provides=[SiteDeployments]
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040013
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040014 def fetch_pending(self, deletion):
Sapan Bhatiae7e4ca12014-07-22 01:27:02 -040015 if (deletion):
16 return []
17
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040018 zone_ret = aws_run('ec2 describe-availability-zones')
19 zones = zone_ret['AvailabilityZones']
20 available_sites = [zone['ZoneName'] for zone in zones]
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040021
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040022 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()