~andreserl/+junk/juju-cloud-init

« back to all changes in this revision

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

  • Committer: William Reade
  • Date: 2011-09-13 11:15:18 UTC
  • mfrom: (335.2.9 cloud-init-class-used)
  • Revision ID: fwereade@gmail.com-20110913111518-61neeujr0m73mbw0
merge lp:~fwereade/ensemble/cloud-init-class-used: machine_data is now more structured, and cloud-init data is generated from it in a more ensemble-specific way [f=820892][r=hazmat,niemeyer]

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
from twisted.web.client import getPage
6
6
from twisted.internet.defer import succeed
7
7
 
8
 
from ensemble.providers.common.utils import format_cloud_init
9
 
 
10
8
log = logging.getLogger("ensemble.ec2")
11
9
 
12
 
# default image for each region.
13
 
AMI_REGION_MAP = {
14
 
    "us-east-1": "ami-d2ba45bb",
15
 
    "us-west-1": "ami-15693a50",
16
 
    "ap-northeast-1": "ami-80bb1181",
17
 
    "ap-southeast-1": "ami-b4671ee6",
18
 
    "eu-west-1": "ami-0991a67d"
19
 
    }
20
 
 
21
 
BASE_IMAGE_URI = "http://uec-images.ubuntu.com/query/"
22
 
CURRENT_IMAGE_URI = BASE_IMAGE_URI + \
23
 
              "%(ubuntu_release_name)s/%(variant)s/%(version)s.current.txt"
 
10
_CURRENT_IMAGE_URI_TEMPLATE = (
 
11
    "http://uec-images.ubuntu.com/query/"
 
12
    "%(ubuntu_release_name)s/%(variant)s/%(version)s.current.txt")
24
13
 
25
14
 
26
15
def get_region_uri(region):
28
17
    return "https://ec2.%s.amazonaws.com" % region
29
18
 
30
19
 
31
 
def get_current_ami(ubuntu_release_name="natty", arch="i386", ebs=True,
32
 
                    region="us-east-1", daily=False, desktop=False,
33
 
                    url_fetch=None):
 
20
# XXX ideally should come from latest available or client release name.
 
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):
34
24
    """Get the latest ami for the last release of ubuntu."""
35
25
    data = {}
36
 
    data["ubuntu_release_name"] = ubuntu_release_name
 
26
    data["ubuntu_release_name"] = ubuntu_release
37
27
    data["version"] = daily and "daily" or "released"
38
28
    data["variant"] = desktop and "desktop" or "server"
39
 
    ebs_match = ebs and "ebs" or "instance-store"
 
29
    ebs_match = persistent_storage and "ebs" or "instance-store"
40
30
 
41
 
    url = CURRENT_IMAGE_URI % data
 
31
    url = _CURRENT_IMAGE_URI_TEMPLATE % data
42
32
    url_fetch = url_fetch or getPage
43
33
 
44
34
    d = url_fetch(url)
48
38
        for tokens in csv.reader(data_stream, "excel-tab"):
49
39
            if tokens[4] != ebs_match:
50
40
                continue
51
 
            if tokens[5] == arch and tokens[6] == region:
 
41
            if tokens[5] == architecture and tokens[6] == region:
52
42
                return tokens[7]
53
 
        raise LookupError((ubuntu_release_name, arch, region,
 
43
        raise LookupError((ubuntu_release, architecture, region,
54
44
                           data["version"], data["variant"]))
55
45
 
56
46
    d.addCallback(extract_ami)
57
47
    return d
58
48
 
59
49
 
60
 
def get_launch_options(config, authorized_keys, packages=None,
61
 
                       repositories=None, scripts=None, **kw):
62
 
    """Determine launch options for the machine.
63
 
 
64
 
    Given an ensemble environment configuration, extract the proper machine
65
 
    information to be used for launching the machine via the ec2 api.
66
 
    """
67
 
 
68
 
    instance_type = config.get("default-instance-type", "m1.small")
69
 
 
70
 
    # XXX ideally should come from latest available or client release name.
 
50
def get_image_id(config, constraints):
 
51
    image_id = config.get("default-image-id", None)
 
52
    if image_id:
 
53
        return succeed(image_id)
71
54
    region = config.get("region", "us-east-1")
72
 
    image_id = config.get("default-image-id", None)
73
 
 
74
 
    # Lookup a standard ami by region, if none is specified, use any
75
 
    # specified image specification to locate an image, and remove the
76
 
    # image specification so it doesn't propogate to user data.
77
 
    if not image_id:
78
 
        params = {}
79
 
        for a_name, p_name  in (
80
 
            ("image_release_name", "ubuntu_release_name"),
81
 
            ("image_arch", "arch"),
82
 
            ("image_ebs", "ebs"),
83
 
            ("image_daily", "daily")):
84
 
            if a_name in kw:
85
 
                params[p_name] = kw[a_name]
86
 
                del kw[a_name]
87
 
        params["region"] = region
88
 
        d = get_current_ami(**params)
89
 
    else:
90
 
        d = succeed(image_id)
91
 
 
92
 
    user_data = format_cloud_init(
93
 
        authorized_keys=authorized_keys,
94
 
        packages=packages,
95
 
        repositories=repositories,
96
 
        scripts=scripts,
97
 
        data=kw)
98
 
 
99
 
    def on_machine_image(current_image_id):
100
 
        return {"image_id": current_image_id,
101
 
                "min_count": 1,
102
 
                "max_count": 1,
103
 
                "instance_type": instance_type,
104
 
                "user_data": user_data,
105
 
                "security_groups": []}
106
 
    d.addCallback(on_machine_image)
107
 
    return d
 
55
    return get_current_ami(region=region, **constraints)