This tree was mixed up, with an old version of the EC2 Observer. This
change copies across the version demo'd the OpenCloud developer meeting.
diff --git a/planetstack/ec2_observer/steps/__init__.py b/planetstack/ec2_observer/steps/__init__.py
index eabf46c..e56c4b1 100644
--- a/planetstack/ec2_observer/steps/__init__.py
+++ b/planetstack/ec2_observer/steps/__init__.py
@@ -1,15 +1,15 @@
 #from .sync_external_routes import SyncExternalRoutes
-from .sync_network_slivers import SyncNetworkSlivers
-from .sync_networks import SyncNetworks
-from .sync_network_deployments import SyncNetworkDeployments
-from .sync_site_privileges import SyncSitePrivileges
-from .sync_sites import SyncSites
-from .sync_slice_memberships import SyncSliceMemberships
-from .sync_slices import SyncSlices
+#from .sync_network_slivers import SyncNetworkSlivers
+#from .sync_networks import SyncNetworks
+#from .sync_network_deployments import SyncNetworkDeployments
+#from .sync_site_privileges import SyncSitePrivileges
+#from .sync_slice_memberships import SyncSliceMemberships
+#from .sync_slices import SyncSlices
 #from .sync_sliver_ips import SyncSliverIps
-from .sync_slivers import SyncSlivers
-from .sync_users import SyncUsers
-from .sync_roles import SyncRoles
-from .sync_nodes import SyncNodes
-from .sync_images import SyncImages
-from .garbage_collector import GarbageCollector
+#from .sync_slivers import SyncSlivers
+#from .sync_users import SyncUsers
+#from .sync_roles import SyncRoles
+#from .sync_nodes import SyncNodes
+#from .sync_images import SyncImages
+#from .garbage_collector import GarbageCollector
+
diff --git a/planetstack/ec2_observer/steps/sync_deployments.py b/planetstack/ec2_observer/steps/sync_deployments.py
new file mode 100644
index 0000000..8a258f1
--- /dev/null
+++ b/planetstack/ec2_observer/steps/sync_deployments.py
@@ -0,0 +1,22 @@
+import os
+import base64
+from django.db.models import F, Q
+from planetstack.config import Config
+from ec2_observer.syncstep import SyncStep
+from core.models.site import *
+
+class SyncDeployments(SyncStep):
+    requested_interval=86400
+    provides=[Deployment]
+
+    def fetch_pending(self,deletion):
+        deployments = Deployment.objects.filter(Q(name="Amazon EC2"))
+        if (not deployments):
+            deployments = [Deployment(name="Amazon EC2")]
+        else:
+            deployments = []
+
+        return deployments
+
+    def sync_record(self, deployment):
+        deployment.save()
diff --git a/planetstack/ec2_observer/steps/sync_images.py b/planetstack/ec2_observer/steps/sync_images.py
index 32b3363..3a15b33 100644
--- a/planetstack/ec2_observer/steps/sync_images.py
+++ b/planetstack/ec2_observer/steps/sync_images.py
@@ -2,30 +2,59 @@
 import base64
 from django.db.models import F, Q
 from planetstack.config import Config
-from observer.syncstep import SyncStep
+from ec2_observer.syncstep import SyncStep
 from core.models.image import Image
-from awslib import *
+from ec2_observer.awslib import *
 
-class SyncImages(OpenStackSyncStep):
+
+class SyncImages(SyncStep):
     provides=[Image]
     requested_interval=3600
 
-    def fetch_pending(self):
+    def fetch_pending(self,deletion):
         images = Image.objects.all()
         image_names = [image.name for image in images]
        
         new_images = []
 
-		aws_images = aws_run('ec2 describe-images')
+        try:
+            aws_images = json.loads(open('/opt/planetstack/aws-images').read())
+        except:
+            aws_images = aws_run('ec2 describe-images --owner 099720109477')
+            open('/opt/planetstack/aws-images','w').write(json.dumps(aws_images))
 
+        
+
+        aws_images=aws_images['Images']
+        aws_images=filter(lambda x:x['ImageType']=='machine',aws_images)[:50]
+
+        names = set([])
         for aws_image in aws_images:
