~fwereade/pyjuju/cloud-init-class-used

« back to all changes in this revision

Viewing changes to ensemble/providers/ec2/utils.py

  • Committer: William Reade
  • Date: 2011-09-06 13:11:30 UTC
  • Revision ID: fwereade@gmail.com-20110906131130-lxq0xlswvsm4ohlz
get_current_ami now uses same keys as machine_data constraints dict

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
    "http://uec-images.ubuntu.com/query/"
12
12
    "%(ubuntu_release_name)s/%(variant)s/%(version)s.current.txt")
13
13
 
14
 
_KWARG_NAMES = {
15
 
    "ubuntu_release": "ubuntu_release_name",
16
 
    "architecture": "arch",
17
 
    "daily": "daily",
18
 
    "persistent_storage": "ebs"}
19
 
 
20
14
 
21
15
def get_region_uri(region):
22
16
    """Get the URL endpoint for the region."""
24
18
 
25
19
 
26
20
# XXX ideally should come from latest available or client release name.
27
 
def get_current_ami(ubuntu_release_name="natty", arch="i386", ebs=True,
28
 
                    region="us-east-1", daily=False, desktop=False,
29
 
                    url_fetch=None):
 
21
def get_current_ami(ubuntu_release="natty", architecture="i386",
 
22
                    persistent_storage=True, region="us-east-1", daily=False,
 
23
                    desktop=False, url_fetch=None):
30
24
    """Get the latest ami for the last release of ubuntu."""
31
25
    data = {}
32
 
    data["ubuntu_release_name"] = ubuntu_release_name
 
26
    data["ubuntu_release_name"] = ubuntu_release
33
27
    data["version"] = daily and "daily" or "released"
34
28
    data["variant"] = desktop and "desktop" or "server"
35
 
    ebs_match = ebs and "ebs" or "instance-store"
 
29
    ebs_match = persistent_storage and "ebs" or "instance-store"
36
30
 
37
31
    url = _CURRENT_IMAGE_URI_TEMPLATE % data
38
32
    url_fetch = url_fetch or getPage
44
38
        for tokens in csv.reader(data_stream, "excel-tab"):
45
39
            if tokens[4] != ebs_match:
46
40
                continue
47
 
            if tokens[5] == arch and tokens[6] == region:
 
41
            if tokens[5] == architecture and tokens[6] == region:
48
42
                return tokens[7]
49
 
        raise LookupError((ubuntu_release_name, arch, region,
 
43
        raise LookupError((ubuntu_release, architecture, region,
50
44
                           data["version"], data["variant"]))
51
45
 
52
46
    d.addCallback(extract_ami)
53
47
    return d
54
48
 
55
49
 
56
 
def _convert_constraints(constraints):
57
 
    kwargs = {}
58
 
    for (constraint_name, kwarg_name) in _KWARG_NAMES.items():
59
 
        if constraint_name in constraints:
60
 
            kwargs[kwarg_name] = constraints[constraint_name]
61
 
    return kwargs
62
 
 
63
 
 
64
50
def get_image_id(config, constraints):
65
51
    image_id = config.get("default-image-id", None)
66
52
    if image_id:
67
53
        return succeed(image_id)
68
54
    region = config.get("region", "us-east-1")
69
 
    kwargs = _convert_constraints(constraints)
70
 
    return get_current_ami(region=region, **kwargs)
 
55
    return get_current_ami(region=region, **constraints)