blob: c3dc5a126046d71c74274b6f19b7ae54be4debe6 [file] [log] [blame]
Sapan Bhatia26d40bc2014-05-12 15:28:02 -04001import os
2import base64
3from django.db.models import F, Q
Scott Baker86e132c2015-02-11 21:38:09 -08004from xos.config import Config, XOS_DIR
Sapan Bhatia511ea5f2014-07-21 22:53:58 -04005from ec2_observer.syncstep import SyncStep
Sapan Bhatia26d40bc2014-05-12 15:28:02 -04006from core.models.image import Image
Sapan Bhatia511ea5f2014-07-21 22:53:58 -04007from ec2_observer.awslib import *
Sapan Bhatia26d40bc2014-05-12 15:28:02 -04008
Sapan Bhatia511ea5f2014-07-21 22:53:58 -04009
10class SyncImages(SyncStep):
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040011 provides=[Image]
12 requested_interval=3600
13
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 []
17
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040018 images = Image.objects.all()
19 image_names = [image.name for image in images]
20
21 new_images = []
22
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040023 try:
Scott Bakera0a86e62015-02-04 23:30:19 -080024 aws_images = json.loads(open(XOS_DIR + '/aws-images').read())
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040025 except:
26 aws_images = aws_run('ec2 describe-images --owner 099720109477')
Scott Bakera0a86e62015-02-04 23:30:19 -080027 open(XOS_DIR + '/aws-images','w').write(json.dumps(aws_images))
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040028
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040029
30
31 aws_images=aws_images['Images']
32 aws_images=filter(lambda x:x['ImageType']=='machine',aws_images)[:50]
33
34 names = set([])
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040035 for aws_image in aws_images:
Sapan Bhatia511ea5f2014-07-21 22:53:58 -040036 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 Bhatia26d40bc2014-05-12 15:28:02 -040061 return new_images
62
63 def sync_record(self, image):
64 image.save()