-            if aws_image not in image_names:
-                image = Image(image_id=image_id,
-                              name=aws_image['name'],
-                              disk_format='XXX'
-                              container_format='XXX'
-                new_images.append(image)   
- 
+            desc_ok = True
+
+            try:
+                desc = aws_image['Description']
+            except:
+                try:
+                    desc = aws_image['ImageLocation']
+                except:
+                    desc_ok = False
+            
+            if (desc_ok):
+                try:
+                    desc_ok =  desc and desc not in names and desc not in image_names and '14.04' in desc
+                except KeyError:
+                    desc_ok = False
+
+            if desc_ok and aws_image['ImageType']=='machine':
+                image = Image(disk_format=aws_image['ImageLocation'],
+                                name=desc,
+                                container_format=aws_image['Hypervisor'],
+                                path=aws_image['ImageId'])
+                new_images.append(image)
+                names.add(image.name)
+
+        #print new_images
         return new_images
 
     def sync_record(self, image):
diff --git a/planetstack/ec2_observer/steps/sync_nodes.py b/planetstack/ec2_observer/steps/sync_nodes.py
new file mode 100644
index 0000000..b66eb2e
--- /dev/null
+++ b/planetstack/ec2_observer/steps/sync_nodes.py
@@ -0,0 +1,48 @@
+import os
+import base64
+import random
+import time
+from datetime import datetime 
+from django.db.models import F, Q
+from planetstack.config import Config
+from ec2_observer.syncstep import SyncStep
+from core.models.node import Node
+from core.models.site import *
+from ec2_observer.awslib import *
+import pdb
+
+class SyncNodes(SyncStep):
+	provides=[Node]
+	requested_interval=0
+
+	def fetch_pending(self, deletion):
+		deployment = Deployment.objects.filter(Q(name="Amazon EC2"))[0]
+		current_site_deployments = SiteDeployments.objects.filter(Q(deployment=deployment))
+
+		zone_ret = aws_run('ec2 describe-availability-zones')
+		zones = zone_ret['AvailabilityZones']
+
+		# collect local nodes
+		instance_types = 'm1.small | m1.medium | m1.large | m1.xlarge | m3.medium | m3.large | m3.xlarge | m3.2xlarge'.split(' | ')
+
+		all_new_nodes = []
+		for sd in current_site_deployments:
+			s = sd.site
+			current_fqns = [n.name for n in s.nodes.all()]
+			all_fqns = ['.'.join([n,s.name]) for n in instance_types]
+			new_node_names = list(set(all_fqns) - set(current_fqns))
+
+			new_nodes = []
+			for node_name in new_node_names:
+				node = Node(name=node_name,
+							site=s,deployment=deployment)
+				new_nodes.append(node)
+
+			all_new_nodes.extend(new_nodes)
+
+		return all_new_nodes
+				 
+
+	def sync_record(self, node):
+		node.save()
+		  
diff --git a/planetstack/ec2_observer/steps/sync_site_deployments.py b/planetstack/ec2_observer/steps/sync_site_deployments.py
index a996c85..30fdf97 100644
--- a/planetstack/ec2_observer/steps/sync_site_deployments.py
+++ b/planetstack/ec2_observer/steps/sync_site_deployments.py
@@ -2,27 +2,46 @@
 import base64
 from django.db.models import F, Q
 from planetstack.config import Config
-from observer.openstacksyncstep import OpenStackSyncStep
+from ec2_observer.syncstep import SyncStep
 from core.models.site import *
+from ec2_observer.awslib import *
+import pdb
 
-class SyncSiteDeployments(OpenStackSyncStep):
-    requested_interval=0
-    provides=[Site, SiteDeployments]
+class SyncSiteDeployments(SyncStep):
+	requested_interval=86400
+	provides=[SiteDeployments]
 
-    def fetch_pending(self):
-        return SiteDeployments.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
+	def fetch_pending(self, deletion):
+		zone_ret = aws_run('ec2 describe-availability-zones')
+		zones = zone_ret['AvailabilityZones']
+		available_sites = [zone['ZoneName'] for zone in zones]
 
-    def sync_record(self, site_deployment):
-        if not site_deployment.tenant_id:
-            driver = self.driver.admin_driver(deployment=site_deployment.deployment.name)
-            tenant = driver.create_tenant(tenant_name=site_deployment.site.login_base,
-                                               description=site_deployment.site.name,
-                                               enabled=site_deployment.site.enabled)
-            site_deployment.tenant_id = tenant.id
-            site_deployment.save()
-        elif site_deployment.site.id and site_deployment.tenant_id:
-            driver = self.driver.admin_driver(deployment=site_deployment.name)
-            driver.update_tenant(site_deployment.tenant_id,
-                                 description=site_deployment.site.name,
-                                 enabled=site_deployment.site.enabled)
-            
+		current_sites = []
+		for s in available_sites:
+			site = Site.objects.filter(Q(name=s))
+			if (site):
+				current_sites.append(site[0])
+
+		# OK not to catch exception
+		# The syncstep should catch it
+		# At any rate, we should not run if there are no deployments
+		deployment = Deployment.objects.filter(Q(name="Amazon EC2"))[0]
+		current_site_deployments = SiteDeployments.objects.filter(Q(deployment=deployment))
+		site_dict = {}
+
+		for sd in current_site_deployments:
+			site_dict[sd.site]=sd
+
+		updated_site_deployments = []
+		for site in current_sites:
+			try:
+				site_record = site_dict[site]
+			except KeyError:
+				sd = SiteDeployments(site=site,deployment=deployment,tenant_id=base64.urlsafe_b64encode(os.urandom(12)))
+				updated_site_deployments.append(sd)
+
+		return updated_site_deployments
+
+
+	def sync_record(self, site_deployment):
+		site_deployment.save()
diff --git a/planetstack/ec2_observer/steps/sync_sites.py b/planetstack/ec2_observer/steps/sync_sites.py
index 5771aef..7a5c0dc 100644
--- a/planetstack/ec2_observer/steps/sync_sites.py
+++ b/planetstack/ec2_observer/steps/sync_sites.py
@@ -2,20 +2,27 @@
 import base64
 from django.db.models import F, Q
 from planetstack.config import Config
-from observer.syncstep import SyncStep
-from core.models.site import Site
+from ec2_observer.syncstep import SyncStep
+from core.models.site import *
 from ec2_observer.awslib import *
+import pdb
 
 class SyncSites(SyncStep):
-    provides=[Site]
-    requested_interval=3600
+	provides=[Site]
+	requested_interval=3600
 
-    def fetch_pending(self):
-		current_sites = Site.objects.all()
-		zones = aws_run('ec2 describe-availability-zones')
+	def fetch_pending(self, deletion):
+
+		deployment = Deployment.objects.filter(Q(name="Amazon EC2"))[0]
+		current_site_deployments = SiteDeployments.objects.filter(Q(deployment=deployment))
+
+		zone_ret = aws_run('ec2 describe-availability-zones')
+		zones = zone_ret['AvailabilityZones']
+
 		available_sites = [zone['ZoneName'] for zone in zones]
+		site_names = [sd.site.name for sd in current_site_deployments]
 
-		new_site_names = list(set(available_sites) - set(zones))
+		new_site_names = list(set(available_sites) - set(site_names))
 
 		new_sites = []
 		for s in new_site_names:
@@ -27,8 +34,8 @@
 						abbreviated_name=s)
 			new_sites.append(site)
 		
