blob: 7a5c0dc559432936bd130e9ac07a50480c63d74c [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
6from core.models.site import *
Sapan Bhatia26d40bc2014-05-12 15:28:02 -04007from ec2_observer.awslib import *
Sapan Bhatia511ea5f2014-07-21 22:53:58 -04008import pdb
Sapan Bhatia26d40bc2014-05-12 15:28:02 -04009
10class SyncSites(SyncStep):
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040011 provides=[Site]
12 requested_interval=3600
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040013
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040014 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 Bhatia26d40bc2014-05-12 15:28:02 -040022 available_sites = [zone['ZoneName'] for zone in zones]
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040023 site_names = [sd.site.name for sd in current_site_deployments]
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040024
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040025 new_site_names = list(set(available_sites) - set(site_names))
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040026
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 Bhatia511ea5f2014-07-21 22:53:58 -040037 return new_sites
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040038
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040039 def sync_record(self, site):
40 site.save()
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040041