blob: 0959c71b34ec68088f3fde730f5353b8a4ea8f97 [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 Bhatia7b130102014-07-23 16:39:41 -040011 provides=[Site]
12 requested_interval=3600
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040013
Sapan Bhatia7b130102014-07-23 16:39:41 -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
Sapan Bhatia7b130102014-07-23 16:39:41 -040018 deployment = Deployment.objects.filter(Q(name="Amazon EC2"))[0]
Tony Macka7dbd422015-01-05 22:48:11 -050019 current_site_deployments = SiteDeployment.objects.filter(Q(deployment=deployment))
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040020
Sapan Bhatia7b130102014-07-23 16:39:41 -040021 zone_ret = aws_run('ec2 describe-availability-zones')
22 zones = zone_ret['AvailabilityZones']
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040023
Sapan Bhatia7b130102014-07-23 16:39:41 -040024 available_sites = [zone['ZoneName'] for zone in zones]
25 site_names = [sd.site.name for sd in current_site_deployments]
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040026
Sapan Bhatia7b130102014-07-23 16:39:41 -040027 new_site_names = list(set(available_sites) - set(site_names))
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040028
Sapan Bhatia7b130102014-07-23 16:39:41 -040029 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
39 return new_sites
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040040
Sapan Bhatia7b130102014-07-23 16:39:41 -040041 def sync_record(self, site):
42 site.save()
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040043