-        return new_sites
+		return new_sites
 
-    def sync_record(self, site):
-        site.save()
+	def sync_record(self, site):
+		site.save()
 
diff --git a/planetstack/ec2_observer/steps/sync_slivers.py b/planetstack/ec2_observer/steps/sync_slivers.py
index b576bbc..2079859 100644
--- a/planetstack/ec2_observer/steps/sync_slivers.py
+++ b/planetstack/ec2_observer/steps/sync_slivers.py
@@ -1,91 +1,103 @@
 import os
+import json
 import base64
 from django.db.models import F, Q
 from planetstack.config import Config
-from observer.openstacksyncstep import OpenStackSyncStep
+from ec2_observer.syncstep import SyncStep
 from core.models.sliver import Sliver
 from core.models.slice import SlicePrivilege, SliceDeployments
 from core.models.network import Network, NetworkSlice, NetworkDeployments
 from util.logger import Logger, logging
+from ec2_observer.awslib import *
+from core.models.site import *
+from core.models.slice import *
+import pdb
 
 logger = Logger(level=logging.INFO)
 
-class SyncSlivers(OpenStackSyncStep):
-    provides=[Sliver]
-    requested_interval=0
+class SyncSlivers(SyncStep):
+	provides=[Sliver]
+	requested_interval=0
 
-    def fetch_pending(self):
-        return Sliver.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
+	def fetch_pending(self, deletion):
+		all_slivers = Sliver.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
+		my_slivers = []	
 
