Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
| 3 | from django.db.models import F, Q |
Scott Baker | 86e132c | 2015-02-11 21:38:09 -0800 | [diff] [blame^] | 4 | from xos.config import Config, XOS_DIR |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 5 | from ec2_observer.syncstep import SyncStep |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 6 | from core.models.image import Image |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 7 | from ec2_observer.awslib import * |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 8 | |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 9 | |
| 10 | class SyncImages(SyncStep): |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 11 | provides=[Image] |
| 12 | requested_interval=3600 |
| 13 | |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 14 | def fetch_pending(self,deletion): |
Sapan Bhatia | e7e4ca1 | 2014-07-22 01:27:02 -0400 | [diff] [blame] | 15 | if (deletion): |
| 16 | return [] |
| 17 | |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 18 | images = Image.objects.all() |
| 19 | image_names = [image.name for image in images] |
| 20 | |
| 21 | new_images = [] |
| 22 | |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 23 | try: |
Scott Baker | a0a86e6 | 2015-02-04 23:30:19 -0800 | [diff] [blame] | 24 | aws_images = json.loads(open(XOS_DIR + '/aws-images').read()) |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 25 | except: |
| 26 | aws_images = aws_run('ec2 describe-images --owner 099720109477') |
Scott Baker | a0a86e6 | 2015-02-04 23:30:19 -0800 | [diff] [blame] | 27 | open(XOS_DIR + '/aws-images','w').write(json.dumps(aws_images)) |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 28 | |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 29 | |
| 30 | |
| 31 | aws_images=aws_images['Images'] |
| 32 | aws_images=filter(lambda x:x['ImageType']=='machine',aws_images)[:50] |
| 33 | |
| 34 | names = set([]) |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 35 | for aws_image in aws_images: |
Sapan Bhatia | 511ea5f | 2014-07-21 22:53:58 -0400 | [diff] [blame] | 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 |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 61 | return new_images |
| 62 | |
| 63 | def sync_record(self, image): |
| 64 | image.save() |