~kim0/+junk/cloudubuntu

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from django.http import HttpResponse, Http404
from django.shortcuts import render_to_response
from django.views.decorators.cache import cache_page
from amilocator.models import *
import urllib2
from django.forms.models import model_to_dict
import simplejson

@cache_page(60 * 60 * 1)
def releasesTable(request):
    refresh()
    amilist = [ model_to_dict(x) for x in AMI.objects.all() ]
    entry = []
    jsonResp = '{ "aaData": \n'
    for ami in amilist:
        entry.append( [
                ami['awszone'],
                ami['name'],
                ami['arch'],
                ami['ebs'],
                ami['serial'],
                '<a href="https://console.aws.amazon.com/ec2/home?region=%s#launchAmi=%s">%s</a>' % (ami['awszone'],ami['amiid'],ami['amiid']),
                ami['akiid']
                ] )
    jsonResp += simplejson.dumps(entry)
    jsonResp += '\n}'
    return HttpResponse(jsonResp, mimetype='text/plain')

def refresh():
    #print "Expiring DB, full refresh..."
    releases=list()
    try:
        for line in urllib2.urlopen('http://uec-images.ubuntu.com/query/released.latest.txt').readlines():
            releases.append( line.split('\t')[0] )
    except:
	#print 'Exception updating releases list, cowardly walking away'
        return

    flist = []
    try:
        for release in releases:
            for line in urllib2.urlopen('http://uec-images.ubuntu.com/query/%s/server/released.current.txt' %
            release).readlines():
                f = line.split('\t')
                flist.append( f )
    except:
	#print 'Exception retrieving releases information, cowardly walking away'
        return
    # All fine, let's drop the DB and write new data
    AMI.objects.all().delete()
    for f in flist:
        if len(f)==11:
            if f[10] == 'hvm\n':
                f[4]='%s-hvm-cluster' % f[4]
        ami = AMI.objects.create( name=f[0],server=f[1],release=f[2],serial=f[3],ebs=f[4],arch=f[5],awszone=f[6],amiid=f[7],akiid=f[8] )