-    def sync_record(self, sliver):
-        logger.info("sync'ing sliver:%s deployment:%s " % (sliver, sliver.node.deployment))
-        metadata_update = {}
-        if ("numberCores" in sliver.changed_fields):
-            metadata_update["cpu_cores"] = str(sliver.numberCores)
+		for sliver in all_slivers:
+			sd = SliceDeployments.objects.filter(Q(slice=sliver.slice))
+			if (sd):
+				if (sd.deployment.name=='Amazon EC2'):
+					my_slivers.append(sliver)
+			if (sliver.node.deployment.name=='Amazon EC2'):
+				my_slivers.append(sliver)
+		return my_slivers
 
-        for tag in sliver.slice.tags.all():
-            if tag.name.startswith("sysctl-"):
-                metadata_update[tag.name] = tag.value
+	def sync_record(self, sliver):
+		logger.info("sync'ing sliver:%s deployment:%s " % (sliver, sliver.node.deployment))
 
-        if not sliver.instance_id:
-            driver = self.driver.client_driver(caller=sliver.creator, tenant=sliver.slice.name, deployment=sliver.deploymentNetwork.name)
-            # public keys
-            slice_memberships = SlicePrivilege.objects.filter(slice=sliver.slice)
-            pubkeys = [sm.user.public_key for sm in slice_memberships if sm.user.public_key]
-            if sliver.creator.public_key:
-                pubkeys.append(sliver.creator.public_key)
-            if sliver.slice.creator.public_key:
-                pubkeys.append(sliver.slice.creator.public_key) 
-            # netowrks
-            # include all networks available to the slice and/or associated network templates
-            nics = []
-            networks = [ns.network for ns in NetworkSlice.objects.filter(slice=sliver.slice)]   
-            network_deployments = NetworkDeployments.objects.filter(network__in=networks, 
-                                                                    deployment=sliver.node.deployment)
-            # Gather private networks first. This includes networks with a template that has
-            # visibility = private and translation = none
-            for network_deployment in network_deployments:
-                if network_deployment.network.template.visibility == 'private' and \
-                   network_deployment.network.template.translation == 'none': 
-                    nics.append({'net-id': network_deployment.net_id})
-    
-            # now include network template
-            network_templates = [network.template.sharedNetworkName for network in networks \
-                                 if network.template.sharedNetworkName]
-            for net in driver.shell.quantum.list_networks()['networks']:
-                if net['name'] in network_templates: 
-                    nics.append({'net-id': net['id']}) 
+		if not sliver.instance_id:
+			# public keys
+			slice_memberships = SlicePrivilege.objects.filter(slice=sliver.slice)
+			pubkeys = [sm.user.public_key for sm in slice_memberships if sm.user.public_key]
 
-            file("/tmp/scott-manager","a").write("slice: %s\nreq: %s\n" % (str(sliver.slice.name), str(nics)))
-         
-            # look up image id
-            deployment_driver = self.driver.admin_driver(deployment=sliver.deploymentNetwork.name)
-            image_id = None
-            images = deployment_driver.shell.glance.get_images()
-            for image in images:
-                if image['name'] == sliver.image.name:
-                    image_id = image['id']
-                    
-            # look up key name at the deployment
-            # create/fetch keypair
-            keyname = None
-            if sliver.creator.public_key:
-                keyname = sliver.creator.email.lower().replace('@', 'AT').replace('.', '') +\
-                          sliver.slice.name
-                key_fields =  {'name': keyname,
-                               'public_key': sliver.creator.public_key}
-                driver.create_keypair(**key_fields)       
- 
-            instance = driver.spawn_instance(name=sliver.name,
-                                key_name = keyname,
-                                image_id = image_id,
-                                hostname = sliver.node.name,
-                                pubkeys = pubkeys,
-                                nics = nics )
-            sliver.instance_id = instance.id
-            sliver.instance_name = getattr(instance, 'OS-EXT-SRV-ATTR:instance_name')
-            sliver.save()    
+			if sliver.creator.public_key:
+				pubkeys.append(sliver.creator.public_key)
 
