blob: c8d649046cbb9cdc01a3087ed6b4d3c31b03a807 [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):
Sapan Bhatiae7e4ca12014-07-22 01:27:02 -040015 if (deletion):
16 return []
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040017
18 deployment = Deployment.objects.filter(Q(name="Amazon EC2"))[0]
19 current_site_deployments = SiteDeployments.objects.filter(Q(deployment=deployment))
20
21 zone_ret = aws_run('ec2 describe-availability-zones')
22 zones = zone_ret['AvailabilityZones']
23
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040024 available_sites = [zone['ZoneName'] for zone in zones]
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040025 site_names = [sd.site.name for sd in current_site_deployments]
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040026
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040027 new_site_names = list(set(available_sites) - set(site_names))
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040028
29 new_sites = []
30 for s in new_site_names:
31 site = Site(name=s,
32 login_base=s,
33 site_url="www.amazon.com",
34 enabled=True,
35 is_public=True,
36 abbreviated_name=s)
37 new_sites.append(site)
38
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040039 return new_sites
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040040
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040041 def sync_record(self, site):
42 site.save()
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040043