Explicitly prevent deletion processing in steps in which it doesn't make
[plstackapi.git] / planetstack / ec2_observer / steps / sync_images.py
1 import os
2 import base64
3 from django.db.models import F, Q
4 from planetstack.config import Config
5 from ec2_observer.syncstep import SyncStep
6 from core.models.image import Image
7 from ec2_observer.awslib import *
8
9
10 class SyncImages(SyncStep):
11     provides=[Image]
12     requested_interval=3600
13
14     def fetch_pending(self,deletion):
15         if (deletion):
16             return []
17
18         images = Image.objects.all()
19         image_names = [image.name for image in images]
20        
21         new_images = []
22
23         try:
24             aws_images = json.loads(open('/opt/planetstack/aws-images').read())
25         except:
26             aws_images = aws_run('ec2 describe-images --owner 099720109477')
27             open('/opt/planetstack/aws-images','w').write(json.dumps(aws_images))
28
29         
30
31         aws_images=aws_images['Images']
32         aws_images=filter(lambda x:x['ImageType']=='machine',aws_images)[:50]
33
34         names = set([])
35         for aws_image in aws_images:
36             desc_ok = True
37
38             try:
39                 desc = aws_image['Description']
40             except:
41                 try:
42                     desc = aws_image['ImageLocation']
43                 except:
44                     desc_ok = False
45             
46             if (desc_ok):
47                 try:
48                     desc_ok =  desc and desc not in names and desc not in image_names and '14.04' in desc
49                 except KeyError:
50                     desc_ok = False
51
52             if desc_ok and aws_image['ImageType']=='machine':
53                 image = Image(disk_format=aws_image['ImageLocation'],
54                                 name=desc,
55                                 container_format=aws_image['Hypervisor'],
56                                 path=aws_image['ImageId'])
57                 new_images.append(image)
58                 names.add(image.name)
59
60         #print new_images
61         return new_images
62
63     def sync_record(self, image):
64         image.save()