-        if sliver.instance_id and metadata_update:
-            driver.update_instance_metadata(sliver.instance_id, metadata_update)
+			if sliver.slice.creator.public_key:
+				pubkeys.append(sliver.slice.creator.public_key) 
+
+			# netowrks
+			# include all networks available to the slice and/or associated network templates
+			#nics = []
+			#networks = [ns.network for ns in NetworkSlice.objects.filter(slice=sliver.slice)]	
+			#network_deployments = NetworkDeployments.objects.filter(network__in=networks, 
+																	#deployment=sliver.node.deployment)
+			# Gather private networks first. This includes networks with a template that has
+			# visibility = private and translation = none
+			#for network_deployment in network_deployments:
+			#	if network_deployment.network.template.visibility == 'private' and \
+			#	   network_deployment.network.template.translation == 'none': 
+			#		nics.append({'net-id': network_deployment.net_id})
+	
+			# now include network template
+			#network_templates = [network.template.sharedNetworkName for network in networks \
+			#					 if network.template.sharedNetworkName]
+			#for net in driver.shell.quantum.list_networks()['networks']:
+			#	if net['name'] in network_templates: 
+			#		nics.append({'net-id': net['id']}) 
+			# look up image id
+
+			instance_type = sliver.node.name.rsplit('.',1)[0]
+
+			# Bail out of we don't have a key
+			key_name = sliver.creator.email.lower().replace('@', 'AT').replace('.', '')
+			key_sig = aws_run('ec2 describe-key-pairs')
+			ec2_keys = key_sig['KeyPairs']
+			key_found = False
+			for key in ec2_keys:
+				if (key['KeyName']==key_name):
+					key_found = True
+					break
+
+			if (not key_found):
+				# set backend_status
+				raise Exception('Will not sync sliver without key')
+
+			image_id = sliver.image.path
+			instance_sig = aws_run('ec2 run-instances --image-id %s --instance-type %s --count 1 --key-name %s --placement AvailabilityZone=%s'%(image_id,instance_type,key_name,sliver.node.site.name))
+			sliver.instance_id = instance_sig['Instances'][0]['InstanceId']
+			sliver.save()
+			state = instance_sig['Instances'][0]['State']['Code']
+			if (state==16):
+				sliver.ip = instance_sig['Instances'][0]['PublicIpAddress']
+				sliver.save()
+			else:
+				# This status message should go into backend_status
+				raise Exception('Waiting for instance to start')
+		else:
+			ret = aws_run('ec2 describe-instances --instance-ids %s'%sliver.instance_id)
+			state = ret['Reservations'][0]['Instances'][0]['State']['Code']
+			if (state==16):
+				sliver.ip = ret['Reservations'][0]['Instances'][0]['PublicIpAddress']
+				sliver.save()
 
diff --git a/planetstack/ec2_observer/steps/sync_users.py b/planetstack/ec2_observer/steps/sync_users.py
index 71f9c0f..d742186 100644
--- a/planetstack/ec2_observer/steps/sync_users.py
+++ b/planetstack/ec2_observer/steps/sync_users.py
@@ -1,20 +1,43 @@
 import os
 import base64
-import hashlib
+import random
+import time
+from datetime import datetime 
 from django.db.models import F, Q
 from planetstack.config import Config
-from observer.openstacksyncstep import OpenStackSyncStep
-from core.models.user import User, UserDeployments
+from ec2_observer.syncstep import SyncStep
+from core.models.user import User
+from core.models.site import *
+from ec2_observer.awslib import *
+import pdb
 
-class SyncUsers(OpenStackSyncStep):
-    provides=[User]
-    requested_interval=0
+class SyncUsers(SyncStep):
+	provides=[User]
+	requested_interval=0
 
-    def fetch_pending(self):
-        return User.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
+	def fetch_pending(self, deletion):
+		users = User.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
+		if (users):
+			key_sig = aws_run('ec2 describe-key-pairs')
+			ec2_keys = key_sig['KeyPairs']
+		else:
+			ec2_keys = []
 
-    def sync_record(self, user):
-        for user_deployment in UserDeployments.objects.filter(user=user):
-            # bump the 'updated' field so user account are updated across 
-            # deployments.
-            user_deployment.save()
+		for user in users:
+			if (user.public_key): 
+				key_name = user.email.lower().replace('@', 'AT').replace('.', '')
+				key_found = False
+
+				for key in ec2_keys:
+					if (key['KeyName']==key_name):
+						key_found = True
+						break
+
+				if (not key_found):
+					aws_run('ec2 import-key-pair --key-name %s --public-key-material "%s"'%(key_name, user.public_key))
+					
+		return users
+
+	def sync_record(self, node):
+		node.save()
+