5
5
from twisted.web.client import getPage
6
6
from twisted.internet.defer import succeed
8
from ensemble.providers.common.utils import format_cloud_init
10
8
log = logging.getLogger("ensemble.ec2")
12
# default image for each region.
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"
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")
26
15
def get_region_uri(region):
28
17
return "https://ec2.%s.amazonaws.com" % region
31
def get_current_ami(ubuntu_release_name="natty", arch="i386", ebs=True,
32
region="us-east-1", daily=False, desktop=False,
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."""
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"
41
url = CURRENT_IMAGE_URI % data
31
url = _CURRENT_IMAGE_URI_TEMPLATE % data
42
32
url_fetch = url_fetch or getPage
48
38
for tokens in csv.reader(data_stream, "excel-tab"):
49
39
if tokens[4] != ebs_match:
51
if tokens[5] == arch and tokens[6] == region:
41
if tokens[5] == architecture and tokens[6] == region:
53
raise LookupError((ubuntu_release_name, arch, region,
43
raise LookupError((ubuntu_release, architecture, region,
54
44
data["version"], data["variant"]))
56
46
d.addCallback(extract_ami)
60
def get_launch_options(config, authorized_keys, packages=None,
61
repositories=None, scripts=None, **kw):
62
"""Determine launch options for the machine.
64
Given an ensemble environment configuration, extract the proper machine
65
information to be used for launching the machine via the ec2 api.
68
instance_type = config.get("default-instance-type", "m1.small")
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)
53
return succeed(image_id)
71
54
region = config.get("region", "us-east-1")
72
image_id = config.get("default-image-id", None)
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.
79
for a_name, p_name in (
80
("image_release_name", "ubuntu_release_name"),
81
("image_arch", "arch"),
83
("image_daily", "daily")):
85
params[p_name] = kw[a_name]
87
params["region"] = region
88
d = get_current_ami(**params)
92
user_data = format_cloud_init(
93
authorized_keys=authorized_keys,
95
repositories=repositories,
99
def on_machine_image(current_image_id):
100
return {"image_id": current_image_id,
103
"instance_type": instance_type,
104
"user_data": user_data,
105
"security_groups": []}
106
d.addCallback(on_machine_image)
55
return get_current_ami(region=region, **constraints)